blob: 0db6cfac535ebb3290782be25d8eea53789a07c5 [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 Moolenaar3ea0f1a2016-02-23 22:07:32 +010030#if defined(FEAT_FLOAT)
31# include <float.h>
32# if defined(HAVE_MATH_H)
33# include <math.h>
34# endif
35# if defined(WIN32) && !defined(isnan)
36# define isnan(x) _isnan(x)
37# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#endif
39
Bram Moolenaar33570922005-01-25 22:26:29 +000040#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000042#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
43 be freed. */
44
Bram Moolenaar071d4272004-06-13 20:20:40 +000045/*
Bram Moolenaar33570922005-01-25 22:26:29 +000046 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
47 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000048 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
49 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
50 * HI2DI() converts a hashitem pointer to a dictitem pointer.
51 */
Bram Moolenaar33570922005-01-25 22:26:29 +000052static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000053#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000054#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000055#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000056
57/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000058 * Structure returned by get_lval() and used by set_var_lval().
59 * For a plain name:
60 * "name" points to the variable name.
61 * "exp_name" is NULL.
62 * "tv" is NULL
63 * For a magic braces name:
64 * "name" points to the expanded variable name.
65 * "exp_name" is non-NULL, to be freed later.
66 * "tv" is NULL
67 * For an index in a list:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the (first) list item value
71 * "li" points to the (first) list item
72 * "range", "n1", "n2" and "empty2" indicate what items are used.
73 * For an existing Dict item:
74 * "name" points to the (expanded) variable name.
75 * "exp_name" NULL or non-NULL, to be freed later.
76 * "tv" points to the dict item value
77 * "newkey" is NULL
78 * For a non-existing Dict item:
79 * "name" points to the (expanded) variable name.
80 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000081 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000082 * "newkey" is the key for the new item.
83 */
84typedef struct lval_S
85{
86 char_u *ll_name; /* start of variable name (can be NULL) */
87 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000089 isn't NULL it's the Dict to which to add
90 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 listitem_T *ll_li; /* The list item or NULL. */
92 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000093 int ll_range; /* TRUE when a [i:j] range was used */
94 long ll_n1; /* First index for list */
95 long ll_n2; /* Second index for list range */
96 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000097 dict_T *ll_dict; /* The Dictionary or NULL */
98 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000100} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000101
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000102static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000103static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000104static char *e_undefvar = N_("E121: Undefined variable: %s");
105static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000107static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000108static char *e_listreq = N_("E714: List required");
109static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000110static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000111static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
112static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
113static char *e_funcdict = N_("E717: Dictionary entry already exists");
114static char *e_funcref = N_("E718: Funcref required");
115static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
116static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000117static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000118static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200119#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200120static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200121#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000122
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100123#define NAMESPACE_CHAR (char_u *)"abglstvw"
124
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200125static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000126#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
128/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129 * Old Vim variables such as "v:version" are also available without the "v:".
130 * Also in functions. We need a special hashtable for them.
131 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000132static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133
134/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135 * When recursively copying lists and dicts we need to remember which ones we
136 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000137 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000138 */
139static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000140#define COPYID_INC 2
141#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000142
Bram Moolenaar8502c702014-06-17 12:51:16 +0200143/* Abort conversion to string after a recursion error. */
144static int did_echo_string_emsg = FALSE;
145
Bram Moolenaard9fba312005-06-26 22:34:35 +0000146/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000147 * Array to hold the hashtab with variables local to each sourced script.
148 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000150typedef struct
151{
152 dictitem_T sv_var;
153 dict_T sv_dict;
154} scriptvar_T;
155
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200156static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
157#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
158#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
160static int echo_attr = 0; /* attributes used for ":echo" */
161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000162/* Values for trans_function_name() argument: */
163#define TFN_INT 1 /* internal function name OK */
164#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100165#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
166
167/* Values for get_lval() flags argument: */
168#define GLV_QUIET TFN_QUIET /* no error messages */
169#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000170
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171/*
172 * Structure to hold info for a user function.
173 */
174typedef struct ufunc ufunc_T;
175
176struct ufunc
177{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000178 int uf_varargs; /* variable nr of arguments */
179 int uf_flags;
180 int uf_calls; /* nr of active calls */
181 garray_T uf_args; /* arguments */
182 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183#ifdef FEAT_PROFILE
184 int uf_profiling; /* TRUE when func is being profiled */
185 /* profiling the function as a whole */
186 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000187 proftime_T uf_tm_total; /* time spent in function + children */
188 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000189 proftime_T uf_tm_children; /* time spent in children this call */
190 /* profiling the function per line */
191 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000192 proftime_T *uf_tml_total; /* time spent in a line + children */
193 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000194 proftime_T uf_tml_start; /* start time for current line */
195 proftime_T uf_tml_children; /* time spent in children for this line */
196 proftime_T uf_tml_wait; /* start wait time for current line */
197 int uf_tml_idx; /* index of line being timed; -1 if none */
198 int uf_tml_execed; /* line being timed was executed */
199#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000200 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000202 int uf_refcount; /* for numbered function: reference count */
203 char_u uf_name[1]; /* name of function (actually longer); can
204 start with <SNR>123_ (<SNR> is K_SPECIAL
205 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206};
207
208/* function flags */
209#define FC_ABORT 1 /* abort function on error */
210#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000211#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
213/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000216static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000218/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000219static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
220
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000221/* list heads for garbage collection */
222static dict_T *first_dict = NULL; /* list of all dicts */
223static list_T *first_list = NULL; /* list of all lists */
224
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000225/* From user function to hashitem and back. */
226static ufunc_T dumuf;
227#define UF2HIKEY(fp) ((fp)->uf_name)
228#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
229#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
230
231#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
232#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Bram Moolenaar33570922005-01-25 22:26:29 +0000234#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
235#define VAR_SHORT_LEN 20 /* short variable name length */
236#define FIXVAR_CNT 12 /* number of fixed variables */
237
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000239typedef struct funccall_S funccall_T;
240
241struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242{
243 ufunc_T *func; /* function being called */
244 int linenr; /* next line to be executed */
245 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000246 struct /* fixed variables for arguments */
247 {
248 dictitem_T var; /* variable (without room for name) */
249 char_u room[VAR_SHORT_LEN]; /* room for the name */
250 } fixvar[FIXVAR_CNT];
251 dict_T l_vars; /* l: local function variables */
252 dictitem_T l_vars_var; /* variable for l: scope */
253 dict_T l_avars; /* a: argument variables */
254 dictitem_T l_avars_var; /* variable for a: scope */
255 list_T l_varlist; /* list for a:000 */
256 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
257 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 linenr_T breakpoint; /* next line with breakpoint or zero */
259 int dbg_tick; /* debug_tick when breakpoint was set */
260 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000261#ifdef FEAT_PROFILE
262 proftime_T prof_child; /* time spent in a child */
263#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000264 funccall_T *caller; /* calling function or NULL */
265};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
267/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268 * Info used by a ":for" loop.
269 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271{
272 int fi_semicolon; /* TRUE if ending in '; var]' */
273 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 listwatch_T fi_lw; /* keep an eye on the item used. */
275 list_T *fi_list; /* list being used */
276} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000277
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000278/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000279 * Struct used by trans_function_name()
280 */
281typedef struct
282{
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000284 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000285 dictitem_T *fd_di; /* Dictionary item used */
286} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000287
Bram Moolenaara7043832005-01-21 11:56:39 +0000288
289/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000290 * Array to hold the value of v: variables.
291 * The value is in a dictitem, so that it can also be used in the v: scope.
292 * The reason to use this table anyway is for very quick access to the
293 * variables with the VV_ defines.
294 */
295#include "version.h"
296
297/* values for vv_flags: */
298#define VV_COMPAT 1 /* compatible, also used without "v:" */
299#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000300#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000302#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000303
304static struct vimvar
305{
306 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000307 dictitem_T vv_di; /* value and name for key */
308 char vv_filler[16]; /* space for LONGEST name below!!! */
309 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
310} vimvars[VV_LEN] =
311{
312 /*
313 * The order here must match the VV_ defines in vim.h!
314 * Initializing a union does not work, leave tv.vval empty to get zero's.
315 */
316 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("count1", VAR_NUMBER), VV_RO},
318 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
319 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
320 {VV_NAME("warningmsg", VAR_STRING), 0},
321 {VV_NAME("statusmsg", VAR_STRING), 0},
322 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
323 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
324 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
325 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("termresponse", VAR_STRING), VV_RO},
327 {VV_NAME("fname", VAR_STRING), VV_RO},
328 {VV_NAME("lang", VAR_STRING), VV_RO},
329 {VV_NAME("lc_time", VAR_STRING), VV_RO},
330 {VV_NAME("ctype", VAR_STRING), VV_RO},
331 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
332 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
333 {VV_NAME("fname_in", VAR_STRING), VV_RO},
334 {VV_NAME("fname_out", VAR_STRING), VV_RO},
335 {VV_NAME("fname_new", VAR_STRING), VV_RO},
336 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
337 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
338 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
339 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
340 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
341 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
342 {VV_NAME("progname", VAR_STRING), VV_RO},
343 {VV_NAME("servername", VAR_STRING), VV_RO},
344 {VV_NAME("dying", VAR_NUMBER), VV_RO},
345 {VV_NAME("exception", VAR_STRING), VV_RO},
346 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
347 {VV_NAME("register", VAR_STRING), VV_RO},
348 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
349 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000350 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
351 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000352 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000353 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
354 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000355 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
356 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
357 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
358 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
359 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000360 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000361 {VV_NAME("swapname", VAR_STRING), VV_RO},
362 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000363 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200364 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000365 {VV_NAME("mouse_win", VAR_NUMBER), 0},
366 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
367 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000368 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100370 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000371 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200372 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200373 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200374 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200375 {VV_NAME("option_new", VAR_STRING), VV_RO},
376 {VV_NAME("option_old", VAR_STRING), VV_RO},
377 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100378 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100379 {VV_NAME("false", VAR_SPECIAL), VV_RO},
380 {VV_NAME("true", VAR_SPECIAL), VV_RO},
381 {VV_NAME("null", VAR_SPECIAL), VV_RO},
382 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000383};
384
385/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_type vv_di.di_tv.v_type
387#define vv_nr vv_di.di_tv.vval.v_number
388#define vv_float vv_di.di_tv.vval.v_float
389#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000390#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200391#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000392#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000393
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200394static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000395#define vimvarht vimvardict.dv_hashtab
396
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100397static void prepare_vimvar(int idx, typval_T *save_tv);
398static void restore_vimvar(int idx, typval_T *save_tv);
399static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
400static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
401static char_u *skip_var_one(char_u *arg);
402static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
403static void list_glob_vars(int *first);
404static void list_buf_vars(int *first);
405static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000406#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100407static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000408#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100409static void list_vim_vars(int *first);
410static void list_script_vars(int *first);
411static void list_func_vars(int *first);
412static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
413static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
414static int check_changedtick(char_u *arg);
415static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
416static void clear_lval(lval_T *lp);
417static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
418static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
419static void list_fix_watch(list_T *l, listitem_T *item);
420static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
421static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
422static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
423static void item_lock(typval_T *tv, int deep, int lock);
424static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000425
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100426static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
427static int eval1(char_u **arg, typval_T *rettv, int evaluate);
428static int eval2(char_u **arg, typval_T *rettv, int evaluate);
429static int eval3(char_u **arg, typval_T *rettv, int evaluate);
430static int eval4(char_u **arg, typval_T *rettv, int evaluate);
431static int eval5(char_u **arg, typval_T *rettv, int evaluate);
432static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
433static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000434
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100435static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
436static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
437static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
438static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
439static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
440static long list_len(list_T *l);
441static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
442static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
443static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
444static long list_find_nr(list_T *l, long idx, int *errorp);
445static long list_idx_of_item(list_T *l, listitem_T *item);
446static int list_append_number(list_T *l, varnumber_T n);
447static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
448static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
449static list_T *list_copy(list_T *orig, int deep, int copyID);
450static char_u *list2string(typval_T *tv, int copyID);
451static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
452static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
453static int free_unref_items(int copyID);
454static dictitem_T *dictitem_copy(dictitem_T *org);
455static void dictitem_remove(dict_T *dict, dictitem_T *item);
456static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
457static long dict_len(dict_T *d);
458static char_u *dict2string(typval_T *tv, int copyID);
459static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
460static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
461static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
462static char_u *string_quote(char_u *str, int function);
463static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
464static int find_internal_func(char_u *name);
465static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
466static 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 +0100467static void emsg_funcname(char *ermsg, char_u *name);
468static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000469
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_abs(typval_T *argvars, typval_T *rettv);
472static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_add(typval_T *argvars, typval_T *rettv);
475static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
476static void f_and(typval_T *argvars, typval_T *rettv);
477static void f_append(typval_T *argvars, typval_T *rettv);
478static void f_argc(typval_T *argvars, typval_T *rettv);
479static void f_argidx(typval_T *argvars, typval_T *rettv);
480static void f_arglistid(typval_T *argvars, typval_T *rettv);
481static void f_argv(typval_T *argvars, typval_T *rettv);
482static void f_assert_equal(typval_T *argvars, typval_T *rettv);
483static void f_assert_exception(typval_T *argvars, typval_T *rettv);
484static void f_assert_fails(typval_T *argvars, typval_T *rettv);
485static void f_assert_false(typval_T *argvars, typval_T *rettv);
486static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100488static void f_asin(typval_T *argvars, typval_T *rettv);
489static void f_atan(typval_T *argvars, typval_T *rettv);
490static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000491#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100492static void f_browse(typval_T *argvars, typval_T *rettv);
493static void f_browsedir(typval_T *argvars, typval_T *rettv);
494static void f_bufexists(typval_T *argvars, typval_T *rettv);
495static void f_buflisted(typval_T *argvars, typval_T *rettv);
496static void f_bufloaded(typval_T *argvars, typval_T *rettv);
497static void f_bufname(typval_T *argvars, typval_T *rettv);
498static void f_bufnr(typval_T *argvars, typval_T *rettv);
499static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
500static void f_byte2line(typval_T *argvars, typval_T *rettv);
501static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
502static void f_byteidx(typval_T *argvars, typval_T *rettv);
503static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
504static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100506static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000507#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100508#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100509static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100510# ifdef FEAT_JOB
511static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
512# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100513static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100514static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
515static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100516static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100517static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100518static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
519static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100520static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100521static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100522#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100523static void f_changenr(typval_T *argvars, typval_T *rettv);
524static void f_char2nr(typval_T *argvars, typval_T *rettv);
525static void f_cindent(typval_T *argvars, typval_T *rettv);
526static void f_clearmatches(typval_T *argvars, typval_T *rettv);
527static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000528#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100529static void f_complete(typval_T *argvars, typval_T *rettv);
530static void f_complete_add(typval_T *argvars, typval_T *rettv);
531static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000532#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_confirm(typval_T *argvars, typval_T *rettv);
534static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000535#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100536static void f_cos(typval_T *argvars, typval_T *rettv);
537static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_count(typval_T *argvars, typval_T *rettv);
540static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
541static void f_cursor(typval_T *argsvars, typval_T *rettv);
542static void f_deepcopy(typval_T *argvars, typval_T *rettv);
543static void f_delete(typval_T *argvars, typval_T *rettv);
544static void f_did_filetype(typval_T *argvars, typval_T *rettv);
545static void f_diff_filler(typval_T *argvars, typval_T *rettv);
546static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100547static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100548static void f_empty(typval_T *argvars, typval_T *rettv);
549static void f_escape(typval_T *argvars, typval_T *rettv);
550static void f_eval(typval_T *argvars, typval_T *rettv);
551static void f_eventhandler(typval_T *argvars, typval_T *rettv);
552static void f_executable(typval_T *argvars, typval_T *rettv);
553static void f_exepath(typval_T *argvars, typval_T *rettv);
554static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200555#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100556static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200557#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_expand(typval_T *argvars, typval_T *rettv);
559static void f_extend(typval_T *argvars, typval_T *rettv);
560static void f_feedkeys(typval_T *argvars, typval_T *rettv);
561static void f_filereadable(typval_T *argvars, typval_T *rettv);
562static void f_filewritable(typval_T *argvars, typval_T *rettv);
563static void f_filter(typval_T *argvars, typval_T *rettv);
564static void f_finddir(typval_T *argvars, typval_T *rettv);
565static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000566#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100567static void f_float2nr(typval_T *argvars, typval_T *rettv);
568static void f_floor(typval_T *argvars, typval_T *rettv);
569static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000570#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100571static void f_fnameescape(typval_T *argvars, typval_T *rettv);
572static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
573static void f_foldclosed(typval_T *argvars, typval_T *rettv);
574static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
575static void f_foldlevel(typval_T *argvars, typval_T *rettv);
576static void f_foldtext(typval_T *argvars, typval_T *rettv);
577static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
578static void f_foreground(typval_T *argvars, typval_T *rettv);
579static void f_function(typval_T *argvars, typval_T *rettv);
580static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
581static void f_get(typval_T *argvars, typval_T *rettv);
582static void f_getbufline(typval_T *argvars, typval_T *rettv);
583static void f_getbufvar(typval_T *argvars, typval_T *rettv);
584static void f_getchar(typval_T *argvars, typval_T *rettv);
585static void f_getcharmod(typval_T *argvars, typval_T *rettv);
586static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
587static void f_getcmdline(typval_T *argvars, typval_T *rettv);
588static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
589static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
590static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
591static void f_getcwd(typval_T *argvars, typval_T *rettv);
592static void f_getfontname(typval_T *argvars, typval_T *rettv);
593static void f_getfperm(typval_T *argvars, typval_T *rettv);
594static void f_getfsize(typval_T *argvars, typval_T *rettv);
595static void f_getftime(typval_T *argvars, typval_T *rettv);
596static void f_getftype(typval_T *argvars, typval_T *rettv);
597static void f_getline(typval_T *argvars, typval_T *rettv);
598static void f_getmatches(typval_T *argvars, typval_T *rettv);
599static void f_getpid(typval_T *argvars, typval_T *rettv);
600static void f_getcurpos(typval_T *argvars, typval_T *rettv);
601static void f_getpos(typval_T *argvars, typval_T *rettv);
602static void f_getqflist(typval_T *argvars, typval_T *rettv);
603static void f_getreg(typval_T *argvars, typval_T *rettv);
604static void f_getregtype(typval_T *argvars, typval_T *rettv);
605static void f_gettabvar(typval_T *argvars, typval_T *rettv);
606static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
607static void f_getwinposx(typval_T *argvars, typval_T *rettv);
608static void f_getwinposy(typval_T *argvars, typval_T *rettv);
609static void f_getwinvar(typval_T *argvars, typval_T *rettv);
610static void f_glob(typval_T *argvars, typval_T *rettv);
611static void f_globpath(typval_T *argvars, typval_T *rettv);
612static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
613static void f_has(typval_T *argvars, typval_T *rettv);
614static void f_has_key(typval_T *argvars, typval_T *rettv);
615static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
616static void f_hasmapto(typval_T *argvars, typval_T *rettv);
617static void f_histadd(typval_T *argvars, typval_T *rettv);
618static void f_histdel(typval_T *argvars, typval_T *rettv);
619static void f_histget(typval_T *argvars, typval_T *rettv);
620static void f_histnr(typval_T *argvars, typval_T *rettv);
621static void f_hlID(typval_T *argvars, typval_T *rettv);
622static void f_hlexists(typval_T *argvars, typval_T *rettv);
623static void f_hostname(typval_T *argvars, typval_T *rettv);
624static void f_iconv(typval_T *argvars, typval_T *rettv);
625static void f_indent(typval_T *argvars, typval_T *rettv);
626static void f_index(typval_T *argvars, typval_T *rettv);
627static void f_input(typval_T *argvars, typval_T *rettv);
628static void f_inputdialog(typval_T *argvars, typval_T *rettv);
629static void f_inputlist(typval_T *argvars, typval_T *rettv);
630static void f_inputrestore(typval_T *argvars, typval_T *rettv);
631static void f_inputsave(typval_T *argvars, typval_T *rettv);
632static void f_inputsecret(typval_T *argvars, typval_T *rettv);
633static void f_insert(typval_T *argvars, typval_T *rettv);
634static void f_invert(typval_T *argvars, typval_T *rettv);
635static void f_isdirectory(typval_T *argvars, typval_T *rettv);
636static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100637#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
638static void f_isnan(typval_T *argvars, typval_T *rettv);
639#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100640static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100641#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100642# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100643static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100644# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100645static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100646static void f_job_start(typval_T *argvars, typval_T *rettv);
647static void f_job_stop(typval_T *argvars, typval_T *rettv);
648static void f_job_status(typval_T *argvars, typval_T *rettv);
649#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100650static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100651static void f_js_decode(typval_T *argvars, typval_T *rettv);
652static void f_js_encode(typval_T *argvars, typval_T *rettv);
653static void f_json_decode(typval_T *argvars, typval_T *rettv);
654static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_keys(typval_T *argvars, typval_T *rettv);
656static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
657static void f_len(typval_T *argvars, typval_T *rettv);
658static void f_libcall(typval_T *argvars, typval_T *rettv);
659static void f_libcallnr(typval_T *argvars, typval_T *rettv);
660static void f_line(typval_T *argvars, typval_T *rettv);
661static void f_line2byte(typval_T *argvars, typval_T *rettv);
662static void f_lispindent(typval_T *argvars, typval_T *rettv);
663static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_log(typval_T *argvars, typval_T *rettv);
666static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000667#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200668#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_map(typval_T *argvars, typval_T *rettv);
672static void f_maparg(typval_T *argvars, typval_T *rettv);
673static void f_mapcheck(typval_T *argvars, typval_T *rettv);
674static void f_match(typval_T *argvars, typval_T *rettv);
675static void f_matchadd(typval_T *argvars, typval_T *rettv);
676static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
677static void f_matcharg(typval_T *argvars, typval_T *rettv);
678static void f_matchdelete(typval_T *argvars, typval_T *rettv);
679static void f_matchend(typval_T *argvars, typval_T *rettv);
680static void f_matchlist(typval_T *argvars, typval_T *rettv);
681static void f_matchstr(typval_T *argvars, typval_T *rettv);
682static void f_max(typval_T *argvars, typval_T *rettv);
683static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000684#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000686#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100688#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
692static void f_nr2char(typval_T *argvars, typval_T *rettv);
693static void f_or(typval_T *argvars, typval_T *rettv);
694static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100695#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100697#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
702static void f_printf(typval_T *argvars, typval_T *rettv);
703static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200704#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200706#endif
707#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_range(typval_T *argvars, typval_T *rettv);
711static void f_readfile(typval_T *argvars, typval_T *rettv);
712static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100713#ifdef FEAT_FLOAT
714static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
715#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_reltimestr(typval_T *argvars, typval_T *rettv);
717static void f_remote_expr(typval_T *argvars, typval_T *rettv);
718static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
719static void f_remote_peek(typval_T *argvars, typval_T *rettv);
720static void f_remote_read(typval_T *argvars, typval_T *rettv);
721static void f_remote_send(typval_T *argvars, typval_T *rettv);
722static void f_remove(typval_T *argvars, typval_T *rettv);
723static void f_rename(typval_T *argvars, typval_T *rettv);
724static void f_repeat(typval_T *argvars, typval_T *rettv);
725static void f_resolve(typval_T *argvars, typval_T *rettv);
726static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000727#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100728static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000729#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100730static void f_screenattr(typval_T *argvars, typval_T *rettv);
731static void f_screenchar(typval_T *argvars, typval_T *rettv);
732static void f_screencol(typval_T *argvars, typval_T *rettv);
733static void f_screenrow(typval_T *argvars, typval_T *rettv);
734static void f_search(typval_T *argvars, typval_T *rettv);
735static void f_searchdecl(typval_T *argvars, typval_T *rettv);
736static void f_searchpair(typval_T *argvars, typval_T *rettv);
737static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
738static void f_searchpos(typval_T *argvars, typval_T *rettv);
739static void f_server2client(typval_T *argvars, typval_T *rettv);
740static void f_serverlist(typval_T *argvars, typval_T *rettv);
741static void f_setbufvar(typval_T *argvars, typval_T *rettv);
742static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
743static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
744static void f_setline(typval_T *argvars, typval_T *rettv);
745static void f_setloclist(typval_T *argvars, typval_T *rettv);
746static void f_setmatches(typval_T *argvars, typval_T *rettv);
747static void f_setpos(typval_T *argvars, typval_T *rettv);
748static void f_setqflist(typval_T *argvars, typval_T *rettv);
749static void f_setreg(typval_T *argvars, typval_T *rettv);
750static void f_settabvar(typval_T *argvars, typval_T *rettv);
751static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
752static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100753#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100755#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_shellescape(typval_T *argvars, typval_T *rettv);
757static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
758static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000759#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sin(typval_T *argvars, typval_T *rettv);
761static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sort(typval_T *argvars, typval_T *rettv);
764static void f_soundfold(typval_T *argvars, typval_T *rettv);
765static void f_spellbadword(typval_T *argvars, typval_T *rettv);
766static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
767static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000768#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_sqrt(typval_T *argvars, typval_T *rettv);
770static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_str2nr(typval_T *argvars, typval_T *rettv);
773static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000774#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000776#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100777static void f_stridx(typval_T *argvars, typval_T *rettv);
778static void f_string(typval_T *argvars, typval_T *rettv);
779static void f_strlen(typval_T *argvars, typval_T *rettv);
780static void f_strpart(typval_T *argvars, typval_T *rettv);
781static void f_strridx(typval_T *argvars, typval_T *rettv);
782static void f_strtrans(typval_T *argvars, typval_T *rettv);
783static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
784static void f_strwidth(typval_T *argvars, typval_T *rettv);
785static void f_submatch(typval_T *argvars, typval_T *rettv);
786static void f_substitute(typval_T *argvars, typval_T *rettv);
787static void f_synID(typval_T *argvars, typval_T *rettv);
788static void f_synIDattr(typval_T *argvars, typval_T *rettv);
789static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
790static void f_synstack(typval_T *argvars, typval_T *rettv);
791static void f_synconcealed(typval_T *argvars, typval_T *rettv);
792static void f_system(typval_T *argvars, typval_T *rettv);
793static void f_systemlist(typval_T *argvars, typval_T *rettv);
794static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
795static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
796static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
797static void f_taglist(typval_T *argvars, typval_T *rettv);
798static void f_tagfiles(typval_T *argvars, typval_T *rettv);
799static void f_tempname(typval_T *argvars, typval_T *rettv);
800static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200801#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100802static void f_tan(typval_T *argvars, typval_T *rettv);
803static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200804#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100805static void f_tolower(typval_T *argvars, typval_T *rettv);
806static void f_toupper(typval_T *argvars, typval_T *rettv);
807static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000808#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100809static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000810#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100811static void f_type(typval_T *argvars, typval_T *rettv);
812static void f_undofile(typval_T *argvars, typval_T *rettv);
813static void f_undotree(typval_T *argvars, typval_T *rettv);
814static void f_uniq(typval_T *argvars, typval_T *rettv);
815static void f_values(typval_T *argvars, typval_T *rettv);
816static void f_virtcol(typval_T *argvars, typval_T *rettv);
817static void f_visualmode(typval_T *argvars, typval_T *rettv);
818static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
819static void f_winbufnr(typval_T *argvars, typval_T *rettv);
820static void f_wincol(typval_T *argvars, typval_T *rettv);
821static void f_winheight(typval_T *argvars, typval_T *rettv);
822static void f_winline(typval_T *argvars, typval_T *rettv);
823static void f_winnr(typval_T *argvars, typval_T *rettv);
824static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
825static void f_winrestview(typval_T *argvars, typval_T *rettv);
826static void f_winsaveview(typval_T *argvars, typval_T *rettv);
827static void f_winwidth(typval_T *argvars, typval_T *rettv);
828static void f_writefile(typval_T *argvars, typval_T *rettv);
829static void f_wordcount(typval_T *argvars, typval_T *rettv);
830static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000831
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
833static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
834static int get_env_len(char_u **arg);
835static int get_id_len(char_u **arg);
836static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
837static 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 +0000838#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
839#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
840 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
842static int eval_isnamec(int c);
843static int eval_isnamec1(int c);
844static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
845static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static typval_T *alloc_string_tv(char_u *string);
847static void init_tv(typval_T *varp);
848static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100849#ifdef FEAT_FLOAT
850static float_T get_tv_float(typval_T *varp);
851#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static linenr_T get_tv_lnum(typval_T *argvars);
853static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
854static char_u *get_tv_string(typval_T *varp);
855static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
856static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
857static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
858static hashtab_T *find_var_ht(char_u *name, char_u **varname);
859static funccall_T *get_funccal(void);
860static void vars_clear_ext(hashtab_T *ht, int free_val);
861static void delete_var(hashtab_T *ht, hashitem_T *hi);
862static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
863static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
864static void set_var(char_u *name, typval_T *varp, int copy);
865static int var_check_ro(int flags, char_u *name, int use_gettext);
866static int var_check_fixed(int flags, char_u *name, int use_gettext);
867static int var_check_func_name(char_u *name, int new_var);
868static int valid_varname(char_u *varname);
869static int tv_check_lock(int lock, char_u *name, int use_gettext);
870static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
871static char_u *find_option_end(char_u **arg, int *opt_flags);
872static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
873static int eval_fname_script(char_u *p);
874static int eval_fname_sid(char_u *p);
875static void list_func_head(ufunc_T *fp, int indent);
876static ufunc_T *find_func(char_u *name);
877static int function_exists(char_u *name);
878static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000879#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static void func_do_profile(ufunc_T *fp);
881static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
882static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000883static int
884# ifdef __BORLANDC__
885 _RTLENTRYF
886# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100887 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000888static int
889# ifdef __BORLANDC__
890 _RTLENTRYF
891# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100892 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100894static int script_autoload(char_u *name, int reload);
895static char_u *autoload_name(char_u *name);
896static void cat_func_name(char_u *buf, ufunc_T *fp);
897static void func_free(ufunc_T *fp);
898static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
899static int can_free_funccal(funccall_T *fc, int copyID) ;
900static void free_funccal(funccall_T *fc, int free_val);
901static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
902static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
903static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
904static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
905static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
906static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
907static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
908static int write_list(FILE *fd, list_T *list, int binary);
909static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000910
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200911
912#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100913static int compare_func_name(const void *s1, const void *s2);
914static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200915#endif
916
Bram Moolenaar33570922005-01-25 22:26:29 +0000917/*
918 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000919 */
920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100921eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000922{
Bram Moolenaar33570922005-01-25 22:26:29 +0000923 int i;
924 struct vimvar *p;
925
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200926 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
927 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200928 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000929 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000930 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000931
932 for (i = 0; i < VV_LEN; ++i)
933 {
934 p = &vimvars[i];
935 STRCPY(p->vv_di.di_key, p->vv_name);
936 if (p->vv_flags & VV_RO)
937 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
938 else if (p->vv_flags & VV_RO_SBX)
939 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
940 else
941 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000942
943 /* add to v: scope dict, unless the value is not always available */
944 if (p->vv_type != VAR_UNKNOWN)
945 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000946 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000947 /* add to compat scope dict */
948 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000949 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100950 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
951
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000952 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100953 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200954 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100955 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100956
957 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
958 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
959 set_vim_var_nr(VV_NONE, VVAL_NONE);
960 set_vim_var_nr(VV_NULL, VVAL_NULL);
961
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200962 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200963
964#ifdef EBCDIC
965 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100966 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200967 */
968 sortFunctions();
969#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000970}
971
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000972#if defined(EXITFREE) || defined(PROTO)
973 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100974eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000975{
976 int i;
977 struct vimvar *p;
978
979 for (i = 0; i < VV_LEN; ++i)
980 {
981 p = &vimvars[i];
982 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000983 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000984 vim_free(p->vv_str);
985 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000986 }
987 else if (p->vv_di.di_tv.v_type == VAR_LIST)
988 {
989 list_unref(p->vv_list);
990 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000991 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992 }
993 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000994 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995 hash_clear(&compat_hashtab);
996
Bram Moolenaard9fba312005-06-26 22:34:35 +0000997 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100998# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200999 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001000# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001001
1002 /* global variables */
1003 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001004
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001005 /* autoloaded script names */
1006 ga_clear_strings(&ga_loaded);
1007
Bram Moolenaarcca74132013-09-25 21:00:28 +02001008 /* Script-local variables. First clear all the variables and in a second
1009 * loop free the scriptvar_T, because a variable in one script might hold
1010 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001011 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001012 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001013 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001014 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001015 ga_clear(&ga_scripts);
1016
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001017 /* unreferenced lists and dicts */
1018 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001019
1020 /* functions */
1021 free_all_functions();
1022 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001023}
1024#endif
1025
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 * Return the name of the executed function.
1028 */
1029 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001030func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001032 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033}
1034
1035/*
1036 * Return the address holding the next breakpoint line for a funccall cookie.
1037 */
1038 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001039func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
Bram Moolenaar33570922005-01-25 22:26:29 +00001041 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042}
1043
1044/*
1045 * Return the address holding the debug tick for a funccall cookie.
1046 */
1047 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001048func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
Bram Moolenaar33570922005-01-25 22:26:29 +00001050 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
1052
1053/*
1054 * Return the nesting level for a funccall cookie.
1055 */
1056 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001057func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
Bram Moolenaar33570922005-01-25 22:26:29 +00001059 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060}
1061
1062/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001063funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001065/* pointer to list of previously used funccal, still around because some
1066 * item in it is still being used. */
1067funccall_T *previous_funccal = NULL;
1068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069/*
1070 * Return TRUE when a function was ended by a ":return" command.
1071 */
1072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001073current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
1075 return current_funccal->returned;
1076}
1077
1078
1079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * Set an internal variable to a string value. Creates the variable if it does
1081 * not already exist.
1082 */
1083 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001084set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001086 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001087 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088
1089 val = vim_strsave(value);
1090 if (val != NULL)
1091 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001092 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001093 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001095 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001096 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098 }
1099}
1100
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001102static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103static char_u *redir_endp = NULL;
1104static char_u *redir_varname = NULL;
1105
1106/*
1107 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001108 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 * Returns OK if successfully completed the setup. FAIL otherwise.
1110 */
1111 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001112var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113{
1114 int save_emsg;
1115 int err;
1116 typval_T tv;
1117
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001119 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 {
1121 EMSG(_(e_invarg));
1122 return FAIL;
1123 }
1124
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001125 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 redir_varname = vim_strsave(name);
1127 if (redir_varname == NULL)
1128 return FAIL;
1129
1130 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1131 if (redir_lval == NULL)
1132 {
1133 var_redir_stop();
1134 return FAIL;
1135 }
1136
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137 /* The output is stored in growarray "redir_ga" until redirection ends. */
1138 ga_init2(&redir_ga, (int)sizeof(char), 500);
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001141 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001142 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1144 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 if (redir_endp != NULL && *redir_endp != NUL)
1147 /* Trailing characters are present after the variable name */
1148 EMSG(_(e_trailing));
1149 else
1150 EMSG(_(e_invarg));
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 }
1155
1156 /* check if we can write to the variable: set it to or append an empty
1157 * string */
1158 save_emsg = did_emsg;
1159 did_emsg = FALSE;
1160 tv.v_type = VAR_STRING;
1161 tv.vval.v_string = (char_u *)"";
1162 if (append)
1163 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1164 else
1165 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001166 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001168 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 if (err)
1170 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001171 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 var_redir_stop();
1173 return FAIL;
1174 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175
1176 return OK;
1177}
1178
1179/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001180 * Append "value[value_len]" to the variable set by var_redir_start().
1181 * The actual appending is postponed until redirection ends, because the value
1182 * appended may in fact be the string we write to, changing it may cause freed
1183 * memory to be used:
1184 * :redir => foo
1185 * :let foo
1186 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 */
1188 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001189var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192
1193 if (redir_lval == NULL)
1194 return;
1195
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001197 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001199 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001201 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001202 {
1203 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001204 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001205 }
1206 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208}
1209
1210/*
1211 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001212 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001213 */
1214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001215var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001217 typval_T tv;
1218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001219 if (redir_lval != NULL)
1220 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001221 /* If there was no error: assign the text to the variable. */
1222 if (redir_endp != NULL)
1223 {
1224 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1225 tv.v_type = VAR_STRING;
1226 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001227 /* Call get_lval() again, if it's inside a Dict or List it may
1228 * have changed. */
1229 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001230 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001231 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1232 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1233 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001234 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001235
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001236 /* free the collected output */
1237 vim_free(redir_ga.ga_data);
1238 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001239
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001240 vim_free(redir_lval);
1241 redir_lval = NULL;
1242 }
1243 vim_free(redir_varname);
1244 redir_varname = NULL;
1245}
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247# if defined(FEAT_MBYTE) || defined(PROTO)
1248 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001249eval_charconvert(
1250 char_u *enc_from,
1251 char_u *enc_to,
1252 char_u *fname_from,
1253 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254{
1255 int err = FALSE;
1256
1257 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1258 set_vim_var_string(VV_CC_TO, enc_to, -1);
1259 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1260 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1261 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1262 err = TRUE;
1263 set_vim_var_string(VV_CC_FROM, NULL, -1);
1264 set_vim_var_string(VV_CC_TO, NULL, -1);
1265 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1266 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1267
1268 if (err)
1269 return FAIL;
1270 return OK;
1271}
1272# endif
1273
1274# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1275 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001276eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277{
1278 int err = FALSE;
1279
1280 set_vim_var_string(VV_FNAME_IN, fname, -1);
1281 set_vim_var_string(VV_CMDARG, args, -1);
1282 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1283 err = TRUE;
1284 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1285 set_vim_var_string(VV_CMDARG, NULL, -1);
1286
1287 if (err)
1288 {
1289 mch_remove(fname);
1290 return FAIL;
1291 }
1292 return OK;
1293}
1294# endif
1295
1296# if defined(FEAT_DIFF) || defined(PROTO)
1297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001298eval_diff(
1299 char_u *origfile,
1300 char_u *newfile,
1301 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302{
1303 int err = FALSE;
1304
1305 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1306 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1307 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1308 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1309 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1310 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1311 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1312}
1313
1314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001315eval_patch(
1316 char_u *origfile,
1317 char_u *difffile,
1318 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 int err;
1321
1322 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1323 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1324 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1325 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1326 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1327 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1328 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1329}
1330# endif
1331
1332/*
1333 * Top level evaluation function, returning a boolean.
1334 * Sets "error" to TRUE if there was an error.
1335 * Return TRUE or FALSE.
1336 */
1337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001338eval_to_bool(
1339 char_u *arg,
1340 int *error,
1341 char_u **nextcmd,
1342 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 int retval = FALSE;
1346
1347 if (skip)
1348 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 else
1352 {
1353 *error = FALSE;
1354 if (!skip)
1355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001356 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 }
1360 if (skip)
1361 --emsg_skip;
1362
1363 return retval;
1364}
1365
1366/*
1367 * Top level evaluation function, returning a string. If "skip" is TRUE,
1368 * only parsing to "nextcmd" is done, without reporting errors. Return
1369 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1370 */
1371 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001372eval_to_string_skip(
1373 char_u *arg,
1374 char_u **nextcmd,
1375 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
Bram Moolenaar33570922005-01-25 22:26:29 +00001377 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 char_u *retval;
1379
1380 if (skip)
1381 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 retval = NULL;
1384 else
1385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 retval = vim_strsave(get_tv_string(&tv));
1387 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 if (skip)
1390 --emsg_skip;
1391
1392 return retval;
1393}
1394
1395/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001396 * Skip over an expression at "*pp".
1397 * Return FAIL for an error, OK otherwise.
1398 */
1399 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001400skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001401{
Bram Moolenaar33570922005-01-25 22:26:29 +00001402 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001403
1404 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001405 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001406}
1407
1408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001410 * When "convert" is TRUE convert a List into a sequence of lines and convert
1411 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 * Return pointer to allocated memory, or NULL for failure.
1413 */
1414 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001415eval_to_string(
1416 char_u *arg,
1417 char_u **nextcmd,
1418 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001422 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001423#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001424 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 retval = NULL;
1429 else
1430 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001431 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001432 {
1433 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001434 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001435 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001436 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001437 if (tv.vval.v_list->lv_len > 0)
1438 ga_append(&ga, NL);
1439 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001440 ga_append(&ga, NUL);
1441 retval = (char_u *)ga.ga_data;
1442 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001443#ifdef FEAT_FLOAT
1444 else if (convert && tv.v_type == VAR_FLOAT)
1445 {
1446 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1447 retval = vim_strsave(numbuf);
1448 }
1449#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001450 else
1451 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001452 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
1454
1455 return retval;
1456}
1457
1458/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001459 * Call eval_to_string() without using current local variables and using
1460 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 */
1462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001463eval_to_string_safe(
1464 char_u *arg,
1465 char_u **nextcmd,
1466 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 char_u *retval;
1469 void *save_funccalp;
1470
1471 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001472 if (use_sandbox)
1473 ++sandbox;
1474 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001475 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001476 if (use_sandbox)
1477 --sandbox;
1478 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 restore_funccal(save_funccalp);
1480 return retval;
1481}
1482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483/*
1484 * Top level evaluation function, returning a number.
1485 * Evaluates "expr" silently.
1486 * Returns -1 for an error.
1487 */
1488 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001489eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
Bram Moolenaar33570922005-01-25 22:26:29 +00001491 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001493 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
1495 ++emsg_off;
1496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 retval = -1;
1499 else
1500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001501 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001502 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504 --emsg_off;
1505
1506 return retval;
1507}
1508
Bram Moolenaara40058a2005-07-11 22:42:07 +00001509/*
1510 * Prepare v: variable "idx" to be used.
1511 * Save the current typeval in "save_tv".
1512 * When not used yet add the variable to the v: hashtable.
1513 */
1514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001515prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001516{
1517 *save_tv = vimvars[idx].vv_tv;
1518 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1519 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1520}
1521
1522/*
1523 * Restore v: variable "idx" to typeval "save_tv".
1524 * When no longer defined, remove the variable from the v: hashtable.
1525 */
1526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001527restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001528{
1529 hashitem_T *hi;
1530
Bram Moolenaara40058a2005-07-11 22:42:07 +00001531 vimvars[idx].vv_tv = *save_tv;
1532 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1533 {
1534 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1535 if (HASHITEM_EMPTY(hi))
1536 EMSG2(_(e_intern2), "restore_vimvar()");
1537 else
1538 hash_remove(&vimvarht, hi);
1539 }
1540}
1541
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001542#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001543/*
1544 * Evaluate an expression to a list with suggestions.
1545 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001546 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001547 */
1548 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001549eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001550{
1551 typval_T save_val;
1552 typval_T rettv;
1553 list_T *list = NULL;
1554 char_u *p = skipwhite(expr);
1555
1556 /* Set "v:val" to the bad word. */
1557 prepare_vimvar(VV_VAL, &save_val);
1558 vimvars[VV_VAL].vv_type = VAR_STRING;
1559 vimvars[VV_VAL].vv_str = badword;
1560 if (p_verbose == 0)
1561 ++emsg_off;
1562
1563 if (eval1(&p, &rettv, TRUE) == OK)
1564 {
1565 if (rettv.v_type != VAR_LIST)
1566 clear_tv(&rettv);
1567 else
1568 list = rettv.vval.v_list;
1569 }
1570
1571 if (p_verbose == 0)
1572 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001573 restore_vimvar(VV_VAL, &save_val);
1574
1575 return list;
1576}
1577
1578/*
1579 * "list" is supposed to contain two items: a word and a number. Return the
1580 * word in "pp" and the number as the return value.
1581 * Return -1 if anything isn't right.
1582 * Used to get the good word and score from the eval_spell_expr() result.
1583 */
1584 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001585get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001586{
1587 listitem_T *li;
1588
1589 li = list->lv_first;
1590 if (li == NULL)
1591 return -1;
1592 *pp = get_tv_string(&li->li_tv);
1593
1594 li = li->li_next;
1595 if (li == NULL)
1596 return -1;
1597 return get_tv_number(&li->li_tv);
1598}
1599#endif
1600
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001601/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001602 * Top level evaluation function.
1603 * Returns an allocated typval_T with the result.
1604 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001605 */
1606 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001607eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001608{
1609 typval_T *tv;
1610
1611 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001612 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001613 {
1614 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001615 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001616 }
1617
1618 return tv;
1619}
1620
1621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 * Uses argv[argc] for the function arguments. Only Number and String
1625 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001629call_vim_function(
1630 char_u *func,
1631 int argc,
1632 char_u **argv,
1633 int safe, /* use the sandbox */
1634 int str_arg_only, /* all arguments are strings */
1635 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
Bram Moolenaar33570922005-01-25 22:26:29 +00001637 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 long n;
1639 int len;
1640 int i;
1641 int doesrange;
1642 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001645 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649 for (i = 0; i < argc; i++)
1650 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001651 /* Pass a NULL or empty argument as an empty string */
1652 if (argv[i] == NULL || *argv[i] == NUL)
1653 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001654 argvars[i].v_type = VAR_STRING;
1655 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001656 continue;
1657 }
1658
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001659 if (str_arg_only)
1660 len = 0;
1661 else
1662 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001663 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (len != 0 && len == (int)STRLEN(argv[i]))
1665 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001666 argvars[i].v_type = VAR_NUMBER;
1667 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669 else
1670 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001671 argvars[i].v_type = VAR_STRING;
1672 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 }
1674 }
1675
1676 if (safe)
1677 {
1678 save_funccalp = save_funccal();
1679 ++sandbox;
1680 }
1681
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1683 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (safe)
1687 {
1688 --sandbox;
1689 restore_funccal(save_funccalp);
1690 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691 vim_free(argvars);
1692
1693 if (ret == FAIL)
1694 clear_tv(rettv);
1695
1696 return ret;
1697}
1698
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001699/*
1700 * Call vimL function "func" and return the result as a number.
1701 * Returns -1 when calling the function fails.
1702 * Uses argv[argc] for the function arguments.
1703 */
1704 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001705call_func_retnr(
1706 char_u *func,
1707 int argc,
1708 char_u **argv,
1709 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001710{
1711 typval_T rettv;
1712 long retval;
1713
1714 /* All arguments are passed as strings, no conversion to number. */
1715 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1716 return -1;
1717
1718 retval = get_tv_number_chk(&rettv, NULL);
1719 clear_tv(&rettv);
1720 return retval;
1721}
1722
1723#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1724 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1725
Bram Moolenaar4f688582007-07-24 12:34:30 +00001726# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001728 * Call vimL function "func" and return the result as a string.
1729 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730 * Uses argv[argc] for the function arguments.
1731 */
1732 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001733call_func_retstr(
1734 char_u *func,
1735 int argc,
1736 char_u **argv,
1737 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738{
1739 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001740 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001742 /* All arguments are passed as strings, no conversion to number. */
1743 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744 return NULL;
1745
1746 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 return retval;
1749}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001750# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001752/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001753 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001754 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001755 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756 */
1757 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001758call_func_retlist(
1759 char_u *func,
1760 int argc,
1761 char_u **argv,
1762 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001763{
1764 typval_T rettv;
1765
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001766 /* All arguments are passed as strings, no conversion to number. */
1767 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001768 return NULL;
1769
1770 if (rettv.v_type != VAR_LIST)
1771 {
1772 clear_tv(&rettv);
1773 return NULL;
1774 }
1775
1776 return rettv.vval.v_list;
1777}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778#endif
1779
1780/*
1781 * Save the current function call pointer, and set it to NULL.
1782 * Used when executing autocommands and for ":source".
1783 */
1784 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001787 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 current_funccal = NULL;
1790 return (void *)fc;
1791}
1792
1793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001794restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001796 funccall_T *fc = (funccall_T *)vfc;
1797
1798 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799}
1800
Bram Moolenaar05159a02005-02-26 23:04:13 +00001801#if defined(FEAT_PROFILE) || defined(PROTO)
1802/*
1803 * Prepare profiling for entering a child or something else that is not
1804 * counted for the script/function itself.
1805 * Should always be called in pair with prof_child_exit().
1806 */
1807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001808prof_child_enter(
1809 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001810{
1811 funccall_T *fc = current_funccal;
1812
1813 if (fc != NULL && fc->func->uf_profiling)
1814 profile_start(&fc->prof_child);
1815 script_prof_save(tm);
1816}
1817
1818/*
1819 * Take care of time spent in a child.
1820 * Should always be called after prof_child_enter().
1821 */
1822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823prof_child_exit(
1824 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001825{
1826 funccall_T *fc = current_funccal;
1827
1828 if (fc != NULL && fc->func->uf_profiling)
1829 {
1830 profile_end(&fc->prof_child);
1831 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1832 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1833 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1834 }
1835 script_prof_restore(tm);
1836}
1837#endif
1838
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840#ifdef FEAT_FOLDING
1841/*
1842 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1843 * it in "*cp". Doesn't give error messages.
1844 */
1845 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001846eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
Bram Moolenaar33570922005-01-25 22:26:29 +00001848 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 int retval;
1850 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001851 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1852 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
1854 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001855 if (use_sandbox)
1856 ++sandbox;
1857 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 retval = 0;
1861 else
1862 {
1863 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 if (tv.v_type == VAR_NUMBER)
1865 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001866 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 retval = 0;
1868 else
1869 {
1870 /* If the result is a string, check if there is a non-digit before
1871 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (!VIM_ISDIGIT(*s) && *s != '-')
1874 *cp = *s++;
1875 retval = atol((char *)s);
1876 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 }
1879 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001880 if (use_sandbox)
1881 --sandbox;
1882 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884 return retval;
1885}
1886#endif
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 * ":let" list all variable values
1890 * ":let var1 var2" list variable values
1891 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 * ":let var += expr" assignment command.
1893 * ":let var -= expr" assignment command.
1894 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 */
1897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001898ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899{
1900 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001902 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 int var_count = 0;
1905 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001907 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001908 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
Bram Moolenaardb552d602006-03-23 22:59:57 +00001910 argend = skip_var_list(arg, &var_count, &semicolon);
1911 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001913 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1914 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001915 expr = skipwhite(argend);
1916 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1917 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001919 /*
1920 * ":let" without "=": list variables
1921 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 if (*arg == '[')
1923 EMSG(_(e_invarg));
1924 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001926 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001930 list_glob_vars(&first);
1931 list_buf_vars(&first);
1932 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001933#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001934 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001935#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001936 list_script_vars(&first);
1937 list_func_vars(&first);
1938 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 eap->nextcmd = check_nextcmd(arg);
1941 }
1942 else
1943 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 op[0] = '=';
1945 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001946 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001948 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1949 op[0] = *expr; /* +=, -= or .= */
1950 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001952 else
1953 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (eap->skip)
1956 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (eap->skip)
1959 {
1960 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 --emsg_skip;
1963 }
1964 else if (i != FAIL)
1965 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970 }
1971}
1972
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973/*
1974 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1975 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 * When "nextchars" is not NULL it points to a string with characters that
1977 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1978 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 * Returns OK or FAIL;
1980 */
1981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001982ex_let_vars(
1983 char_u *arg_start,
1984 typval_T *tv,
1985 int copy, /* copy values from "tv", don't move */
1986 int semicolon, /* from skip_var_list() */
1987 int var_count, /* from skip_var_list() */
1988 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989{
1990 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001991 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001993 listitem_T *item;
1994 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995
1996 if (*arg != '[')
1997 {
1998 /*
1999 * ":let var = expr" or ":for var in list"
2000 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 return FAIL;
2003 return OK;
2004 }
2005
2006 /*
2007 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2008 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002009 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 {
2011 EMSG(_(e_listreq));
2012 return FAIL;
2013 }
2014
2015 i = list_len(l);
2016 if (semicolon == 0 && var_count < i)
2017 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002018 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 return FAIL;
2020 }
2021 if (var_count - semicolon > i)
2022 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002023 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 return FAIL;
2025 }
2026
2027 item = l->lv_first;
2028 while (*arg != ']')
2029 {
2030 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002031 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 item = item->li_next;
2033 if (arg == NULL)
2034 return FAIL;
2035
2036 arg = skipwhite(arg);
2037 if (*arg == ';')
2038 {
2039 /* Put the rest of the list (may be empty) in the var after ';'.
2040 * Create a new list for this. */
2041 l = list_alloc();
2042 if (l == NULL)
2043 return FAIL;
2044 while (item != NULL)
2045 {
2046 list_append_tv(l, &item->li_tv);
2047 item = item->li_next;
2048 }
2049
2050 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002051 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 ltv.vval.v_list = l;
2053 l->lv_refcount = 1;
2054
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002055 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2056 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 clear_tv(&ltv);
2058 if (arg == NULL)
2059 return FAIL;
2060 break;
2061 }
2062 else if (*arg != ',' && *arg != ']')
2063 {
2064 EMSG2(_(e_intern2), "ex_let_vars()");
2065 return FAIL;
2066 }
2067 }
2068
2069 return OK;
2070}
2071
2072/*
2073 * Skip over assignable variable "var" or list of variables "[var, var]".
2074 * Used for ":let varvar = expr" and ":for varvar in expr".
2075 * For "[var, var]" increment "*var_count" for each variable.
2076 * for "[var, var; var]" set "semicolon".
2077 * Return NULL for an error.
2078 */
2079 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002080skip_var_list(
2081 char_u *arg,
2082 int *var_count,
2083 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084{
2085 char_u *p, *s;
2086
2087 if (*arg == '[')
2088 {
2089 /* "[var, var]": find the matching ']'. */
2090 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002091 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092 {
2093 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2094 s = skip_var_one(p);
2095 if (s == p)
2096 {
2097 EMSG2(_(e_invarg2), p);
2098 return NULL;
2099 }
2100 ++*var_count;
2101
2102 p = skipwhite(s);
2103 if (*p == ']')
2104 break;
2105 else if (*p == ';')
2106 {
2107 if (*semicolon == 1)
2108 {
2109 EMSG(_("Double ; in list of variables"));
2110 return NULL;
2111 }
2112 *semicolon = 1;
2113 }
2114 else if (*p != ',')
2115 {
2116 EMSG2(_(e_invarg2), p);
2117 return NULL;
2118 }
2119 }
2120 return p + 1;
2121 }
2122 else
2123 return skip_var_one(arg);
2124}
2125
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002127 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002128 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002129 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002131skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002132{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002133 if (*arg == '@' && arg[1] != NUL)
2134 return arg + 2;
2135 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2136 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002137}
2138
Bram Moolenaara7043832005-01-21 11:56:39 +00002139/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002140 * List variables for hashtab "ht" with prefix "prefix".
2141 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002142 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002144list_hashtable_vars(
2145 hashtab_T *ht,
2146 char_u *prefix,
2147 int empty,
2148 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar33570922005-01-25 22:26:29 +00002150 hashitem_T *hi;
2151 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002152 int todo;
2153
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002154 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002155 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2156 {
2157 if (!HASHITEM_EMPTY(hi))
2158 {
2159 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 di = HI2DI(hi);
2161 if (empty || di->di_tv.v_type != VAR_STRING
2162 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002164 }
2165 }
2166}
2167
2168/*
2169 * List global variables.
2170 */
2171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002172list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002173{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002175}
2176
2177/*
2178 * List buffer variables.
2179 */
2180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002181list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002182{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002183 char_u numbuf[NUMBUFLEN];
2184
Bram Moolenaar429fa852013-04-15 12:27:36 +02002185 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002187
2188 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2190 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002191}
2192
2193/*
2194 * List window variables.
2195 */
2196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002197list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002198{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002199 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002201}
2202
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203#ifdef FEAT_WINDOWS
2204/*
2205 * List tab page variables.
2206 */
2207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002209{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002210 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002211 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002212}
2213#endif
2214
Bram Moolenaara7043832005-01-21 11:56:39 +00002215/*
2216 * List Vim variables.
2217 */
2218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002219list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002221 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222}
2223
2224/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002225 * List script-local variables, if there is a script.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002229{
2230 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2232 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002233}
2234
2235/*
2236 * List function variables, if there is a function.
2237 */
2238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002240{
2241 if (current_funccal != NULL)
2242 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002243 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002244}
2245
2246/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 * List variables in "arg".
2248 */
2249 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002250list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251{
2252 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 char_u *name_start;
2256 char_u *arg_subsc;
2257 char_u *tofree;
2258 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259
2260 while (!ends_excmd(*arg) && !got_int)
2261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002264 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2266 {
2267 emsg_severe = TRUE;
2268 EMSG(_(e_trailing));
2269 break;
2270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 /* get_name_len() takes care of expanding curly braces */
2275 name_start = name = arg;
2276 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2277 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 /* This is mainly to keep test 49 working: when expanding
2280 * curly braces fails overrule the exception error message. */
2281 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 emsg_severe = TRUE;
2284 EMSG2(_(e_invarg2), arg);
2285 break;
2286 }
2287 error = TRUE;
2288 }
2289 else
2290 {
2291 if (tofree != NULL)
2292 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002293 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 else
2296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 /* handle d.key, l[idx], f(expr) */
2298 arg_subsc = arg;
2299 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002304 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 case 'g': list_glob_vars(first); break;
2308 case 'b': list_buf_vars(first); break;
2309 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002310#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002311 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002312#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002313 case 'v': list_vim_vars(first); break;
2314 case 's': list_script_vars(first); break;
2315 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 default:
2317 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002318 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002319 }
2320 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 {
2322 char_u numbuf[NUMBUFLEN];
2323 char_u *tf;
2324 int c;
2325 char_u *s;
2326
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002327 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328 c = *arg;
2329 *arg = NUL;
2330 list_one_var_a((char_u *)"",
2331 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002332 tv.v_type,
2333 s == NULL ? (char_u *)"" : s,
2334 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335 *arg = c;
2336 vim_free(tf);
2337 }
2338 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
2341 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002342
2343 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345
2346 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 }
2348
2349 return arg;
2350}
2351
2352/*
2353 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2354 * Returns a pointer to the char just after the var name.
2355 * Returns NULL if there is an error.
2356 */
2357 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002358ex_let_one(
2359 char_u *arg, /* points to variable name */
2360 typval_T *tv, /* value to assign to variable */
2361 int copy, /* copy value from "tv" */
2362 char_u *endchars, /* valid chars after variable name or NULL */
2363 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364{
2365 int c1;
2366 char_u *name;
2367 char_u *p;
2368 char_u *arg_end = NULL;
2369 int len;
2370 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372
2373 /*
2374 * ":let $VAR = expr": Set environment variable.
2375 */
2376 if (*arg == '$')
2377 {
2378 /* Find the end of the name. */
2379 ++arg;
2380 name = arg;
2381 len = get_env_len(&arg);
2382 if (len == 0)
2383 EMSG2(_(e_invarg2), name - 1);
2384 else
2385 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 if (op != NULL && (*op == '+' || *op == '-'))
2387 EMSG2(_(e_letwrong), op);
2388 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002389 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002391 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 {
2393 c1 = name[len];
2394 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 p = get_tv_string_chk(tv);
2396 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 {
2398 int mustfree = FALSE;
2399 char_u *s = vim_getenv(name, &mustfree);
2400
2401 if (s != NULL)
2402 {
2403 p = tofree = concat_str(s, p);
2404 if (mustfree)
2405 vim_free(s);
2406 }
2407 }
2408 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002411 if (STRICMP(name, "HOME") == 0)
2412 init_homedir();
2413 else if (didset_vim && STRICMP(name, "VIM") == 0)
2414 didset_vim = FALSE;
2415 else if (didset_vimruntime
2416 && STRICMP(name, "VIMRUNTIME") == 0)
2417 didset_vimruntime = FALSE;
2418 arg_end = arg;
2419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002421 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 }
2423 }
2424 }
2425
2426 /*
2427 * ":let &option = expr": Set option value.
2428 * ":let &l:option = expr": Set local option value.
2429 * ":let &g:option = expr": Set global option value.
2430 */
2431 else if (*arg == '&')
2432 {
2433 /* Find the end of the name. */
2434 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435 if (p == NULL || (endchars != NULL
2436 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 EMSG(_(e_letunexp));
2438 else
2439 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 long n;
2441 int opt_type;
2442 long numval;
2443 char_u *stringval = NULL;
2444 char_u *s;
2445
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 c1 = *p;
2447 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448
2449 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 s = get_tv_string_chk(tv); /* != NULL if number or string */
2451 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 {
2453 opt_type = get_option_value(arg, &numval,
2454 &stringval, opt_flags);
2455 if ((opt_type == 1 && *op == '.')
2456 || (opt_type == 0 && *op != '.'))
2457 EMSG2(_(e_letwrong), op);
2458 else
2459 {
2460 if (opt_type == 1) /* number */
2461 {
2462 if (*op == '+')
2463 n = numval + n;
2464 else
2465 n = numval - n;
2466 }
2467 else if (opt_type == 0 && stringval != NULL) /* string */
2468 {
2469 s = concat_str(stringval, s);
2470 vim_free(stringval);
2471 stringval = s;
2472 }
2473 }
2474 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002475 if (s != NULL)
2476 {
2477 set_option_value(arg, n, s, opt_flags);
2478 arg_end = p;
2479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
2483 }
2484
2485 /*
2486 * ":let @r = expr": Set register contents.
2487 */
2488 else if (*arg == '@')
2489 {
2490 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 if (op != NULL && (*op == '+' || *op == '-'))
2492 EMSG2(_(e_letwrong), op);
2493 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002494 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 EMSG(_(e_letunexp));
2496 else
2497 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002498 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 char_u *s;
2500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 p = get_tv_string_chk(tv);
2502 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002504 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 if (s != NULL)
2506 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 vim_free(s);
2509 }
2510 }
2511 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002512 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 arg_end = arg + 1;
2515 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002516 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 }
2518 }
2519
2520 /*
2521 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002524 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002526 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002528 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2532 EMSG(_(e_letunexp));
2533 else
2534 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002535 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 arg_end = p;
2537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 }
2541
2542 else
2543 EMSG2(_(e_invarg2), arg);
2544
2545 return arg_end;
2546}
2547
2548/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2550 */
2551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002552check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553{
2554 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2555 {
2556 EMSG2(_(e_readonlyvar), arg);
2557 return TRUE;
2558 }
2559 return FALSE;
2560}
2561
2562/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 * Get an lval: variable, Dict item or List item that can be assigned a value
2564 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2565 * "name.key", "name.key[expr]" etc.
2566 * Indexing only works if "name" is an existing List or Dictionary.
2567 * "name" points to the start of the name.
2568 * If "rettv" is not NULL it points to the value to be assigned.
2569 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2570 * wrong; must end in space or cmd separator.
2571 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002572 * flags:
2573 * GLV_QUIET: do not give error messages
2574 * GLV_NO_AUTOLOAD: do not use script autoloading
2575 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002577 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579 */
2580 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002581get_lval(
2582 char_u *name,
2583 typval_T *rettv,
2584 lval_T *lp,
2585 int unlet,
2586 int skip,
2587 int flags, /* GLV_ values */
2588 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 char_u *expr_start, *expr_end;
2592 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 dictitem_T *v;
2594 typval_T var1;
2595 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002600 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002604 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605
2606 if (skip)
2607 {
2608 /* When skipping just find the end of the name. */
2609 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002610 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 }
2612
2613 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002614 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (expr_start != NULL)
2616 {
2617 /* Don't expand the name when we already know there is an error. */
2618 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2619 && *p != '[' && *p != '.')
2620 {
2621 EMSG(_(e_trailing));
2622 return NULL;
2623 }
2624
2625 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2626 if (lp->ll_exp_name == NULL)
2627 {
2628 /* Report an invalid expression in braces, unless the
2629 * expression evaluation has been cancelled due to an
2630 * aborting error, an interrupt, or an exception. */
2631 if (!aborting() && !quiet)
2632 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002633 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 EMSG2(_(e_invarg2), name);
2635 return NULL;
2636 }
2637 }
2638 lp->ll_name = lp->ll_exp_name;
2639 }
2640 else
2641 lp->ll_name = name;
2642
2643 /* Without [idx] or .key we are done. */
2644 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2645 return p;
2646
2647 cc = *p;
2648 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002649 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (v == NULL && !quiet)
2651 EMSG2(_(e_undefvar), lp->ll_name);
2652 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 if (v == NULL)
2654 return NULL;
2655
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 /*
2657 * Loop until no more [idx] or .key is following.
2658 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002659 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2663 && !(lp->ll_tv->v_type == VAR_DICT
2664 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_("E689: Can only index a List or Dictionary"));
2668 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (!quiet)
2673 EMSG(_("E708: [:] must come last"));
2674 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002675 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 len = -1;
2678 if (*p == '.')
2679 {
2680 key = p + 1;
2681 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2682 ;
2683 if (len == 0)
2684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
2686 EMSG(_(e_emptykey));
2687 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689 p = key + len;
2690 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 else
2692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (*p == ':')
2696 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 else
2698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 empty1 = FALSE;
2700 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002702 if (get_tv_string_chk(&var1) == NULL)
2703 {
2704 /* not a number or string */
2705 clear_tv(&var1);
2706 return NULL;
2707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
2709
2710 /* Optionally get the second index [ :expr]. */
2711 if (*p == ':')
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002716 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 if (!empty1)
2718 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (rettv != NULL && (rettv->v_type != VAR_LIST
2722 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (!quiet)
2725 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (!empty1)
2727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 }
2730 p = skipwhite(p + 1);
2731 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 else
2734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 if (!empty1)
2739 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002742 if (get_tv_string_chk(&var2) == NULL)
2743 {
2744 /* not a number or string */
2745 if (!empty1)
2746 clear_tv(&var1);
2747 clear_tv(&var2);
2748 return NULL;
2749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002755
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002757 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (!quiet)
2759 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 if (!empty1)
2761 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 }
2766
2767 /* Skip to past ']'. */
2768 ++p;
2769 }
2770
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 {
2773 if (len == -1)
2774 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002776 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 if (*key == NUL)
2778 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 if (!quiet)
2780 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
2784 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785 lp->ll_list = NULL;
2786 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002787 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002788
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002789 /* When assigning to a scope dictionary check that a function and
2790 * variable name is valid (only variable name unless it is l: or
2791 * g: dictionary). Disallow overwriting a builtin function. */
2792 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002794 int prevval;
2795 int wrong;
2796
2797 if (len != -1)
2798 {
2799 prevval = key[len];
2800 key[len] = NUL;
2801 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002802 else
2803 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002804 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2805 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002806 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002807 || !valid_varname(key);
2808 if (len != -1)
2809 key[len] = prevval;
2810 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 return NULL;
2812 }
2813
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002816 /* Can't add "v:" variable. */
2817 if (lp->ll_dict == &vimvardict)
2818 {
2819 EMSG2(_(e_illvar), name);
2820 return NULL;
2821 }
2822
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002823 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002827 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 if (len == -1)
2829 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 }
2832 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 if (len == -1)
2837 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 p = NULL;
2840 break;
2841 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002842 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002843 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002844 return NULL;
2845
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 if (len == -1)
2847 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 }
2850 else
2851 {
2852 /*
2853 * Get the number and item for the only or first index of the List.
2854 */
2855 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 else
2858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002859 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 clear_tv(&var1);
2861 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002862 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_list = lp->ll_tv->vval.v_list;
2864 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2865 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002867 if (lp->ll_n1 < 0)
2868 {
2869 lp->ll_n1 = 0;
2870 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2871 }
2872 }
2873 if (lp->ll_li == NULL)
2874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 }
2881
2882 /*
2883 * May need to find the item or absolute index for the second
2884 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 * When no index given: "lp->ll_empty2" is TRUE.
2886 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002890 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002895 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002896 {
2897 if (!quiet)
2898 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002900 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002902 }
2903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2905 if (lp->ll_n1 < 0)
2906 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2907 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002908 {
2909 if (!quiet)
2910 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002912 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002913 }
2914
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917 }
2918
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 return p;
2920}
2921
2922/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 */
2925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927{
2928 vim_free(lp->ll_exp_name);
2929 vim_free(lp->ll_newkey);
2930}
2931
2932/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002933 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 */
2937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002938set_var_lval(
2939 lval_T *lp,
2940 char_u *endp,
2941 typval_T *rettv,
2942 int copy,
2943 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944{
2945 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 listitem_T *ri;
2947 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948
2949 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 cc = *endp;
2954 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 if (op != NULL && *op != '=')
2956 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002957 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002959 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002960 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002961 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002962 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002964 if ((di == NULL
2965 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2966 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2967 FALSE)))
2968 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002969 set_var(lp->ll_name, &tv, FALSE);
2970 clear_tv(&tv);
2971 }
2972 }
2973 else
2974 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002978 else if (tv_check_lock(lp->ll_newkey == NULL
2979 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002980 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002981 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 else if (lp->ll_range)
2983 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002984 listitem_T *ll_li = lp->ll_li;
2985 int ll_n1 = lp->ll_n1;
2986
2987 /*
2988 * Check whether any of the list items is locked
2989 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002990 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002991 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002992 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002993 return;
2994 ri = ri->li_next;
2995 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2996 break;
2997 ll_li = ll_li->li_next;
2998 ++ll_n1;
2999 }
3000
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 /*
3002 * Assign the List values to the list items.
3003 */
3004 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 if (op != NULL && *op != '=')
3007 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3008 else
3009 {
3010 clear_tv(&lp->ll_li->li_tv);
3011 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3012 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 ri = ri->li_next;
3014 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3015 break;
3016 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003019 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 ri = NULL;
3022 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003023 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003024 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 lp->ll_li = lp->ll_li->li_next;
3026 ++lp->ll_n1;
3027 }
3028 if (ri != NULL)
3029 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003030 else if (lp->ll_empty2
3031 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 : lp->ll_n1 != lp->ll_n2)
3033 EMSG(_("E711: List value has not enough items"));
3034 }
3035 else
3036 {
3037 /*
3038 * Assign to a List or Dictionary item.
3039 */
3040 if (lp->ll_newkey != NULL)
3041 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 if (op != NULL && *op != '=')
3043 {
3044 EMSG2(_(e_letwrong), op);
3045 return;
3046 }
3047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003049 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 if (di == NULL)
3051 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003052 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3053 {
3054 vim_free(di);
3055 return;
3056 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003058 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 else if (op != NULL && *op != '=')
3060 {
3061 tv_op(lp->ll_tv, rettv, op);
3062 return;
3063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003065 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003066
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 /*
3068 * Assign the value to the variable or list item.
3069 */
3070 if (copy)
3071 copy_tv(rettv, lp->ll_tv);
3072 else
3073 {
3074 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003075 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003076 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003077 }
3078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079}
3080
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003081/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3083 * Returns OK or FAIL.
3084 */
3085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087{
3088 long n;
3089 char_u numbuf[NUMBUFLEN];
3090 char_u *s;
3091
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003092 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3093 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3094 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 {
3096 switch (tv1->v_type)
3097 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003098 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 case VAR_DICT:
3100 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003101 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003102 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003103 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003104 break;
3105
3106 case VAR_LIST:
3107 if (*op != '+' || tv2->v_type != VAR_LIST)
3108 break;
3109 /* List += List */
3110 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3111 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3112 return OK;
3113
3114 case VAR_NUMBER:
3115 case VAR_STRING:
3116 if (tv2->v_type == VAR_LIST)
3117 break;
3118 if (*op == '+' || *op == '-')
3119 {
3120 /* nr += nr or nr -= nr*/
3121 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122#ifdef FEAT_FLOAT
3123 if (tv2->v_type == VAR_FLOAT)
3124 {
3125 float_T f = n;
3126
3127 if (*op == '+')
3128 f += tv2->vval.v_float;
3129 else
3130 f -= tv2->vval.v_float;
3131 clear_tv(tv1);
3132 tv1->v_type = VAR_FLOAT;
3133 tv1->vval.v_float = f;
3134 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136#endif
3137 {
3138 if (*op == '+')
3139 n += get_tv_number(tv2);
3140 else
3141 n -= get_tv_number(tv2);
3142 clear_tv(tv1);
3143 tv1->v_type = VAR_NUMBER;
3144 tv1->vval.v_number = n;
3145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 }
3147 else
3148 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149 if (tv2->v_type == VAR_FLOAT)
3150 break;
3151
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003152 /* str .= str */
3153 s = get_tv_string(tv1);
3154 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3155 clear_tv(tv1);
3156 tv1->v_type = VAR_STRING;
3157 tv1->vval.v_string = s;
3158 }
3159 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003160
3161#ifdef FEAT_FLOAT
3162 case VAR_FLOAT:
3163 {
3164 float_T f;
3165
3166 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3167 && tv2->v_type != VAR_NUMBER
3168 && tv2->v_type != VAR_STRING))
3169 break;
3170 if (tv2->v_type == VAR_FLOAT)
3171 f = tv2->vval.v_float;
3172 else
3173 f = get_tv_number(tv2);
3174 if (*op == '+')
3175 tv1->vval.v_float += f;
3176 else
3177 tv1->vval.v_float -= f;
3178 }
3179 return OK;
3180#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 }
3182 }
3183
3184 EMSG2(_(e_letwrong), op);
3185 return FAIL;
3186}
3187
3188/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 * Add a watcher to a list.
3190 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003192list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193{
3194 lw->lw_next = l->lv_watch;
3195 l->lv_watch = lw;
3196}
3197
3198/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003199 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 * No warning when it isn't found...
3201 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003203list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204{
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206
3207 lwp = &l->lv_watch;
3208 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3209 {
3210 if (lw == lwrem)
3211 {
3212 *lwp = lw->lw_next;
3213 break;
3214 }
3215 lwp = &lw->lw_next;
3216 }
3217}
3218
3219/*
3220 * Just before removing an item from a list: advance watchers to the next
3221 * item.
3222 */
3223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003224list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225{
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227
3228 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3229 if (lw->lw_item == item)
3230 lw->lw_item = item->li_next;
3231}
3232
3233/*
3234 * Evaluate the expression used in a ":for var in expr" command.
3235 * "arg" points to "var".
3236 * Set "*errp" to TRUE for an error, FALSE otherwise;
3237 * Return a pointer that holds the info. Null when there is an error.
3238 */
3239 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003240eval_for_line(
3241 char_u *arg,
3242 int *errp,
3243 char_u **nextcmdp,
3244 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245{
Bram Moolenaar33570922005-01-25 22:26:29 +00003246 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 typval_T tv;
3249 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250
3251 *errp = TRUE; /* default: there is an error */
3252
Bram Moolenaar33570922005-01-25 22:26:29 +00003253 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 if (fi == NULL)
3255 return NULL;
3256
3257 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3258 if (expr == NULL)
3259 return fi;
3260
3261 expr = skipwhite(expr);
3262 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3263 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003264 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 return fi;
3266 }
3267
3268 if (skip)
3269 ++emsg_skip;
3270 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3271 {
3272 *errp = FALSE;
3273 if (!skip)
3274 {
3275 l = tv.vval.v_list;
3276 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003277 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003279 clear_tv(&tv);
3280 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 else
3282 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003283 /* No need to increment the refcount, it's already set for the
3284 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 fi->fi_list = l;
3286 list_add_watch(l, &fi->fi_lw);
3287 fi->fi_lw.lw_item = l->lv_first;
3288 }
3289 }
3290 }
3291 if (skip)
3292 --emsg_skip;
3293
3294 return fi;
3295}
3296
3297/*
3298 * Use the first item in a ":for" list. Advance to the next.
3299 * Assign the values to the variable (list). "arg" points to the first one.
3300 * Return TRUE when a valid item was found, FALSE when at end of list or
3301 * something wrong.
3302 */
3303 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003304next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003306 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003308 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309
3310 item = fi->fi_lw.lw_item;
3311 if (item == NULL)
3312 result = FALSE;
3313 else
3314 {
3315 fi->fi_lw.lw_item = item->li_next;
3316 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3317 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3318 }
3319 return result;
3320}
3321
3322/*
3323 * Free the structure used to store info used by ":for".
3324 */
3325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003326free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327{
Bram Moolenaar33570922005-01-25 22:26:29 +00003328 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003330 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003331 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003332 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003333 list_unref(fi->fi_list);
3334 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003335 vim_free(fi);
3336}
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3339
3340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003341set_context_for_expression(
3342 expand_T *xp,
3343 char_u *arg,
3344 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 int got_eq = FALSE;
3347 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 if (cmdidx == CMD_let)
3351 {
3352 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003353 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003354 {
3355 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003356 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003357 {
3358 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003359 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360 if (vim_iswhite(*p))
3361 break;
3362 }
3363 return;
3364 }
3365 }
3366 else
3367 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3368 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 while ((xp->xp_pattern = vim_strpbrk(arg,
3370 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3371 {
3372 c = *xp->xp_pattern;
3373 if (c == '&')
3374 {
3375 c = xp->xp_pattern[1];
3376 if (c == '&')
3377 {
3378 ++xp->xp_pattern;
3379 xp->xp_context = cmdidx != CMD_let || got_eq
3380 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3381 }
3382 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003385 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3386 xp->xp_pattern += 2;
3387
3388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390 else if (c == '$')
3391 {
3392 /* environment variable */
3393 xp->xp_context = EXPAND_ENV_VARS;
3394 }
3395 else if (c == '=')
3396 {
3397 got_eq = TRUE;
3398 xp->xp_context = EXPAND_EXPRESSION;
3399 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003400 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 && xp->xp_context == EXPAND_FUNCTIONS
3402 && vim_strchr(xp->xp_pattern, '(') == NULL)
3403 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003404 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 break;
3406 }
3407 else if (cmdidx != CMD_let || got_eq)
3408 {
3409 if (c == '"') /* string */
3410 {
3411 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3412 if (c == '\\' && xp->xp_pattern[1] != NUL)
3413 ++xp->xp_pattern;
3414 xp->xp_context = EXPAND_NOTHING;
3415 }
3416 else if (c == '\'') /* literal string */
3417 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003418 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3420 /* skip */ ;
3421 xp->xp_context = EXPAND_NOTHING;
3422 }
3423 else if (c == '|')
3424 {
3425 if (xp->xp_pattern[1] == '|')
3426 {
3427 ++xp->xp_pattern;
3428 xp->xp_context = EXPAND_EXPRESSION;
3429 }
3430 else
3431 xp->xp_context = EXPAND_COMMANDS;
3432 }
3433 else
3434 xp->xp_context = EXPAND_EXPRESSION;
3435 }
3436 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003437 /* Doesn't look like something valid, expand as an expression
3438 * anyway. */
3439 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 arg = xp->xp_pattern;
3441 if (*arg != NUL)
3442 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3443 /* skip */ ;
3444 }
3445 xp->xp_pattern = arg;
3446}
3447
3448#endif /* FEAT_CMDL_COMPL */
3449
3450/*
3451 * ":1,25call func(arg1, arg2)" function call.
3452 */
3453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003454ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 char_u *arg = eap->arg;
3457 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003459 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 linenr_T lnum;
3463 int doesrange;
3464 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003465 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003467 if (eap->skip)
3468 {
3469 /* trans_function_name() doesn't work well when skipping, use eval0()
3470 * instead to skip to any following command, e.g. for:
3471 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003472 ++emsg_skip;
3473 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3474 clear_tv(&rettv);
3475 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003476 return;
3477 }
3478
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003479 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003480 if (fudi.fd_newkey != NULL)
3481 {
3482 /* Still need to give an error message for missing key. */
3483 EMSG2(_(e_dictkey), fudi.fd_newkey);
3484 vim_free(fudi.fd_newkey);
3485 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003486 if (tofree == NULL)
3487 return;
3488
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003489 /* Increase refcount on dictionary, it could get deleted when evaluating
3490 * the arguments. */
3491 if (fudi.fd_dict != NULL)
3492 ++fudi.fd_dict->dv_refcount;
3493
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003494 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003495 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003496 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaar532c7802005-01-27 14:44:31 +00003498 /* Skip white space to allow ":call func ()". Not good, but required for
3499 * backward compatibility. */
3500 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503 if (*startarg != '(')
3504 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003505 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 goto end;
3507 }
3508
3509 /*
3510 * When skipping, evaluate the function once, to find the end of the
3511 * arguments.
3512 * When the function takes a range, this is discovered after the first
3513 * call, and the loop is broken.
3514 */
3515 if (eap->skip)
3516 {
3517 ++emsg_skip;
3518 lnum = eap->line2; /* do it once, also with an invalid range */
3519 }
3520 else
3521 lnum = eap->line1;
3522 for ( ; lnum <= eap->line2; ++lnum)
3523 {
3524 if (!eap->skip && eap->addr_count > 0)
3525 {
3526 curwin->w_cursor.lnum = lnum;
3527 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003528#ifdef FEAT_VIRTUALEDIT
3529 curwin->w_cursor.coladd = 0;
3530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003533 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003534 eap->line1, eap->line2, &doesrange,
3535 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
3537 failed = TRUE;
3538 break;
3539 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003540
3541 /* Handle a function returning a Funcref, Dictionary or List. */
3542 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3543 {
3544 failed = TRUE;
3545 break;
3546 }
3547
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003548 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (doesrange || eap->skip)
3550 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003553 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003554 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003555 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (aborting())
3557 break;
3558 }
3559 if (eap->skip)
3560 --emsg_skip;
3561
3562 if (!failed)
3563 {
3564 /* Check for trailing illegal characters and a following command. */
3565 if (!ends_excmd(*arg))
3566 {
3567 emsg_severe = TRUE;
3568 EMSG(_(e_trailing));
3569 }
3570 else
3571 eap->nextcmd = check_nextcmd(arg);
3572 }
3573
3574end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003575 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003576 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577}
3578
3579/*
3580 * ":unlet[!] var1 ... " command.
3581 */
3582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003583ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003585 ex_unletlock(eap, eap->arg, 0);
3586}
3587
3588/*
3589 * ":lockvar" and ":unlockvar" commands
3590 */
3591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003592ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003595 int deep = 2;
3596
3597 if (eap->forceit)
3598 deep = -1;
3599 else if (vim_isdigit(*arg))
3600 {
3601 deep = getdigits(&arg);
3602 arg = skipwhite(arg);
3603 }
3604
3605 ex_unletlock(eap, arg, deep);
3606}
3607
3608/*
3609 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3610 */
3611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003612ex_unletlock(
3613 exarg_T *eap,
3614 char_u *argstart,
3615 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616{
3617 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003620 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622 do
3623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003625 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003626 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 if (lv.ll_name == NULL)
3628 error = TRUE; /* error but continue parsing */
3629 if (name_end == NULL || (!vim_iswhite(*name_end)
3630 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 if (name_end != NULL)
3633 {
3634 emsg_severe = TRUE;
3635 EMSG(_(e_trailing));
3636 }
3637 if (!(eap->skip || error))
3638 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 break;
3640 }
3641
3642 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 {
3644 if (eap->cmdidx == CMD_unlet)
3645 {
3646 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3647 error = TRUE;
3648 }
3649 else
3650 {
3651 if (do_lock_var(&lv, name_end, deep,
3652 eap->cmdidx == CMD_lockvar) == FAIL)
3653 error = TRUE;
3654 }
3655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 if (!eap->skip)
3658 clear_lval(&lv);
3659
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 arg = skipwhite(name_end);
3661 } while (!ends_excmd(*arg));
3662
3663 eap->nextcmd = check_nextcmd(arg);
3664}
3665
Bram Moolenaar8c711452005-01-14 21:53:12 +00003666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003667do_unlet_var(
3668 lval_T *lp,
3669 char_u *name_end,
3670 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 int ret = OK;
3673 int cc;
3674
3675 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 cc = *name_end;
3678 *name_end = NUL;
3679
3680 /* Normal name or expanded name. */
3681 if (check_changedtick(lp->ll_name))
3682 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003684 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003686 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003687 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003688 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003689 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003690 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003691 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 else if (lp->ll_range)
3693 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003694 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003695 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003696 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697
3698 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3699 {
3700 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003701 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003702 return FAIL;
3703 ll_li = li;
3704 ++ll_n1;
3705 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706
3707 /* Delete a range of List items. */
3708 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3709 {
3710 li = lp->ll_li->li_next;
3711 listitem_remove(lp->ll_list, lp->ll_li);
3712 lp->ll_li = li;
3713 ++lp->ll_n1;
3714 }
3715 }
3716 else
3717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 /* unlet a List item. */
3720 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003722 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003723 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 }
3725
3726 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003727}
3728
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729/*
3730 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003731 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 */
3733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003734do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 hashtab_T *ht;
3737 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003738 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003739 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003740 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 ht = find_var_ht(name, &varname);
3743 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003745 if (ht == &globvarht)
3746 d = &globvardict;
3747 else if (current_funccal != NULL
3748 && ht == &current_funccal->l_vars.dv_hashtab)
3749 d = &current_funccal->l_vars;
3750 else if (ht == &compat_hashtab)
3751 d = &vimvardict;
3752 else
3753 {
3754 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3755 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3756 }
3757 if (d == NULL)
3758 {
3759 EMSG2(_(e_intern2), "do_unlet()");
3760 return FAIL;
3761 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003762 hi = hash_find(ht, varname);
3763 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003764 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003765 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003766 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003767 || var_check_ro(di->di_flags, name, FALSE)
3768 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003769 return FAIL;
3770
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003771 delete_var(ht, hi);
3772 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 if (forceit)
3776 return OK;
3777 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return FAIL;
3779}
3780
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003781/*
3782 * Lock or unlock variable indicated by "lp".
3783 * "deep" is the levels to go (-1 for unlimited);
3784 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3785 */
3786 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003787do_lock_var(
3788 lval_T *lp,
3789 char_u *name_end,
3790 int deep,
3791 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003792{
3793 int ret = OK;
3794 int cc;
3795 dictitem_T *di;
3796
3797 if (deep == 0) /* nothing to do */
3798 return OK;
3799
3800 if (lp->ll_tv == NULL)
3801 {
3802 cc = *name_end;
3803 *name_end = NUL;
3804
3805 /* Normal name or expanded name. */
3806 if (check_changedtick(lp->ll_name))
3807 ret = FAIL;
3808 else
3809 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003810 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003811 if (di == NULL)
3812 ret = FAIL;
3813 else
3814 {
3815 if (lock)
3816 di->di_flags |= DI_FLAGS_LOCK;
3817 else
3818 di->di_flags &= ~DI_FLAGS_LOCK;
3819 item_lock(&di->di_tv, deep, lock);
3820 }
3821 }
3822 *name_end = cc;
3823 }
3824 else if (lp->ll_range)
3825 {
3826 listitem_T *li = lp->ll_li;
3827
3828 /* (un)lock a range of List items. */
3829 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3830 {
3831 item_lock(&li->li_tv, deep, lock);
3832 li = li->li_next;
3833 ++lp->ll_n1;
3834 }
3835 }
3836 else if (lp->ll_list != NULL)
3837 /* (un)lock a List item. */
3838 item_lock(&lp->ll_li->li_tv, deep, lock);
3839 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003840 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003841 item_lock(&lp->ll_di->di_tv, deep, lock);
3842
3843 return ret;
3844}
3845
3846/*
3847 * Lock or unlock an item. "deep" is nr of levels to go.
3848 */
3849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003850item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003851{
3852 static int recurse = 0;
3853 list_T *l;
3854 listitem_T *li;
3855 dict_T *d;
3856 hashitem_T *hi;
3857 int todo;
3858
3859 if (recurse >= DICT_MAXNEST)
3860 {
3861 EMSG(_("E743: variable nested too deep for (un)lock"));
3862 return;
3863 }
3864 if (deep == 0)
3865 return;
3866 ++recurse;
3867
3868 /* lock/unlock the item itself */
3869 if (lock)
3870 tv->v_lock |= VAR_LOCKED;
3871 else
3872 tv->v_lock &= ~VAR_LOCKED;
3873
3874 switch (tv->v_type)
3875 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003876 case VAR_UNKNOWN:
3877 case VAR_NUMBER:
3878 case VAR_STRING:
3879 case VAR_FUNC:
3880 case VAR_FLOAT:
3881 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003882 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003883 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003884 break;
3885
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003886 case VAR_LIST:
3887 if ((l = tv->vval.v_list) != NULL)
3888 {
3889 if (lock)
3890 l->lv_lock |= VAR_LOCKED;
3891 else
3892 l->lv_lock &= ~VAR_LOCKED;
3893 if (deep < 0 || deep > 1)
3894 /* recursive: lock/unlock the items the List contains */
3895 for (li = l->lv_first; li != NULL; li = li->li_next)
3896 item_lock(&li->li_tv, deep - 1, lock);
3897 }
3898 break;
3899 case VAR_DICT:
3900 if ((d = tv->vval.v_dict) != NULL)
3901 {
3902 if (lock)
3903 d->dv_lock |= VAR_LOCKED;
3904 else
3905 d->dv_lock &= ~VAR_LOCKED;
3906 if (deep < 0 || deep > 1)
3907 {
3908 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003909 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003910 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3911 {
3912 if (!HASHITEM_EMPTY(hi))
3913 {
3914 --todo;
3915 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3916 }
3917 }
3918 }
3919 }
3920 }
3921 --recurse;
3922}
3923
Bram Moolenaara40058a2005-07-11 22:42:07 +00003924/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003925 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3926 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003927 */
3928 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003929tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003930{
3931 return (tv->v_lock & VAR_LOCKED)
3932 || (tv->v_type == VAR_LIST
3933 && tv->vval.v_list != NULL
3934 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3935 || (tv->v_type == VAR_DICT
3936 && tv->vval.v_dict != NULL
3937 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3938}
3939
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3941/*
3942 * Delete all "menutrans_" variables.
3943 */
3944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003945del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946{
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003951 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 {
3954 if (!HASHITEM_EMPTY(hi))
3955 {
3956 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3958 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003959 }
3960 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962}
3963#endif
3964
3965#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3966
3967/*
3968 * Local string buffer for the next two functions to store a variable name
3969 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3970 * get_user_var_name().
3971 */
3972
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003973static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975static char_u *varnamebuf = NULL;
3976static int varnamebuflen = 0;
3977
3978/*
3979 * Function to concatenate a prefix and a variable name.
3980 */
3981 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003982cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
3984 int len;
3985
3986 len = (int)STRLEN(name) + 3;
3987 if (len > varnamebuflen)
3988 {
3989 vim_free(varnamebuf);
3990 len += 10; /* some additional space */
3991 varnamebuf = alloc(len);
3992 if (varnamebuf == NULL)
3993 {
3994 varnamebuflen = 0;
3995 return NULL;
3996 }
3997 varnamebuflen = len;
3998 }
3999 *varnamebuf = prefix;
4000 varnamebuf[1] = ':';
4001 STRCPY(varnamebuf + 2, name);
4002 return varnamebuf;
4003}
4004
4005/*
4006 * Function given to ExpandGeneric() to obtain the list of user defined
4007 * (global/buffer/window/built-in) variable names.
4008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004010get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004012 static long_u gdone;
4013 static long_u bdone;
4014 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004015#ifdef FEAT_WINDOWS
4016 static long_u tdone;
4017#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004018 static int vidx;
4019 static hashitem_T *hi;
4020 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
4022 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004023 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004025#ifdef FEAT_WINDOWS
4026 tdone = 0;
4027#endif
4028 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004029
4030 /* Global variables */
4031 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004033 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004035 else
4036 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 while (HASHITEM_EMPTY(hi))
4038 ++hi;
4039 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4040 return cat_prefix_varname('g', hi->hi_key);
4041 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004043
4044 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004045 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004046 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004048 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004050 else
4051 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 while (HASHITEM_EMPTY(hi))
4053 ++hi;
4054 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004058 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 return (char_u *)"b:changedtick";
4060 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004061
4062 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004063 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004064 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004066 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004067 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004068 else
4069 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004070 while (HASHITEM_EMPTY(hi))
4071 ++hi;
4072 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004074
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004075#ifdef FEAT_WINDOWS
4076 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004077 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004078 if (tdone < ht->ht_used)
4079 {
4080 if (tdone++ == 0)
4081 hi = ht->ht_array;
4082 else
4083 ++hi;
4084 while (HASHITEM_EMPTY(hi))
4085 ++hi;
4086 return cat_prefix_varname('t', hi->hi_key);
4087 }
4088#endif
4089
Bram Moolenaar33570922005-01-25 22:26:29 +00004090 /* v: variables */
4091 if (vidx < VV_LEN)
4092 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
4094 vim_free(varnamebuf);
4095 varnamebuf = NULL;
4096 varnamebuflen = 0;
4097 return NULL;
4098}
4099
4100#endif /* FEAT_CMDL_COMPL */
4101
4102/*
4103 * types for expressions.
4104 */
4105typedef enum
4106{
4107 TYPE_UNKNOWN = 0
4108 , TYPE_EQUAL /* == */
4109 , TYPE_NEQUAL /* != */
4110 , TYPE_GREATER /* > */
4111 , TYPE_GEQUAL /* >= */
4112 , TYPE_SMALLER /* < */
4113 , TYPE_SEQUAL /* <= */
4114 , TYPE_MATCH /* =~ */
4115 , TYPE_NOMATCH /* !~ */
4116} exptype_T;
4117
4118/*
4119 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4122 */
4123
4124/*
4125 * Handle zero level expression.
4126 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004128 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 * Return OK or FAIL.
4130 */
4131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004132eval0(
4133 char_u *arg,
4134 typval_T *rettv,
4135 char_u **nextcmd,
4136 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137{
4138 int ret;
4139 char_u *p;
4140
4141 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if (ret == FAIL || !ends_excmd(*p))
4144 {
4145 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004146 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 /*
4148 * Report the invalid expression unless the expression evaluation has
4149 * been cancelled due to an aborting error, an interrupt, or an
4150 * exception.
4151 */
4152 if (!aborting())
4153 EMSG2(_(e_invexpr2), arg);
4154 ret = FAIL;
4155 }
4156 if (nextcmd != NULL)
4157 *nextcmd = check_nextcmd(p);
4158
4159 return ret;
4160}
4161
4162/*
4163 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004164 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 *
4166 * "arg" must point to the first non-white of the expression.
4167 * "arg" is advanced to the next non-white after the recognized expression.
4168 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004169 * Note: "rettv.v_lock" is not set.
4170 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 * Return OK or FAIL.
4172 */
4173 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004174eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
4176 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004177 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
4179 /*
4180 * Get the first variable.
4181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 return FAIL;
4184
4185 if ((*arg)[0] == '?')
4186 {
4187 result = FALSE;
4188 if (evaluate)
4189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 int error = FALSE;
4191
4192 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 if (error)
4196 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198
4199 /*
4200 * Get the second variable.
4201 */
4202 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 return FAIL;
4205
4206 /*
4207 * Check for the ":".
4208 */
4209 if ((*arg)[0] != ':')
4210 {
4211 EMSG(_("E109: Missing ':' after '?'"));
4212 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return FAIL;
4215 }
4216
4217 /*
4218 * Get the third variable.
4219 */
4220 *arg = skipwhite(*arg + 1);
4221 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4222 {
4223 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 return FAIL;
4226 }
4227 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
4230
4231 return OK;
4232}
4233
4234/*
4235 * Handle first level expression:
4236 * expr2 || expr2 || expr2 logical OR
4237 *
4238 * "arg" must point to the first non-white of the expression.
4239 * "arg" is advanced to the next non-white after the recognized expression.
4240 *
4241 * Return OK or FAIL.
4242 */
4243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004244eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
Bram Moolenaar33570922005-01-25 22:26:29 +00004246 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 long result;
4248 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 /*
4252 * Get the first variable.
4253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return FAIL;
4256
4257 /*
4258 * Repeat until there is no following "||".
4259 */
4260 first = TRUE;
4261 result = FALSE;
4262 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4263 {
4264 if (evaluate && first)
4265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (error)
4270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 first = FALSE;
4272 }
4273
4274 /*
4275 * Get the second variable.
4276 */
4277 *arg = skipwhite(*arg + 2);
4278 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4279 return FAIL;
4280
4281 /*
4282 * Compute the result.
4283 */
4284 if (evaluate && !result)
4285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004286 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004289 if (error)
4290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 if (evaluate)
4293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 rettv->v_type = VAR_NUMBER;
4295 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 }
4298
4299 return OK;
4300}
4301
4302/*
4303 * Handle second level expression:
4304 * expr3 && expr3 && expr3 logical AND
4305 *
4306 * "arg" must point to the first non-white of the expression.
4307 * "arg" is advanced to the next non-white after the recognized expression.
4308 *
4309 * Return OK or FAIL.
4310 */
4311 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004312eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313{
Bram Moolenaar33570922005-01-25 22:26:29 +00004314 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 long result;
4316 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
4319 /*
4320 * Get the first variable.
4321 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 return FAIL;
4324
4325 /*
4326 * Repeat until there is no following "&&".
4327 */
4328 first = TRUE;
4329 result = TRUE;
4330 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4331 {
4332 if (evaluate && first)
4333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (error)
4338 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 first = FALSE;
4340 }
4341
4342 /*
4343 * Get the second variable.
4344 */
4345 *arg = skipwhite(*arg + 2);
4346 if (eval4(arg, &var2, evaluate && result) == FAIL)
4347 return FAIL;
4348
4349 /*
4350 * Compute the result.
4351 */
4352 if (evaluate && result)
4353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004354 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 if (error)
4358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 if (evaluate)
4361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004362 rettv->v_type = VAR_NUMBER;
4363 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365 }
4366
4367 return OK;
4368}
4369
4370/*
4371 * Handle third level expression:
4372 * var1 == var2
4373 * var1 =~ var2
4374 * var1 != var2
4375 * var1 !~ var2
4376 * var1 > var2
4377 * var1 >= var2
4378 * var1 < var2
4379 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004380 * var1 is var2
4381 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 *
4383 * "arg" must point to the first non-white of the expression.
4384 * "arg" is advanced to the next non-white after the recognized expression.
4385 *
4386 * Return OK or FAIL.
4387 */
4388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004389eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
Bram Moolenaar33570922005-01-25 22:26:29 +00004391 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 char_u *p;
4393 int i;
4394 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004395 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 int len = 2;
4397 long n1, n2;
4398 char_u *s1, *s2;
4399 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4400 regmatch_T regmatch;
4401 int ic;
4402 char_u *save_cpo;
4403
4404 /*
4405 * Get the first variable.
4406 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004407 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
4409
4410 p = *arg;
4411 switch (p[0])
4412 {
4413 case '=': if (p[1] == '=')
4414 type = TYPE_EQUAL;
4415 else if (p[1] == '~')
4416 type = TYPE_MATCH;
4417 break;
4418 case '!': if (p[1] == '=')
4419 type = TYPE_NEQUAL;
4420 else if (p[1] == '~')
4421 type = TYPE_NOMATCH;
4422 break;
4423 case '>': if (p[1] != '=')
4424 {
4425 type = TYPE_GREATER;
4426 len = 1;
4427 }
4428 else
4429 type = TYPE_GEQUAL;
4430 break;
4431 case '<': if (p[1] != '=')
4432 {
4433 type = TYPE_SMALLER;
4434 len = 1;
4435 }
4436 else
4437 type = TYPE_SEQUAL;
4438 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 case 'i': if (p[1] == 's')
4440 {
4441 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4442 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004443 i = p[len];
4444 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 {
4446 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4447 type_is = TRUE;
4448 }
4449 }
4450 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
4452
4453 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004454 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 */
4456 if (type != TYPE_UNKNOWN)
4457 {
4458 /* extra question mark appended: ignore case */
4459 if (p[len] == '?')
4460 {
4461 ic = TRUE;
4462 ++len;
4463 }
4464 /* extra '#' appended: match case */
4465 else if (p[len] == '#')
4466 {
4467 ic = FALSE;
4468 ++len;
4469 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004470 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 else
4472 ic = p_ic;
4473
4474 /*
4475 * Get the second variable.
4476 */
4477 *arg = skipwhite(p + len);
4478 if (eval5(arg, &var2, evaluate) == FAIL)
4479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004480 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 return FAIL;
4482 }
4483
4484 if (evaluate)
4485 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 if (type_is && rettv->v_type != var2.v_type)
4487 {
4488 /* For "is" a different type always means FALSE, for "notis"
4489 * it means TRUE. */
4490 n1 = (type == TYPE_NEQUAL);
4491 }
4492 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4493 {
4494 if (type_is)
4495 {
4496 n1 = (rettv->v_type == var2.v_type
4497 && rettv->vval.v_list == var2.vval.v_list);
4498 if (type == TYPE_NEQUAL)
4499 n1 = !n1;
4500 }
4501 else if (rettv->v_type != var2.v_type
4502 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4503 {
4504 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004505 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004507 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 clear_tv(rettv);
4509 clear_tv(&var2);
4510 return FAIL;
4511 }
4512 else
4513 {
4514 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004515 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4516 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004517 if (type == TYPE_NEQUAL)
4518 n1 = !n1;
4519 }
4520 }
4521
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004522 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4523 {
4524 if (type_is)
4525 {
4526 n1 = (rettv->v_type == var2.v_type
4527 && rettv->vval.v_dict == var2.vval.v_dict);
4528 if (type == TYPE_NEQUAL)
4529 n1 = !n1;
4530 }
4531 else if (rettv->v_type != var2.v_type
4532 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4533 {
4534 if (rettv->v_type != var2.v_type)
4535 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4536 else
4537 EMSG(_("E736: Invalid operation for Dictionary"));
4538 clear_tv(rettv);
4539 clear_tv(&var2);
4540 return FAIL;
4541 }
4542 else
4543 {
4544 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004545 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4546 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004547 if (type == TYPE_NEQUAL)
4548 n1 = !n1;
4549 }
4550 }
4551
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004552 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4553 {
4554 if (rettv->v_type != var2.v_type
4555 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4556 {
4557 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004558 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004560 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 clear_tv(rettv);
4562 clear_tv(&var2);
4563 return FAIL;
4564 }
4565 else
4566 {
4567 /* Compare two Funcrefs for being equal or unequal. */
4568 if (rettv->vval.v_string == NULL
4569 || var2.vval.v_string == NULL)
4570 n1 = FALSE;
4571 else
4572 n1 = STRCMP(rettv->vval.v_string,
4573 var2.vval.v_string) == 0;
4574 if (type == TYPE_NEQUAL)
4575 n1 = !n1;
4576 }
4577 }
4578
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004579#ifdef FEAT_FLOAT
4580 /*
4581 * If one of the two variables is a float, compare as a float.
4582 * When using "=~" or "!~", always compare as string.
4583 */
4584 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4585 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4586 {
4587 float_T f1, f2;
4588
4589 if (rettv->v_type == VAR_FLOAT)
4590 f1 = rettv->vval.v_float;
4591 else
4592 f1 = get_tv_number(rettv);
4593 if (var2.v_type == VAR_FLOAT)
4594 f2 = var2.vval.v_float;
4595 else
4596 f2 = get_tv_number(&var2);
4597 n1 = FALSE;
4598 switch (type)
4599 {
4600 case TYPE_EQUAL: n1 = (f1 == f2); break;
4601 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4602 case TYPE_GREATER: n1 = (f1 > f2); break;
4603 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4604 case TYPE_SMALLER: n1 = (f1 < f2); break;
4605 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4606 case TYPE_UNKNOWN:
4607 case TYPE_MATCH:
4608 case TYPE_NOMATCH: break; /* avoid gcc warning */
4609 }
4610 }
4611#endif
4612
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /*
4614 * If one of the two variables is a number, compare as a number.
4615 * When using "=~" or "!~", always compare as string.
4616 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004617 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4619 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620 n1 = get_tv_number(rettv);
4621 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 switch (type)
4623 {
4624 case TYPE_EQUAL: n1 = (n1 == n2); break;
4625 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4626 case TYPE_GREATER: n1 = (n1 > n2); break;
4627 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4628 case TYPE_SMALLER: n1 = (n1 < n2); break;
4629 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4630 case TYPE_UNKNOWN:
4631 case TYPE_MATCH:
4632 case TYPE_NOMATCH: break; /* avoid gcc warning */
4633 }
4634 }
4635 else
4636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 s1 = get_tv_string_buf(rettv, buf1);
4638 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4640 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4641 else
4642 i = 0;
4643 n1 = FALSE;
4644 switch (type)
4645 {
4646 case TYPE_EQUAL: n1 = (i == 0); break;
4647 case TYPE_NEQUAL: n1 = (i != 0); break;
4648 case TYPE_GREATER: n1 = (i > 0); break;
4649 case TYPE_GEQUAL: n1 = (i >= 0); break;
4650 case TYPE_SMALLER: n1 = (i < 0); break;
4651 case TYPE_SEQUAL: n1 = (i <= 0); break;
4652
4653 case TYPE_MATCH:
4654 case TYPE_NOMATCH:
4655 /* avoid 'l' flag in 'cpoptions' */
4656 save_cpo = p_cpo;
4657 p_cpo = (char_u *)"";
4658 regmatch.regprog = vim_regcomp(s2,
4659 RE_MAGIC + RE_STRING);
4660 regmatch.rm_ic = ic;
4661 if (regmatch.regprog != NULL)
4662 {
4663 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004664 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (type == TYPE_NOMATCH)
4666 n1 = !n1;
4667 }
4668 p_cpo = save_cpo;
4669 break;
4670
4671 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4672 }
4673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 clear_tv(rettv);
4675 clear_tv(&var2);
4676 rettv->v_type = VAR_NUMBER;
4677 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679 }
4680
4681 return OK;
4682}
4683
4684/*
4685 * Handle fourth level expression:
4686 * + number addition
4687 * - number subtraction
4688 * . string concatenation
4689 *
4690 * "arg" must point to the first non-white of the expression.
4691 * "arg" is advanced to the next non-white after the recognized expression.
4692 *
4693 * Return OK or FAIL.
4694 */
4695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004696eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
Bram Moolenaar33570922005-01-25 22:26:29 +00004698 typval_T var2;
4699 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 int op;
4701 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702#ifdef FEAT_FLOAT
4703 float_T f1 = 0, f2 = 0;
4704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 char_u *s1, *s2;
4706 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4707 char_u *p;
4708
4709 /*
4710 * Get the first variable.
4711 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004712 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 return FAIL;
4714
4715 /*
4716 * Repeat computing, until no '+', '-' or '.' is following.
4717 */
4718 for (;;)
4719 {
4720 op = **arg;
4721 if (op != '+' && op != '-' && op != '.')
4722 break;
4723
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004724 if ((op != '+' || rettv->v_type != VAR_LIST)
4725#ifdef FEAT_FLOAT
4726 && (op == '.' || rettv->v_type != VAR_FLOAT)
4727#endif
4728 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004729 {
4730 /* For "list + ...", an illegal use of the first operand as
4731 * a number cannot be determined before evaluating the 2nd
4732 * operand: if this is also a list, all is ok.
4733 * For "something . ...", "something - ..." or "non-list + ...",
4734 * we know that the first operand needs to be a string or number
4735 * without evaluating the 2nd operand. So check before to avoid
4736 * side effects after an error. */
4737 if (evaluate && get_tv_string_chk(rettv) == NULL)
4738 {
4739 clear_tv(rettv);
4740 return FAIL;
4741 }
4742 }
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 /*
4745 * Get the second variable.
4746 */
4747 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004748 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 return FAIL;
4752 }
4753
4754 if (evaluate)
4755 {
4756 /*
4757 * Compute the result.
4758 */
4759 if (op == '.')
4760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004761 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4762 s2 = get_tv_string_buf_chk(&var2, buf2);
4763 if (s2 == NULL) /* type error ? */
4764 {
4765 clear_tv(rettv);
4766 clear_tv(&var2);
4767 return FAIL;
4768 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004769 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 clear_tv(rettv);
4771 rettv->v_type = VAR_STRING;
4772 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004774 else if (op == '+' && rettv->v_type == VAR_LIST
4775 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004776 {
4777 /* concatenate Lists */
4778 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4779 &var3) == FAIL)
4780 {
4781 clear_tv(rettv);
4782 clear_tv(&var2);
4783 return FAIL;
4784 }
4785 clear_tv(rettv);
4786 *rettv = var3;
4787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 else
4789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004790 int error = FALSE;
4791
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792#ifdef FEAT_FLOAT
4793 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 f1 = rettv->vval.v_float;
4796 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004798 else
4799#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 n1 = get_tv_number_chk(rettv, &error);
4802 if (error)
4803 {
4804 /* This can only happen for "list + non-list". For
4805 * "non-list + ..." or "something - ...", we returned
4806 * before evaluating the 2nd operand. */
4807 clear_tv(rettv);
4808 return FAIL;
4809 }
4810#ifdef FEAT_FLOAT
4811 if (var2.v_type == VAR_FLOAT)
4812 f1 = n1;
4813#endif
4814 }
4815#ifdef FEAT_FLOAT
4816 if (var2.v_type == VAR_FLOAT)
4817 {
4818 f2 = var2.vval.v_float;
4819 n2 = 0;
4820 }
4821 else
4822#endif
4823 {
4824 n2 = get_tv_number_chk(&var2, &error);
4825 if (error)
4826 {
4827 clear_tv(rettv);
4828 clear_tv(&var2);
4829 return FAIL;
4830 }
4831#ifdef FEAT_FLOAT
4832 if (rettv->v_type == VAR_FLOAT)
4833 f2 = n2;
4834#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837
4838#ifdef FEAT_FLOAT
4839 /* If there is a float on either side the result is a float. */
4840 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4841 {
4842 if (op == '+')
4843 f1 = f1 + f2;
4844 else
4845 f1 = f1 - f2;
4846 rettv->v_type = VAR_FLOAT;
4847 rettv->vval.v_float = f1;
4848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004850#endif
4851 {
4852 if (op == '+')
4853 n1 = n1 + n2;
4854 else
4855 n1 = n1 - n2;
4856 rettv->v_type = VAR_NUMBER;
4857 rettv->vval.v_number = n1;
4858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
4862 }
4863 return OK;
4864}
4865
4866/*
4867 * Handle fifth level expression:
4868 * * number multiplication
4869 * / number division
4870 * % number modulo
4871 *
4872 * "arg" must point to the first non-white of the expression.
4873 * "arg" is advanced to the next non-white after the recognized expression.
4874 *
4875 * Return OK or FAIL.
4876 */
4877 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004878eval6(
4879 char_u **arg,
4880 typval_T *rettv,
4881 int evaluate,
4882 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
Bram Moolenaar33570922005-01-25 22:26:29 +00004884 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 int op;
4886 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887#ifdef FEAT_FLOAT
4888 int use_float = FALSE;
4889 float_T f1 = 0, f2;
4890#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004891 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
4893 /*
4894 * Get the first variable.
4895 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004896 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 return FAIL;
4898
4899 /*
4900 * Repeat computing, until no '*', '/' or '%' is following.
4901 */
4902 for (;;)
4903 {
4904 op = **arg;
4905 if (op != '*' && op != '/' && op != '%')
4906 break;
4907
4908 if (evaluate)
4909 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910#ifdef FEAT_FLOAT
4911 if (rettv->v_type == VAR_FLOAT)
4912 {
4913 f1 = rettv->vval.v_float;
4914 use_float = TRUE;
4915 n1 = 0;
4916 }
4917 else
4918#endif
4919 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004921 if (error)
4922 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
4924 else
4925 n1 = 0;
4926
4927 /*
4928 * Get the second variable.
4929 */
4930 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004931 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 return FAIL;
4933
4934 if (evaluate)
4935 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004936#ifdef FEAT_FLOAT
4937 if (var2.v_type == VAR_FLOAT)
4938 {
4939 if (!use_float)
4940 {
4941 f1 = n1;
4942 use_float = TRUE;
4943 }
4944 f2 = var2.vval.v_float;
4945 n2 = 0;
4946 }
4947 else
4948#endif
4949 {
4950 n2 = get_tv_number_chk(&var2, &error);
4951 clear_tv(&var2);
4952 if (error)
4953 return FAIL;
4954#ifdef FEAT_FLOAT
4955 if (use_float)
4956 f2 = n2;
4957#endif
4958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
4960 /*
4961 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004964#ifdef FEAT_FLOAT
4965 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004967 if (op == '*')
4968 f1 = f1 * f2;
4969 else if (op == '/')
4970 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971# ifdef VMS
4972 /* VMS crashes on divide by zero, work around it */
4973 if (f2 == 0.0)
4974 {
4975 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004976 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004977 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004978 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004980 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981 }
4982 else
4983 f1 = f1 / f2;
4984# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 /* We rely on the floating point library to handle divide
4986 * by zero to result in "inf" and not a crash. */
4987 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004988# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004992 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 return FAIL;
4994 }
4995 rettv->v_type = VAR_FLOAT;
4996 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 if (op == '*')
5002 n1 = n1 * n2;
5003 else if (op == '/')
5004 {
5005 if (n2 == 0) /* give an error message? */
5006 {
5007 if (n1 == 0)
5008 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5009 else if (n1 < 0)
5010 n1 = -0x7fffffffL;
5011 else
5012 n1 = 0x7fffffffL;
5013 }
5014 else
5015 n1 = n1 / n2;
5016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 {
5019 if (n2 == 0) /* give an error message? */
5020 n1 = 0;
5021 else
5022 n1 = n1 % n2;
5023 }
5024 rettv->v_type = VAR_NUMBER;
5025 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 }
5029
5030 return OK;
5031}
5032
5033/*
5034 * Handle sixth level expression:
5035 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005036 * "string" string constant
5037 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 * &option-name option value
5039 * @r register contents
5040 * identifier variable value
5041 * function() function call
5042 * $VAR environment variable
5043 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005044 * [expr, expr] List
5045 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 *
5047 * Also handle:
5048 * ! in front logical NOT
5049 * - in front unary minus
5050 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 * trailing [] subscript in String or List
5052 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 *
5054 * "arg" must point to the first non-white of the expression.
5055 * "arg" is advanced to the next non-white after the recognized expression.
5056 *
5057 * Return OK or FAIL.
5058 */
5059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005060eval7(
5061 char_u **arg,
5062 typval_T *rettv,
5063 int evaluate,
5064 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 long n;
5067 int len;
5068 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 char_u *start_leader, *end_leader;
5070 int ret = OK;
5071 char_u *alias;
5072
5073 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005075 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078
5079 /*
5080 * Skip '!' and '-' characters. They are handled later.
5081 */
5082 start_leader = *arg;
5083 while (**arg == '!' || **arg == '-' || **arg == '+')
5084 *arg = skipwhite(*arg + 1);
5085 end_leader = *arg;
5086
5087 switch (**arg)
5088 {
5089 /*
5090 * Number constant.
5091 */
5092 case '0':
5093 case '1':
5094 case '2':
5095 case '3':
5096 case '4':
5097 case '5':
5098 case '6':
5099 case '7':
5100 case '8':
5101 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102 {
5103#ifdef FEAT_FLOAT
5104 char_u *p = skipdigits(*arg + 1);
5105 int get_float = FALSE;
5106
5107 /* We accept a float when the format matches
5108 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005109 * strict to avoid backwards compatibility problems.
5110 * Don't look for a float after the "." operator, so that
5111 * ":let vers = 1.2.3" doesn't fail. */
5112 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005114 get_float = TRUE;
5115 p = skipdigits(p + 2);
5116 if (*p == 'e' || *p == 'E')
5117 {
5118 ++p;
5119 if (*p == '-' || *p == '+')
5120 ++p;
5121 if (!vim_isdigit(*p))
5122 get_float = FALSE;
5123 else
5124 p = skipdigits(p + 1);
5125 }
5126 if (ASCII_ISALPHA(*p) || *p == '.')
5127 get_float = FALSE;
5128 }
5129 if (get_float)
5130 {
5131 float_T f;
5132
5133 *arg += string2float(*arg, &f);
5134 if (evaluate)
5135 {
5136 rettv->v_type = VAR_FLOAT;
5137 rettv->vval.v_float = f;
5138 }
5139 }
5140 else
5141#endif
5142 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005143 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005144 *arg += len;
5145 if (evaluate)
5146 {
5147 rettv->v_type = VAR_NUMBER;
5148 rettv->vval.v_number = n;
5149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153
5154 /*
5155 * String constant: "string".
5156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 break;
5159
5160 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 break;
5165
5166 /*
5167 * List: [expr, expr]
5168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005169 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 break;
5171
5172 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 * Dictionary: {key: val, key: val}
5174 */
5175 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5176 break;
5177
5178 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005179 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005181 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
5185 * Environment variable: $VAR.
5186 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189
5190 /*
5191 * Register contents: @r.
5192 */
5193 case '@': ++*arg;
5194 if (evaluate)
5195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005197 rettv->vval.v_string = get_reg_contents(**arg,
5198 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200 if (**arg != NUL)
5201 ++*arg;
5202 break;
5203
5204 /*
5205 * nested expression: (expression).
5206 */
5207 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (**arg == ')')
5210 ++*arg;
5211 else if (ret == OK)
5212 {
5213 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005214 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 ret = FAIL;
5216 }
5217 break;
5218
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 break;
5221 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222
5223 if (ret == NOTDONE)
5224 {
5225 /*
5226 * Must be a variable or function name.
5227 * Can also be a curly-braces kind of name: {expr}.
5228 */
5229 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005230 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 if (alias != NULL)
5232 s = alias;
5233
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005234 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 ret = FAIL;
5236 else
5237 {
5238 if (**arg == '(') /* recursive! */
5239 {
5240 /* If "s" is the name of a variable of type VAR_FUNC
5241 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005242 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243
5244 /* Invoke the function. */
5245 ret = get_func_tv(s, len, rettv, arg,
5246 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005247 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005248
5249 /* If evaluate is FALSE rettv->v_type was not set in
5250 * get_func_tv, but it's needed in handle_subscript() to parse
5251 * what follows. So set it here. */
5252 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5253 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005254 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005255 rettv->v_type = VAR_FUNC;
5256 }
5257
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 /* Stop the expression evaluation when immediately
5259 * aborting on error, or when an interrupt occurred or
5260 * an exception was thrown but not caught. */
5261 if (aborting())
5262 {
5263 if (ret == OK)
5264 clear_tv(rettv);
5265 ret = FAIL;
5266 }
5267 }
5268 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005269 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005270 else
5271 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005273 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 }
5275
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 *arg = skipwhite(*arg);
5277
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5279 * expr(expr). */
5280 if (ret == OK)
5281 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283 /*
5284 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5285 */
5286 if (ret == OK && evaluate && end_leader > start_leader)
5287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005288 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005289 int val = 0;
5290#ifdef FEAT_FLOAT
5291 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005293 if (rettv->v_type == VAR_FLOAT)
5294 f = rettv->vval.v_float;
5295 else
5296#endif
5297 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005300 clear_tv(rettv);
5301 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 else
5304 {
5305 while (end_leader > start_leader)
5306 {
5307 --end_leader;
5308 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005309 {
5310#ifdef FEAT_FLOAT
5311 if (rettv->v_type == VAR_FLOAT)
5312 f = !f;
5313 else
5314#endif
5315 val = !val;
5316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005318 {
5319#ifdef FEAT_FLOAT
5320 if (rettv->v_type == VAR_FLOAT)
5321 f = -f;
5322 else
5323#endif
5324 val = -val;
5325 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005327#ifdef FEAT_FLOAT
5328 if (rettv->v_type == VAR_FLOAT)
5329 {
5330 clear_tv(rettv);
5331 rettv->vval.v_float = f;
5332 }
5333 else
5334#endif
5335 {
5336 clear_tv(rettv);
5337 rettv->v_type = VAR_NUMBER;
5338 rettv->vval.v_number = val;
5339 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342
5343 return ret;
5344}
5345
5346/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005347 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5348 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5350 */
5351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005352eval_index(
5353 char_u **arg,
5354 typval_T *rettv,
5355 int evaluate,
5356 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357{
5358 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005359 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 long len = -1;
5362 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365
Bram Moolenaara03f2332016-02-06 18:09:59 +01005366 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 case VAR_FUNC:
5369 if (verbose)
5370 EMSG(_("E695: Cannot index a Funcref"));
5371 return FAIL;
5372 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005373#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005374 if (verbose)
5375 EMSG(_(e_float_as_string));
5376 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005377#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005378 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005379 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005380 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005381 if (verbose)
5382 EMSG(_("E909: Cannot index a special variable"));
5383 return FAIL;
5384 case VAR_UNKNOWN:
5385 if (evaluate)
5386 return FAIL;
5387 /* FALLTHROUGH */
5388
5389 case VAR_STRING:
5390 case VAR_NUMBER:
5391 case VAR_LIST:
5392 case VAR_DICT:
5393 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005394 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005396 init_tv(&var1);
5397 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400 /*
5401 * dict.name
5402 */
5403 key = *arg + 1;
5404 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5405 ;
5406 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005408 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 }
5410 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412 /*
5413 * something[idx]
5414 *
5415 * Get the (first) variable from inside the [].
5416 */
5417 *arg = skipwhite(*arg + 1);
5418 if (**arg == ':')
5419 empty1 = TRUE;
5420 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5421 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005422 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5423 {
5424 /* not a number or string */
5425 clear_tv(&var1);
5426 return FAIL;
5427 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005428
5429 /*
5430 * Get the second variable from inside the [:].
5431 */
5432 if (**arg == ':')
5433 {
5434 range = TRUE;
5435 *arg = skipwhite(*arg + 1);
5436 if (**arg == ']')
5437 empty2 = TRUE;
5438 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005440 if (!empty1)
5441 clear_tv(&var1);
5442 return FAIL;
5443 }
5444 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5445 {
5446 /* not a number or string */
5447 if (!empty1)
5448 clear_tv(&var1);
5449 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 return FAIL;
5451 }
5452 }
5453
5454 /* Check for the ']'. */
5455 if (**arg != ']')
5456 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005457 if (verbose)
5458 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 clear_tv(&var1);
5460 if (range)
5461 clear_tv(&var2);
5462 return FAIL;
5463 }
5464 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 }
5466
5467 if (evaluate)
5468 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 n1 = 0;
5470 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 n1 = get_tv_number(&var1);
5473 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
5475 if (range)
5476 {
5477 if (empty2)
5478 n2 = -1;
5479 else
5480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 n2 = get_tv_number(&var2);
5482 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 }
5484 }
5485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005486 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005488 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005489 case VAR_FUNC:
5490 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005491 case VAR_SPECIAL:
5492 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005493 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005494 break; /* not evaluating, skipping over subscript */
5495
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 case VAR_NUMBER:
5497 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005498 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 len = (long)STRLEN(s);
5500 if (range)
5501 {
5502 /* The resulting variable is a substring. If the indexes
5503 * are out of range the result is empty. */
5504 if (n1 < 0)
5505 {
5506 n1 = len + n1;
5507 if (n1 < 0)
5508 n1 = 0;
5509 }
5510 if (n2 < 0)
5511 n2 = len + n2;
5512 else if (n2 >= len)
5513 n2 = len;
5514 if (n1 >= len || n2 < 0 || n1 > n2)
5515 s = NULL;
5516 else
5517 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5518 }
5519 else
5520 {
5521 /* The resulting variable is a string of a single
5522 * character. If the index is too big or negative the
5523 * result is empty. */
5524 if (n1 >= len || n1 < 0)
5525 s = NULL;
5526 else
5527 s = vim_strnsave(s + n1, 1);
5528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 clear_tv(rettv);
5530 rettv->v_type = VAR_STRING;
5531 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 break;
5533
5534 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 if (n1 < 0)
5537 n1 = len + n1;
5538 if (!empty1 && (n1 < 0 || n1 >= len))
5539 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005540 /* For a range we allow invalid values and return an empty
5541 * list. A list index out of range is an error. */
5542 if (!range)
5543 {
5544 if (verbose)
5545 EMSGN(_(e_listidx), n1);
5546 return FAIL;
5547 }
5548 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549 }
5550 if (range)
5551 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 list_T *l;
5553 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554
5555 if (n2 < 0)
5556 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005557 else if (n2 >= len)
5558 n2 = len - 1;
5559 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005560 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 l = list_alloc();
5562 if (l == NULL)
5563 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 n1 <= n2; ++n1)
5566 {
5567 if (list_append_tv(l, &item->li_tv) == FAIL)
5568 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005569 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 return FAIL;
5571 }
5572 item = item->li_next;
5573 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 clear_tv(rettv);
5575 rettv->v_type = VAR_LIST;
5576 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005577 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 }
5579 else
5580 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005581 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 clear_tv(rettv);
5583 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 }
5585 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586
5587 case VAR_DICT:
5588 if (range)
5589 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005590 if (verbose)
5591 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 if (len == -1)
5593 clear_tv(&var1);
5594 return FAIL;
5595 }
5596 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005597 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598
5599 if (len == -1)
5600 {
5601 key = get_tv_string(&var1);
5602 if (*key == NUL)
5603 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005604 if (verbose)
5605 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 clear_tv(&var1);
5607 return FAIL;
5608 }
5609 }
5610
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005611 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005613 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005614 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 if (len == -1)
5616 clear_tv(&var1);
5617 if (item == NULL)
5618 return FAIL;
5619
5620 copy_tv(&item->di_tv, &var1);
5621 clear_tv(rettv);
5622 *rettv = var1;
5623 }
5624 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625 }
5626 }
5627
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628 return OK;
5629}
5630
5631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 * Get an option value.
5633 * "arg" points to the '&' or '+' before the option name.
5634 * "arg" is advanced to character after the option name.
5635 * Return OK or FAIL.
5636 */
5637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005638get_option_tv(
5639 char_u **arg,
5640 typval_T *rettv, /* when NULL, only check if option exists */
5641 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 char_u *option_end;
5644 long numval;
5645 char_u *stringval;
5646 int opt_type;
5647 int c;
5648 int working = (**arg == '+'); /* has("+option") */
5649 int ret = OK;
5650 int opt_flags;
5651
5652 /*
5653 * Isolate the option name and find its value.
5654 */
5655 option_end = find_option_end(arg, &opt_flags);
5656 if (option_end == NULL)
5657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005658 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 EMSG2(_("E112: Option name missing: %s"), *arg);
5660 return FAIL;
5661 }
5662
5663 if (!evaluate)
5664 {
5665 *arg = option_end;
5666 return OK;
5667 }
5668
5669 c = *option_end;
5670 *option_end = NUL;
5671 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
5674 if (opt_type == -3) /* invalid name */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 EMSG2(_("E113: Unknown option: %s"), *arg);
5678 ret = FAIL;
5679 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 {
5682 if (opt_type == -2) /* hidden string option */
5683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 rettv->v_type = VAR_STRING;
5685 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 else if (opt_type == -1) /* hidden number option */
5688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 rettv->v_type = VAR_NUMBER;
5690 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 else if (opt_type == 1) /* number option */
5693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694 rettv->v_type = VAR_NUMBER;
5695 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697 else /* string option */
5698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005699 rettv->v_type = VAR_STRING;
5700 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
5702 }
5703 else if (working && (opt_type == -2 || opt_type == -1))
5704 ret = FAIL;
5705
5706 *option_end = c; /* put back for error messages */
5707 *arg = option_end;
5708
5709 return ret;
5710}
5711
5712/*
5713 * Allocate a variable for a string constant.
5714 * Return OK or FAIL.
5715 */
5716 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005717get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719 char_u *p;
5720 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 int extra = 0;
5722
5723 /*
5724 * Find the end of the string, skipping backslashed characters.
5725 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005726 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 if (*p == '\\' && p[1] != NUL)
5729 {
5730 ++p;
5731 /* A "\<x>" form occupies at least 4 characters, and produces up
5732 * to 6 characters: reserve space for 2 extra */
5733 if (*p == '<')
5734 extra += 2;
5735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
5737
5738 if (*p != '"')
5739 {
5740 EMSG2(_("E114: Missing quote: %s"), *arg);
5741 return FAIL;
5742 }
5743
5744 /* If only parsing, set *arg and return here */
5745 if (!evaluate)
5746 {
5747 *arg = p + 1;
5748 return OK;
5749 }
5750
5751 /*
5752 * Copy the string into allocated memory, handling backslashed
5753 * characters.
5754 */
5755 name = alloc((unsigned)(p - *arg + extra));
5756 if (name == NULL)
5757 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 rettv->v_type = VAR_STRING;
5759 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
5763 if (*p == '\\')
5764 {
5765 switch (*++p)
5766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 case 'b': *name++ = BS; ++p; break;
5768 case 'e': *name++ = ESC; ++p; break;
5769 case 'f': *name++ = FF; ++p; break;
5770 case 'n': *name++ = NL; ++p; break;
5771 case 'r': *name++ = CAR; ++p; break;
5772 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
5774 case 'X': /* hex: "\x1", "\x12" */
5775 case 'x':
5776 case 'u': /* Unicode: "\u0023" */
5777 case 'U':
5778 if (vim_isxdigit(p[1]))
5779 {
5780 int n, nr;
5781 int c = toupper(*p);
5782
5783 if (c == 'X')
5784 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005785 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005787 else
5788 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 nr = 0;
5790 while (--n >= 0 && vim_isxdigit(p[1]))
5791 {
5792 ++p;
5793 nr = (nr << 4) + hex2nr(*p);
5794 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796#ifdef FEAT_MBYTE
5797 /* For "\u" store the number according to
5798 * 'encoding'. */
5799 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 else
5802#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 break;
5806
5807 /* octal: "\1", "\12", "\123" */
5808 case '0':
5809 case '1':
5810 case '2':
5811 case '3':
5812 case '4':
5813 case '5':
5814 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 case '7': *name = *p++ - '0';
5816 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 *name = (*name << 3) + *p++ - '0';
5819 if (*p >= '0' && *p <= '7')
5820 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 break;
5824
5825 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 if (extra != 0)
5828 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 break;
5831 }
5832 /* FALLTHROUGH */
5833
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 break;
5836 }
5837 }
5838 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 *arg = p + 1;
5844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 return OK;
5846}
5847
5848/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 * Return OK or FAIL.
5851 */
5852 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005853get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 int reduce = 0;
5858
5859 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 */
5862 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 break;
5868 ++reduce;
5869 ++p;
5870 }
5871 }
5872
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 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 return FAIL;
5877 }
5878
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 if (!evaluate)
5881 {
5882 *arg = p + 1;
5883 return OK;
5884 }
5885
5886 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 */
5889 str = alloc((unsigned)((p - *arg) - reduce));
5890 if (str == NULL)
5891 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 rettv->v_type = VAR_STRING;
5893 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 break;
5901 ++p;
5902 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 *arg = p + 1;
5907
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 return OK;
5909}
5910
5911/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 * Allocate a variable for a List and fill it from "*arg".
5913 * Return OK or FAIL.
5914 */
5915 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005916get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917{
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 list_T *l = NULL;
5919 typval_T tv;
5920 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921
5922 if (evaluate)
5923 {
5924 l = list_alloc();
5925 if (l == NULL)
5926 return FAIL;
5927 }
5928
5929 *arg = skipwhite(*arg + 1);
5930 while (**arg != ']' && **arg != NUL)
5931 {
5932 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5933 goto failret;
5934 if (evaluate)
5935 {
5936 item = listitem_alloc();
5937 if (item != NULL)
5938 {
5939 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005940 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 list_append(l, item);
5942 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005943 else
5944 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 }
5946
5947 if (**arg == ']')
5948 break;
5949 if (**arg != ',')
5950 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 goto failret;
5953 }
5954 *arg = skipwhite(*arg + 1);
5955 }
5956
5957 if (**arg != ']')
5958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005959 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960failret:
5961 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005962 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 return FAIL;
5964 }
5965
5966 *arg = skipwhite(*arg + 1);
5967 if (evaluate)
5968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005969 rettv->v_type = VAR_LIST;
5970 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 ++l->lv_refcount;
5972 }
5973
5974 return OK;
5975}
5976
5977/*
5978 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005979 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005981 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005982list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005984 list_T *l;
5985
5986 l = (list_T *)alloc_clear(sizeof(list_T));
5987 if (l != NULL)
5988 {
5989 /* Prepend the list to the list of lists for garbage collection. */
5990 if (first_list != NULL)
5991 first_list->lv_used_prev = l;
5992 l->lv_used_prev = NULL;
5993 l->lv_used_next = first_list;
5994 first_list = l;
5995 }
5996 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997}
5998
5999/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006000 * Allocate an empty list for a return value.
6001 * Returns OK or FAIL.
6002 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006004rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006005{
6006 list_T *l = list_alloc();
6007
6008 if (l == NULL)
6009 return FAIL;
6010
6011 rettv->vval.v_list = l;
6012 rettv->v_type = VAR_LIST;
6013 ++l->lv_refcount;
6014 return OK;
6015}
6016
6017/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018 * Unreference a list: decrement the reference count and free it when it
6019 * becomes zero.
6020 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006022list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006024 if (l != NULL && --l->lv_refcount <= 0)
6025 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026}
6027
6028/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006029 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 * Ignores the reference count.
6031 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006032 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006033list_free(
6034 list_T *l,
6035 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036{
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006039 /* Remove the list from the list of lists for garbage collection. */
6040 if (l->lv_used_prev == NULL)
6041 first_list = l->lv_used_next;
6042 else
6043 l->lv_used_prev->lv_used_next = l->lv_used_next;
6044 if (l->lv_used_next != NULL)
6045 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6046
Bram Moolenaard9fba312005-06-26 22:34:35 +00006047 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006049 /* Remove the item before deleting it. */
6050 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006051 if (recurse || (item->li_tv.v_type != VAR_LIST
6052 && item->li_tv.v_type != VAR_DICT))
6053 clear_tv(&item->li_tv);
6054 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055 }
6056 vim_free(l);
6057}
6058
6059/*
6060 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006061 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006063 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006064listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065{
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067}
6068
6069/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006070 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006072 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006073listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006075 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 vim_free(item);
6077}
6078
6079/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006080 * Remove a list item from a List and free it. Also clears the value.
6081 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006082 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006083listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006084{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006085 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006086 listitem_free(item);
6087}
6088
6089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 * Get the number of items in a list.
6091 */
6092 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006093list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095 if (l == NULL)
6096 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006097 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098}
6099
6100/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006101 * Return TRUE when two lists have exactly the same values.
6102 */
6103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006104list_equal(
6105 list_T *l1,
6106 list_T *l2,
6107 int ic, /* ignore case for strings */
6108 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109{
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006112 if (l1 == NULL || l2 == NULL)
6113 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006114 if (l1 == l2)
6115 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006116 if (list_len(l1) != list_len(l2))
6117 return FALSE;
6118
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006119 for (item1 = l1->lv_first, item2 = l2->lv_first;
6120 item1 != NULL && item2 != NULL;
6121 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006122 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123 return FALSE;
6124 return item1 == NULL && item2 == NULL;
6125}
6126
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006127/*
6128 * Return the dictitem that an entry in a hashtable points to.
6129 */
6130 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006131dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006132{
6133 return HI2DI(hi);
6134}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006135
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006136/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006137 * Return TRUE when two dictionaries have exactly the same key/values.
6138 */
6139 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006140dict_equal(
6141 dict_T *d1,
6142 dict_T *d2,
6143 int ic, /* ignore case for strings */
6144 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145{
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 hashitem_T *hi;
6147 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006148 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006150 if (d1 == NULL || d2 == NULL)
6151 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006152 if (d1 == d2)
6153 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 if (dict_len(d1) != dict_len(d2))
6155 return FALSE;
6156
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006157 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006159 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160 if (!HASHITEM_EMPTY(hi))
6161 {
6162 item2 = dict_find(d2, hi->hi_key, -1);
6163 if (item2 == NULL)
6164 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006165 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 return FALSE;
6167 --todo;
6168 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169 }
6170 return TRUE;
6171}
6172
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006173static int tv_equal_recurse_limit;
6174
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006175/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006177 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006178 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006179 */
6180 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006181tv_equal(
6182 typval_T *tv1,
6183 typval_T *tv2,
6184 int ic, /* ignore case */
6185 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186{
6187 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006188 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006190 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006191
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006192 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006195 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196 * recursiveness to a limit. We guess they are equal then.
6197 * A fixed limit has the problem of still taking an awful long time.
6198 * Reduce the limit every time running into it. That should work fine for
6199 * deeply linked structures that are not recursively linked and catch
6200 * recursiveness quickly. */
6201 if (!recursive)
6202 tv_equal_recurse_limit = 1000;
6203 if (recursive_cnt >= tv_equal_recurse_limit)
6204 {
6205 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006206 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006208
6209 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006210 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006211 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006212 ++recursive_cnt;
6213 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6214 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006215 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006216
6217 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006218 ++recursive_cnt;
6219 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6220 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006221 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006222
6223 case VAR_FUNC:
6224 return (tv1->vval.v_string != NULL
6225 && tv2->vval.v_string != NULL
6226 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6227
6228 case VAR_NUMBER:
6229 return tv1->vval.v_number == tv2->vval.v_number;
6230
6231 case VAR_STRING:
6232 s1 = get_tv_string_buf(tv1, buf1);
6233 s2 = get_tv_string_buf(tv2, buf2);
6234 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006235
6236 case VAR_SPECIAL:
6237 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006238
6239 case VAR_FLOAT:
6240#ifdef FEAT_FLOAT
6241 return tv1->vval.v_float == tv2->vval.v_float;
6242#endif
6243 case VAR_JOB:
6244#ifdef FEAT_JOB
6245 return tv1->vval.v_job == tv2->vval.v_job;
6246#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006247 case VAR_CHANNEL:
6248#ifdef FEAT_CHANNEL
6249 return tv1->vval.v_channel == tv2->vval.v_channel;
6250#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006251 case VAR_UNKNOWN:
6252 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006254
Bram Moolenaara03f2332016-02-06 18:09:59 +01006255 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6256 * does not equal anything, not even itself. */
6257 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006258}
6259
6260/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 * Locate item with index "n" in list "l" and return it.
6262 * A negative index is counted from the end; -1 is the last item.
6263 * Returns NULL when "n" is out of range.
6264 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006265 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006266list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267{
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 long idx;
6270
6271 if (l == NULL)
6272 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006273
6274 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006276 n = l->lv_len + n;
6277
6278 /* Check for index out of range. */
6279 if (n < 0 || n >= l->lv_len)
6280 return NULL;
6281
6282 /* When there is a cached index may start search from there. */
6283 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006284 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006285 if (n < l->lv_idx / 2)
6286 {
6287 /* closest to the start of the list */
6288 item = l->lv_first;
6289 idx = 0;
6290 }
6291 else if (n > (l->lv_idx + l->lv_len) / 2)
6292 {
6293 /* closest to the end of the list */
6294 item = l->lv_last;
6295 idx = l->lv_len - 1;
6296 }
6297 else
6298 {
6299 /* closest to the cached index */
6300 item = l->lv_idx_item;
6301 idx = l->lv_idx;
6302 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006303 }
6304 else
6305 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006306 if (n < l->lv_len / 2)
6307 {
6308 /* closest to the start of the list */
6309 item = l->lv_first;
6310 idx = 0;
6311 }
6312 else
6313 {
6314 /* closest to the end of the list */
6315 item = l->lv_last;
6316 idx = l->lv_len - 1;
6317 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006319
6320 while (n > idx)
6321 {
6322 /* search forward */
6323 item = item->li_next;
6324 ++idx;
6325 }
6326 while (n < idx)
6327 {
6328 /* search backward */
6329 item = item->li_prev;
6330 --idx;
6331 }
6332
6333 /* cache the used index */
6334 l->lv_idx = idx;
6335 l->lv_idx_item = item;
6336
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006337 return item;
6338}
6339
6340/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006341 * Get list item "l[idx]" as a number.
6342 */
6343 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006344list_find_nr(
6345 list_T *l,
6346 long idx,
6347 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006348{
6349 listitem_T *li;
6350
6351 li = list_find(l, idx);
6352 if (li == NULL)
6353 {
6354 if (errorp != NULL)
6355 *errorp = TRUE;
6356 return -1L;
6357 }
6358 return get_tv_number_chk(&li->li_tv, errorp);
6359}
6360
6361/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006362 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6363 */
6364 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006365list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006366{
6367 listitem_T *li;
6368
6369 li = list_find(l, idx - 1);
6370 if (li == NULL)
6371 {
6372 EMSGN(_(e_listidx), idx);
6373 return NULL;
6374 }
6375 return get_tv_string(&li->li_tv);
6376}
6377
6378/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006379 * Locate "item" list "l" and return its index.
6380 * Returns -1 when "item" is not in the list.
6381 */
6382 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006383list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006384{
6385 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006387
6388 if (l == NULL)
6389 return -1;
6390 idx = 0;
6391 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6392 ++idx;
6393 if (li == NULL)
6394 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006395 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006396}
6397
6398/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399 * Append item "item" to the end of list "l".
6400 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006402list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403{
6404 if (l->lv_last == NULL)
6405 {
6406 /* empty list */
6407 l->lv_first = item;
6408 l->lv_last = item;
6409 item->li_prev = NULL;
6410 }
6411 else
6412 {
6413 l->lv_last->li_next = item;
6414 item->li_prev = l->lv_last;
6415 l->lv_last = item;
6416 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006417 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 item->li_next = NULL;
6419}
6420
6421/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006425 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006426list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006428 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432 copy_tv(tv, &li->li_tv);
6433 list_append(l, li);
6434 return OK;
6435}
6436
6437/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006438 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006439 * Return FAIL when out of memory.
6440 */
6441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006442list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006443{
6444 listitem_T *li = listitem_alloc();
6445
6446 if (li == NULL)
6447 return FAIL;
6448 li->li_tv.v_type = VAR_DICT;
6449 li->li_tv.v_lock = 0;
6450 li->li_tv.vval.v_dict = dict;
6451 list_append(list, li);
6452 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006453 return OK;
6454}
6455
6456/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006457 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006458 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006459 * Returns FAIL when out of memory.
6460 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006461 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006462list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463{
6464 listitem_T *li = listitem_alloc();
6465
6466 if (li == NULL)
6467 return FAIL;
6468 list_append(l, li);
6469 li->li_tv.v_type = VAR_STRING;
6470 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006471 if (str == NULL)
6472 li->li_tv.vval.v_string = NULL;
6473 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006474 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006475 return FAIL;
6476 return OK;
6477}
6478
6479/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006480 * Append "n" to list "l".
6481 * Returns FAIL when out of memory.
6482 */
6483 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006484list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006485{
6486 listitem_T *li;
6487
6488 li = listitem_alloc();
6489 if (li == NULL)
6490 return FAIL;
6491 li->li_tv.v_type = VAR_NUMBER;
6492 li->li_tv.v_lock = 0;
6493 li->li_tv.vval.v_number = n;
6494 list_append(l, li);
6495 return OK;
6496}
6497
6498/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 * If "item" is NULL append at the end.
6501 * Return FAIL when out of memory.
6502 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006503 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006504list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505{
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507
6508 if (ni == NULL)
6509 return FAIL;
6510 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006511 list_insert(l, ni, item);
6512 return OK;
6513}
6514
6515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006516list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006517{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 if (item == NULL)
6519 /* Append new item at end of list. */
6520 list_append(l, ni);
6521 else
6522 {
6523 /* Insert new item before existing item. */
6524 ni->li_prev = item->li_prev;
6525 ni->li_next = item;
6526 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006527 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006529 ++l->lv_idx;
6530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006532 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006534 l->lv_idx_item = NULL;
6535 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006537 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539}
6540
6541/*
6542 * Extend "l1" with "l2".
6543 * If "bef" is NULL append at the end, otherwise insert before this item.
6544 * Returns FAIL when out of memory.
6545 */
6546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006547list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548{
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006550 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006552 /* We also quit the loop when we have inserted the original item count of
6553 * the list, avoid a hang when we extend a list with itself. */
6554 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6556 return FAIL;
6557 return OK;
6558}
6559
6560/*
6561 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6562 * Return FAIL when out of memory.
6563 */
6564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006565list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006569 if (l1 == NULL || l2 == NULL)
6570 return FAIL;
6571
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006572 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 if (l == NULL)
6575 return FAIL;
6576 tv->v_type = VAR_LIST;
6577 tv->vval.v_list = l;
6578
6579 /* append all items from the second list */
6580 return list_extend(l, l2, NULL);
6581}
6582
6583/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006584 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006586 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 * Returns NULL when out of memory.
6588 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006590list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591{
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 list_T *copy;
6593 listitem_T *item;
6594 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595
6596 if (orig == NULL)
6597 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598
6599 copy = list_alloc();
6600 if (copy != NULL)
6601 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006602 if (copyID != 0)
6603 {
6604 /* Do this before adding the items, because one of the items may
6605 * refer back to this list. */
6606 orig->lv_copyID = copyID;
6607 orig->lv_copylist = copy;
6608 }
6609 for (item = orig->lv_first; item != NULL && !got_int;
6610 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 {
6612 ni = listitem_alloc();
6613 if (ni == NULL)
6614 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006616 {
6617 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6618 {
6619 vim_free(ni);
6620 break;
6621 }
6622 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006624 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 list_append(copy, ni);
6626 }
6627 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006628 if (item != NULL)
6629 {
6630 list_unref(copy);
6631 copy = NULL;
6632 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 }
6634
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 return copy;
6636}
6637
6638/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006639 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006640 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006641 * This used to be called list_remove, but that conflicts with a Sun header
6642 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006645vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646{
Bram Moolenaar33570922005-01-25 22:26:29 +00006647 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006649 /* notify watchers */
6650 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006652 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006653 list_fix_watch(l, ip);
6654 if (ip == item2)
6655 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657
6658 if (item2->li_next == NULL)
6659 l->lv_last = item->li_prev;
6660 else
6661 item2->li_next->li_prev = item->li_prev;
6662 if (item->li_prev == NULL)
6663 l->lv_first = item2->li_next;
6664 else
6665 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006666 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667}
6668
6669/*
6670 * Return an allocated string with the string representation of a list.
6671 * May return NULL.
6672 */
6673 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675{
6676 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677
6678 if (tv->vval.v_list == NULL)
6679 return NULL;
6680 ga_init2(&ga, (int)sizeof(char), 80);
6681 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006682 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006683 {
6684 vim_free(ga.ga_data);
6685 return NULL;
6686 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687 ga_append(&ga, ']');
6688 ga_append(&ga, NUL);
6689 return (char_u *)ga.ga_data;
6690}
6691
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692typedef struct join_S {
6693 char_u *s;
6694 char_u *tofree;
6695} join_T;
6696
6697 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698list_join_inner(
6699 garray_T *gap, /* to store the result in */
6700 list_T *l,
6701 char_u *sep,
6702 int echo_style,
6703 int copyID,
6704 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006705{
6706 int i;
6707 join_T *p;
6708 int len;
6709 int sumlen = 0;
6710 int first = TRUE;
6711 char_u *tofree;
6712 char_u numbuf[NUMBUFLEN];
6713 listitem_T *item;
6714 char_u *s;
6715
6716 /* Stringify each item in the list. */
6717 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6718 {
6719 if (echo_style)
6720 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6721 else
6722 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6723 if (s == NULL)
6724 return FAIL;
6725
6726 len = (int)STRLEN(s);
6727 sumlen += len;
6728
Bram Moolenaarcde88542015-08-11 19:14:00 +02006729 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006730 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6731 if (tofree != NULL || s != numbuf)
6732 {
6733 p->s = s;
6734 p->tofree = tofree;
6735 }
6736 else
6737 {
6738 p->s = vim_strnsave(s, len);
6739 p->tofree = p->s;
6740 }
6741
6742 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006743 if (did_echo_string_emsg) /* recursion error, bail out */
6744 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006745 }
6746
6747 /* Allocate result buffer with its total size, avoid re-allocation and
6748 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6749 if (join_gap->ga_len >= 2)
6750 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6751 if (ga_grow(gap, sumlen + 2) == FAIL)
6752 return FAIL;
6753
6754 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6755 {
6756 if (first)
6757 first = FALSE;
6758 else
6759 ga_concat(gap, sep);
6760 p = ((join_T *)join_gap->ga_data) + i;
6761
6762 if (p->s != NULL)
6763 ga_concat(gap, p->s);
6764 line_breakcheck();
6765 }
6766
6767 return OK;
6768}
6769
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006770/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006771 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006772 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006773 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006776list_join(
6777 garray_T *gap,
6778 list_T *l,
6779 char_u *sep,
6780 int echo_style,
6781 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006783 garray_T join_ga;
6784 int retval;
6785 join_T *p;
6786 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787
Bram Moolenaard39a7512015-04-16 22:51:22 +02006788 if (l->lv_len < 1)
6789 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006790 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6791 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6792
6793 /* Dispose each item in join_ga. */
6794 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006796 p = (join_T *)join_ga.ga_data;
6797 for (i = 0; i < join_ga.ga_len; ++i)
6798 {
6799 vim_free(p->tofree);
6800 ++p;
6801 }
6802 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006804
6805 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806}
6807
6808/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006809 * Return the next (unique) copy ID.
6810 * Used for serializing nested structures.
6811 */
6812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006813get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006814{
6815 current_copyID += COPYID_INC;
6816 return current_copyID;
6817}
6818
6819/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820 * Garbage collection for lists and dictionaries.
6821 *
6822 * We use reference counts to be able to free most items right away when they
6823 * are no longer used. But for composite items it's possible that it becomes
6824 * unused while the reference count is > 0: When there is a recursive
6825 * reference. Example:
6826 * :let l = [1, 2, 3]
6827 * :let d = {9: l}
6828 * :let l[1] = d
6829 *
6830 * Since this is quite unusual we handle this with garbage collection: every
6831 * once in a while find out which lists and dicts are not referenced from any
6832 * variable.
6833 *
6834 * Here is a good reference text about garbage collection (refers to Python
6835 * but it applies to all reference-counting mechanisms):
6836 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006837 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006838
6839/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 * Do garbage collection for lists and dicts.
6841 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006842 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006843 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006844garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006845{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006846 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006847 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848 buf_T *buf;
6849 win_T *wp;
6850 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006851 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006852 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006854#ifdef FEAT_WINDOWS
6855 tabpage_T *tp;
6856#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006858 /* Only do this once. */
6859 want_garbage_collect = FALSE;
6860 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006861 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006862
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 /* We advance by two because we add one for items referenced through
6864 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006865 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 /*
6868 * 1. Go through all accessible variables and mark all lists and dicts
6869 * with copyID.
6870 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871
6872 /* Don't free variables in the previous_funccal list unless they are only
6873 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006874 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6876 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6878 NULL);
6879 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6880 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 }
6882
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883 /* script-local variables */
6884 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886
6887 /* buffer-local variables */
6888 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6890 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891
6892 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006893 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006894 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6895 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006896#ifdef FEAT_AUTOCMD
6897 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006898 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6899 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006900#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006902#ifdef FEAT_WINDOWS
6903 /* tabpage-local variables */
6904 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6906 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006907#endif
6908
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911
6912 /* function-local variables */
6913 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6914 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6916 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 }
6918
Bram Moolenaard812df62008-11-09 12:46:09 +00006919 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006921
Bram Moolenaar1dced572012-04-05 16:54:08 +02006922#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006924#endif
6925
Bram Moolenaardb913952012-06-29 12:54:53 +02006926#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006928#endif
6929
6930#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006932#endif
6933
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006934#ifdef FEAT_CHANNEL
6935 abort = abort || set_ref_in_channel(copyID);
6936#endif
6937
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006939 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006940 /*
6941 * 2. Free lists and dictionaries that are not referenced.
6942 */
6943 did_free = free_unref_items(copyID);
6944
6945 /*
6946 * 3. Check if any funccal can be freed now.
6947 */
6948 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 if (can_free_funccal(*pfc, copyID))
6951 {
6952 fc = *pfc;
6953 *pfc = fc->caller;
6954 free_funccal(fc, TRUE);
6955 did_free = TRUE;
6956 did_free_funccal = TRUE;
6957 }
6958 else
6959 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006960 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006961 if (did_free_funccal)
6962 /* When a funccal was freed some more items might be garbage
6963 * collected, so run again. */
6964 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006966 else if (p_verbose > 0)
6967 {
6968 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6969 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970
6971 return did_free;
6972}
6973
6974/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006975 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 */
6977 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006978free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006980 dict_T *dd, *dd_next;
6981 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 int did_free = FALSE;
6983
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 */
6987 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006988 {
6989 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006992 /* Free the Dictionary and ordinary items it contains, but don't
6993 * recurse into Lists and Dictionaries, they will be in the list
6994 * of dicts or list of lists. */
6995 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006998 dd = dd_next;
6999 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000
7001 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007002 * Go through the list of lists and free items without the copyID.
7003 * But don't free a list that has a watcher (used in a for loop), these
7004 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 */
7006 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 {
7008 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007009 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7010 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007012 /* Free the List and ordinary items it contains, but don't recurse
7013 * into Lists and Dictionaries, they will be in the list of dicts
7014 * or list of lists. */
7015 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007018 ll = ll_next;
7019 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007020
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 return did_free;
7022}
7023
7024/*
7025 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007026 * "list_stack" is used to add lists to be marked. Can be NULL.
7027 *
7028 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007031set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032{
7033 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 hashtab_T *cur_ht;
7037 ht_stack_T *ht_stack = NULL;
7038 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 cur_ht = ht;
7041 for (;;)
7042 {
7043 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 /* Mark each item in the hashtab. If the item contains a hashtab
7046 * it is added to ht_stack, if it contains a list it is added to
7047 * list_stack. */
7048 todo = (int)cur_ht->ht_used;
7049 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7050 if (!HASHITEM_EMPTY(hi))
7051 {
7052 --todo;
7053 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7054 &ht_stack, list_stack);
7055 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057
7058 if (ht_stack == NULL)
7059 break;
7060
7061 /* take an item from the stack */
7062 cur_ht = ht_stack->ht;
7063 tempitem = ht_stack;
7064 ht_stack = ht_stack->prev;
7065 free(tempitem);
7066 }
7067
7068 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069}
7070
7071/*
7072 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7074 *
7075 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007077 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 listitem_T *li;
7081 int abort = FALSE;
7082 list_T *cur_l;
7083 list_stack_T *list_stack = NULL;
7084 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007086 cur_l = l;
7087 for (;;)
7088 {
7089 if (!abort)
7090 /* Mark each item in the list. If the item contains a hashtab
7091 * it is added to ht_stack, if it contains a list it is added to
7092 * list_stack. */
7093 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7094 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7095 ht_stack, &list_stack);
7096 if (list_stack == NULL)
7097 break;
7098
7099 /* take an item from the stack */
7100 cur_l = list_stack->list;
7101 tempitem = list_stack;
7102 list_stack = list_stack->prev;
7103 free(tempitem);
7104 }
7105
7106 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107}
7108
7109/*
7110 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007111 * "list_stack" is used to add lists to be marked. Can be NULL.
7112 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7113 *
7114 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007117set_ref_in_item(
7118 typval_T *tv,
7119 int copyID,
7120 ht_stack_T **ht_stack,
7121 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007122{
7123 dict_T *dd;
7124 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007125 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007126
Bram Moolenaara03f2332016-02-06 18:09:59 +01007127 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007128 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007129 dd = tv->vval.v_dict;
7130 if (dd != NULL && dd->dv_copyID != copyID)
7131 {
7132 /* Didn't see this dict yet. */
7133 dd->dv_copyID = copyID;
7134 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007135 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007136 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7137 }
7138 else
7139 {
7140 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7141 if (newitem == NULL)
7142 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007143 else
7144 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007145 newitem->ht = &dd->dv_hashtab;
7146 newitem->prev = *ht_stack;
7147 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007148 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007149 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007150 }
7151 }
7152 else if (tv->v_type == VAR_LIST)
7153 {
7154 ll = tv->vval.v_list;
7155 if (ll != NULL && ll->lv_copyID != copyID)
7156 {
7157 /* Didn't see this list yet. */
7158 ll->lv_copyID = copyID;
7159 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007160 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007161 abort = set_ref_in_list(ll, copyID, ht_stack);
7162 }
7163 else
7164 {
7165 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007167 if (newitem == NULL)
7168 abort = TRUE;
7169 else
7170 {
7171 newitem->list = ll;
7172 newitem->prev = *list_stack;
7173 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007174 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007175 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007176 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007177 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007178 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007179}
7180
7181/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182 * Allocate an empty header for a dictionary.
7183 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007184 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007185dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007186{
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007192 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193 if (first_dict != NULL)
7194 first_dict->dv_used_prev = d;
7195 d->dv_used_next = first_dict;
7196 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007197 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007198
Bram Moolenaar33570922005-01-25 22:26:29 +00007199 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007200 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007201 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007202 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007203 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007204 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206}
7207
7208/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007209 * Allocate an empty dict for a return value.
7210 * Returns OK or FAIL.
7211 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007212 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007213rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007214{
7215 dict_T *d = dict_alloc();
7216
7217 if (d == NULL)
7218 return FAIL;
7219
7220 rettv->vval.v_dict = d;
7221 rettv->v_type = VAR_DICT;
7222 ++d->dv_refcount;
7223 return OK;
7224}
7225
7226
7227/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228 * Unreference a Dictionary: decrement the reference count and free it when it
7229 * becomes zero.
7230 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007232dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007234 if (d != NULL && --d->dv_refcount <= 0)
7235 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236}
7237
7238/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007239 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 * Ignores the reference count.
7241 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007242 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007243dict_free(
7244 dict_T *d,
7245 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007249 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007251 /* Remove the dict from the list of dicts for garbage collection. */
7252 if (d->dv_used_prev == NULL)
7253 first_dict = d->dv_used_next;
7254 else
7255 d->dv_used_prev->dv_used_next = d->dv_used_next;
7256 if (d->dv_used_next != NULL)
7257 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7258
7259 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007260 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007261 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007264 if (!HASHITEM_EMPTY(hi))
7265 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007266 /* Remove the item before deleting it, just in case there is
7267 * something recursive causing trouble. */
7268 di = HI2DI(hi);
7269 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007270 if (recurse || (di->di_tv.v_type != VAR_LIST
7271 && di->di_tv.v_type != VAR_DICT))
7272 clear_tv(&di->di_tv);
7273 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 --todo;
7275 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 vim_free(d);
7279}
7280
7281/*
7282 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 * The "key" is copied to the new item.
7284 * Note that the value of the item "di_tv" still needs to be initialized!
7285 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007287 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007288dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289{
Bram Moolenaar33570922005-01-25 22:26:29 +00007290 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007291
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007292 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007294 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007295 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007296 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007297 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299}
7300
7301/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 * Make a copy of a Dictionary item.
7303 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007305dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306{
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007309 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7310 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007311 if (di != NULL)
7312 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007314 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315 copy_tv(&org->di_tv, &di->di_tv);
7316 }
7317 return di;
7318}
7319
7320/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 * Remove item "item" from Dictionary "dict" and free it.
7322 */
7323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007324dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325{
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 if (HASHITEM_EMPTY(hi))
7330 EMSG2(_(e_intern2), "dictitem_remove()");
7331 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 dictitem_free(item);
7334}
7335
7336/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 * Free a dict item. Also clears the value.
7338 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007340dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007343 if (item->di_flags & DI_FLAGS_ALLOC)
7344 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345}
7346
7347/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7349 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007350 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351 * Returns NULL when out of memory.
7352 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007354dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355{
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 dict_T *copy;
7357 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360
7361 if (orig == NULL)
7362 return NULL;
7363
7364 copy = dict_alloc();
7365 if (copy != NULL)
7366 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007367 if (copyID != 0)
7368 {
7369 orig->dv_copyID = copyID;
7370 orig->dv_copydict = copy;
7371 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007372 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007373 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007375 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377 --todo;
7378
7379 di = dictitem_alloc(hi->hi_key);
7380 if (di == NULL)
7381 break;
7382 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007383 {
7384 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7385 copyID) == FAIL)
7386 {
7387 vim_free(di);
7388 break;
7389 }
7390 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 else
7392 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7393 if (dict_add(copy, di) == FAIL)
7394 {
7395 dictitem_free(di);
7396 break;
7397 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007402 if (todo > 0)
7403 {
7404 dict_unref(copy);
7405 copy = NULL;
7406 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007407 }
7408
7409 return copy;
7410}
7411
7412/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007414 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007417dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418{
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420}
7421
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007423 * Add a number or string entry to dictionary "d".
7424 * When "str" is NULL use number "nr", otherwise use "str".
7425 * Returns FAIL when out of memory and when key already exists.
7426 */
7427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007428dict_add_nr_str(
7429 dict_T *d,
7430 char *key,
7431 long nr,
7432 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007433{
7434 dictitem_T *item;
7435
7436 item = dictitem_alloc((char_u *)key);
7437 if (item == NULL)
7438 return FAIL;
7439 item->di_tv.v_lock = 0;
7440 if (str == NULL)
7441 {
7442 item->di_tv.v_type = VAR_NUMBER;
7443 item->di_tv.vval.v_number = nr;
7444 }
7445 else
7446 {
7447 item->di_tv.v_type = VAR_STRING;
7448 item->di_tv.vval.v_string = vim_strsave(str);
7449 }
7450 if (dict_add(d, item) == FAIL)
7451 {
7452 dictitem_free(item);
7453 return FAIL;
7454 }
7455 return OK;
7456}
7457
7458/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007459 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007460 * Returns FAIL when out of memory and when key already exists.
7461 */
7462 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007463dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007464{
7465 dictitem_T *item;
7466
7467 item = dictitem_alloc((char_u *)key);
7468 if (item == NULL)
7469 return FAIL;
7470 item->di_tv.v_lock = 0;
7471 item->di_tv.v_type = VAR_LIST;
7472 item->di_tv.vval.v_list = list;
7473 if (dict_add(d, item) == FAIL)
7474 {
7475 dictitem_free(item);
7476 return FAIL;
7477 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007478 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007479 return OK;
7480}
7481
7482/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007483 * Get the number of items in a Dictionary.
7484 */
7485 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007486dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007487{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007488 if (d == NULL)
7489 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007490 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007491}
7492
7493/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 * Find item "key[len]" in Dictionary "d".
7495 * If "len" is negative use strlen(key).
7496 * Returns NULL when not found.
7497 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007498 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007499dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007501#define AKEYLEN 200
7502 char_u buf[AKEYLEN];
7503 char_u *akey;
7504 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007505 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007507 if (len < 0)
7508 akey = key;
7509 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 tofree = akey = vim_strnsave(key, len);
7512 if (akey == NULL)
7513 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007514 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 else
7516 {
7517 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007518 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 akey = buf;
7520 }
7521
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 vim_free(tofree);
7524 if (HASHITEM_EMPTY(hi))
7525 return NULL;
7526 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527}
7528
7529/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007530 * Get a string item from a dictionary.
7531 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007532 * Returns NULL if the entry doesn't exist or out of memory.
7533 */
7534 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007535get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007536{
7537 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007538 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007539
7540 di = dict_find(d, key, -1);
7541 if (di == NULL)
7542 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007543 s = get_tv_string(&di->di_tv);
7544 if (save && s != NULL)
7545 s = vim_strsave(s);
7546 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007547}
7548
7549/*
7550 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007551 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007552 */
7553 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007554get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007555{
7556 dictitem_T *di;
7557
7558 di = dict_find(d, key, -1);
7559 if (di == NULL)
7560 return 0;
7561 return get_tv_number(&di->di_tv);
7562}
7563
7564/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 * Return an allocated string with the string representation of a Dictionary.
7566 * May return NULL.
7567 */
7568 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007569dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570{
7571 garray_T ga;
7572 int first = TRUE;
7573 char_u *tofree;
7574 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007578 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007580 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 return NULL;
7582 ga_init2(&ga, (int)sizeof(char), 80);
7583 ga_append(&ga, '{');
7584
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007585 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007586 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007588 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 --todo;
7591
7592 if (first)
7593 first = FALSE;
7594 else
7595 ga_concat(&ga, (char_u *)", ");
7596
7597 tofree = string_quote(hi->hi_key, FALSE);
7598 if (tofree != NULL)
7599 {
7600 ga_concat(&ga, tofree);
7601 vim_free(tofree);
7602 }
7603 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007604 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007605 if (s != NULL)
7606 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007608 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007610 line_breakcheck();
7611
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007614 if (todo > 0)
7615 {
7616 vim_free(ga.ga_data);
7617 return NULL;
7618 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619
7620 ga_append(&ga, '}');
7621 ga_append(&ga, NUL);
7622 return (char_u *)ga.ga_data;
7623}
7624
7625/*
7626 * Allocate a variable for a Dictionary and fill it from "*arg".
7627 * Return OK or FAIL. Returns NOTDONE for {expr}.
7628 */
7629 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007630get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631{
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 dict_T *d = NULL;
7633 typval_T tvkey;
7634 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007635 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639
7640 /*
7641 * First check if it's not a curly-braces thing: {expr}.
7642 * Must do this without evaluating, otherwise a function may be called
7643 * twice. Unfortunately this means we need to call eval1() twice for the
7644 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 if (*start != '}')
7648 {
7649 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7650 return FAIL;
7651 if (*start == '}')
7652 return NOTDONE;
7653 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654
7655 if (evaluate)
7656 {
7657 d = dict_alloc();
7658 if (d == NULL)
7659 return FAIL;
7660 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 tvkey.v_type = VAR_UNKNOWN;
7662 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663
7664 *arg = skipwhite(*arg + 1);
7665 while (**arg != '}' && **arg != NUL)
7666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 goto failret;
7669 if (**arg != ':')
7670 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007671 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007672 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 goto failret;
7674 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007675 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007677 key = get_tv_string_buf_chk(&tvkey, buf);
7678 if (key == NULL || *key == NUL)
7679 {
7680 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7681 if (key != NULL)
7682 EMSG(_(e_emptykey));
7683 clear_tv(&tvkey);
7684 goto failret;
7685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687
7688 *arg = skipwhite(*arg + 1);
7689 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7690 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007691 if (evaluate)
7692 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 goto failret;
7694 }
7695 if (evaluate)
7696 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007697 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 if (item != NULL)
7699 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007700 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007701 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 clear_tv(&tv);
7703 goto failret;
7704 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007705 item = dictitem_alloc(key);
7706 clear_tv(&tvkey);
7707 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007709 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007710 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007711 if (dict_add(d, item) == FAIL)
7712 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713 }
7714 }
7715
7716 if (**arg == '}')
7717 break;
7718 if (**arg != ',')
7719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007720 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 goto failret;
7722 }
7723 *arg = skipwhite(*arg + 1);
7724 }
7725
7726 if (**arg != '}')
7727 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007728 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729failret:
7730 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007731 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 return FAIL;
7733 }
7734
7735 *arg = skipwhite(*arg + 1);
7736 if (evaluate)
7737 {
7738 rettv->v_type = VAR_DICT;
7739 rettv->vval.v_dict = d;
7740 ++d->dv_refcount;
7741 }
7742
7743 return OK;
7744}
7745
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007746#if defined(FEAT_CHANNEL) || defined(PROTO)
7747/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007748 * Decrement the reference count on "channel" and maybe free it when it goes
7749 * down to zero. Don't free it if there is a pending action.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007750 * Returns TRUE when the channel was freed.
7751 */
7752 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007753channel_unref(channel_T *channel)
7754{
7755 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007756 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007757 channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007758 return TRUE;
7759 }
7760 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007761}
7762#endif
7763
Bram Moolenaar65edff82016-02-21 16:40:11 +01007764#if defined(FEAT_JOB) || defined(PROTO)
7765static job_T *first_job = NULL;
7766
Bram Moolenaar835dc632016-02-07 14:27:38 +01007767 static void
7768job_free(job_T *job)
7769{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007770# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007771 if (job->jv_channel != NULL)
7772 {
7773 /* The channel doesn't count as a references for the job, we need to
7774 * NULL the reference when the job is freed. */
7775 job->jv_channel->ch_job = NULL;
7776 channel_unref(job->jv_channel);
7777 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007778# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007779 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007780
7781 if (job->jv_next != NULL)
7782 job->jv_next->jv_prev = job->jv_prev;
7783 if (job->jv_prev == NULL)
7784 first_job = job->jv_next;
7785 else
7786 job->jv_prev->jv_next = job->jv_next;
7787
7788 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007789 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007790 vim_free(job);
7791}
7792
7793 static void
7794job_unref(job_T *job)
7795{
7796 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007797 {
7798 /* Do not free the job when it has not ended yet and there is a
7799 * "stoponexit" flag or an exit callback. */
7800 if (job->jv_status != JOB_STARTED
7801 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
7802 job_free(job);
7803 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007804}
7805
7806/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007807 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007808 */
7809 static job_T *
7810job_alloc(void)
7811{
7812 job_T *job;
7813
7814 job = (job_T *)alloc_clear(sizeof(job_T));
7815 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007816 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007817 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007818 job->jv_stoponexit = vim_strsave((char_u *)"term");
7819
7820 if (first_job != NULL)
7821 {
7822 first_job->jv_prev = job;
7823 job->jv_next = first_job;
7824 }
7825 first_job = job;
7826 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007827 return job;
7828}
7829
Bram Moolenaar65edff82016-02-21 16:40:11 +01007830 static void
7831job_set_options(job_T *job, jobopt_T *opt)
7832{
7833 if (opt->jo_set & JO_STOPONEXIT)
7834 {
7835 vim_free(job->jv_stoponexit);
7836 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7837 job->jv_stoponexit = NULL;
7838 else
7839 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7840 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007841 if (opt->jo_set & JO_EXIT_CB)
7842 {
7843 vim_free(job->jv_exit_cb);
7844 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7845 job->jv_exit_cb = NULL;
7846 else
7847 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7848 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007849}
7850
7851/*
7852 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7853 */
7854 void
7855job_stop_on_exit()
7856{
7857 job_T *job;
7858
7859 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007860 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007861 mch_stop_job(job, job->jv_stoponexit);
7862}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007863#endif
7864
Bram Moolenaar17a13432016-01-24 14:22:10 +01007865 static char *
7866get_var_special_name(int nr)
7867{
7868 switch (nr)
7869 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007870 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007871 case VVAL_TRUE: return "v:true";
7872 case VVAL_NONE: return "v:none";
7873 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007874 }
7875 EMSG2(_(e_intern2), "get_var_special_name()");
7876 return "42";
7877}
7878
Bram Moolenaar8c711452005-01-14 21:53:12 +00007879/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007880 * Return a string with the string representation of a variable.
7881 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007882 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007884 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007885 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007886 */
7887 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007888echo_string(
7889 typval_T *tv,
7890 char_u **tofree,
7891 char_u *numbuf,
7892 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007893{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007894 static int recurse = 0;
7895 char_u *r = NULL;
7896
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007899 if (!did_echo_string_emsg)
7900 {
7901 /* Only give this message once for a recursive call to avoid
7902 * flooding the user with errors. And stop iterating over lists
7903 * and dicts. */
7904 did_echo_string_emsg = TRUE;
7905 EMSG(_("E724: variable nested too deep for displaying"));
7906 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007907 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007908 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007909 }
7910 ++recurse;
7911
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007912 switch (tv->v_type)
7913 {
7914 case VAR_FUNC:
7915 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 r = tv->vval.v_string;
7917 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007918
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007920 if (tv->vval.v_list == NULL)
7921 {
7922 *tofree = NULL;
7923 r = NULL;
7924 }
7925 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7926 {
7927 *tofree = NULL;
7928 r = (char_u *)"[...]";
7929 }
7930 else
7931 {
7932 tv->vval.v_list->lv_copyID = copyID;
7933 *tofree = list2string(tv, copyID);
7934 r = *tofree;
7935 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007936 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007937
Bram Moolenaar8c711452005-01-14 21:53:12 +00007938 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007939 if (tv->vval.v_dict == NULL)
7940 {
7941 *tofree = NULL;
7942 r = NULL;
7943 }
7944 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7945 {
7946 *tofree = NULL;
7947 r = (char_u *)"{...}";
7948 }
7949 else
7950 {
7951 tv->vval.v_dict->dv_copyID = copyID;
7952 *tofree = dict2string(tv, copyID);
7953 r = *tofree;
7954 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007955 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007956
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007957 case VAR_STRING:
7958 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007959 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007960 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007961 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007962 *tofree = NULL;
7963 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007964 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007965
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007966 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007967#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007968 *tofree = NULL;
7969 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7970 r = numbuf;
7971 break;
7972#endif
7973
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007974 case VAR_SPECIAL:
7975 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007976 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007977 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007979
Bram Moolenaar8502c702014-06-17 12:51:16 +02007980 if (--recurse == 0)
7981 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007982 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007983}
7984
7985/*
7986 * Return a string with the string representation of a variable.
7987 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7988 * "numbuf" is used for a number.
7989 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007990 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007991 */
7992 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007993tv2string(
7994 typval_T *tv,
7995 char_u **tofree,
7996 char_u *numbuf,
7997 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007998{
7999 switch (tv->v_type)
8000 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008001 case VAR_FUNC:
8002 *tofree = string_quote(tv->vval.v_string, TRUE);
8003 return *tofree;
8004 case VAR_STRING:
8005 *tofree = string_quote(tv->vval.v_string, FALSE);
8006 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008007#ifdef FEAT_FLOAT
8008 case VAR_FLOAT:
8009 *tofree = NULL;
8010 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8011 return numbuf;
8012#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008013 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008014 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008015 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008016 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008017 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008018 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008019 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008020 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008021 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008022 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008023}
8024
8025/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008026 * Return string "str" in ' quotes, doubling ' characters.
8027 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008028 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008029 */
8030 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008031string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008032{
Bram Moolenaar33570922005-01-25 22:26:29 +00008033 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008034 char_u *p, *r, *s;
8035
Bram Moolenaar33570922005-01-25 22:26:29 +00008036 len = (function ? 13 : 3);
8037 if (str != NULL)
8038 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008039 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 for (p = str; *p != NUL; mb_ptr_adv(p))
8041 if (*p == '\'')
8042 ++len;
8043 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008044 s = r = alloc(len);
8045 if (r != NULL)
8046 {
8047 if (function)
8048 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008049 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008050 r += 10;
8051 }
8052 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008053 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008054 if (str != NULL)
8055 for (p = str; *p != NUL; )
8056 {
8057 if (*p == '\'')
8058 *r++ = '\'';
8059 MB_COPY_CHAR(p, r);
8060 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008062 if (function)
8063 *r++ = ')';
8064 *r++ = NUL;
8065 }
8066 return s;
8067}
8068
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008069#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070/*
8071 * Convert the string "text" to a floating point number.
8072 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8073 * this always uses a decimal point.
8074 * Returns the length of the text that was consumed.
8075 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008076 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008077string2float(
8078 char_u *text,
8079 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080{
8081 char *s = (char *)text;
8082 float_T f;
8083
8084 f = strtod(s, &s);
8085 *value = f;
8086 return (int)((char_u *)s - text);
8087}
8088#endif
8089
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 * Get the value of an environment variable.
8092 * "arg" is pointing to the '$'. It is advanced to after the name.
8093 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008094 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 */
8096 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008097get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 char_u *string = NULL;
8100 int len;
8101 int cc;
8102 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008103 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104
8105 ++*arg;
8106 name = *arg;
8107 len = get_env_len(arg);
8108 if (evaluate)
8109 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008110 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008111 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008112
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008113 cc = name[len];
8114 name[len] = NUL;
8115 /* first try vim_getenv(), fast for normal environment vars */
8116 string = vim_getenv(name, &mustfree);
8117 if (string != NULL && *string != NUL)
8118 {
8119 if (!mustfree)
8120 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008122 else
8123 {
8124 if (mustfree)
8125 vim_free(string);
8126
8127 /* next try expanding things like $VIM and ${HOME} */
8128 string = expand_env_save(name - 1);
8129 if (string != NULL && *string == '$')
8130 {
8131 vim_free(string);
8132 string = NULL;
8133 }
8134 }
8135 name[len] = cc;
8136
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 rettv->v_type = VAR_STRING;
8138 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 }
8140
8141 return OK;
8142}
8143
8144/*
8145 * Array with names and number of arguments of all internal functions
8146 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8147 */
8148static struct fst
8149{
8150 char *f_name; /* function name */
8151 char f_min_argc; /* minimal number of arguments */
8152 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008153 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008154 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155} functions[] =
8156{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008157#ifdef FEAT_FLOAT
8158 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008159 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008160#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008161 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008162 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008163 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"append", 2, 2, f_append},
8165 {"argc", 0, 0, f_argc},
8166 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008167 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008168 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008169#ifdef FEAT_FLOAT
8170 {"asin", 1, 1, f_asin}, /* WJMc */
8171#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008172 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008173 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008174 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008175 {"assert_false", 1, 2, f_assert_false},
8176 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008177#ifdef FEAT_FLOAT
8178 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008179 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008182 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"bufexists", 1, 1, f_bufexists},
8184 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8185 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8186 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8187 {"buflisted", 1, 1, f_buflisted},
8188 {"bufloaded", 1, 1, f_bufloaded},
8189 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008190 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {"bufwinnr", 1, 1, f_bufwinnr},
8192 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008193 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008194 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008195 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008196#ifdef FEAT_FLOAT
8197 {"ceil", 1, 1, f_ceil},
8198#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008199#ifdef FEAT_CHANNEL
8200 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008201# ifdef FEAT_JOB
8202 {"ch_getjob", 1, 1, f_ch_getjob},
8203# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008204 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008205 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008206 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008207 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008208 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008209 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8210 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008211 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008212 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008213#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008214 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008215 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008217 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008219#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008220 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008221 {"complete_add", 1, 1, f_complete_add},
8222 {"complete_check", 0, 0, f_complete_check},
8223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008225 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008226#ifdef FEAT_FLOAT
8227 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008228 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008229#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008230 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008232 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008233 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008234 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008236 {"diff_filler", 1, 1, f_diff_filler},
8237 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008238 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008239 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"eventhandler", 0, 0, f_eventhandler},
8243 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008244 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008246#ifdef FEAT_FLOAT
8247 {"exp", 1, 1, f_exp},
8248#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008249 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008250 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008251 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8253 {"filereadable", 1, 1, f_filereadable},
8254 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008255 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008256 {"finddir", 1, 3, f_finddir},
8257 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008258#ifdef FEAT_FLOAT
8259 {"float2nr", 1, 1, f_float2nr},
8260 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008261 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008262#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008263 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {"fnamemodify", 2, 2, f_fnamemodify},
8265 {"foldclosed", 1, 1, f_foldclosed},
8266 {"foldclosedend", 1, 1, f_foldclosedend},
8267 {"foldlevel", 1, 1, f_foldlevel},
8268 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008269 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008271 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008272 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008273 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008274 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008275 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {"getchar", 0, 1, f_getchar},
8277 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008278 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"getcmdline", 0, 0, f_getcmdline},
8280 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008281 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008282 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008283 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008284 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008285 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008286 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"getfsize", 1, 1, f_getfsize},
8288 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008289 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008290 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008291 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008292 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008293 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008294 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008295 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008296 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008298 {"gettabvar", 2, 3, f_gettabvar},
8299 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"getwinposx", 0, 0, f_getwinposx},
8301 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008302 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008303 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008304 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008305 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008307 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008308 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008309 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8311 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8312 {"histadd", 2, 2, f_histadd},
8313 {"histdel", 1, 2, f_histdel},
8314 {"histget", 1, 2, f_histget},
8315 {"histnr", 1, 1, f_histnr},
8316 {"hlID", 1, 1, f_hlID},
8317 {"hlexists", 1, 1, f_hlexists},
8318 {"hostname", 0, 0, f_hostname},
8319 {"iconv", 3, 3, f_iconv},
8320 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008321 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008322 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008324 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"inputrestore", 0, 0, f_inputrestore},
8326 {"inputsave", 0, 0, f_inputsave},
8327 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008328 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008329 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008331 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008332#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8333 {"isnan", 1, 1, f_isnan},
8334#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008335 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008336#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008337# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008338 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008339# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008340 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008341 {"job_start", 1, 2, f_job_start},
8342 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008343 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008344#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008345 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008346 {"js_decode", 1, 1, f_js_decode},
8347 {"js_encode", 1, 1, f_js_encode},
8348 {"json_decode", 1, 1, f_json_decode},
8349 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008350 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008352 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"libcall", 3, 3, f_libcall},
8354 {"libcallnr", 3, 3, f_libcallnr},
8355 {"line", 1, 1, f_line},
8356 {"line2byte", 1, 1, f_line2byte},
8357 {"lispindent", 1, 1, f_lispindent},
8358 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008359#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008360 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008361 {"log10", 1, 1, f_log10},
8362#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008363#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008364 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008365#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008366 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008367 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008368 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008369 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008370 {"matchadd", 2, 5, f_matchadd},
8371 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008372 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008373 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008374 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008375 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008376 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008377 {"max", 1, 1, f_max},
8378 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008379#ifdef vim_mkdir
8380 {"mkdir", 1, 3, f_mkdir},
8381#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008382 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008383#ifdef FEAT_MZSCHEME
8384 {"mzeval", 1, 1, f_mzeval},
8385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008387 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008388 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008389 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008390#ifdef FEAT_PERL
8391 {"perleval", 1, 1, f_perleval},
8392#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008393#ifdef FEAT_FLOAT
8394 {"pow", 2, 2, f_pow},
8395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008397 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008398 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008399#ifdef FEAT_PYTHON3
8400 {"py3eval", 1, 1, f_py3eval},
8401#endif
8402#ifdef FEAT_PYTHON
8403 {"pyeval", 1, 1, f_pyeval},
8404#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008405 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008406 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008407 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008408 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008409 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {"remote_expr", 2, 3, f_remote_expr},
8411 {"remote_foreground", 1, 1, f_remote_foreground},
8412 {"remote_peek", 1, 2, f_remote_peek},
8413 {"remote_read", 1, 1, f_remote_read},
8414 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008415 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008417 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008419 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008420#ifdef FEAT_FLOAT
8421 {"round", 1, 1, f_round},
8422#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008423 {"screenattr", 2, 2, f_screenattr},
8424 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008425 {"screencol", 0, 0, f_screencol},
8426 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008427 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008428 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008429 {"searchpair", 3, 7, f_searchpair},
8430 {"searchpairpos", 3, 7, f_searchpairpos},
8431 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {"server2client", 2, 2, f_server2client},
8433 {"serverlist", 0, 0, f_serverlist},
8434 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008435 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"setcmdpos", 1, 1, f_setcmdpos},
8437 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008438 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008439 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008440 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008441 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008443 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008444 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008446#ifdef FEAT_CRYPT
8447 {"sha256", 1, 1, f_sha256},
8448#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008449 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008450 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008452#ifdef FEAT_FLOAT
8453 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008454 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008455#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008456 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008457 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008458 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008459 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008460 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008461#ifdef FEAT_FLOAT
8462 {"sqrt", 1, 1, f_sqrt},
8463 {"str2float", 1, 1, f_str2float},
8464#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008465 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008466 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008467 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468#ifdef HAVE_STRFTIME
8469 {"strftime", 1, 2, f_strftime},
8470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008471 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008472 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {"strlen", 1, 1, f_strlen},
8474 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008475 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008477 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008478 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {"substitute", 4, 4, f_substitute},
8480 {"synID", 3, 3, f_synID},
8481 {"synIDattr", 2, 3, f_synIDattr},
8482 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008483 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008484 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008485 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008486 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008487 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008488 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008489 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008490 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008491 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008492#ifdef FEAT_FLOAT
8493 {"tan", 1, 1, f_tan},
8494 {"tanh", 1, 1, f_tanh},
8495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008497 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {"tolower", 1, 1, f_tolower},
8499 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008500 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008501#ifdef FEAT_FLOAT
8502 {"trunc", 1, 1, f_trunc},
8503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008505 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008506 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008507 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008508 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {"virtcol", 1, 1, f_virtcol},
8510 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008511 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {"winbufnr", 1, 1, f_winbufnr},
8513 {"wincol", 0, 0, f_wincol},
8514 {"winheight", 1, 1, f_winheight},
8515 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008516 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008518 {"winrestview", 1, 1, f_winrestview},
8519 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008521 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008522 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008523 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524};
8525
8526#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8527
8528/*
8529 * Function given to ExpandGeneric() to obtain the list of internal
8530 * or user defined function names.
8531 */
8532 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008533get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
8535 static int intidx = -1;
8536 char_u *name;
8537
8538 if (idx == 0)
8539 intidx = -1;
8540 if (intidx < 0)
8541 {
8542 name = get_user_func_name(xp, idx);
8543 if (name != NULL)
8544 return name;
8545 }
8546 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8547 {
8548 STRCPY(IObuff, functions[intidx].f_name);
8549 STRCAT(IObuff, "(");
8550 if (functions[intidx].f_max_argc == 0)
8551 STRCAT(IObuff, ")");
8552 return IObuff;
8553 }
8554
8555 return NULL;
8556}
8557
8558/*
8559 * Function given to ExpandGeneric() to obtain the list of internal or
8560 * user defined variable or function names.
8561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008563get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 static int intidx = -1;
8566 char_u *name;
8567
8568 if (idx == 0)
8569 intidx = -1;
8570 if (intidx < 0)
8571 {
8572 name = get_function_name(xp, idx);
8573 if (name != NULL)
8574 return name;
8575 }
8576 return get_user_var_name(xp, ++intidx);
8577}
8578
8579#endif /* FEAT_CMDL_COMPL */
8580
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008581#if defined(EBCDIC) || defined(PROTO)
8582/*
8583 * Compare struct fst by function name.
8584 */
8585 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008586compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008587{
8588 struct fst *p1 = (struct fst *)s1;
8589 struct fst *p2 = (struct fst *)s2;
8590
8591 return STRCMP(p1->f_name, p2->f_name);
8592}
8593
8594/*
8595 * Sort the function table by function name.
8596 * The sorting of the table above is ASCII dependant.
8597 * On machines using EBCDIC we have to sort it.
8598 */
8599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008600sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008601{
8602 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8603
8604 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8605}
8606#endif
8607
8608
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609/*
8610 * Find internal function in table above.
8611 * Return index, or -1 if not found
8612 */
8613 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008614find_internal_func(
8615 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 int first = 0;
8618 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8619 int cmp;
8620 int x;
8621
8622 /*
8623 * Find the function name in the table. Binary search.
8624 */
8625 while (first <= last)
8626 {
8627 x = first + ((unsigned)(last - first) >> 1);
8628 cmp = STRCMP(name, functions[x].f_name);
8629 if (cmp < 0)
8630 last = x - 1;
8631 else if (cmp > 0)
8632 first = x + 1;
8633 else
8634 return x;
8635 }
8636 return -1;
8637}
8638
8639/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008640 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8641 * name it contains, otherwise return "name".
8642 */
8643 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008644deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008645{
Bram Moolenaar33570922005-01-25 22:26:29 +00008646 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008647 int cc;
8648
8649 cc = name[*lenp];
8650 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008651 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008652 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656 {
8657 *lenp = 0;
8658 return (char_u *)""; /* just in case */
8659 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008660 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008661 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008662 }
8663
8664 return name;
8665}
8666
8667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 * Allocate a variable for the result of a function.
8669 * Return OK or FAIL.
8670 */
8671 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008672get_func_tv(
8673 char_u *name, /* name of the function */
8674 int len, /* length of "name" */
8675 typval_T *rettv,
8676 char_u **arg, /* argument, pointing to the '(' */
8677 linenr_T firstline, /* first line of range */
8678 linenr_T lastline, /* last line of range */
8679 int *doesrange, /* return: function handled range */
8680 int evaluate,
8681 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682{
8683 char_u *argp;
8684 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008685 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 int argcount = 0; /* number of arguments found */
8687
8688 /*
8689 * Get the arguments.
8690 */
8691 argp = *arg;
8692 while (argcount < MAX_FUNC_ARGS)
8693 {
8694 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8695 if (*argp == ')' || *argp == ',' || *argp == NUL)
8696 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8698 {
8699 ret = FAIL;
8700 break;
8701 }
8702 ++argcount;
8703 if (*argp != ',')
8704 break;
8705 }
8706 if (*argp == ')')
8707 ++argp;
8708 else
8709 ret = FAIL;
8710
8711 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008713 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008715 {
8716 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008717 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008718 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008719 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721
8722 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
8725 *arg = skipwhite(argp);
8726 return ret;
8727}
8728
8729
8730/*
8731 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008732 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008733 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008735 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008736call_func(
8737 char_u *funcname, /* name of the function */
8738 int len, /* length of "name" */
8739 typval_T *rettv, /* return value goes here */
8740 int argcount, /* number of "argvars" */
8741 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008742 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008743 linenr_T firstline, /* first line of range */
8744 linenr_T lastline, /* last line of range */
8745 int *doesrange, /* return: function handled range */
8746 int evaluate,
8747 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
8749 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750#define ERROR_UNKNOWN 0
8751#define ERROR_TOOMANY 1
8752#define ERROR_TOOFEW 2
8753#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754#define ERROR_DICT 4
8755#define ERROR_NONE 5
8756#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 int error = ERROR_NONE;
8758 int i;
8759 int llen;
8760 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761#define FLEN_FIXED 40
8762 char_u fname_buf[FLEN_FIXED + 1];
8763 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008764 char_u *name;
8765
8766 /* Make a copy of the name, if it comes from a funcref variable it could
8767 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008768 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008769 if (name == NULL)
8770 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771
8772 /*
8773 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8774 * Change <SNR>123_name() to K_SNR 123_name().
8775 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 llen = eval_fname_script(name);
8778 if (llen > 0)
8779 {
8780 fname_buf[0] = K_SPECIAL;
8781 fname_buf[1] = KS_EXTRA;
8782 fname_buf[2] = (int)KE_SNR;
8783 i = 3;
8784 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8785 {
8786 if (current_SID <= 0)
8787 error = ERROR_SCRIPT;
8788 else
8789 {
8790 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8791 i = (int)STRLEN(fname_buf);
8792 }
8793 }
8794 if (i + STRLEN(name + llen) < FLEN_FIXED)
8795 {
8796 STRCPY(fname_buf + i, name + llen);
8797 fname = fname_buf;
8798 }
8799 else
8800 {
8801 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8802 if (fname == NULL)
8803 error = ERROR_OTHER;
8804 else
8805 {
8806 mch_memmove(fname, fname_buf, (size_t)i);
8807 STRCPY(fname + i, name + llen);
8808 }
8809 }
8810 }
8811 else
8812 fname = name;
8813
8814 *doesrange = FALSE;
8815
8816
8817 /* execute the function if no errors detected and executing */
8818 if (evaluate && error == ERROR_NONE)
8819 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008820 char_u *rfname = fname;
8821
8822 /* Ignore "g:" before a function name. */
8823 if (fname[0] == 'g' && fname[1] == ':')
8824 rfname = fname + 2;
8825
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008826 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8827 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 error = ERROR_UNKNOWN;
8829
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008830 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {
8832 /*
8833 * User defined function.
8834 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008835 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008836
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008838 /* Trigger FuncUndefined event, may load the function. */
8839 if (fp == NULL
8840 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008841 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008842 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008844 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008845 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 }
8847#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008848 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008849 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008850 {
8851 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008852 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008853 }
8854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 if (fp != NULL)
8856 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008857 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008859 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008861 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008863 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008864 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 else
8866 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008867 int did_save_redo = FALSE;
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 /*
8870 * Call the user function.
8871 * Save and restore search patterns, script variables and
8872 * redo buffer.
8873 */
8874 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008875#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008876 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008877#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008878 {
8879 saveRedobuff();
8880 did_save_redo = TRUE;
8881 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008882 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008884 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008885 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8886 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8887 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008888 /* Function was unreferenced while being used, free it
8889 * now. */
8890 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008891 if (did_save_redo)
8892 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 restore_search_patterns();
8894 error = ERROR_NONE;
8895 }
8896 }
8897 }
8898 else
8899 {
8900 /*
8901 * Find the function name in the table, call its implementation.
8902 */
8903 i = find_internal_func(fname);
8904 if (i >= 0)
8905 {
8906 if (argcount < functions[i].f_min_argc)
8907 error = ERROR_TOOFEW;
8908 else if (argcount > functions[i].f_max_argc)
8909 error = ERROR_TOOMANY;
8910 else
8911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008912 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 error = ERROR_NONE;
8915 }
8916 }
8917 }
8918 /*
8919 * The function call (or "FuncUndefined" autocommand sequence) might
8920 * have been aborted by an error, an interrupt, or an explicitly thrown
8921 * exception that has not been caught so far. This situation can be
8922 * tested for by calling aborting(). For an error in an internal
8923 * function or for the "E132" error in call_user_func(), however, the
8924 * throw point at which the "force_abort" flag (temporarily reset by
8925 * emsg()) is normally updated has not been reached yet. We need to
8926 * update that flag first to make aborting() reliable.
8927 */
8928 update_force_abort();
8929 }
8930 if (error == ERROR_NONE)
8931 ret = OK;
8932
8933 /*
8934 * Report an error unless the argument evaluation or function call has been
8935 * cancelled due to an aborting error, an interrupt, or an exception.
8936 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008937 if (!aborting())
8938 {
8939 switch (error)
8940 {
8941 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008942 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008943 break;
8944 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008945 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008946 break;
8947 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008948 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008949 name);
8950 break;
8951 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008952 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008953 name);
8954 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008955 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008956 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957 name);
8958 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008959 }
8960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 if (fname != name && fname != fname_buf)
8963 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008964 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965
8966 return ret;
8967}
8968
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008969/*
8970 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008971 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008972 */
8973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008974emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008975{
8976 char_u *p;
8977
8978 if (*name == K_SPECIAL)
8979 p = concat_str((char_u *)"<SNR>", name + 3);
8980 else
8981 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008982 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008983 if (p != name)
8984 vim_free(p);
8985}
8986
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008987/*
8988 * Return TRUE for a non-zero Number and a non-empty String.
8989 */
8990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008991non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008992{
8993 return ((argvars[0].v_type == VAR_NUMBER
8994 && argvars[0].vval.v_number != 0)
8995 || (argvars[0].v_type == VAR_STRING
8996 && argvars[0].vval.v_string != NULL
8997 && *argvars[0].vval.v_string != NUL));
8998}
8999
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000/*********************************************
9001 * Implementation of the built-in functions
9002 */
9003
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009004#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009005static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009006
9007/*
9008 * Get the float value of "argvars[0]" into "f".
9009 * Returns FAIL when the argument is not a Number or Float.
9010 */
9011 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009012get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009013{
9014 if (argvars[0].v_type == VAR_FLOAT)
9015 {
9016 *f = argvars[0].vval.v_float;
9017 return OK;
9018 }
9019 if (argvars[0].v_type == VAR_NUMBER)
9020 {
9021 *f = (float_T)argvars[0].vval.v_number;
9022 return OK;
9023 }
9024 EMSG(_("E808: Number or Float required"));
9025 return FAIL;
9026}
9027
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009028/*
9029 * "abs(expr)" function
9030 */
9031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009032f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009033{
9034 if (argvars[0].v_type == VAR_FLOAT)
9035 {
9036 rettv->v_type = VAR_FLOAT;
9037 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9038 }
9039 else
9040 {
9041 varnumber_T n;
9042 int error = FALSE;
9043
9044 n = get_tv_number_chk(&argvars[0], &error);
9045 if (error)
9046 rettv->vval.v_number = -1;
9047 else if (n > 0)
9048 rettv->vval.v_number = n;
9049 else
9050 rettv->vval.v_number = -n;
9051 }
9052}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009053
9054/*
9055 * "acos()" function
9056 */
9057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009058f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009059{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009060 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009061
9062 rettv->v_type = VAR_FLOAT;
9063 if (get_float_arg(argvars, &f) == OK)
9064 rettv->vval.v_float = acos(f);
9065 else
9066 rettv->vval.v_float = 0.0;
9067}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009068#endif
9069
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009071 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 */
9073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009074f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075{
Bram Moolenaar33570922005-01-25 22:26:29 +00009076 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009079 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009081 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009082 && !tv_check_lock(l->lv_lock,
9083 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009084 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009086 }
9087 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009088 EMSG(_(e_listreq));
9089}
9090
9091/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009092 * "alloc_fail(id, countdown, repeat)" function
9093 */
9094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009095f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009096{
9097 if (argvars[0].v_type != VAR_NUMBER
9098 || argvars[0].vval.v_number <= 0
9099 || argvars[1].v_type != VAR_NUMBER
9100 || argvars[1].vval.v_number < 0
9101 || argvars[2].v_type != VAR_NUMBER)
9102 EMSG(_(e_invarg));
9103 else
9104 {
9105 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009106 if (alloc_fail_id >= aid_last)
9107 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009108 alloc_fail_countdown = argvars[1].vval.v_number;
9109 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009110 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009111 }
9112}
9113
9114/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009115 * "and(expr, expr)" function
9116 */
9117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009118f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009119{
9120 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9121 & get_tv_number_chk(&argvars[1], NULL);
9122}
9123
9124/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009125 * "append(lnum, string/list)" function
9126 */
9127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009128f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009129{
9130 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009131 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 list_T *l = NULL;
9133 listitem_T *li = NULL;
9134 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009135 long added = 0;
9136
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009137 /* When coming here from Insert mode, sync undo, so that this can be
9138 * undone separately from what was previously inserted. */
9139 if (u_sync_once == 2)
9140 {
9141 u_sync_once = 1; /* notify that u_sync() was called */
9142 u_sync(TRUE);
9143 }
9144
Bram Moolenaar0d660222005-01-07 21:51:51 +00009145 lnum = get_tv_lnum(argvars);
9146 if (lnum >= 0
9147 && lnum <= curbuf->b_ml.ml_line_count
9148 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009149 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009150 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009151 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009152 l = argvars[1].vval.v_list;
9153 if (l == NULL)
9154 return;
9155 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009156 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009157 for (;;)
9158 {
9159 if (l == NULL)
9160 tv = &argvars[1]; /* append a string */
9161 else if (li == NULL)
9162 break; /* end of list */
9163 else
9164 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009165 line = get_tv_string_chk(tv);
9166 if (line == NULL) /* type error */
9167 {
9168 rettv->vval.v_number = 1; /* Failed */
9169 break;
9170 }
9171 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009172 ++added;
9173 if (l == NULL)
9174 break;
9175 li = li->li_next;
9176 }
9177
9178 appended_lines_mark(lnum, added);
9179 if (curwin->w_cursor.lnum > lnum)
9180 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 else
9183 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184}
9185
9186/*
9187 * "argc()" function
9188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009190f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193}
9194
9195/*
9196 * "argidx()" function
9197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009199f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202}
9203
9204/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009205 * "arglistid()" function
9206 */
9207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009208f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009209{
9210 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009211
9212 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009213 wp = find_tabwin(&argvars[0], &argvars[1]);
9214 if (wp != NULL)
9215 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009216}
9217
9218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 * "argv(nr)" function
9220 */
9221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009222f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 int idx;
9225
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009226 if (argvars[0].v_type != VAR_UNKNOWN)
9227 {
9228 idx = get_tv_number_chk(&argvars[0], NULL);
9229 if (idx >= 0 && idx < ARGCOUNT)
9230 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9231 else
9232 rettv->vval.v_string = NULL;
9233 rettv->v_type = VAR_STRING;
9234 }
9235 else if (rettv_list_alloc(rettv) == OK)
9236 for (idx = 0; idx < ARGCOUNT; ++idx)
9237 list_append_string(rettv->vval.v_list,
9238 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239}
9240
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009241static void prepare_assert_error(garray_T*gap);
9242static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9243static void assert_error(garray_T *gap);
9244static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009245
9246/*
9247 * Prepare "gap" for an assert error and add the sourcing position.
9248 */
9249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009250prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009251{
9252 char buf[NUMBUFLEN];
9253
9254 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009255 if (sourcing_name != NULL)
9256 {
9257 ga_concat(gap, sourcing_name);
9258 if (sourcing_lnum > 0)
9259 ga_concat(gap, (char_u *)" ");
9260 }
9261 if (sourcing_lnum > 0)
9262 {
9263 sprintf(buf, "line %ld", (long)sourcing_lnum);
9264 ga_concat(gap, (char_u *)buf);
9265 }
9266 if (sourcing_name != NULL || sourcing_lnum > 0)
9267 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009268}
9269
9270/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009271 * Append "str" to "gap", escaping unprintable characters.
9272 * Changes NL to \n, CR to \r, etc.
9273 */
9274 static void
9275ga_concat_esc(garray_T *gap, char_u *str)
9276{
9277 char_u *p;
9278 char_u buf[NUMBUFLEN];
9279
9280 for (p = str; *p != NUL; ++p)
9281 switch (*p)
9282 {
9283 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9284 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9285 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9286 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9287 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9288 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9289 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9290 default:
9291 if (*p < ' ')
9292 {
9293 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9294 ga_concat(gap, buf);
9295 }
9296 else
9297 ga_append(gap, *p);
9298 break;
9299 }
9300}
9301
9302/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009303 * Fill "gap" with information about an assert error.
9304 */
9305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009306fill_assert_error(
9307 garray_T *gap,
9308 typval_T *opt_msg_tv,
9309 char_u *exp_str,
9310 typval_T *exp_tv,
9311 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009312{
9313 char_u numbuf[NUMBUFLEN];
9314 char_u *tofree;
9315
9316 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9317 {
9318 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9319 vim_free(tofree);
9320 }
9321 else
9322 {
9323 ga_concat(gap, (char_u *)"Expected ");
9324 if (exp_str == NULL)
9325 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009326 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009327 vim_free(tofree);
9328 }
9329 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009330 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009331 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009332 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009333 vim_free(tofree);
9334 }
9335}
Bram Moolenaar43345542015-11-29 17:35:35 +01009336
9337/*
9338 * Add an assert error to v:errors.
9339 */
9340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009341assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009342{
9343 struct vimvar *vp = &vimvars[VV_ERRORS];
9344
9345 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9346 /* Make sure v:errors is a list. */
9347 set_vim_var_list(VV_ERRORS, list_alloc());
9348 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9349}
9350
Bram Moolenaar43345542015-11-29 17:35:35 +01009351/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009352 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009353 */
9354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009355f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009356{
9357 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009358
9359 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9360 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009361 prepare_assert_error(&ga);
9362 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9363 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009364 ga_clear(&ga);
9365 }
9366}
9367
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009368/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009369 * "assert_exception(string[, msg])" function
9370 */
9371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009372f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009373{
9374 garray_T ga;
9375 char *error;
9376
9377 error = (char *)get_tv_string_chk(&argvars[0]);
9378 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9379 {
9380 prepare_assert_error(&ga);
9381 ga_concat(&ga, (char_u *)"v:exception is not set");
9382 assert_error(&ga);
9383 ga_clear(&ga);
9384 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009385 else if (error != NULL
9386 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009387 {
9388 prepare_assert_error(&ga);
9389 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9390 &vimvars[VV_EXCEPTION].vv_tv);
9391 assert_error(&ga);
9392 ga_clear(&ga);
9393 }
9394}
9395
9396/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009397 * "assert_fails(cmd [, error])" function
9398 */
9399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009400f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009401{
9402 char_u *cmd = get_tv_string_chk(&argvars[0]);
9403 garray_T ga;
9404
9405 called_emsg = FALSE;
9406 suppress_errthrow = TRUE;
9407 emsg_silent = TRUE;
9408 do_cmdline_cmd(cmd);
9409 if (!called_emsg)
9410 {
9411 prepare_assert_error(&ga);
9412 ga_concat(&ga, (char_u *)"command did not fail: ");
9413 ga_concat(&ga, cmd);
9414 assert_error(&ga);
9415 ga_clear(&ga);
9416 }
9417 else if (argvars[1].v_type != VAR_UNKNOWN)
9418 {
9419 char_u buf[NUMBUFLEN];
9420 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9421
9422 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9423 {
9424 prepare_assert_error(&ga);
9425 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9426 &vimvars[VV_ERRMSG].vv_tv);
9427 assert_error(&ga);
9428 ga_clear(&ga);
9429 }
9430 }
9431
9432 called_emsg = FALSE;
9433 suppress_errthrow = FALSE;
9434 emsg_silent = FALSE;
9435 emsg_on_display = FALSE;
9436 set_vim_var_string(VV_ERRMSG, NULL, 0);
9437}
9438
9439/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009440 * Common for assert_true() and assert_false().
9441 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009443assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009444{
9445 int error = FALSE;
9446 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009447
Bram Moolenaar37127922016-02-06 20:29:28 +01009448 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009449 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009450 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009451 if (argvars[0].v_type != VAR_NUMBER
9452 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9453 || error)
9454 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009455 prepare_assert_error(&ga);
9456 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009457 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009458 NULL, &argvars[0]);
9459 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009460 ga_clear(&ga);
9461 }
9462}
9463
9464/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009465 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009466 */
9467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009468f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009469{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009470 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009471}
9472
9473/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009474 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009475 */
9476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009477f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009478{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009479 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009480}
9481
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009482#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009483/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009484 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009485 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009487f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009488{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009489 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009490
9491 rettv->v_type = VAR_FLOAT;
9492 if (get_float_arg(argvars, &f) == OK)
9493 rettv->vval.v_float = asin(f);
9494 else
9495 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009496}
9497
9498/*
9499 * "atan()" function
9500 */
9501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009502f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009503{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009504 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009505
9506 rettv->v_type = VAR_FLOAT;
9507 if (get_float_arg(argvars, &f) == OK)
9508 rettv->vval.v_float = atan(f);
9509 else
9510 rettv->vval.v_float = 0.0;
9511}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009512
9513/*
9514 * "atan2()" function
9515 */
9516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009517f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009518{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009519 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009520
9521 rettv->v_type = VAR_FLOAT;
9522 if (get_float_arg(argvars, &fx) == OK
9523 && get_float_arg(&argvars[1], &fy) == OK)
9524 rettv->vval.v_float = atan2(fx, fy);
9525 else
9526 rettv->vval.v_float = 0.0;
9527}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009528#endif
9529
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530/*
9531 * "browse(save, title, initdir, default)" function
9532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009534f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535{
9536#ifdef FEAT_BROWSE
9537 int save;
9538 char_u *title;
9539 char_u *initdir;
9540 char_u *defname;
9541 char_u buf[NUMBUFLEN];
9542 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009543 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009545 save = get_tv_number_chk(&argvars[0], &error);
9546 title = get_tv_string_chk(&argvars[1]);
9547 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9548 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550 if (error || title == NULL || initdir == NULL || defname == NULL)
9551 rettv->vval.v_string = NULL;
9552 else
9553 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009554 do_browse(save ? BROWSE_SAVE : 0,
9555 title, defname, NULL, initdir, NULL, curbuf);
9556#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009558#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009560}
9561
9562/*
9563 * "browsedir(title, initdir)" function
9564 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009566f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009567{
9568#ifdef FEAT_BROWSE
9569 char_u *title;
9570 char_u *initdir;
9571 char_u buf[NUMBUFLEN];
9572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009573 title = get_tv_string_chk(&argvars[0]);
9574 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 if (title == NULL || initdir == NULL)
9577 rettv->vval.v_string = NULL;
9578 else
9579 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009580 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585}
9586
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009587static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589/*
9590 * Find a buffer by number or exact name.
9591 */
9592 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009593find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594{
9595 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009597 if (avar->v_type == VAR_NUMBER)
9598 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009599 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009601 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009602 if (buf == NULL)
9603 {
9604 /* No full path name match, try a match with a URL or a "nofile"
9605 * buffer, these don't use the full path. */
9606 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9607 if (buf->b_fname != NULL
9608 && (path_with_url(buf->b_fname)
9609#ifdef FEAT_QUICKFIX
9610 || bt_nofile(buf)
9611#endif
9612 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009614 break;
9615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 }
9617 return buf;
9618}
9619
9620/*
9621 * "bufexists(expr)" function
9622 */
9623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009624f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627}
9628
9629/*
9630 * "buflisted(expr)" function
9631 */
9632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009633f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
9635 buf_T *buf;
9636
9637 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639}
9640
9641/*
9642 * "bufloaded(expr)" function
9643 */
9644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009645f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646{
9647 buf_T *buf;
9648
9649 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651}
9652
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009653static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009654
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655/*
9656 * Get buffer by number or pattern.
9657 */
9658 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009659get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 int save_magic;
9663 char_u *save_cpo;
9664 buf_T *buf;
9665
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666 if (tv->v_type == VAR_NUMBER)
9667 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009668 if (tv->v_type != VAR_STRING)
9669 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 if (name == NULL || *name == NUL)
9671 return curbuf;
9672 if (name[0] == '$' && name[1] == NUL)
9673 return lastbuf;
9674
9675 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9676 save_magic = p_magic;
9677 p_magic = TRUE;
9678 save_cpo = p_cpo;
9679 p_cpo = (char_u *)"";
9680
9681 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009682 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683
9684 p_magic = save_magic;
9685 p_cpo = save_cpo;
9686
9687 /* If not found, try expanding the name, like done for bufexists(). */
9688 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690
9691 return buf;
9692}
9693
9694/*
9695 * "bufname(expr)" function
9696 */
9697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009698f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
9700 buf_T *buf;
9701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009702 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009704 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 --emsg_off;
9711}
9712
9713/*
9714 * "bufnr(expr)" function
9715 */
9716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009717f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
9719 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009720 int error = FALSE;
9721 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009723 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009725 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009726 --emsg_off;
9727
9728 /* If the buffer isn't found and the second argument is not zero create a
9729 * new buffer. */
9730 if (buf == NULL
9731 && argvars[1].v_type != VAR_UNKNOWN
9732 && get_tv_number_chk(&argvars[1], &error) != 0
9733 && !error
9734 && (name = get_tv_string_chk(&argvars[0])) != NULL
9735 && !error)
9736 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9737
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742}
9743
9744/*
9745 * "bufwinnr(nr)" function
9746 */
9747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009748f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
9750#ifdef FEAT_WINDOWS
9751 win_T *wp;
9752 int winnr = 0;
9753#endif
9754 buf_T *buf;
9755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009756 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009758 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759#ifdef FEAT_WINDOWS
9760 for (wp = firstwin; wp; wp = wp->w_next)
9761 {
9762 ++winnr;
9763 if (wp->w_buffer == buf)
9764 break;
9765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769#endif
9770 --emsg_off;
9771}
9772
9773/*
9774 * "byte2line(byte)" function
9775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009777f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781#else
9782 long boff = 0;
9783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 (linenr_T)0, &boff);
9790#endif
9791}
9792
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009794byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009795{
9796#ifdef FEAT_MBYTE
9797 char_u *t;
9798#endif
9799 char_u *str;
9800 long idx;
9801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009802 str = get_tv_string_chk(&argvars[0]);
9803 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009805 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009806 return;
9807
9808#ifdef FEAT_MBYTE
9809 t = str;
9810 for ( ; idx > 0; idx--)
9811 {
9812 if (*t == NUL) /* EOL reached */
9813 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009814 if (enc_utf8 && comp)
9815 t += utf_ptr2len(t);
9816 else
9817 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009818 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009819 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009820#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009821 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009823#endif
9824}
9825
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009826/*
9827 * "byteidx()" function
9828 */
9829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009830f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009831{
9832 byteidx(argvars, rettv, FALSE);
9833}
9834
9835/*
9836 * "byteidxcomp()" function
9837 */
9838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009839f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009840{
9841 byteidx(argvars, rettv, TRUE);
9842}
9843
Bram Moolenaardb913952012-06-29 12:54:53 +02009844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009845func_call(
9846 char_u *name,
9847 typval_T *args,
9848 dict_T *selfdict,
9849 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009850{
9851 listitem_T *item;
9852 typval_T argv[MAX_FUNC_ARGS + 1];
9853 int argc = 0;
9854 int dummy;
9855 int r = 0;
9856
9857 for (item = args->vval.v_list->lv_first; item != NULL;
9858 item = item->li_next)
9859 {
9860 if (argc == MAX_FUNC_ARGS)
9861 {
9862 EMSG(_("E699: Too many arguments"));
9863 break;
9864 }
9865 /* Make a copy of each argument. This is needed to be able to set
9866 * v_lock to VAR_FIXED in the copy without changing the original list.
9867 */
9868 copy_tv(&item->li_tv, &argv[argc++]);
9869 }
9870
9871 if (item == NULL)
9872 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9873 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9874 &dummy, TRUE, selfdict);
9875
9876 /* Free the arguments. */
9877 while (argc > 0)
9878 clear_tv(&argv[--argc]);
9879
9880 return r;
9881}
9882
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009883/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009884 * "call(func, arglist)" function
9885 */
9886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009887f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009888{
9889 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009891
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009892 if (argvars[1].v_type != VAR_LIST)
9893 {
9894 EMSG(_(e_listreq));
9895 return;
9896 }
9897 if (argvars[1].vval.v_list == NULL)
9898 return;
9899
9900 if (argvars[0].v_type == VAR_FUNC)
9901 func = argvars[0].vval.v_string;
9902 else
9903 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 if (*func == NUL)
9905 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009906
Bram Moolenaare9a41262005-01-15 22:18:47 +00009907 if (argvars[2].v_type != VAR_UNKNOWN)
9908 {
9909 if (argvars[2].v_type != VAR_DICT)
9910 {
9911 EMSG(_(e_dictreq));
9912 return;
9913 }
9914 selfdict = argvars[2].vval.v_dict;
9915 }
9916
Bram Moolenaardb913952012-06-29 12:54:53 +02009917 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009918}
9919
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009920#ifdef FEAT_FLOAT
9921/*
9922 * "ceil({float})" function
9923 */
9924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009925f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009926{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009927 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009928
9929 rettv->v_type = VAR_FLOAT;
9930 if (get_float_arg(argvars, &f) == OK)
9931 rettv->vval.v_float = ceil(f);
9932 else
9933 rettv->vval.v_float = 0.0;
9934}
9935#endif
9936
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009937#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009938/*
9939 * Get a callback from "arg". It can be a Funcref or a function name.
9940 * When "arg" is zero return an empty string.
9941 * Return NULL for an invalid argument.
9942 */
9943 static char_u *
9944get_callback(typval_T *arg)
9945{
9946 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9947 return arg->vval.v_string;
9948 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9949 return (char_u *)"";
9950 EMSG(_("E999: Invalid callback argument"));
9951 return NULL;
9952}
9953
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009954 static int
9955handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9956{
9957 char_u *val = get_tv_string(item);
9958
9959 opt->jo_set |= jo;
9960 if (STRCMP(val, "nl") == 0)
9961 *modep = MODE_NL;
9962 else if (STRCMP(val, "raw") == 0)
9963 *modep = MODE_RAW;
9964 else if (STRCMP(val, "js") == 0)
9965 *modep = MODE_JS;
9966 else if (STRCMP(val, "json") == 0)
9967 *modep = MODE_JSON;
9968 else
9969 {
9970 EMSG2(_(e_invarg2), val);
9971 return FAIL;
9972 }
9973 return OK;
9974}
9975
9976 static void
9977clear_job_options(jobopt_T *opt)
9978{
9979 vim_memset(opt, 0, sizeof(jobopt_T));
9980}
9981
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009982/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009983 * Get the option entries from the dict in "tv", parse them and put the result
9984 * in "opt".
9985 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009986 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009987 */
9988 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009989get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009990{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009991 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01009992 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009993 dict_T *dict;
9994 int todo;
9995 hashitem_T *hi;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009996
Bram Moolenaar8600ace2016-02-19 23:31:40 +01009997 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009998 if (tv->v_type == VAR_UNKNOWN)
9999 return OK;
10000 if (tv->v_type != VAR_DICT)
10001 {
10002 EMSG(_(e_invarg));
10003 return FAIL;
10004 }
10005 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010006 if (dict == NULL)
10007 return OK;
10008
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010009 todo = (int)dict->dv_hashtab.ht_used;
10010 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10011 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010012 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010013 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010014
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010015 if (STRCMP(hi->hi_key, "mode") == 0)
10016 {
10017 if (!(supported & JO_MODE))
10018 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010019 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010020 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010021 }
10022 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10023 {
10024 if (!(supported & JO_IN_MODE))
10025 break;
10026 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10027 == FAIL)
10028 return FAIL;
10029 }
10030 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10031 {
10032 if (!(supported & JO_OUT_MODE))
10033 break;
10034 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10035 == FAIL)
10036 return FAIL;
10037 }
10038 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10039 {
10040 if (!(supported & JO_ERR_MODE))
10041 break;
10042 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10043 == FAIL)
10044 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010045 }
10046 else if (STRCMP(hi->hi_key, "callback") == 0)
10047 {
10048 if (!(supported & JO_CALLBACK))
10049 break;
10050 opt->jo_set |= JO_CALLBACK;
10051 opt->jo_callback = get_callback(item);
10052 if (opt->jo_callback == NULL)
10053 {
10054 EMSG2(_(e_invarg2), "callback");
10055 return FAIL;
10056 }
10057 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010058 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10059 {
10060 if (!(supported & JO_OUT_CALLBACK))
10061 break;
10062 opt->jo_set |= JO_OUT_CALLBACK;
10063 opt->jo_out_cb = get_callback(item);
10064 if (opt->jo_out_cb == NULL)
10065 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010066 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010067 return FAIL;
10068 }
10069 }
10070 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10071 {
10072 if (!(supported & JO_ERR_CALLBACK))
10073 break;
10074 opt->jo_set |= JO_ERR_CALLBACK;
10075 opt->jo_err_cb = get_callback(item);
10076 if (opt->jo_err_cb == NULL)
10077 {
10078 EMSG2(_(e_invarg2), "err-cb");
10079 return FAIL;
10080 }
10081 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010082 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10083 {
10084 if (!(supported & JO_CLOSE_CALLBACK))
10085 break;
10086 opt->jo_set |= JO_CLOSE_CALLBACK;
10087 opt->jo_close_cb = get_callback(item);
10088 if (opt->jo_close_cb == NULL)
10089 {
10090 EMSG2(_(e_invarg2), "close-cb");
10091 return FAIL;
10092 }
10093 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010094 else if (STRCMP(hi->hi_key, "waittime") == 0)
10095 {
10096 if (!(supported & JO_WAITTIME))
10097 break;
10098 opt->jo_set |= JO_WAITTIME;
10099 opt->jo_waittime = get_tv_number(item);
10100 }
10101 else if (STRCMP(hi->hi_key, "timeout") == 0)
10102 {
10103 if (!(supported & JO_TIMEOUT))
10104 break;
10105 opt->jo_set |= JO_TIMEOUT;
10106 opt->jo_timeout = get_tv_number(item);
10107 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010108 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10109 {
10110 if (!(supported & JO_OUT_TIMEOUT))
10111 break;
10112 opt->jo_set |= JO_OUT_TIMEOUT;
10113 opt->jo_out_timeout = get_tv_number(item);
10114 }
10115 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10116 {
10117 if (!(supported & JO_ERR_TIMEOUT))
10118 break;
10119 opt->jo_set |= JO_ERR_TIMEOUT;
10120 opt->jo_err_timeout = get_tv_number(item);
10121 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010122 else if (STRCMP(hi->hi_key, "part") == 0)
10123 {
10124 if (!(supported & JO_PART))
10125 break;
10126 opt->jo_set |= JO_PART;
10127 val = get_tv_string(item);
10128 if (STRCMP(val, "err") == 0)
10129 opt->jo_part = PART_ERR;
10130 else
10131 {
10132 EMSG2(_(e_invarg2), val);
10133 return FAIL;
10134 }
10135 }
10136 else if (STRCMP(hi->hi_key, "id") == 0)
10137 {
10138 if (!(supported & JO_ID))
10139 break;
10140 opt->jo_set |= JO_ID;
10141 opt->jo_id = get_tv_number(item);
10142 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010143 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10144 {
10145 if (!(supported & JO_STOPONEXIT))
10146 break;
10147 opt->jo_set |= JO_STOPONEXIT;
10148 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10149 opt->jo_soe_buf);
10150 if (opt->jo_stoponexit == NULL)
10151 {
10152 EMSG2(_(e_invarg2), "stoponexit");
10153 return FAIL;
10154 }
10155 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010156 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10157 {
10158 if (!(supported & JO_EXIT_CB))
10159 break;
10160 opt->jo_set |= JO_EXIT_CB;
10161 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010162 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010163 {
10164 EMSG2(_(e_invarg2), "exit-cb");
10165 return FAIL;
10166 }
10167 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010168 else
10169 break;
10170 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010171 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010172 if (todo > 0)
10173 {
10174 EMSG2(_(e_invarg2), hi->hi_key);
10175 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010176 }
10177
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010178 return OK;
10179}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010180#endif
10181
10182#ifdef FEAT_CHANNEL
10183/*
10184 * Get the channel from the argument.
10185 * Returns NULL if the handle is invalid.
10186 */
10187 static channel_T *
10188get_channel_arg(typval_T *tv)
10189{
10190 channel_T *channel;
10191
10192 if (tv->v_type != VAR_CHANNEL)
10193 {
10194 EMSG2(_(e_invarg2), get_tv_string(tv));
10195 return NULL;
10196 }
10197 channel = tv->vval.v_channel;
10198
10199 if (channel == NULL || !channel_is_open(channel))
10200 {
10201 EMSG(_("E906: not an open channel"));
10202 return NULL;
10203 }
10204 return channel;
10205}
10206
10207/*
10208 * "ch_close()" function
10209 */
10210 static void
10211f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10212{
10213 channel_T *channel = get_channel_arg(&argvars[0]);
10214
10215 if (channel != NULL)
Bram Moolenaar8b374212016-02-24 20:43:06 +010010216 channel_close(channel, FALSE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010217}
10218
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010219# ifdef FEAT_JOB
10220/*
10221 * "ch_getjob()" function
10222 */
10223 static void
10224f_ch_getjob(typval_T *argvars, typval_T *rettv)
10225{
10226 channel_T *channel = get_channel_arg(&argvars[0]);
10227
10228 if (channel != NULL)
10229 {
10230 rettv->v_type = VAR_JOB;
10231 rettv->vval.v_job = channel->ch_job;
10232 if (channel->ch_job != NULL)
10233 ++channel->ch_job->jv_refcount;
10234 }
10235}
10236# endif
10237
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010238/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010239 * "ch_log()" function
10240 */
10241 static void
10242f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10243{
10244 char_u *msg = get_tv_string(&argvars[0]);
10245 channel_T *channel = NULL;
10246
10247 if (argvars[1].v_type != VAR_UNKNOWN)
10248 channel = get_channel_arg(&argvars[1]);
10249
10250 ch_log(channel, (char *)msg);
10251}
10252
10253/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010254 * "ch_logfile()" function
10255 */
10256 static void
10257f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10258{
10259 char_u *fname;
10260 char_u *opt = (char_u *)"";
10261 char_u buf[NUMBUFLEN];
10262 FILE *file = NULL;
10263
10264 fname = get_tv_string(&argvars[0]);
10265 if (argvars[1].v_type == VAR_STRING)
10266 opt = get_tv_string_buf(&argvars[1], buf);
10267 if (*fname != NUL)
10268 {
10269 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10270 if (file == NULL)
10271 {
10272 EMSG2(_(e_notopen), fname);
10273 return;
10274 }
10275 }
10276 ch_logfile(file);
10277}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010278
10279/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010280 * "ch_open()" function
10281 */
10282 static void
10283f_ch_open(typval_T *argvars, typval_T *rettv)
10284{
10285 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010286 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010287 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010288 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010289 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010290 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010291
10292 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010293 rettv->v_type = VAR_CHANNEL;
10294 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010295
10296 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010297 if (argvars[1].v_type != VAR_UNKNOWN
10298 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010299 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010300 EMSG(_(e_invarg));
10301 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010302 }
10303
10304 /* parse address */
10305 p = vim_strchr(address, ':');
10306 if (p == NULL)
10307 {
10308 EMSG2(_(e_invarg2), address);
10309 return;
10310 }
10311 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010312 port = strtol((char *)p, &rest, 10);
10313 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010314 {
10315 p[-1] = ':';
10316 EMSG2(_(e_invarg2), address);
10317 return;
10318 }
10319
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010320 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010321 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010322 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010323 opt.jo_timeout = 2000;
10324 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010325 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010326 return;
10327 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010328 {
10329 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010330 return;
10331 }
10332
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010333 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010334 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010335 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010336 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010337 opt.jo_set = JO_ALL;
10338 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010339 }
10340}
10341
10342/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010343 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010344 */
10345 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010346common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010347{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010348 channel_T *channel;
10349 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010350 jobopt_T opt;
10351 int mode;
10352 int timeout;
10353 int id = -1;
10354 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010355
10356 /* return an empty string by default */
10357 rettv->v_type = VAR_STRING;
10358 rettv->vval.v_string = NULL;
10359
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010360 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010361 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10362 == FAIL)
10363 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010364
Bram Moolenaar77073442016-02-13 23:23:53 +010010365 channel = get_channel_arg(&argvars[0]);
10366 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010367 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010368 if (opt.jo_set & JO_PART)
10369 part = opt.jo_part;
10370 else
10371 part = channel_part_read(channel);
10372 mode = channel_get_mode(channel, part);
10373 timeout = channel_get_timeout(channel, part);
10374 if (opt.jo_set & JO_TIMEOUT)
10375 timeout = opt.jo_timeout;
10376
10377 if (raw || mode == MODE_RAW || mode == MODE_NL)
10378 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10379 else
10380 {
10381 if (opt.jo_set & JO_ID)
10382 id = opt.jo_id;
10383 channel_read_json_block(channel, part, timeout, id, &listtv);
10384 if (listtv != NULL)
10385 *rettv = *listtv;
10386 else
10387 {
10388 rettv->v_type = VAR_SPECIAL;
10389 rettv->vval.v_number = VVAL_NONE;
10390 }
10391 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010392 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010393}
10394
10395/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010396 * "ch_read()" function
10397 */
10398 static void
10399f_ch_read(typval_T *argvars, typval_T *rettv)
10400{
10401 common_channel_read(argvars, rettv, FALSE);
10402}
10403
10404/*
10405 * "ch_readraw()" function
10406 */
10407 static void
10408f_ch_readraw(typval_T *argvars, typval_T *rettv)
10409{
10410 common_channel_read(argvars, rettv, TRUE);
10411}
10412
10413/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010414 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010415 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010416 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010417 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010418 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010419 static channel_T *
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010420send_common(typval_T *argvars, char_u *text, int id, char *fun, int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010421{
Bram Moolenaar77073442016-02-13 23:23:53 +010010422 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010423 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010424 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010425
Bram Moolenaar77073442016-02-13 23:23:53 +010010426 channel = get_channel_arg(&argvars[0]);
10427 if (channel == NULL)
10428 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010429 part_send = channel_part_send(channel);
10430 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010431
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010432 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010433 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10434 return NULL;
10435
Bram Moolenaara07fec92016-02-05 21:04:08 +010010436 /* Set the callback. An empty callback means no callback and not reading
10437 * the response. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010438 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010439 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010440
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010441 if (channel_send(channel, part_send, text, fun) == OK
10442 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010443 return channel;
10444 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010445}
10446
10447/*
10448 * "ch_sendexpr()" function
10449 */
10450 static void
10451f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10452{
10453 char_u *text;
10454 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010455 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010456 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010457 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010458 int part_send;
10459 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010460 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010461
10462 /* return an empty string by default */
10463 rettv->v_type = VAR_STRING;
10464 rettv->vval.v_string = NULL;
10465
Bram Moolenaar77073442016-02-13 23:23:53 +010010466 channel = get_channel_arg(&argvars[0]);
10467 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010468 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010469 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010470
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010471 ch_mode = channel_get_mode(channel, part_send);
10472 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010473 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010474 EMSG(_("E912: cannot use ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010475 return;
10476 }
10477
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010478 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010479 text = json_encode_nr_expr(id, &argvars[1],
10480 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010481 if (text == NULL)
10482 return;
10483
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010484 channel = send_common(argvars, text, id, "sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010485 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010486 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010487 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010488 /* TODO: timeout from options */
10489 timeout = channel_get_timeout(channel, part_read);
10490 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10491 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010492 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010493 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010494
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010495 /* Move the item from the list and then change the type to
10496 * avoid the value being freed. */
10497 *rettv = list->lv_last->li_tv;
10498 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010499 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010500 }
10501 }
10502}
10503
10504/*
10505 * "ch_sendraw()" function
10506 */
10507 static void
10508f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10509{
10510 char_u buf[NUMBUFLEN];
10511 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010512 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010513 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010514 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010515
10516 /* return an empty string by default */
10517 rettv->v_type = VAR_STRING;
10518 rettv->vval.v_string = NULL;
10519
10520 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010521 channel = send_common(argvars, text, 0, "sendraw", &part_read);
Bram Moolenaar77073442016-02-13 23:23:53 +010010522 if (channel != NULL)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010523 {
10524 /* TODO: timeout from options */
10525 timeout = channel_get_timeout(channel, part_read);
10526 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10527 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010528}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010529
10530/*
10531 * "ch_setoptions()" function
10532 */
10533 static void
10534f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10535{
10536 channel_T *channel;
10537 jobopt_T opt;
10538
10539 channel = get_channel_arg(&argvars[0]);
10540 if (channel == NULL)
10541 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010542 clear_job_options(&opt);
10543 if (get_job_options(&argvars[1], &opt,
10544 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010545 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010546 channel_set_options(channel, &opt);
10547}
10548
10549/*
10550 * "ch_status()" function
10551 */
10552 static void
10553f_ch_status(typval_T *argvars, typval_T *rettv)
10554{
10555 /* return an empty string by default */
10556 rettv->v_type = VAR_STRING;
10557
10558 if (argvars[0].v_type != VAR_CHANNEL)
10559 {
10560 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10561 rettv->vval.v_string = NULL;
10562 }
10563 else
10564 rettv->vval.v_string = vim_strsave(
10565 (char_u *)channel_status(argvars[0].vval.v_channel));
10566}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010567#endif
10568
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010569/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010570 * "changenr()" function
10571 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010573f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010574{
10575 rettv->vval.v_number = curbuf->b_u_seq_cur;
10576}
10577
10578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 * "char2nr(string)" function
10580 */
10581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010582f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583{
10584#ifdef FEAT_MBYTE
10585 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010586 {
10587 int utf8 = 0;
10588
10589 if (argvars[1].v_type != VAR_UNKNOWN)
10590 utf8 = get_tv_number_chk(&argvars[1], NULL);
10591
10592 if (utf8)
10593 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10594 else
10595 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 else
10598#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600}
10601
10602/*
10603 * "cindent(lnum)" function
10604 */
10605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010606f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607{
10608#ifdef FEAT_CINDENT
10609 pos_T pos;
10610 linenr_T lnum;
10611
10612 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010613 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10615 {
10616 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010617 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 curwin->w_cursor = pos;
10619 }
10620 else
10621#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623}
10624
10625/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010626 * "clearmatches()" function
10627 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010629f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010630{
10631#ifdef FEAT_SEARCH_EXTRA
10632 clear_matches(curwin);
10633#endif
10634}
10635
10636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 * "col(string)" function
10638 */
10639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010640f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641{
10642 colnr_T col = 0;
10643 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010644 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010646 fp = var2fpos(&argvars[0], FALSE, &fnum);
10647 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 {
10649 if (fp->col == MAXCOL)
10650 {
10651 /* '> can be MAXCOL, get the length of the line then */
10652 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010653 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 else
10655 col = MAXCOL;
10656 }
10657 else
10658 {
10659 col = fp->col + 1;
10660#ifdef FEAT_VIRTUALEDIT
10661 /* col(".") when the cursor is on the NUL at the end of the line
10662 * because of "coladd" can be seen as an extra column. */
10663 if (virtual_active() && fp == &curwin->w_cursor)
10664 {
10665 char_u *p = ml_get_cursor();
10666
10667 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10668 curwin->w_virtcol - curwin->w_cursor.coladd))
10669 {
10670# ifdef FEAT_MBYTE
10671 int l;
10672
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010673 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 col += l;
10675# else
10676 if (*p != NUL && p[1] == NUL)
10677 ++col;
10678# endif
10679 }
10680 }
10681#endif
10682 }
10683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685}
10686
Bram Moolenaar572cb562005-08-05 21:35:02 +000010687#if defined(FEAT_INS_EXPAND)
10688/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010689 * "complete()" function
10690 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010692f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010693{
10694 int startcol;
10695
10696 if ((State & INSERT) == 0)
10697 {
10698 EMSG(_("E785: complete() can only be used in Insert mode"));
10699 return;
10700 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010701
10702 /* Check for undo allowed here, because if something was already inserted
10703 * the line was already saved for undo and this check isn't done. */
10704 if (!undo_allowed())
10705 return;
10706
Bram Moolenaarade00832006-03-10 21:46:58 +000010707 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10708 {
10709 EMSG(_(e_invarg));
10710 return;
10711 }
10712
10713 startcol = get_tv_number_chk(&argvars[0], NULL);
10714 if (startcol <= 0)
10715 return;
10716
10717 set_completion(startcol - 1, argvars[1].vval.v_list);
10718}
10719
10720/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010721 * "complete_add()" function
10722 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010724f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010725{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010726 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010727}
10728
10729/*
10730 * "complete_check()" function
10731 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010733f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010734{
10735 int saved = RedrawingDisabled;
10736
10737 RedrawingDisabled = 0;
10738 ins_compl_check_keys(0);
10739 rettv->vval.v_number = compl_interrupted;
10740 RedrawingDisabled = saved;
10741}
10742#endif
10743
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744/*
10745 * "confirm(message, buttons[, default [, type]])" function
10746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010748f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749{
10750#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10751 char_u *message;
10752 char_u *buttons = NULL;
10753 char_u buf[NUMBUFLEN];
10754 char_u buf2[NUMBUFLEN];
10755 int def = 1;
10756 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010757 char_u *typestr;
10758 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010760 message = get_tv_string_chk(&argvars[0]);
10761 if (message == NULL)
10762 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010763 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010765 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10766 if (buttons == NULL)
10767 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010768 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010770 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010771 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10774 if (typestr == NULL)
10775 error = TRUE;
10776 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010778 switch (TOUPPER_ASC(*typestr))
10779 {
10780 case 'E': type = VIM_ERROR; break;
10781 case 'Q': type = VIM_QUESTION; break;
10782 case 'I': type = VIM_INFO; break;
10783 case 'W': type = VIM_WARNING; break;
10784 case 'G': type = VIM_GENERIC; break;
10785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 }
10787 }
10788 }
10789 }
10790
10791 if (buttons == NULL || *buttons == NUL)
10792 buttons = (char_u *)_("&Ok");
10793
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010794 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010796 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797#endif
10798}
10799
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010800/*
10801 * "copy()" function
10802 */
10803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010804f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010805{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010806 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010807}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010809#ifdef FEAT_FLOAT
10810/*
10811 * "cos()" function
10812 */
10813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010814f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010815{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010816 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010817
10818 rettv->v_type = VAR_FLOAT;
10819 if (get_float_arg(argvars, &f) == OK)
10820 rettv->vval.v_float = cos(f);
10821 else
10822 rettv->vval.v_float = 0.0;
10823}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010824
10825/*
10826 * "cosh()" function
10827 */
10828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010829f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010830{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010831 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010832
10833 rettv->v_type = VAR_FLOAT;
10834 if (get_float_arg(argvars, &f) == OK)
10835 rettv->vval.v_float = cosh(f);
10836 else
10837 rettv->vval.v_float = 0.0;
10838}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010839#endif
10840
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010842 * "count()" function
10843 */
10844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010845f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010846{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010847 long n = 0;
10848 int ic = FALSE;
10849
Bram Moolenaare9a41262005-01-15 22:18:47 +000010850 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010851 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010852 listitem_T *li;
10853 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010854 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010855
Bram Moolenaare9a41262005-01-15 22:18:47 +000010856 if ((l = argvars[0].vval.v_list) != NULL)
10857 {
10858 li = l->lv_first;
10859 if (argvars[2].v_type != VAR_UNKNOWN)
10860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010861 int error = FALSE;
10862
10863 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864 if (argvars[3].v_type != VAR_UNKNOWN)
10865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010866 idx = get_tv_number_chk(&argvars[3], &error);
10867 if (!error)
10868 {
10869 li = list_find(l, idx);
10870 if (li == NULL)
10871 EMSGN(_(e_listidx), idx);
10872 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010873 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 if (error)
10875 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010876 }
10877
10878 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010879 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010880 ++n;
10881 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010882 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010883 else if (argvars[0].v_type == VAR_DICT)
10884 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010885 int todo;
10886 dict_T *d;
10887 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010888
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010889 if ((d = argvars[0].vval.v_dict) != NULL)
10890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 int error = FALSE;
10892
Bram Moolenaare9a41262005-01-15 22:18:47 +000010893 if (argvars[2].v_type != VAR_UNKNOWN)
10894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010895 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010896 if (argvars[3].v_type != VAR_UNKNOWN)
10897 EMSG(_(e_invarg));
10898 }
10899
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010900 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010901 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010902 {
10903 if (!HASHITEM_EMPTY(hi))
10904 {
10905 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010906 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010907 ++n;
10908 }
10909 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010910 }
10911 }
10912 else
10913 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010914 rettv->vval.v_number = n;
10915}
10916
10917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10919 *
10920 * Checks the existence of a cscope connection.
10921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010923f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924{
10925#ifdef FEAT_CSCOPE
10926 int num = 0;
10927 char_u *dbpath = NULL;
10928 char_u *prepend = NULL;
10929 char_u buf[NUMBUFLEN];
10930
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010931 if (argvars[0].v_type != VAR_UNKNOWN
10932 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010934 num = (int)get_tv_number(&argvars[0]);
10935 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010936 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 }
10939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010940 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941#endif
10942}
10943
10944/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010945 * "cursor(lnum, col)" function, or
10946 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010948 * Moves the cursor to the specified line and column.
10949 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010952f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953{
10954 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010955#ifdef FEAT_VIRTUALEDIT
10956 long coladd = 0;
10957#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010958 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010960 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010961 if (argvars[1].v_type == VAR_UNKNOWN)
10962 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010963 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010964 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010965
Bram Moolenaar493c1782014-05-28 14:34:46 +020010966 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010967 {
10968 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010969 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010970 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010971 line = pos.lnum;
10972 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010973#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010974 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010975#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010976 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010977 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010978 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010979 set_curswant = FALSE;
10980 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010981 }
10982 else
10983 {
10984 line = get_tv_lnum(argvars);
10985 col = get_tv_number_chk(&argvars[1], NULL);
10986#ifdef FEAT_VIRTUALEDIT
10987 if (argvars[2].v_type != VAR_UNKNOWN)
10988 coladd = get_tv_number_chk(&argvars[2], NULL);
10989#endif
10990 }
10991 if (line < 0 || col < 0
10992#ifdef FEAT_VIRTUALEDIT
10993 || coladd < 0
10994#endif
10995 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010996 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 if (line > 0)
10998 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 if (col > 0)
11000 curwin->w_cursor.col = col - 1;
11001#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011002 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003#endif
11004
11005 /* Make sure the cursor is in a valid position. */
11006 check_cursor();
11007#ifdef FEAT_MBYTE
11008 /* Correct cursor for multi-byte character. */
11009 if (has_mbyte)
11010 mb_adjust_cursor();
11011#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011012
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011013 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011014 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015}
11016
11017/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011018 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 */
11020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011021f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011023 int noref = 0;
11024
11025 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011026 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011027 if (noref < 0 || noref > 1)
11028 EMSG(_(e_invarg));
11029 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011030 {
11031 current_copyID += COPYID_INC;
11032 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034}
11035
11036/*
11037 * "delete()" function
11038 */
11039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011040f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011042 char_u nbuf[NUMBUFLEN];
11043 char_u *name;
11044 char_u *flags;
11045
11046 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011048 return;
11049
11050 name = get_tv_string(&argvars[0]);
11051 if (name == NULL || *name == NUL)
11052 {
11053 EMSG(_(e_invarg));
11054 return;
11055 }
11056
11057 if (argvars[1].v_type != VAR_UNKNOWN)
11058 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011060 flags = (char_u *)"";
11061
11062 if (*flags == NUL)
11063 /* delete a file */
11064 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11065 else if (STRCMP(flags, "d") == 0)
11066 /* delete an empty directory */
11067 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11068 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011069 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011070 rettv->vval.v_number = delete_recursive(name);
11071 else
11072 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073}
11074
11075/*
11076 * "did_filetype()" function
11077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011079f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080{
11081#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011082 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083#endif
11084}
11085
11086/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011087 * "diff_filler()" function
11088 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011090f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011091{
11092#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011093 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011094#endif
11095}
11096
11097/*
11098 * "diff_hlID()" function
11099 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011101f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011102{
11103#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011104 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011105 static linenr_T prev_lnum = 0;
11106 static int changedtick = 0;
11107 static int fnum = 0;
11108 static int change_start = 0;
11109 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011110 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011111 int filler_lines;
11112 int col;
11113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011114 if (lnum < 0) /* ignore type error in {lnum} arg */
11115 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011116 if (lnum != prev_lnum
11117 || changedtick != curbuf->b_changedtick
11118 || fnum != curbuf->b_fnum)
11119 {
11120 /* New line, buffer, change: need to get the values. */
11121 filler_lines = diff_check(curwin, lnum);
11122 if (filler_lines < 0)
11123 {
11124 if (filler_lines == -1)
11125 {
11126 change_start = MAXCOL;
11127 change_end = -1;
11128 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11129 hlID = HLF_ADD; /* added line */
11130 else
11131 hlID = HLF_CHD; /* changed line */
11132 }
11133 else
11134 hlID = HLF_ADD; /* added line */
11135 }
11136 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011137 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011138 prev_lnum = lnum;
11139 changedtick = curbuf->b_changedtick;
11140 fnum = curbuf->b_fnum;
11141 }
11142
11143 if (hlID == HLF_CHD || hlID == HLF_TXD)
11144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011146 if (col >= change_start && col <= change_end)
11147 hlID = HLF_TXD; /* changed text */
11148 else
11149 hlID = HLF_CHD; /* changed line */
11150 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011151 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011152#endif
11153}
11154
11155/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011156 * "disable_char_avail_for_testing({expr})" function
11157 */
11158 static void
11159f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11160{
11161 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11162}
11163
11164/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011165 * "empty({expr})" function
11166 */
11167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011168f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011169{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011170 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011171
11172 switch (argvars[0].v_type)
11173 {
11174 case VAR_STRING:
11175 case VAR_FUNC:
11176 n = argvars[0].vval.v_string == NULL
11177 || *argvars[0].vval.v_string == NUL;
11178 break;
11179 case VAR_NUMBER:
11180 n = argvars[0].vval.v_number == 0;
11181 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011182 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011183#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011184 n = argvars[0].vval.v_float == 0.0;
11185 break;
11186#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011187 case VAR_LIST:
11188 n = argvars[0].vval.v_list == NULL
11189 || argvars[0].vval.v_list->lv_first == NULL;
11190 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011191 case VAR_DICT:
11192 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011193 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011194 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011195 case VAR_SPECIAL:
11196 n = argvars[0].vval.v_number != VVAL_TRUE;
11197 break;
11198
Bram Moolenaar835dc632016-02-07 14:27:38 +010011199 case VAR_JOB:
11200#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011201 n = argvars[0].vval.v_job == NULL
11202 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11203 break;
11204#endif
11205 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011206#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011207 n = argvars[0].vval.v_channel == NULL
11208 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011209 break;
11210#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011211 case VAR_UNKNOWN:
11212 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11213 n = TRUE;
11214 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011215 }
11216
11217 rettv->vval.v_number = n;
11218}
11219
11220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 * "escape({string}, {chars})" function
11222 */
11223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011224f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225{
11226 char_u buf[NUMBUFLEN];
11227
Bram Moolenaar758711c2005-02-02 23:11:38 +000011228 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11229 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011230 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231}
11232
11233/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011234 * "eval()" function
11235 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011237f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011238{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011239 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 s = get_tv_string_chk(&argvars[0]);
11242 if (s != NULL)
11243 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011244
Bram Moolenaar615b9972015-01-14 17:15:05 +010011245 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011246 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11247 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011248 if (p != NULL && !aborting())
11249 EMSG2(_(e_invexpr2), p);
11250 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011252 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011253 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011254 else if (*s != NUL)
11255 EMSG(_(e_trailing));
11256}
11257
11258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 * "eventhandler()" function
11260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011262f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265}
11266
11267/*
11268 * "executable()" function
11269 */
11270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011271f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011273 char_u *name = get_tv_string(&argvars[0]);
11274
11275 /* Check in $PATH and also check directly if there is a directory name. */
11276 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11277 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011278}
11279
11280/*
11281 * "exepath()" function
11282 */
11283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011284f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011285{
11286 char_u *p = NULL;
11287
Bram Moolenaarb5971142015-03-21 17:32:19 +010011288 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011289 rettv->v_type = VAR_STRING;
11290 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291}
11292
11293/*
11294 * "exists()" function
11295 */
11296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011297f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298{
11299 char_u *p;
11300 char_u *name;
11301 int n = FALSE;
11302 int len = 0;
11303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011304 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 if (*p == '$') /* environment variable */
11306 {
11307 /* first try "normal" environment variables (fast) */
11308 if (mch_getenv(p + 1) != NULL)
11309 n = TRUE;
11310 else
11311 {
11312 /* try expanding things like $VIM and ${HOME} */
11313 p = expand_env_save(p);
11314 if (p != NULL && *p != '$')
11315 n = TRUE;
11316 vim_free(p);
11317 }
11318 }
11319 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011322 if (*skipwhite(p) != NUL)
11323 n = FALSE; /* trailing garbage */
11324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 else if (*p == '*') /* internal or user defined function */
11326 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011327 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 }
11329 else if (*p == ':')
11330 {
11331 n = cmd_exists(p + 1);
11332 }
11333 else if (*p == '#')
11334 {
11335#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011336 if (p[1] == '#')
11337 n = autocmd_supported(p + 2);
11338 else
11339 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340#endif
11341 }
11342 else /* internal variable */
11343 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011344 char_u *tofree;
11345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011347 /* get_name_len() takes care of expanding curly braces */
11348 name = p;
11349 len = get_name_len(&p, &tofree, TRUE, FALSE);
11350 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011352 if (tofree != NULL)
11353 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011354 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011355 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011357 /* handle d.key, l[idx], f(expr) */
11358 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11359 if (n)
11360 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 }
11362 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011363 if (*p != NUL)
11364 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011366 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 }
11368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370}
11371
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011372#ifdef FEAT_FLOAT
11373/*
11374 * "exp()" function
11375 */
11376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011377f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011378{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011379 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011380
11381 rettv->v_type = VAR_FLOAT;
11382 if (get_float_arg(argvars, &f) == OK)
11383 rettv->vval.v_float = exp(f);
11384 else
11385 rettv->vval.v_float = 0.0;
11386}
11387#endif
11388
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389/*
11390 * "expand()" function
11391 */
11392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011393f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394{
11395 char_u *s;
11396 int len;
11397 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011398 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011401 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011404 if (argvars[1].v_type != VAR_UNKNOWN
11405 && argvars[2].v_type != VAR_UNKNOWN
11406 && get_tv_number_chk(&argvars[2], &error)
11407 && !error)
11408 {
11409 rettv->v_type = VAR_LIST;
11410 rettv->vval.v_list = NULL;
11411 }
11412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 if (*s == '%' || *s == '#' || *s == '<')
11415 {
11416 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011417 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011419 if (rettv->v_type == VAR_LIST)
11420 {
11421 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11422 list_append_string(rettv->vval.v_list, result, -1);
11423 else
11424 vim_free(result);
11425 }
11426 else
11427 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 }
11429 else
11430 {
11431 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011432 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011433 if (argvars[1].v_type != VAR_UNKNOWN
11434 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011435 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 if (!error)
11437 {
11438 ExpandInit(&xpc);
11439 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011440 if (p_wic)
11441 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011442 if (rettv->v_type == VAR_STRING)
11443 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11444 options, WILD_ALL);
11445 else if (rettv_list_alloc(rettv) != FAIL)
11446 {
11447 int i;
11448
11449 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11450 for (i = 0; i < xpc.xp_numfiles; i++)
11451 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11452 ExpandCleanup(&xpc);
11453 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011454 }
11455 else
11456 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 }
11458}
11459
11460/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011461 * Go over all entries in "d2" and add them to "d1".
11462 * When "action" is "error" then a duplicate key is an error.
11463 * When "action" is "force" then a duplicate key is overwritten.
11464 * Otherwise duplicate keys are ignored ("action" is "keep").
11465 */
11466 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011467dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011468{
11469 dictitem_T *di1;
11470 hashitem_T *hi2;
11471 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011472 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011473
11474 todo = (int)d2->dv_hashtab.ht_used;
11475 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11476 {
11477 if (!HASHITEM_EMPTY(hi2))
11478 {
11479 --todo;
11480 di1 = dict_find(d1, hi2->hi_key, -1);
11481 if (d1->dv_scope != 0)
11482 {
11483 /* Disallow replacing a builtin function in l: and g:.
11484 * Check the key to be valid when adding to any
11485 * scope. */
11486 if (d1->dv_scope == VAR_DEF_SCOPE
11487 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11488 && var_check_func_name(hi2->hi_key,
11489 di1 == NULL))
11490 break;
11491 if (!valid_varname(hi2->hi_key))
11492 break;
11493 }
11494 if (di1 == NULL)
11495 {
11496 di1 = dictitem_copy(HI2DI(hi2));
11497 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11498 dictitem_free(di1);
11499 }
11500 else if (*action == 'e')
11501 {
11502 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11503 break;
11504 }
11505 else if (*action == 'f' && HI2DI(hi2) != di1)
11506 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011507 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11508 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011509 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011510 clear_tv(&di1->di_tv);
11511 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11512 }
11513 }
11514 }
11515}
11516
11517/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011518 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011519 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011520 */
11521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011522f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011523{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011524 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011525
Bram Moolenaare9a41262005-01-15 22:18:47 +000011526 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011527 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011528 list_T *l1, *l2;
11529 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011530 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011532
Bram Moolenaare9a41262005-01-15 22:18:47 +000011533 l1 = argvars[0].vval.v_list;
11534 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011535 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011536 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011537 {
11538 if (argvars[2].v_type != VAR_UNKNOWN)
11539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011540 before = get_tv_number_chk(&argvars[2], &error);
11541 if (error)
11542 return; /* type error; errmsg already given */
11543
Bram Moolenaar758711c2005-02-02 23:11:38 +000011544 if (before == l1->lv_len)
11545 item = NULL;
11546 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011547 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011548 item = list_find(l1, before);
11549 if (item == NULL)
11550 {
11551 EMSGN(_(e_listidx), before);
11552 return;
11553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011554 }
11555 }
11556 else
11557 item = NULL;
11558 list_extend(l1, l2, item);
11559
Bram Moolenaare9a41262005-01-15 22:18:47 +000011560 copy_tv(&argvars[0], rettv);
11561 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011562 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11564 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011565 dict_T *d1, *d2;
11566 char_u *action;
11567 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011568
11569 d1 = argvars[0].vval.v_dict;
11570 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011571 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011572 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011573 {
11574 /* Check the third argument. */
11575 if (argvars[2].v_type != VAR_UNKNOWN)
11576 {
11577 static char *(av[]) = {"keep", "force", "error"};
11578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011579 action = get_tv_string_chk(&argvars[2]);
11580 if (action == NULL)
11581 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011582 for (i = 0; i < 3; ++i)
11583 if (STRCMP(action, av[i]) == 0)
11584 break;
11585 if (i == 3)
11586 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011587 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011588 return;
11589 }
11590 }
11591 else
11592 action = (char_u *)"force";
11593
Bram Moolenaara9922d62013-05-30 13:01:18 +020011594 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011595
Bram Moolenaare9a41262005-01-15 22:18:47 +000011596 copy_tv(&argvars[0], rettv);
11597 }
11598 }
11599 else
11600 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011601}
11602
11603/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011604 * "feedkeys()" function
11605 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011607f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011608{
11609 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011610 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011611 char_u *keys, *flags;
11612 char_u nbuf[NUMBUFLEN];
11613 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011614 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011615 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011616
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011617 /* This is not allowed in the sandbox. If the commands would still be
11618 * executed in the sandbox it would be OK, but it probably happens later,
11619 * when "sandbox" is no longer set. */
11620 if (check_secure())
11621 return;
11622
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011623 keys = get_tv_string(&argvars[0]);
11624 if (*keys != NUL)
11625 {
11626 if (argvars[1].v_type != VAR_UNKNOWN)
11627 {
11628 flags = get_tv_string_buf(&argvars[1], nbuf);
11629 for ( ; *flags != NUL; ++flags)
11630 {
11631 switch (*flags)
11632 {
11633 case 'n': remap = FALSE; break;
11634 case 'm': remap = TRUE; break;
11635 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011636 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011637 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011638 }
11639 }
11640 }
11641
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011642 /* Need to escape K_SPECIAL and CSI before putting the string in the
11643 * typeahead buffer. */
11644 keys_esc = vim_strsave_escape_csi(keys);
11645 if (keys_esc != NULL)
11646 {
11647 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011648 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011649 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011650 if (vgetc_busy)
11651 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011652 if (execute)
11653 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011654 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011655 }
11656}
11657
11658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 * "filereadable()" function
11660 */
11661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011662f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011664 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 char_u *p;
11666 int n;
11667
Bram Moolenaarc236c162008-07-13 17:41:49 +000011668#ifndef O_NONBLOCK
11669# define O_NONBLOCK 0
11670#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011672 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11673 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 {
11675 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011676 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 }
11678 else
11679 n = FALSE;
11680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682}
11683
11684/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011685 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 * rights to write into.
11687 */
11688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011689f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011691 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692}
11693
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011694 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011695findfilendir(
11696 typval_T *argvars UNUSED,
11697 typval_T *rettv,
11698 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011699{
11700#ifdef FEAT_SEARCHPATH
11701 char_u *fname;
11702 char_u *fresult = NULL;
11703 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11704 char_u *p;
11705 char_u pathbuf[NUMBUFLEN];
11706 int count = 1;
11707 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011708 int error = FALSE;
11709#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011710
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011711 rettv->vval.v_string = NULL;
11712 rettv->v_type = VAR_STRING;
11713
11714#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011716
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011717 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011719 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11720 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011721 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011722 else
11723 {
11724 if (*p != NUL)
11725 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011726
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011727 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011728 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011729 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011730 }
11731
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011732 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11733 error = TRUE;
11734
11735 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011737 do
11738 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011739 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011740 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011741 fresult = find_file_in_path_option(first ? fname : NULL,
11742 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011743 0, first, path,
11744 find_what,
11745 curbuf->b_ffname,
11746 find_what == FINDFILE_DIR
11747 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011748 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011749
11750 if (fresult != NULL && rettv->v_type == VAR_LIST)
11751 list_append_string(rettv->vval.v_list, fresult, -1);
11752
11753 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011755
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011756 if (rettv->v_type == VAR_STRING)
11757 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011758#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011759}
11760
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011761static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11762static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011763
11764/*
11765 * Implementation of map() and filter().
11766 */
11767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011768filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011769{
11770 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011771 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011772 listitem_T *li, *nli;
11773 list_T *l = NULL;
11774 dictitem_T *di;
11775 hashtab_T *ht;
11776 hashitem_T *hi;
11777 dict_T *d = NULL;
11778 typval_T save_val;
11779 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011780 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011781 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011782 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011783 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011784 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011785 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011786 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011787
Bram Moolenaare9a41262005-01-15 22:18:47 +000011788 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011789 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011790 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011791 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011792 return;
11793 }
11794 else if (argvars[0].v_type == VAR_DICT)
11795 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011796 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011797 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011798 return;
11799 }
11800 else
11801 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011802 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011803 return;
11804 }
11805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011806 expr = get_tv_string_buf_chk(&argvars[1], buf);
11807 /* On type errors, the preceding call has already displayed an error
11808 * message. Avoid a misleading error message for an empty string that
11809 * was not passed as argument. */
11810 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 prepare_vimvar(VV_VAL, &save_val);
11813 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011814
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011815 /* We reset "did_emsg" to be able to detect whether an error
11816 * occurred during evaluation of the expression. */
11817 save_did_emsg = did_emsg;
11818 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011819
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011820 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011821 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 vimvars[VV_KEY].vv_type = VAR_STRING;
11824
11825 ht = &d->dv_hashtab;
11826 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011827 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011828 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011830 if (!HASHITEM_EMPTY(hi))
11831 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011832 int r;
11833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011834 --todo;
11835 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011836 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011837 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11838 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011839 break;
11840 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011841 r = filter_map_one(&di->di_tv, expr, map, &rem);
11842 clear_tv(&vimvars[VV_KEY].vv_tv);
11843 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011844 break;
11845 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011846 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011847 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11848 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011849 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011850 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011851 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011852 }
11853 }
11854 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 }
11856 else
11857 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011858 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011860 for (li = l->lv_first; li != NULL; li = nli)
11861 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011862 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011863 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011864 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011865 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011866 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011867 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011868 break;
11869 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011870 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011871 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011872 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011873 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011874
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011875 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011876 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011877
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011878 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011879 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011880
11881 copy_tv(&argvars[0], rettv);
11882}
11883
11884 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011885filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011886{
Bram Moolenaar33570922005-01-25 22:26:29 +000011887 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011888 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011889 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011890
Bram Moolenaar33570922005-01-25 22:26:29 +000011891 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011892 s = expr;
11893 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011894 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011895 if (*s != NUL) /* check for trailing chars after expr */
11896 {
11897 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011898 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011899 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011900 }
11901 if (map)
11902 {
11903 /* map(): replace the list item value */
11904 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011905 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011906 *tv = rettv;
11907 }
11908 else
11909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011910 int error = FALSE;
11911
Bram Moolenaare9a41262005-01-15 22:18:47 +000011912 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011913 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011914 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011915 /* On type error, nothing has been removed; return FAIL to stop the
11916 * loop. The error message was given by get_tv_number_chk(). */
11917 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011918 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011919 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011920 retval = OK;
11921theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011922 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011923 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011924}
11925
11926/*
11927 * "filter()" function
11928 */
11929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011930f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011931{
11932 filter_map(argvars, rettv, FALSE);
11933}
11934
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011935/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011936 * "finddir({fname}[, {path}[, {count}]])" function
11937 */
11938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011939f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011940{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011941 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011942}
11943
11944/*
11945 * "findfile({fname}[, {path}[, {count}]])" function
11946 */
11947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011948f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011949{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011950 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011951}
11952
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011953#ifdef FEAT_FLOAT
11954/*
11955 * "float2nr({float})" function
11956 */
11957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011958f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011959{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011960 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011961
11962 if (get_float_arg(argvars, &f) == OK)
11963 {
11964 if (f < -0x7fffffff)
11965 rettv->vval.v_number = -0x7fffffff;
11966 else if (f > 0x7fffffff)
11967 rettv->vval.v_number = 0x7fffffff;
11968 else
11969 rettv->vval.v_number = (varnumber_T)f;
11970 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011971}
11972
11973/*
11974 * "floor({float})" function
11975 */
11976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011977f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011978{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011979 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011980
11981 rettv->v_type = VAR_FLOAT;
11982 if (get_float_arg(argvars, &f) == OK)
11983 rettv->vval.v_float = floor(f);
11984 else
11985 rettv->vval.v_float = 0.0;
11986}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011987
11988/*
11989 * "fmod()" function
11990 */
11991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011992f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011993{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011994 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011995
11996 rettv->v_type = VAR_FLOAT;
11997 if (get_float_arg(argvars, &fx) == OK
11998 && get_float_arg(&argvars[1], &fy) == OK)
11999 rettv->vval.v_float = fmod(fx, fy);
12000 else
12001 rettv->vval.v_float = 0.0;
12002}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012003#endif
12004
Bram Moolenaar0d660222005-01-07 21:51:51 +000012005/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012006 * "fnameescape({string})" function
12007 */
12008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012009f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012010{
12011 rettv->vval.v_string = vim_strsave_fnameescape(
12012 get_tv_string(&argvars[0]), FALSE);
12013 rettv->v_type = VAR_STRING;
12014}
12015
12016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 * "fnamemodify({fname}, {mods})" function
12018 */
12019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012020f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021{
12022 char_u *fname;
12023 char_u *mods;
12024 int usedlen = 0;
12025 int len;
12026 char_u *fbuf = NULL;
12027 char_u buf[NUMBUFLEN];
12028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012029 fname = get_tv_string_chk(&argvars[0]);
12030 mods = get_tv_string_buf_chk(&argvars[1], buf);
12031 if (fname == NULL || mods == NULL)
12032 fname = NULL;
12033 else
12034 {
12035 len = (int)STRLEN(fname);
12036 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012039 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012041 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012043 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 vim_free(fbuf);
12045}
12046
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012047static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048
12049/*
12050 * "foldclosed()" function
12051 */
12052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012053foldclosed_both(
12054 typval_T *argvars UNUSED,
12055 typval_T *rettv,
12056 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057{
12058#ifdef FEAT_FOLDING
12059 linenr_T lnum;
12060 linenr_T first, last;
12061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12064 {
12065 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12066 {
12067 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012068 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012070 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 return;
12072 }
12073 }
12074#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012075 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076}
12077
12078/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012079 * "foldclosed()" function
12080 */
12081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012082f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012083{
12084 foldclosed_both(argvars, rettv, FALSE);
12085}
12086
12087/*
12088 * "foldclosedend()" function
12089 */
12090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012091f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092{
12093 foldclosed_both(argvars, rettv, TRUE);
12094}
12095
12096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 * "foldlevel()" function
12098 */
12099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012100f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101{
12102#ifdef FEAT_FOLDING
12103 linenr_T lnum;
12104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012107 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109}
12110
12111/*
12112 * "foldtext()" function
12113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012115f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116{
12117#ifdef FEAT_FOLDING
12118 linenr_T lnum;
12119 char_u *s;
12120 char_u *r;
12121 int len;
12122 char *txt;
12123#endif
12124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125 rettv->v_type = VAR_STRING;
12126 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012128 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12129 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12130 <= curbuf->b_ml.ml_line_count
12131 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 {
12133 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012134 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12135 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 {
12137 if (!linewhite(lnum))
12138 break;
12139 ++lnum;
12140 }
12141
12142 /* Find interesting text in this line. */
12143 s = skipwhite(ml_get(lnum));
12144 /* skip C comment-start */
12145 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012146 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012148 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012149 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012150 {
12151 s = skipwhite(ml_get(lnum + 1));
12152 if (*s == '*')
12153 s = skipwhite(s + 1);
12154 }
12155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156 txt = _("+-%s%3ld lines: ");
12157 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012158 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 + 20 /* for %3ld */
12160 + STRLEN(s))); /* concatenated */
12161 if (r != NULL)
12162 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012163 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12164 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12165 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 len = (int)STRLEN(r);
12167 STRCAT(r, s);
12168 /* remove 'foldmarker' and 'commentstring' */
12169 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 }
12172 }
12173#endif
12174}
12175
12176/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012177 * "foldtextresult(lnum)" function
12178 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012180f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012181{
12182#ifdef FEAT_FOLDING
12183 linenr_T lnum;
12184 char_u *text;
12185 char_u buf[51];
12186 foldinfo_T foldinfo;
12187 int fold_count;
12188#endif
12189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190 rettv->v_type = VAR_STRING;
12191 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012192#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012194 /* treat illegal types and illegal string values for {lnum} the same */
12195 if (lnum < 0)
12196 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012197 fold_count = foldedCount(curwin, lnum, &foldinfo);
12198 if (fold_count > 0)
12199 {
12200 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12201 &foldinfo, buf);
12202 if (text == buf)
12203 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012205 }
12206#endif
12207}
12208
12209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210 * "foreground()" function
12211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012213f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215#ifdef FEAT_GUI
12216 if (gui.in_use)
12217 gui_mch_set_foreground();
12218#else
12219# ifdef WIN32
12220 win32_set_foreground();
12221# endif
12222#endif
12223}
12224
12225/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012226 * "function()" function
12227 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012229f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012230{
12231 char_u *s;
12232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012234 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012235 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012236 /* Don't check an autoload name for existence here. */
12237 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012238 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012239 else
12240 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012241 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012242 {
12243 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012244 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012245
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012246 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12247 * also be called from another script. Using trans_function_name()
12248 * would also work, but some plugins depend on the name being
12249 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012250 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012251 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012252 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012253 if (rettv->vval.v_string != NULL)
12254 {
12255 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012256 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012257 }
12258 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012259 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012260 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012261 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012262 }
12263}
12264
12265/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012266 * "garbagecollect()" function
12267 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012269f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012270{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012271 /* This is postponed until we are back at the toplevel, because we may be
12272 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12273 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012274
12275 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12276 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012277}
12278
12279/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012280 * "get()" function
12281 */
12282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012283f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012284{
Bram Moolenaar33570922005-01-25 22:26:29 +000012285 listitem_T *li;
12286 list_T *l;
12287 dictitem_T *di;
12288 dict_T *d;
12289 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012290
Bram Moolenaare9a41262005-01-15 22:18:47 +000012291 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012292 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012293 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012295 int error = FALSE;
12296
12297 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12298 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012299 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012300 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012301 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012302 else if (argvars[0].v_type == VAR_DICT)
12303 {
12304 if ((d = argvars[0].vval.v_dict) != NULL)
12305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012306 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012307 if (di != NULL)
12308 tv = &di->di_tv;
12309 }
12310 }
12311 else
12312 EMSG2(_(e_listdictarg), "get()");
12313
12314 if (tv == NULL)
12315 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012316 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012317 copy_tv(&argvars[2], rettv);
12318 }
12319 else
12320 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012321}
12322
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012323static 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 +000012324
12325/*
12326 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012327 * Return a range (from start to end) of lines in rettv from the specified
12328 * buffer.
12329 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012330 */
12331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012332get_buffer_lines(
12333 buf_T *buf,
12334 linenr_T start,
12335 linenr_T end,
12336 int retlist,
12337 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012338{
12339 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012340
Bram Moolenaar959a1432013-12-14 12:17:38 +010012341 rettv->v_type = VAR_STRING;
12342 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012343 if (retlist && rettv_list_alloc(rettv) == FAIL)
12344 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012345
12346 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12347 return;
12348
12349 if (!retlist)
12350 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012351 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12352 p = ml_get_buf(buf, start, FALSE);
12353 else
12354 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012355 rettv->vval.v_string = vim_strsave(p);
12356 }
12357 else
12358 {
12359 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012360 return;
12361
12362 if (start < 1)
12363 start = 1;
12364 if (end > buf->b_ml.ml_line_count)
12365 end = buf->b_ml.ml_line_count;
12366 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012367 if (list_append_string(rettv->vval.v_list,
12368 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012369 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012370 }
12371}
12372
12373/*
12374 * "getbufline()" function
12375 */
12376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012377f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012378{
12379 linenr_T lnum;
12380 linenr_T end;
12381 buf_T *buf;
12382
12383 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12384 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012385 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012386 --emsg_off;
12387
Bram Moolenaar661b1822005-07-28 22:36:45 +000012388 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012389 if (argvars[2].v_type == VAR_UNKNOWN)
12390 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012391 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012392 end = get_tv_lnum_buf(&argvars[2], buf);
12393
Bram Moolenaar342337a2005-07-21 21:11:17 +000012394 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012395}
12396
Bram Moolenaar0d660222005-01-07 21:51:51 +000012397/*
12398 * "getbufvar()" function
12399 */
12400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012401f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012402{
12403 buf_T *buf;
12404 buf_T *save_curbuf;
12405 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012406 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012407 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012409 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12410 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012411 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012412 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012413
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012414 rettv->v_type = VAR_STRING;
12415 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416
12417 if (buf != NULL && varname != NULL)
12418 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012419 /* set curbuf to be our buf, temporarily */
12420 save_curbuf = curbuf;
12421 curbuf = buf;
12422
Bram Moolenaar0d660222005-01-07 21:51:51 +000012423 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012424 {
12425 if (get_option_tv(&varname, rettv, TRUE) == OK)
12426 done = TRUE;
12427 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012428 else if (STRCMP(varname, "changedtick") == 0)
12429 {
12430 rettv->v_type = VAR_NUMBER;
12431 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012432 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012433 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012434 else
12435 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012436 /* Look up the variable. */
12437 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12438 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12439 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012440 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012441 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012442 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012443 done = TRUE;
12444 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012445 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012446
12447 /* restore previous notion of curbuf */
12448 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012449 }
12450
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012451 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12452 /* use the default value */
12453 copy_tv(&argvars[2], rettv);
12454
Bram Moolenaar0d660222005-01-07 21:51:51 +000012455 --emsg_off;
12456}
12457
12458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 * "getchar()" function
12460 */
12461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012462f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463{
12464 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012465 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012467 /* Position the cursor. Needed after a message that ends in a space. */
12468 windgoto(msg_row, msg_col);
12469
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470 ++no_mapping;
12471 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012472 for (;;)
12473 {
12474 if (argvars[0].v_type == VAR_UNKNOWN)
12475 /* getchar(): blocking wait. */
12476 n = safe_vgetc();
12477 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12478 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012479 n = vpeekc_any();
12480 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012481 /* illegal argument or getchar(0) and no char avail: return zero */
12482 n = 0;
12483 else
12484 /* getchar(0) and char avail: return char */
12485 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012486
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012487 if (n == K_IGNORE)
12488 continue;
12489 break;
12490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491 --no_mapping;
12492 --allow_keys;
12493
Bram Moolenaar219b8702006-11-01 14:32:36 +000012494 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12495 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12496 vimvars[VV_MOUSE_COL].vv_nr = 0;
12497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012498 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499 if (IS_SPECIAL(n) || mod_mask != 0)
12500 {
12501 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12502 int i = 0;
12503
12504 /* Turn a special key into three bytes, plus modifier. */
12505 if (mod_mask != 0)
12506 {
12507 temp[i++] = K_SPECIAL;
12508 temp[i++] = KS_MODIFIER;
12509 temp[i++] = mod_mask;
12510 }
12511 if (IS_SPECIAL(n))
12512 {
12513 temp[i++] = K_SPECIAL;
12514 temp[i++] = K_SECOND(n);
12515 temp[i++] = K_THIRD(n);
12516 }
12517#ifdef FEAT_MBYTE
12518 else if (has_mbyte)
12519 i += (*mb_char2bytes)(n, temp + i);
12520#endif
12521 else
12522 temp[i++] = n;
12523 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524 rettv->v_type = VAR_STRING;
12525 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012526
12527#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012528 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012529 {
12530 int row = mouse_row;
12531 int col = mouse_col;
12532 win_T *win;
12533 linenr_T lnum;
12534# ifdef FEAT_WINDOWS
12535 win_T *wp;
12536# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012537 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012538
12539 if (row >= 0 && col >= 0)
12540 {
12541 /* Find the window at the mouse coordinates and compute the
12542 * text position. */
12543 win = mouse_find_win(&row, &col);
12544 (void)mouse_comp_pos(win, &row, &col, &lnum);
12545# ifdef FEAT_WINDOWS
12546 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012547 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012548# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012549 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012550 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12551 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12552 }
12553 }
12554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 }
12556}
12557
12558/*
12559 * "getcharmod()" function
12560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012562f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565}
12566
12567/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012568 * "getcharsearch()" function
12569 */
12570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012571f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012572{
12573 if (rettv_dict_alloc(rettv) != FAIL)
12574 {
12575 dict_T *dict = rettv->vval.v_dict;
12576
12577 dict_add_nr_str(dict, "char", 0L, last_csearch());
12578 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12579 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12580 }
12581}
12582
12583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 * "getcmdline()" function
12585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012587f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012589 rettv->v_type = VAR_STRING;
12590 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591}
12592
12593/*
12594 * "getcmdpos()" function
12595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012597f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012599 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600}
12601
12602/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012603 * "getcmdtype()" function
12604 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012606f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012607{
12608 rettv->v_type = VAR_STRING;
12609 rettv->vval.v_string = alloc(2);
12610 if (rettv->vval.v_string != NULL)
12611 {
12612 rettv->vval.v_string[0] = get_cmdline_type();
12613 rettv->vval.v_string[1] = NUL;
12614 }
12615}
12616
12617/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012618 * "getcmdwintype()" function
12619 */
12620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012621f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012622{
12623 rettv->v_type = VAR_STRING;
12624 rettv->vval.v_string = NULL;
12625#ifdef FEAT_CMDWIN
12626 rettv->vval.v_string = alloc(2);
12627 if (rettv->vval.v_string != NULL)
12628 {
12629 rettv->vval.v_string[0] = cmdwin_type;
12630 rettv->vval.v_string[1] = NUL;
12631 }
12632#endif
12633}
12634
12635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636 * "getcwd()" function
12637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012639f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012641 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012642 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012644 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012645 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012646
12647 wp = find_tabwin(&argvars[0], &argvars[1]);
12648 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012650 if (wp->w_localdir != NULL)
12651 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12652 else if(globaldir != NULL)
12653 rettv->vval.v_string = vim_strsave(globaldir);
12654 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012655 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012656 cwd = alloc(MAXPATHL);
12657 if (cwd != NULL)
12658 {
12659 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12660 rettv->vval.v_string = vim_strsave(cwd);
12661 vim_free(cwd);
12662 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012663 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012664#ifdef BACKSLASH_IN_FILENAME
12665 if (rettv->vval.v_string != NULL)
12666 slash_adjust(rettv->vval.v_string);
12667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 }
12669}
12670
12671/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012672 * "getfontname()" function
12673 */
12674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012675f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012676{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012677 rettv->v_type = VAR_STRING;
12678 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012679#ifdef FEAT_GUI
12680 if (gui.in_use)
12681 {
12682 GuiFont font;
12683 char_u *name = NULL;
12684
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012685 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012686 {
12687 /* Get the "Normal" font. Either the name saved by
12688 * hl_set_font_name() or from the font ID. */
12689 font = gui.norm_font;
12690 name = hl_get_font_name();
12691 }
12692 else
12693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012695 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12696 return;
12697 font = gui_mch_get_font(name, FALSE);
12698 if (font == NOFONT)
12699 return; /* Invalid font name, return empty string. */
12700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012702 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012703 gui_mch_free_font(font);
12704 }
12705#endif
12706}
12707
12708/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012709 * "getfperm({fname})" function
12710 */
12711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012712f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012713{
12714 char_u *fname;
12715 struct stat st;
12716 char_u *perm = NULL;
12717 char_u flags[] = "rwx";
12718 int i;
12719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012720 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012722 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012723 if (mch_stat((char *)fname, &st) >= 0)
12724 {
12725 perm = vim_strsave((char_u *)"---------");
12726 if (perm != NULL)
12727 {
12728 for (i = 0; i < 9; i++)
12729 {
12730 if (st.st_mode & (1 << (8 - i)))
12731 perm[i] = flags[i % 3];
12732 }
12733 }
12734 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012736}
12737
12738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739 * "getfsize({fname})" function
12740 */
12741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012742f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743{
12744 char_u *fname;
12745 struct stat st;
12746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012747 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750
12751 if (mch_stat((char *)fname, &st) >= 0)
12752 {
12753 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012758
12759 /* non-perfect check for overflow */
12760 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12761 rettv->vval.v_number = -2;
12762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 }
12764 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766}
12767
12768/*
12769 * "getftime({fname})" function
12770 */
12771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012772f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773{
12774 char_u *fname;
12775 struct stat st;
12776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778
12779 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783}
12784
12785/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012786 * "getftype({fname})" function
12787 */
12788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012789f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012790{
12791 char_u *fname;
12792 struct stat st;
12793 char_u *type = NULL;
12794 char *t;
12795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012799 if (mch_lstat((char *)fname, &st) >= 0)
12800 {
12801#ifdef S_ISREG
12802 if (S_ISREG(st.st_mode))
12803 t = "file";
12804 else if (S_ISDIR(st.st_mode))
12805 t = "dir";
12806# ifdef S_ISLNK
12807 else if (S_ISLNK(st.st_mode))
12808 t = "link";
12809# endif
12810# ifdef S_ISBLK
12811 else if (S_ISBLK(st.st_mode))
12812 t = "bdev";
12813# endif
12814# ifdef S_ISCHR
12815 else if (S_ISCHR(st.st_mode))
12816 t = "cdev";
12817# endif
12818# ifdef S_ISFIFO
12819 else if (S_ISFIFO(st.st_mode))
12820 t = "fifo";
12821# endif
12822# ifdef S_ISSOCK
12823 else if (S_ISSOCK(st.st_mode))
12824 t = "fifo";
12825# endif
12826 else
12827 t = "other";
12828#else
12829# ifdef S_IFMT
12830 switch (st.st_mode & S_IFMT)
12831 {
12832 case S_IFREG: t = "file"; break;
12833 case S_IFDIR: t = "dir"; break;
12834# ifdef S_IFLNK
12835 case S_IFLNK: t = "link"; break;
12836# endif
12837# ifdef S_IFBLK
12838 case S_IFBLK: t = "bdev"; break;
12839# endif
12840# ifdef S_IFCHR
12841 case S_IFCHR: t = "cdev"; break;
12842# endif
12843# ifdef S_IFIFO
12844 case S_IFIFO: t = "fifo"; break;
12845# endif
12846# ifdef S_IFSOCK
12847 case S_IFSOCK: t = "socket"; break;
12848# endif
12849 default: t = "other";
12850 }
12851# else
12852 if (mch_isdir(fname))
12853 t = "dir";
12854 else
12855 t = "file";
12856# endif
12857#endif
12858 type = vim_strsave((char_u *)t);
12859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012860 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012861}
12862
12863/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012864 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012865 */
12866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012867f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012868{
12869 linenr_T lnum;
12870 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012871 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012872
12873 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012874 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012875 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012876 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012877 retlist = FALSE;
12878 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012879 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012880 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012881 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012882 retlist = TRUE;
12883 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012884
Bram Moolenaar342337a2005-07-21 21:11:17 +000012885 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012886}
12887
12888/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012889 * "getmatches()" function
12890 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012892f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012893{
12894#ifdef FEAT_SEARCH_EXTRA
12895 dict_T *dict;
12896 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012897 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012898
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012899 if (rettv_list_alloc(rettv) == OK)
12900 {
12901 while (cur != NULL)
12902 {
12903 dict = dict_alloc();
12904 if (dict == NULL)
12905 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012906 if (cur->match.regprog == NULL)
12907 {
12908 /* match added with matchaddpos() */
12909 for (i = 0; i < MAXPOSMATCH; ++i)
12910 {
12911 llpos_T *llpos;
12912 char buf[6];
12913 list_T *l;
12914
12915 llpos = &cur->pos.pos[i];
12916 if (llpos->lnum == 0)
12917 break;
12918 l = list_alloc();
12919 if (l == NULL)
12920 break;
12921 list_append_number(l, (varnumber_T)llpos->lnum);
12922 if (llpos->col > 0)
12923 {
12924 list_append_number(l, (varnumber_T)llpos->col);
12925 list_append_number(l, (varnumber_T)llpos->len);
12926 }
12927 sprintf(buf, "pos%d", i + 1);
12928 dict_add_list(dict, buf, l);
12929 }
12930 }
12931 else
12932 {
12933 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12934 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012935 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012936 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12937 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012938# ifdef FEAT_CONCEAL
12939 if (cur->conceal_char)
12940 {
12941 char_u buf[MB_MAXBYTES + 1];
12942
12943 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12944 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12945 }
12946# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012947 list_append_dict(rettv->vval.v_list, dict);
12948 cur = cur->next;
12949 }
12950 }
12951#endif
12952}
12953
12954/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012955 * "getpid()" function
12956 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012958f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012959{
12960 rettv->vval.v_number = mch_get_pid();
12961}
12962
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012963static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012964
12965/*
12966 * "getcurpos()" function
12967 */
12968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012969f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012970{
12971 getpos_both(argvars, rettv, TRUE);
12972}
12973
Bram Moolenaar18081e32008-02-20 19:11:07 +000012974/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012975 * "getpos(string)" function
12976 */
12977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012978f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012979{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012980 getpos_both(argvars, rettv, FALSE);
12981}
12982
12983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012984getpos_both(
12985 typval_T *argvars,
12986 typval_T *rettv,
12987 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012988{
Bram Moolenaara5525202006-03-02 22:52:09 +000012989 pos_T *fp;
12990 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012991 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012992
12993 if (rettv_list_alloc(rettv) == OK)
12994 {
12995 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012996 if (getcurpos)
12997 fp = &curwin->w_cursor;
12998 else
12999 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013000 if (fnum != -1)
13001 list_append_number(l, (varnumber_T)fnum);
13002 else
13003 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013004 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13005 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013006 list_append_number(l, (fp != NULL)
13007 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013008 : (varnumber_T)0);
13009 list_append_number(l,
13010#ifdef FEAT_VIRTUALEDIT
13011 (fp != NULL) ? (varnumber_T)fp->coladd :
13012#endif
13013 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013014 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013015 {
13016 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013017 list_append_number(l, curwin->w_curswant == MAXCOL ?
13018 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013019 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013020 }
13021 else
13022 rettv->vval.v_number = FALSE;
13023}
13024
13025/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013026 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013027 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013029f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013030{
13031#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013032 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013033#endif
13034
Bram Moolenaar2641f772005-03-25 21:58:17 +000013035#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013036 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013037 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013038 wp = NULL;
13039 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13040 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013041 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013042 if (wp == NULL)
13043 return;
13044 }
13045
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013046 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013047 }
13048#endif
13049}
13050
13051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052 * "getreg()" function
13053 */
13054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013055f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056{
13057 char_u *strregname;
13058 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013059 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013060 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013061 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013063 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013064 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013065 strregname = get_tv_string_chk(&argvars[0]);
13066 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013067 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013069 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013070 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13071 return_list = get_tv_number_chk(&argvars[2], &error);
13072 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013075 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013076
13077 if (error)
13078 return;
13079
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080 regname = (strregname == NULL ? '"' : *strregname);
13081 if (regname == 0)
13082 regname = '"';
13083
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013084 if (return_list)
13085 {
13086 rettv->v_type = VAR_LIST;
13087 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13088 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013089 if (rettv->vval.v_list != NULL)
13090 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013091 }
13092 else
13093 {
13094 rettv->v_type = VAR_STRING;
13095 rettv->vval.v_string = get_reg_contents(regname,
13096 arg2 ? GREG_EXPR_SRC : 0);
13097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098}
13099
13100/*
13101 * "getregtype()" function
13102 */
13103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013104f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105{
13106 char_u *strregname;
13107 int regname;
13108 char_u buf[NUMBUFLEN + 2];
13109 long reglen = 0;
13110
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013111 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013112 {
13113 strregname = get_tv_string_chk(&argvars[0]);
13114 if (strregname == NULL) /* type error; errmsg already given */
13115 {
13116 rettv->v_type = VAR_STRING;
13117 rettv->vval.v_string = NULL;
13118 return;
13119 }
13120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 else
13122 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013123 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124
13125 regname = (strregname == NULL ? '"' : *strregname);
13126 if (regname == 0)
13127 regname = '"';
13128
13129 buf[0] = NUL;
13130 buf[1] = NUL;
13131 switch (get_reg_type(regname, &reglen))
13132 {
13133 case MLINE: buf[0] = 'V'; break;
13134 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 case MBLOCK:
13136 buf[0] = Ctrl_V;
13137 sprintf((char *)buf + 1, "%ld", reglen + 1);
13138 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013140 rettv->v_type = VAR_STRING;
13141 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142}
13143
13144/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013145 * "gettabvar()" function
13146 */
13147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013148f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013149{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013150 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013151 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013152 dictitem_T *v;
13153 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013154 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013155
13156 rettv->v_type = VAR_STRING;
13157 rettv->vval.v_string = NULL;
13158
13159 varname = get_tv_string_chk(&argvars[1]);
13160 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13161 if (tp != NULL && varname != NULL)
13162 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013163 /* Set tp to be our tabpage, temporarily. Also set the window to the
13164 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013165 if (switch_win(&oldcurwin, &oldtabpage,
13166 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013167 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013168 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013169 /* look up the variable */
13170 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13171 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13172 if (v != NULL)
13173 {
13174 copy_tv(&v->di_tv, rettv);
13175 done = TRUE;
13176 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013177 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013178
13179 /* restore previous notion of curwin */
13180 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013181 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013182
13183 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013184 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013185}
13186
13187/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013188 * "gettabwinvar()" function
13189 */
13190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013191f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013192{
13193 getwinvar(argvars, rettv, 1);
13194}
13195
13196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197 * "getwinposx()" function
13198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013200f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203#ifdef FEAT_GUI
13204 if (gui.in_use)
13205 {
13206 int x, y;
13207
13208 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 }
13211#endif
13212}
13213
13214/*
13215 * "getwinposy()" function
13216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013218f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221#ifdef FEAT_GUI
13222 if (gui.in_use)
13223 {
13224 int x, y;
13225
13226 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 }
13229#endif
13230}
13231
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013232/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013233 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013234 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013235 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013236find_win_by_nr(
13237 typval_T *vp,
13238 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013239{
13240#ifdef FEAT_WINDOWS
13241 win_T *wp;
13242#endif
13243 int nr;
13244
13245 nr = get_tv_number_chk(vp, NULL);
13246
13247#ifdef FEAT_WINDOWS
13248 if (nr < 0)
13249 return NULL;
13250 if (nr == 0)
13251 return curwin;
13252
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013253 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13254 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013255 if (--nr <= 0)
13256 break;
13257 return wp;
13258#else
13259 if (nr == 0 || nr == 1)
13260 return curwin;
13261 return NULL;
13262#endif
13263}
13264
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013266 * Find window specified by "wvp" in tabpage "tvp".
13267 */
13268 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013269find_tabwin(
13270 typval_T *wvp, /* VAR_UNKNOWN for current window */
13271 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013272{
13273 win_T *wp = NULL;
13274 tabpage_T *tp = NULL;
13275 long n;
13276
13277 if (wvp->v_type != VAR_UNKNOWN)
13278 {
13279 if (tvp->v_type != VAR_UNKNOWN)
13280 {
13281 n = get_tv_number(tvp);
13282 if (n >= 0)
13283 tp = find_tabpage(n);
13284 }
13285 else
13286 tp = curtab;
13287
13288 if (tp != NULL)
13289 wp = find_win_by_nr(wvp, tp);
13290 }
13291 else
13292 wp = curwin;
13293
13294 return wp;
13295}
13296
13297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 * "getwinvar()" function
13299 */
13300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013301f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013303 getwinvar(argvars, rettv, 0);
13304}
13305
13306/*
13307 * getwinvar() and gettabwinvar()
13308 */
13309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013310getwinvar(
13311 typval_T *argvars,
13312 typval_T *rettv,
13313 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013314{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013315 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013317 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013318 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013319 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013320#ifdef FEAT_WINDOWS
13321 win_T *oldcurwin;
13322 tabpage_T *oldtabpage;
13323 int need_switch_win;
13324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013326#ifdef FEAT_WINDOWS
13327 if (off == 1)
13328 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13329 else
13330 tp = curtab;
13331#endif
13332 win = find_win_by_nr(&argvars[off], tp);
13333 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013334 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013336 rettv->v_type = VAR_STRING;
13337 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338
13339 if (win != NULL && varname != NULL)
13340 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013341#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013342 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013343 * otherwise the window is not valid. Only do this when needed,
13344 * autocommands get blocked. */
13345 need_switch_win = !(tp == curtab && win == curwin);
13346 if (!need_switch_win
13347 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13348#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013349 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013350 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013351 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013352 if (get_option_tv(&varname, rettv, 1) == OK)
13353 done = TRUE;
13354 }
13355 else
13356 {
13357 /* Look up the variable. */
13358 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13359 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13360 varname, FALSE);
13361 if (v != NULL)
13362 {
13363 copy_tv(&v->di_tv, rettv);
13364 done = TRUE;
13365 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013368
Bram Moolenaarba117c22015-09-29 16:53:22 +020013369#ifdef FEAT_WINDOWS
13370 if (need_switch_win)
13371 /* restore previous notion of curwin */
13372 restore_win(oldcurwin, oldtabpage, TRUE);
13373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374 }
13375
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013376 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13377 /* use the default return value */
13378 copy_tv(&argvars[off + 2], rettv);
13379
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 --emsg_off;
13381}
13382
13383/*
13384 * "glob()" function
13385 */
13386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013387f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013389 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013391 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013393 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013394 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013395 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013396 if (argvars[1].v_type != VAR_UNKNOWN)
13397 {
13398 if (get_tv_number_chk(&argvars[1], &error))
13399 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013400 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013401 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013402 if (get_tv_number_chk(&argvars[2], &error))
13403 {
13404 rettv->v_type = VAR_LIST;
13405 rettv->vval.v_list = NULL;
13406 }
13407 if (argvars[3].v_type != VAR_UNKNOWN
13408 && get_tv_number_chk(&argvars[3], &error))
13409 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013410 }
13411 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013412 if (!error)
13413 {
13414 ExpandInit(&xpc);
13415 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013416 if (p_wic)
13417 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013418 if (rettv->v_type == VAR_STRING)
13419 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013420 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013421 else if (rettv_list_alloc(rettv) != FAIL)
13422 {
13423 int i;
13424
13425 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13426 NULL, options, WILD_ALL_KEEP);
13427 for (i = 0; i < xpc.xp_numfiles; i++)
13428 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13429
13430 ExpandCleanup(&xpc);
13431 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013432 }
13433 else
13434 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435}
13436
13437/*
13438 * "globpath()" function
13439 */
13440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013441f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013443 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013445 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013446 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013447 garray_T ga;
13448 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013450 /* When the optional second argument is non-zero, don't remove matches
13451 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013453 if (argvars[2].v_type != VAR_UNKNOWN)
13454 {
13455 if (get_tv_number_chk(&argvars[2], &error))
13456 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013457 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013458 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013459 if (get_tv_number_chk(&argvars[3], &error))
13460 {
13461 rettv->v_type = VAR_LIST;
13462 rettv->vval.v_list = NULL;
13463 }
13464 if (argvars[4].v_type != VAR_UNKNOWN
13465 && get_tv_number_chk(&argvars[4], &error))
13466 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013467 }
13468 }
13469 if (file != NULL && !error)
13470 {
13471 ga_init2(&ga, (int)sizeof(char_u *), 10);
13472 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13473 if (rettv->v_type == VAR_STRING)
13474 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13475 else if (rettv_list_alloc(rettv) != FAIL)
13476 for (i = 0; i < ga.ga_len; ++i)
13477 list_append_string(rettv->vval.v_list,
13478 ((char_u **)(ga.ga_data))[i], -1);
13479 ga_clear_strings(&ga);
13480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013481 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013482 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483}
13484
13485/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013486 * "glob2regpat()" function
13487 */
13488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013489f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013490{
13491 char_u *pat = get_tv_string_chk(&argvars[0]);
13492
13493 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013494 rettv->vval.v_string = (pat == NULL)
13495 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013496}
13497
13498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499 * "has()" function
13500 */
13501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013502f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503{
13504 int i;
13505 char_u *name;
13506 int n = FALSE;
13507 static char *(has_list[]) =
13508 {
13509#ifdef AMIGA
13510 "amiga",
13511# ifdef FEAT_ARP
13512 "arp",
13513# endif
13514#endif
13515#ifdef __BEOS__
13516 "beos",
13517#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013518#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 "mac",
13520#endif
13521#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013522 "macunix", /* built with 'darwin' enabled */
13523#endif
13524#if defined(__APPLE__) && __APPLE__ == 1
13525 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527#ifdef __QNX__
13528 "qnx",
13529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530#ifdef UNIX
13531 "unix",
13532#endif
13533#ifdef VMS
13534 "vms",
13535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536#ifdef WIN32
13537 "win32",
13538#endif
13539#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13540 "win32unix",
13541#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013542#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 "win64",
13544#endif
13545#ifdef EBCDIC
13546 "ebcdic",
13547#endif
13548#ifndef CASE_INSENSITIVE_FILENAME
13549 "fname_case",
13550#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013551#ifdef HAVE_ACL
13552 "acl",
13553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554#ifdef FEAT_ARABIC
13555 "arabic",
13556#endif
13557#ifdef FEAT_AUTOCMD
13558 "autocmd",
13559#endif
13560#ifdef FEAT_BEVAL
13561 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013562# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13563 "balloon_multiline",
13564# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565#endif
13566#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13567 "builtin_terms",
13568# ifdef ALL_BUILTIN_TCAPS
13569 "all_builtin_terms",
13570# endif
13571#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013572#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13573 || defined(FEAT_GUI_W32) \
13574 || defined(FEAT_GUI_MOTIF))
13575 "browsefilter",
13576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577#ifdef FEAT_BYTEOFF
13578 "byte_offset",
13579#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013580#ifdef FEAT_CHANNEL
13581 "channel",
13582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583#ifdef FEAT_CINDENT
13584 "cindent",
13585#endif
13586#ifdef FEAT_CLIENTSERVER
13587 "clientserver",
13588#endif
13589#ifdef FEAT_CLIPBOARD
13590 "clipboard",
13591#endif
13592#ifdef FEAT_CMDL_COMPL
13593 "cmdline_compl",
13594#endif
13595#ifdef FEAT_CMDHIST
13596 "cmdline_hist",
13597#endif
13598#ifdef FEAT_COMMENTS
13599 "comments",
13600#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013601#ifdef FEAT_CONCEAL
13602 "conceal",
13603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604#ifdef FEAT_CRYPT
13605 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013606 "crypt-blowfish",
13607 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608#endif
13609#ifdef FEAT_CSCOPE
13610 "cscope",
13611#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013612#ifdef FEAT_CURSORBIND
13613 "cursorbind",
13614#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013615#ifdef CURSOR_SHAPE
13616 "cursorshape",
13617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618#ifdef DEBUG
13619 "debug",
13620#endif
13621#ifdef FEAT_CON_DIALOG
13622 "dialog_con",
13623#endif
13624#ifdef FEAT_GUI_DIALOG
13625 "dialog_gui",
13626#endif
13627#ifdef FEAT_DIFF
13628 "diff",
13629#endif
13630#ifdef FEAT_DIGRAPHS
13631 "digraphs",
13632#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013633#ifdef FEAT_DIRECTX
13634 "directx",
13635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636#ifdef FEAT_DND
13637 "dnd",
13638#endif
13639#ifdef FEAT_EMACS_TAGS
13640 "emacs_tags",
13641#endif
13642 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013643 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644#ifdef FEAT_SEARCH_EXTRA
13645 "extra_search",
13646#endif
13647#ifdef FEAT_FKMAP
13648 "farsi",
13649#endif
13650#ifdef FEAT_SEARCHPATH
13651 "file_in_path",
13652#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013653#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013654 "filterpipe",
13655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656#ifdef FEAT_FIND_ID
13657 "find_in_path",
13658#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013659#ifdef FEAT_FLOAT
13660 "float",
13661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662#ifdef FEAT_FOLDING
13663 "folding",
13664#endif
13665#ifdef FEAT_FOOTER
13666 "footer",
13667#endif
13668#if !defined(USE_SYSTEM) && defined(UNIX)
13669 "fork",
13670#endif
13671#ifdef FEAT_GETTEXT
13672 "gettext",
13673#endif
13674#ifdef FEAT_GUI
13675 "gui",
13676#endif
13677#ifdef FEAT_GUI_ATHENA
13678# ifdef FEAT_GUI_NEXTAW
13679 "gui_neXtaw",
13680# else
13681 "gui_athena",
13682# endif
13683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684#ifdef FEAT_GUI_GTK
13685 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013686# ifdef USE_GTK3
13687 "gui_gtk3",
13688# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013692#ifdef FEAT_GUI_GNOME
13693 "gui_gnome",
13694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695#ifdef FEAT_GUI_MAC
13696 "gui_mac",
13697#endif
13698#ifdef FEAT_GUI_MOTIF
13699 "gui_motif",
13700#endif
13701#ifdef FEAT_GUI_PHOTON
13702 "gui_photon",
13703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704#ifdef FEAT_GUI_W32
13705 "gui_win32",
13706#endif
13707#ifdef FEAT_HANGULIN
13708 "hangul_input",
13709#endif
13710#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13711 "iconv",
13712#endif
13713#ifdef FEAT_INS_EXPAND
13714 "insert_expand",
13715#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013716#ifdef FEAT_JOB
13717 "job",
13718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719#ifdef FEAT_JUMPLIST
13720 "jumplist",
13721#endif
13722#ifdef FEAT_KEYMAP
13723 "keymap",
13724#endif
13725#ifdef FEAT_LANGMAP
13726 "langmap",
13727#endif
13728#ifdef FEAT_LIBCALL
13729 "libcall",
13730#endif
13731#ifdef FEAT_LINEBREAK
13732 "linebreak",
13733#endif
13734#ifdef FEAT_LISP
13735 "lispindent",
13736#endif
13737#ifdef FEAT_LISTCMDS
13738 "listcmds",
13739#endif
13740#ifdef FEAT_LOCALMAP
13741 "localmap",
13742#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013743#ifdef FEAT_LUA
13744# ifndef DYNAMIC_LUA
13745 "lua",
13746# endif
13747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748#ifdef FEAT_MENU
13749 "menu",
13750#endif
13751#ifdef FEAT_SESSION
13752 "mksession",
13753#endif
13754#ifdef FEAT_MODIFY_FNAME
13755 "modify_fname",
13756#endif
13757#ifdef FEAT_MOUSE
13758 "mouse",
13759#endif
13760#ifdef FEAT_MOUSESHAPE
13761 "mouseshape",
13762#endif
13763#if defined(UNIX) || defined(VMS)
13764# ifdef FEAT_MOUSE_DEC
13765 "mouse_dec",
13766# endif
13767# ifdef FEAT_MOUSE_GPM
13768 "mouse_gpm",
13769# endif
13770# ifdef FEAT_MOUSE_JSB
13771 "mouse_jsbterm",
13772# endif
13773# ifdef FEAT_MOUSE_NET
13774 "mouse_netterm",
13775# endif
13776# ifdef FEAT_MOUSE_PTERM
13777 "mouse_pterm",
13778# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013779# ifdef FEAT_MOUSE_SGR
13780 "mouse_sgr",
13781# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013782# ifdef FEAT_SYSMOUSE
13783 "mouse_sysmouse",
13784# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013785# ifdef FEAT_MOUSE_URXVT
13786 "mouse_urxvt",
13787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788# ifdef FEAT_MOUSE_XTERM
13789 "mouse_xterm",
13790# endif
13791#endif
13792#ifdef FEAT_MBYTE
13793 "multi_byte",
13794#endif
13795#ifdef FEAT_MBYTE_IME
13796 "multi_byte_ime",
13797#endif
13798#ifdef FEAT_MULTI_LANG
13799 "multi_lang",
13800#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013801#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013802#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013803 "mzscheme",
13804#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806#ifdef FEAT_OLE
13807 "ole",
13808#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013809 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810#ifdef FEAT_PATH_EXTRA
13811 "path_extra",
13812#endif
13813#ifdef FEAT_PERL
13814#ifndef DYNAMIC_PERL
13815 "perl",
13816#endif
13817#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013818#ifdef FEAT_PERSISTENT_UNDO
13819 "persistent_undo",
13820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821#ifdef FEAT_PYTHON
13822#ifndef DYNAMIC_PYTHON
13823 "python",
13824#endif
13825#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013826#ifdef FEAT_PYTHON3
13827#ifndef DYNAMIC_PYTHON3
13828 "python3",
13829#endif
13830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831#ifdef FEAT_POSTSCRIPT
13832 "postscript",
13833#endif
13834#ifdef FEAT_PRINTER
13835 "printer",
13836#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013837#ifdef FEAT_PROFILE
13838 "profile",
13839#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013840#ifdef FEAT_RELTIME
13841 "reltime",
13842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843#ifdef FEAT_QUICKFIX
13844 "quickfix",
13845#endif
13846#ifdef FEAT_RIGHTLEFT
13847 "rightleft",
13848#endif
13849#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13850 "ruby",
13851#endif
13852#ifdef FEAT_SCROLLBIND
13853 "scrollbind",
13854#endif
13855#ifdef FEAT_CMDL_INFO
13856 "showcmd",
13857 "cmdline_info",
13858#endif
13859#ifdef FEAT_SIGNS
13860 "signs",
13861#endif
13862#ifdef FEAT_SMARTINDENT
13863 "smartindent",
13864#endif
13865#ifdef FEAT_SNIFF
13866 "sniff",
13867#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013868#ifdef STARTUPTIME
13869 "startuptime",
13870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871#ifdef FEAT_STL_OPT
13872 "statusline",
13873#endif
13874#ifdef FEAT_SUN_WORKSHOP
13875 "sun_workshop",
13876#endif
13877#ifdef FEAT_NETBEANS_INTG
13878 "netbeans_intg",
13879#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013880#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013881 "spell",
13882#endif
13883#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884 "syntax",
13885#endif
13886#if defined(USE_SYSTEM) || !defined(UNIX)
13887 "system",
13888#endif
13889#ifdef FEAT_TAG_BINS
13890 "tag_binary",
13891#endif
13892#ifdef FEAT_TAG_OLDSTATIC
13893 "tag_old_static",
13894#endif
13895#ifdef FEAT_TAG_ANYWHITE
13896 "tag_any_white",
13897#endif
13898#ifdef FEAT_TCL
13899# ifndef DYNAMIC_TCL
13900 "tcl",
13901# endif
13902#endif
13903#ifdef TERMINFO
13904 "terminfo",
13905#endif
13906#ifdef FEAT_TERMRESPONSE
13907 "termresponse",
13908#endif
13909#ifdef FEAT_TEXTOBJ
13910 "textobjects",
13911#endif
13912#ifdef HAVE_TGETENT
13913 "tgetent",
13914#endif
13915#ifdef FEAT_TITLE
13916 "title",
13917#endif
13918#ifdef FEAT_TOOLBAR
13919 "toolbar",
13920#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013921#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13922 "unnamedplus",
13923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924#ifdef FEAT_USR_CMDS
13925 "user-commands", /* was accidentally included in 5.4 */
13926 "user_commands",
13927#endif
13928#ifdef FEAT_VIMINFO
13929 "viminfo",
13930#endif
13931#ifdef FEAT_VERTSPLIT
13932 "vertsplit",
13933#endif
13934#ifdef FEAT_VIRTUALEDIT
13935 "virtualedit",
13936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938#ifdef FEAT_VISUALEXTRA
13939 "visualextra",
13940#endif
13941#ifdef FEAT_VREPLACE
13942 "vreplace",
13943#endif
13944#ifdef FEAT_WILDIGN
13945 "wildignore",
13946#endif
13947#ifdef FEAT_WILDMENU
13948 "wildmenu",
13949#endif
13950#ifdef FEAT_WINDOWS
13951 "windows",
13952#endif
13953#ifdef FEAT_WAK
13954 "winaltkeys",
13955#endif
13956#ifdef FEAT_WRITEBACKUP
13957 "writebackup",
13958#endif
13959#ifdef FEAT_XIM
13960 "xim",
13961#endif
13962#ifdef FEAT_XFONTSET
13963 "xfontset",
13964#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013965#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013966 "xpm",
13967 "xpm_w32", /* for backward compatibility */
13968#else
13969# if defined(HAVE_XPM)
13970 "xpm",
13971# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973#ifdef USE_XSMP
13974 "xsmp",
13975#endif
13976#ifdef USE_XSMP_INTERACT
13977 "xsmp_interact",
13978#endif
13979#ifdef FEAT_XCLIPBOARD
13980 "xterm_clipboard",
13981#endif
13982#ifdef FEAT_XTERM_SAVE
13983 "xterm_save",
13984#endif
13985#if defined(UNIX) && defined(FEAT_X11)
13986 "X11",
13987#endif
13988 NULL
13989 };
13990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992 for (i = 0; has_list[i] != NULL; ++i)
13993 if (STRICMP(name, has_list[i]) == 0)
13994 {
13995 n = TRUE;
13996 break;
13997 }
13998
13999 if (n == FALSE)
14000 {
14001 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014002 {
14003 if (name[5] == '-'
14004 && STRLEN(name) > 11
14005 && vim_isdigit(name[6])
14006 && vim_isdigit(name[8])
14007 && vim_isdigit(name[10]))
14008 {
14009 int major = atoi((char *)name + 6);
14010 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014011
14012 /* Expect "patch-9.9.01234". */
14013 n = (major < VIM_VERSION_MAJOR
14014 || (major == VIM_VERSION_MAJOR
14015 && (minor < VIM_VERSION_MINOR
14016 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014017 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014018 }
14019 else
14020 n = has_patch(atoi((char *)name + 5));
14021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 else if (STRICMP(name, "vim_starting") == 0)
14023 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014024#ifdef FEAT_MBYTE
14025 else if (STRICMP(name, "multi_byte_encoding") == 0)
14026 n = has_mbyte;
14027#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014028#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14029 else if (STRICMP(name, "balloon_multiline") == 0)
14030 n = multiline_balloon_available();
14031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032#ifdef DYNAMIC_TCL
14033 else if (STRICMP(name, "tcl") == 0)
14034 n = tcl_enabled(FALSE);
14035#endif
14036#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14037 else if (STRICMP(name, "iconv") == 0)
14038 n = iconv_enabled(FALSE);
14039#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014040#ifdef DYNAMIC_LUA
14041 else if (STRICMP(name, "lua") == 0)
14042 n = lua_enabled(FALSE);
14043#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014044#ifdef DYNAMIC_MZSCHEME
14045 else if (STRICMP(name, "mzscheme") == 0)
14046 n = mzscheme_enabled(FALSE);
14047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048#ifdef DYNAMIC_RUBY
14049 else if (STRICMP(name, "ruby") == 0)
14050 n = ruby_enabled(FALSE);
14051#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014052#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053#ifdef DYNAMIC_PYTHON
14054 else if (STRICMP(name, "python") == 0)
14055 n = python_enabled(FALSE);
14056#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014057#endif
14058#ifdef FEAT_PYTHON3
14059#ifdef DYNAMIC_PYTHON3
14060 else if (STRICMP(name, "python3") == 0)
14061 n = python3_enabled(FALSE);
14062#endif
14063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064#ifdef DYNAMIC_PERL
14065 else if (STRICMP(name, "perl") == 0)
14066 n = perl_enabled(FALSE);
14067#endif
14068#ifdef FEAT_GUI
14069 else if (STRICMP(name, "gui_running") == 0)
14070 n = (gui.in_use || gui.starting);
14071# ifdef FEAT_GUI_W32
14072 else if (STRICMP(name, "gui_win32s") == 0)
14073 n = gui_is_win32s();
14074# endif
14075# ifdef FEAT_BROWSE
14076 else if (STRICMP(name, "browse") == 0)
14077 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14078# endif
14079#endif
14080#ifdef FEAT_SYN_HL
14081 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014082 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083#endif
14084#if defined(WIN3264)
14085 else if (STRICMP(name, "win95") == 0)
14086 n = mch_windows95();
14087#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014088#ifdef FEAT_NETBEANS_INTG
14089 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014090 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092 }
14093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014094 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095}
14096
14097/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014098 * "has_key()" function
14099 */
14100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014101f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014102{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014103 if (argvars[0].v_type != VAR_DICT)
14104 {
14105 EMSG(_(e_dictreq));
14106 return;
14107 }
14108 if (argvars[0].vval.v_dict == NULL)
14109 return;
14110
14111 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014112 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014113}
14114
14115/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014116 * "haslocaldir()" function
14117 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014119f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014120{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014121 win_T *wp = NULL;
14122
14123 wp = find_tabwin(&argvars[0], &argvars[1]);
14124 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014125}
14126
14127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 * "hasmapto()" function
14129 */
14130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014131f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132{
14133 char_u *name;
14134 char_u *mode;
14135 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014136 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014138 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014139 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140 mode = (char_u *)"nvo";
14141 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014143 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014144 if (argvars[2].v_type != VAR_UNKNOWN)
14145 abbr = get_tv_number(&argvars[2]);
14146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147
Bram Moolenaar2c932302006-03-18 21:42:09 +000014148 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014151 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152}
14153
14154/*
14155 * "histadd()" function
14156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014158f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159{
14160#ifdef FEAT_CMDHIST
14161 int histype;
14162 char_u *str;
14163 char_u buf[NUMBUFLEN];
14164#endif
14165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014166 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167 if (check_restricted() || check_secure())
14168 return;
14169#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014170 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14171 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 if (histype >= 0)
14173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014174 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 if (*str != NUL)
14176 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014177 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 return;
14181 }
14182 }
14183#endif
14184}
14185
14186/*
14187 * "histdel()" function
14188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014190f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191{
14192#ifdef FEAT_CMDHIST
14193 int n;
14194 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14198 if (str == NULL)
14199 n = 0;
14200 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014202 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014203 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014205 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014206 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207 else
14208 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014210 get_tv_string_buf(&argvars[1], buf));
14211 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212#endif
14213}
14214
14215/*
14216 * "histget()" function
14217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014219f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220{
14221#ifdef FEAT_CMDHIST
14222 int type;
14223 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014224 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14227 if (str == NULL)
14228 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 {
14231 type = get_histtype(str);
14232 if (argvars[1].v_type == VAR_UNKNOWN)
14233 idx = get_history_idx(type);
14234 else
14235 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14236 /* -1 on type error */
14237 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014240 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243}
14244
14245/*
14246 * "histnr()" function
14247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014249f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250{
14251 int i;
14252
14253#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014254 char_u *history = get_tv_string_chk(&argvars[0]);
14255
14256 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 if (i >= HIST_CMD && i < HIST_COUNT)
14258 i = get_history_idx(i);
14259 else
14260#endif
14261 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014262 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263}
14264
14265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266 * "highlightID(name)" function
14267 */
14268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014269f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272}
14273
14274/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014275 * "highlight_exists()" function
14276 */
14277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014278f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014279{
14280 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14281}
14282
14283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284 * "hostname()" function
14285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014287f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288{
14289 char_u hostname[256];
14290
14291 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014292 rettv->v_type = VAR_STRING;
14293 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294}
14295
14296/*
14297 * iconv() function
14298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014300f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301{
14302#ifdef FEAT_MBYTE
14303 char_u buf1[NUMBUFLEN];
14304 char_u buf2[NUMBUFLEN];
14305 char_u *from, *to, *str;
14306 vimconv_T vimconv;
14307#endif
14308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 rettv->v_type = VAR_STRING;
14310 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311
14312#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014313 str = get_tv_string(&argvars[0]);
14314 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14315 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 vimconv.vc_type = CONV_NONE;
14317 convert_setup(&vimconv, from, to);
14318
14319 /* If the encodings are equal, no conversion needed. */
14320 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014321 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014323 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324
14325 convert_setup(&vimconv, NULL, NULL);
14326 vim_free(from);
14327 vim_free(to);
14328#endif
14329}
14330
14331/*
14332 * "indent()" function
14333 */
14334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014335f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336{
14337 linenr_T lnum;
14338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014339 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014343 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344}
14345
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014346/*
14347 * "index()" function
14348 */
14349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014350f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014351{
Bram Moolenaar33570922005-01-25 22:26:29 +000014352 list_T *l;
14353 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014354 long idx = 0;
14355 int ic = FALSE;
14356
14357 rettv->vval.v_number = -1;
14358 if (argvars[0].v_type != VAR_LIST)
14359 {
14360 EMSG(_(e_listreq));
14361 return;
14362 }
14363 l = argvars[0].vval.v_list;
14364 if (l != NULL)
14365 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014366 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014367 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014369 int error = FALSE;
14370
Bram Moolenaar758711c2005-02-02 23:11:38 +000014371 /* Start at specified item. Use the cached index that list_find()
14372 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014373 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014374 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014375 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014376 ic = get_tv_number_chk(&argvars[3], &error);
14377 if (error)
14378 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014379 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014380
Bram Moolenaar758711c2005-02-02 23:11:38 +000014381 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014382 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014383 {
14384 rettv->vval.v_number = idx;
14385 break;
14386 }
14387 }
14388}
14389
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390static int inputsecret_flag = 0;
14391
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014392static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014393
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014395 * This function is used by f_input() and f_inputdialog() functions. The third
14396 * argument to f_input() specifies the type of completion to use at the
14397 * prompt. The third argument to f_inputdialog() specifies the value to return
14398 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 */
14400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014401get_user_input(
14402 typval_T *argvars,
14403 typval_T *rettv,
14404 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014406 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 char_u *p = NULL;
14408 int c;
14409 char_u buf[NUMBUFLEN];
14410 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014412 int xp_type = EXPAND_NOTHING;
14413 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014416 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417
14418#ifdef NO_CONSOLE_INPUT
14419 /* While starting up, there is no place to enter text. */
14420 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422#endif
14423
14424 cmd_silent = FALSE; /* Want to see the prompt. */
14425 if (prompt != NULL)
14426 {
14427 /* Only the part of the message after the last NL is considered as
14428 * prompt for the command line */
14429 p = vim_strrchr(prompt, '\n');
14430 if (p == NULL)
14431 p = prompt;
14432 else
14433 {
14434 ++p;
14435 c = *p;
14436 *p = NUL;
14437 msg_start();
14438 msg_clr_eos();
14439 msg_puts_attr(prompt, echo_attr);
14440 msg_didout = FALSE;
14441 msg_starthere();
14442 *p = c;
14443 }
14444 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446 if (argvars[1].v_type != VAR_UNKNOWN)
14447 {
14448 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14449 if (defstr != NULL)
14450 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014452 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014453 {
14454 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014455 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014456 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014457
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014458 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014459 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014460
Bram Moolenaar4463f292005-09-25 22:20:24 +000014461 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14462 if (xp_name == NULL)
14463 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014464
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014465 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014466
Bram Moolenaar4463f292005-09-25 22:20:24 +000014467 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14468 &xp_arg) == FAIL)
14469 return;
14470 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014471 }
14472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014473 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014474 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014475 int save_ex_normal_busy = ex_normal_busy;
14476 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014477 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014478 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14479 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014480 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014481 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014482 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014483 && argvars[1].v_type != VAR_UNKNOWN
14484 && argvars[2].v_type != VAR_UNKNOWN)
14485 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14486 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014487
14488 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014490 /* since the user typed this, no need to wait for return */
14491 need_wait_return = FALSE;
14492 msg_didout = FALSE;
14493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494 cmd_silent = cmd_silent_save;
14495}
14496
14497/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014498 * "input()" function
14499 * Also handles inputsecret() when inputsecret is set.
14500 */
14501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014502f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014503{
14504 get_user_input(argvars, rettv, FALSE);
14505}
14506
14507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508 * "inputdialog()" function
14509 */
14510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014511f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512{
14513#if defined(FEAT_GUI_TEXTDIALOG)
14514 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14515 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14516 {
14517 char_u *message;
14518 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014519 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 message = get_tv_string_chk(&argvars[0]);
14522 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014523 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014524 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525 else
14526 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014527 if (message != NULL && defstr != NULL
14528 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014529 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 else
14532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 if (message != NULL && defstr != NULL
14534 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014535 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014536 rettv->vval.v_string = vim_strsave(
14537 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014541 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 }
14543 else
14544#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014545 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546}
14547
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014548/*
14549 * "inputlist()" function
14550 */
14551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014552f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014553{
14554 listitem_T *li;
14555 int selected;
14556 int mouse_used;
14557
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014558#ifdef NO_CONSOLE_INPUT
14559 /* While starting up, there is no place to enter text. */
14560 if (no_console_input())
14561 return;
14562#endif
14563 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14564 {
14565 EMSG2(_(e_listarg), "inputlist()");
14566 return;
14567 }
14568
14569 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014570 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014571 lines_left = Rows; /* avoid more prompt */
14572 msg_scroll = TRUE;
14573 msg_clr_eos();
14574
14575 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14576 {
14577 msg_puts(get_tv_string(&li->li_tv));
14578 msg_putchar('\n');
14579 }
14580
14581 /* Ask for choice. */
14582 selected = prompt_for_number(&mouse_used);
14583 if (mouse_used)
14584 selected -= lines_left;
14585
14586 rettv->vval.v_number = selected;
14587}
14588
14589
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14591
14592/*
14593 * "inputrestore()" function
14594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014596f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597{
14598 if (ga_userinput.ga_len > 0)
14599 {
14600 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14602 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014603 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604 }
14605 else if (p_verbose > 1)
14606 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014607 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014608 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609 }
14610}
14611
14612/*
14613 * "inputsave()" function
14614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014616f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014618 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 if (ga_grow(&ga_userinput, 1) == OK)
14620 {
14621 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14622 + ga_userinput.ga_len);
14623 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014624 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 }
14626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014627 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628}
14629
14630/*
14631 * "inputsecret()" function
14632 */
14633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014634f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635{
14636 ++cmdline_star;
14637 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014638 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639 --cmdline_star;
14640 --inputsecret_flag;
14641}
14642
14643/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014644 * "insert()" function
14645 */
14646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014647f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014648{
14649 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014650 listitem_T *item;
14651 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014652 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014653
14654 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014655 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014656 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014657 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014658 {
14659 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014660 before = get_tv_number_chk(&argvars[2], &error);
14661 if (error)
14662 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014663
Bram Moolenaar758711c2005-02-02 23:11:38 +000014664 if (before == l->lv_len)
14665 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014666 else
14667 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014668 item = list_find(l, before);
14669 if (item == NULL)
14670 {
14671 EMSGN(_(e_listidx), before);
14672 l = NULL;
14673 }
14674 }
14675 if (l != NULL)
14676 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014677 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014678 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014679 }
14680 }
14681}
14682
14683/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014684 * "invert(expr)" function
14685 */
14686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014687f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014688{
14689 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14690}
14691
14692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693 * "isdirectory()" function
14694 */
14695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014696f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014698 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699}
14700
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014701/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014702 * "islocked()" function
14703 */
14704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014705f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014706{
14707 lval_T lv;
14708 char_u *end;
14709 dictitem_T *di;
14710
14711 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014712 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14713 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014714 if (end != NULL && lv.ll_name != NULL)
14715 {
14716 if (*end != NUL)
14717 EMSG(_(e_trailing));
14718 else
14719 {
14720 if (lv.ll_tv == NULL)
14721 {
14722 if (check_changedtick(lv.ll_name))
14723 rettv->vval.v_number = 1; /* always locked */
14724 else
14725 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014726 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014727 if (di != NULL)
14728 {
14729 /* Consider a variable locked when:
14730 * 1. the variable itself is locked
14731 * 2. the value of the variable is locked.
14732 * 3. the List or Dict value is locked.
14733 */
14734 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14735 || tv_islocked(&di->di_tv));
14736 }
14737 }
14738 }
14739 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014740 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014741 else if (lv.ll_newkey != NULL)
14742 EMSG2(_(e_dictkey), lv.ll_newkey);
14743 else if (lv.ll_list != NULL)
14744 /* List item. */
14745 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14746 else
14747 /* Dictionary item. */
14748 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14749 }
14750 }
14751
14752 clear_lval(&lv);
14753}
14754
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014755#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14756/*
14757 * "isnan()" function
14758 */
14759 static void
14760f_isnan(typval_T *argvars, typval_T *rettv)
14761{
14762 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14763 && isnan(argvars[0].vval.v_float);
14764}
14765#endif
14766
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014767static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014768
14769/*
14770 * Turn a dict into a list:
14771 * "what" == 0: list of keys
14772 * "what" == 1: list of values
14773 * "what" == 2: list of items
14774 */
14775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014776dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014777{
Bram Moolenaar33570922005-01-25 22:26:29 +000014778 list_T *l2;
14779 dictitem_T *di;
14780 hashitem_T *hi;
14781 listitem_T *li;
14782 listitem_T *li2;
14783 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014784 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014785
Bram Moolenaar8c711452005-01-14 21:53:12 +000014786 if (argvars[0].v_type != VAR_DICT)
14787 {
14788 EMSG(_(e_dictreq));
14789 return;
14790 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014791 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014792 return;
14793
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014794 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014795 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014796
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014797 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014798 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014799 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014800 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014801 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014802 --todo;
14803 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014804
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014805 li = listitem_alloc();
14806 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014807 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014808 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014809
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014810 if (what == 0)
14811 {
14812 /* keys() */
14813 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014814 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014815 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14816 }
14817 else if (what == 1)
14818 {
14819 /* values() */
14820 copy_tv(&di->di_tv, &li->li_tv);
14821 }
14822 else
14823 {
14824 /* items() */
14825 l2 = list_alloc();
14826 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014827 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014828 li->li_tv.vval.v_list = l2;
14829 if (l2 == NULL)
14830 break;
14831 ++l2->lv_refcount;
14832
14833 li2 = listitem_alloc();
14834 if (li2 == NULL)
14835 break;
14836 list_append(l2, li2);
14837 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014838 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014839 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14840
14841 li2 = listitem_alloc();
14842 if (li2 == NULL)
14843 break;
14844 list_append(l2, li2);
14845 copy_tv(&di->di_tv, &li2->li_tv);
14846 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014847 }
14848 }
14849}
14850
14851/*
14852 * "items(dict)" function
14853 */
14854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014855f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014856{
14857 dict_list(argvars, rettv, 2);
14858}
14859
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014860#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014861/*
14862 * Get the job from the argument.
14863 * Returns NULL if the job is invalid.
14864 */
14865 static job_T *
14866get_job_arg(typval_T *tv)
14867{
14868 job_T *job;
14869
14870 if (tv->v_type != VAR_JOB)
14871 {
14872 EMSG2(_(e_invarg2), get_tv_string(tv));
14873 return NULL;
14874 }
14875 job = tv->vval.v_job;
14876
14877 if (job == NULL)
14878 EMSG(_("E916: not a valid job"));
14879 return job;
14880}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014881
14882# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014883/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014884 * "job_getchannel()" function
14885 */
14886 static void
14887f_job_getchannel(typval_T *argvars, typval_T *rettv)
14888{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014889 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014890
Bram Moolenaar65edff82016-02-21 16:40:11 +010014891 if (job != NULL)
14892 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014893 rettv->v_type = VAR_CHANNEL;
14894 rettv->vval.v_channel = job->jv_channel;
14895 if (job->jv_channel != NULL)
14896 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014897 }
14898}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014899# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014900
14901/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014902 * "job_setoptions()" function
14903 */
14904 static void
14905f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14906{
14907 job_T *job = get_job_arg(&argvars[0]);
14908 jobopt_T opt;
14909
14910 if (job == NULL)
14911 return;
14912 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014913 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014914 return;
14915 job_set_options(job, &opt);
14916}
14917
14918/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014919 * "job_start()" function
14920 */
14921 static void
14922f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14923{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014924 job_T *job;
14925 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014926#if defined(UNIX)
14927# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014928 char **argv = NULL;
14929 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014930#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014931 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014932#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014933 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014934
14935 rettv->v_type = VAR_JOB;
14936 job = job_alloc();
14937 rettv->vval.v_job = job;
14938 if (job == NULL)
14939 return;
14940
14941 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014942
14943 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014944 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014945 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014946 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014947 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
14948 + JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014949 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010014950 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014951
Bram Moolenaar835dc632016-02-07 14:27:38 +010014952#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014953 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014954#endif
14955
14956 if (argvars[0].v_type == VAR_STRING)
14957 {
14958 /* Command is a string. */
14959 cmd = argvars[0].vval.v_string;
14960#ifdef USE_ARGV
14961 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14962 return;
14963 argv[argc] = NULL;
14964#endif
14965 }
14966 else if (argvars[0].v_type != VAR_LIST
14967 || argvars[0].vval.v_list == NULL
14968 || argvars[0].vval.v_list->lv_len < 1)
14969 {
14970 EMSG(_(e_invarg));
14971 return;
14972 }
14973 else
14974 {
14975 list_T *l = argvars[0].vval.v_list;
14976 listitem_T *li;
14977 char_u *s;
14978
14979#ifdef USE_ARGV
14980 /* Pass argv[] to mch_call_shell(). */
14981 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14982 if (argv == NULL)
14983 return;
14984#endif
14985 for (li = l->lv_first; li != NULL; li = li->li_next)
14986 {
14987 s = get_tv_string_chk(&li->li_tv);
14988 if (s == NULL)
14989 goto theend;
14990#ifdef USE_ARGV
14991 argv[argc++] = (char *)s;
14992#else
14993 if (li != l->lv_first)
14994 {
14995 s = vim_strsave_shellescape(s, FALSE, TRUE);
14996 if (s == NULL)
14997 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014998 ga_concat(&ga, s);
14999 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015000 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015001 else
15002 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015003 if (li->li_next != NULL)
15004 ga_append(&ga, ' ');
15005#endif
15006 }
15007#ifdef USE_ARGV
15008 argv[argc] = NULL;
15009#else
15010 cmd = ga.ga_data;
15011#endif
15012 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015013
Bram Moolenaar835dc632016-02-07 14:27:38 +010015014#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015015# ifdef FEAT_CHANNEL
15016 if (ch_log_active())
15017 {
15018 garray_T ga;
15019 int i;
15020
15021 ga_init2(&ga, (int)sizeof(char), 200);
15022 for (i = 0; i < argc; ++i)
15023 {
15024 if (i > 0)
15025 ga_concat(&ga, (char_u *)" ");
15026 ga_concat(&ga, (char_u *)argv[i]);
15027 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015028 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015029 ga_clear(&ga);
15030 }
15031# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015032 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015033#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015034# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015035 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015036# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015037 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015038#endif
15039
15040theend:
15041#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015042 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015043#else
15044 vim_free(ga.ga_data);
15045#endif
15046}
15047
15048/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015049 * Get the status of "job" and invoke the exit callback when needed.
15050 * The returned string is not allocated.
15051 */
15052 static char *
15053job_status(job_T *job)
15054{
15055 char *result;
15056
15057 if (job->jv_status == JOB_ENDED)
15058 /* No need to check, dead is dead. */
15059 result = "dead";
15060 else if (job->jv_status == JOB_FAILED)
15061 result = "fail";
15062 else
15063 {
15064 result = mch_job_status(job);
15065# ifdef FEAT_CHANNEL
15066 if (job->jv_status == JOB_ENDED)
15067 ch_log(job->jv_channel, "Job ended");
15068# endif
15069 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15070 {
15071 typval_T argv[3];
15072 typval_T rettv;
15073 int dummy;
15074
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015075 /* invoke the exit callback; make sure the refcount is > 0 */
15076 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015077 argv[0].v_type = VAR_JOB;
15078 argv[0].vval.v_job = job;
15079 argv[1].v_type = VAR_NUMBER;
15080 argv[1].vval.v_number = job->jv_exitval;
15081 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15082 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15083 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015084 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015085 }
15086 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15087 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015088 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015089 * freed. Careful: caller must not use "job" after this! */
15090 job_free(job);
15091 }
15092 }
15093 return result;
15094}
15095
15096/*
15097 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15098 */
15099 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015100job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015101{
15102 static time_t last_check = 0;
15103 time_t now;
15104 job_T *job;
15105 job_T *next;
15106
15107 /* Only do this once in 10 seconds. */
15108 now = time(NULL);
15109 if (last_check + 10 < now)
15110 {
15111 last_check = now;
15112 for (job = first_job; job != NULL; job = next)
15113 {
15114 next = job->jv_next;
15115 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15116 job_status(job); /* may free "job" */
15117 }
15118 }
15119}
15120
15121/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015122 * "job_status()" function
15123 */
15124 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015125f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015126{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015127 job_T *job = get_job_arg(&argvars[0]);
15128 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015129
Bram Moolenaar65edff82016-02-21 16:40:11 +010015130 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015131 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015132 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015133 rettv->v_type = VAR_STRING;
15134 rettv->vval.v_string = vim_strsave((char_u *)result);
15135 }
15136}
15137
15138/*
15139 * "job_stop()" function
15140 */
15141 static void
15142f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15143{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015144 job_T *job = get_job_arg(&argvars[0]);
15145
15146 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015147 {
15148 char_u *arg;
15149
15150 if (argvars[1].v_type == VAR_UNKNOWN)
15151 arg = (char_u *)"";
15152 else
15153 {
15154 arg = get_tv_string_chk(&argvars[1]);
15155 if (arg == NULL)
15156 {
15157 EMSG(_(e_invarg));
15158 return;
15159 }
15160 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015161 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015162 rettv->vval.v_number = 0;
15163 else
15164 rettv->vval.v_number = 1;
15165 }
15166}
15167#endif
15168
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015170 * "join()" function
15171 */
15172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015173f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015174{
15175 garray_T ga;
15176 char_u *sep;
15177
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015178 if (argvars[0].v_type != VAR_LIST)
15179 {
15180 EMSG(_(e_listreq));
15181 return;
15182 }
15183 if (argvars[0].vval.v_list == NULL)
15184 return;
15185 if (argvars[1].v_type == VAR_UNKNOWN)
15186 sep = (char_u *)" ";
15187 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015188 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015189
15190 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015191
15192 if (sep != NULL)
15193 {
15194 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015195 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015196 ga_append(&ga, NUL);
15197 rettv->vval.v_string = (char_u *)ga.ga_data;
15198 }
15199 else
15200 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015201}
15202
15203/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015204 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015205 */
15206 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015207f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015208{
15209 js_read_T reader;
15210
15211 reader.js_buf = get_tv_string(&argvars[0]);
15212 reader.js_fill = NULL;
15213 reader.js_used = 0;
15214 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15215 EMSG(_(e_invarg));
15216}
15217
15218/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015219 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015220 */
15221 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015222f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015223{
15224 rettv->v_type = VAR_STRING;
15225 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15226}
15227
15228/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015229 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015230 */
15231 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015232f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015233{
15234 js_read_T reader;
15235
15236 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015237 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015238 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015239 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015240 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015241}
15242
15243/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015244 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015245 */
15246 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015247f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015248{
15249 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015250 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015251}
15252
15253/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015254 * "keys()" function
15255 */
15256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015257f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015258{
15259 dict_list(argvars, rettv, 0);
15260}
15261
15262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 * "last_buffer_nr()" function.
15264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015266f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267{
15268 int n = 0;
15269 buf_T *buf;
15270
15271 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15272 if (n < buf->b_fnum)
15273 n = buf->b_fnum;
15274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015275 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015276}
15277
15278/*
15279 * "len()" function
15280 */
15281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015282f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015283{
15284 switch (argvars[0].v_type)
15285 {
15286 case VAR_STRING:
15287 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015288 rettv->vval.v_number = (varnumber_T)STRLEN(
15289 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015290 break;
15291 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015292 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015293 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015294 case VAR_DICT:
15295 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15296 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015297 case VAR_UNKNOWN:
15298 case VAR_SPECIAL:
15299 case VAR_FLOAT:
15300 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015301 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015302 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015303 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015304 break;
15305 }
15306}
15307
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015308static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015309
15310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015311libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015312{
15313#ifdef FEAT_LIBCALL
15314 char_u *string_in;
15315 char_u **string_result;
15316 int nr_result;
15317#endif
15318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015319 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015320 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015321 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015322
15323 if (check_restricted() || check_secure())
15324 return;
15325
15326#ifdef FEAT_LIBCALL
15327 /* The first two args must be strings, otherwise its meaningless */
15328 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15329 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015330 string_in = NULL;
15331 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015332 string_in = argvars[2].vval.v_string;
15333 if (type == VAR_NUMBER)
15334 string_result = NULL;
15335 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015336 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015337 if (mch_libcall(argvars[0].vval.v_string,
15338 argvars[1].vval.v_string,
15339 string_in,
15340 argvars[2].vval.v_number,
15341 string_result,
15342 &nr_result) == OK
15343 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015344 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015345 }
15346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347}
15348
15349/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015350 * "libcall()" function
15351 */
15352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015353f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015354{
15355 libcall_common(argvars, rettv, VAR_STRING);
15356}
15357
15358/*
15359 * "libcallnr()" function
15360 */
15361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015362f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015363{
15364 libcall_common(argvars, rettv, VAR_NUMBER);
15365}
15366
15367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 * "line(string)" function
15369 */
15370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015371f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372{
15373 linenr_T lnum = 0;
15374 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015375 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015377 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378 if (fp != NULL)
15379 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015380 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381}
15382
15383/*
15384 * "line2byte(lnum)" function
15385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015387f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388{
15389#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391#else
15392 linenr_T lnum;
15393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015396 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015398 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15399 if (rettv->vval.v_number >= 0)
15400 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401#endif
15402}
15403
15404/*
15405 * "lispindent(lnum)" function
15406 */
15407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015408f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409{
15410#ifdef FEAT_LISP
15411 pos_T pos;
15412 linenr_T lnum;
15413
15414 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15417 {
15418 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015419 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 curwin->w_cursor = pos;
15421 }
15422 else
15423#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015424 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425}
15426
15427/*
15428 * "localtime()" function
15429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015431f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015433 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434}
15435
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015436static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437
15438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015439get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440{
15441 char_u *keys;
15442 char_u *which;
15443 char_u buf[NUMBUFLEN];
15444 char_u *keys_buf = NULL;
15445 char_u *rhs;
15446 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015447 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015448 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015449 mapblock_T *mp;
15450 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451
15452 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015453 rettv->v_type = VAR_STRING;
15454 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015456 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 if (*keys == NUL)
15458 return;
15459
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015460 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015462 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015463 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015464 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015465 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015466 if (argvars[3].v_type != VAR_UNKNOWN)
15467 get_dict = get_tv_number(&argvars[3]);
15468 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 else
15471 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015472 if (which == NULL)
15473 return;
15474
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475 mode = get_map_mode(&which, 0);
15476
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015477 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015478 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015480
15481 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015483 /* Return a string. */
15484 if (rhs != NULL)
15485 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486
Bram Moolenaarbd743252010-10-20 21:23:33 +020015487 }
15488 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15489 {
15490 /* Return a dictionary. */
15491 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15492 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15493 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494
Bram Moolenaarbd743252010-10-20 21:23:33 +020015495 dict_add_nr_str(dict, "lhs", 0L, lhs);
15496 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15497 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15498 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15499 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15500 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15501 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015502 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015503 dict_add_nr_str(dict, "mode", 0L, mapmode);
15504
15505 vim_free(lhs);
15506 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 }
15508}
15509
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015510#ifdef FEAT_FLOAT
15511/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015512 * "log()" function
15513 */
15514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015515f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015516{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015517 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015518
15519 rettv->v_type = VAR_FLOAT;
15520 if (get_float_arg(argvars, &f) == OK)
15521 rettv->vval.v_float = log(f);
15522 else
15523 rettv->vval.v_float = 0.0;
15524}
15525
15526/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015527 * "log10()" function
15528 */
15529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015530f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015531{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015532 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015533
15534 rettv->v_type = VAR_FLOAT;
15535 if (get_float_arg(argvars, &f) == OK)
15536 rettv->vval.v_float = log10(f);
15537 else
15538 rettv->vval.v_float = 0.0;
15539}
15540#endif
15541
Bram Moolenaar1dced572012-04-05 16:54:08 +020015542#ifdef FEAT_LUA
15543/*
15544 * "luaeval()" function
15545 */
15546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015547f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015548{
15549 char_u *str;
15550 char_u buf[NUMBUFLEN];
15551
15552 str = get_tv_string_buf(&argvars[0], buf);
15553 do_luaeval(str, argvars + 1, rettv);
15554}
15555#endif
15556
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015558 * "map()" function
15559 */
15560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015561f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015562{
15563 filter_map(argvars, rettv, TRUE);
15564}
15565
15566/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015567 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 */
15569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015570f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015572 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573}
15574
15575/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015576 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 */
15578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015579f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015581 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582}
15583
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015584static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585
15586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015587find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015589 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015590 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015591 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 char_u *pat;
15593 regmatch_T regmatch;
15594 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015595 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596 char_u *save_cpo;
15597 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015598 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015599 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015600 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015601 list_T *l = NULL;
15602 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015603 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015604 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605
15606 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15607 save_cpo = p_cpo;
15608 p_cpo = (char_u *)"";
15609
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015610 rettv->vval.v_number = -1;
15611 if (type == 3)
15612 {
15613 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015614 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015615 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015616 }
15617 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015619 rettv->v_type = VAR_STRING;
15620 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015623 if (argvars[0].v_type == VAR_LIST)
15624 {
15625 if ((l = argvars[0].vval.v_list) == NULL)
15626 goto theend;
15627 li = l->lv_first;
15628 }
15629 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015630 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015631 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015632 len = (long)STRLEN(str);
15633 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015635 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15636 if (pat == NULL)
15637 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015638
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015639 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015641 int error = FALSE;
15642
15643 start = get_tv_number_chk(&argvars[2], &error);
15644 if (error)
15645 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015646 if (l != NULL)
15647 {
15648 li = list_find(l, start);
15649 if (li == NULL)
15650 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015651 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015652 }
15653 else
15654 {
15655 if (start < 0)
15656 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015657 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015658 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015659 /* When "count" argument is there ignore matches before "start",
15660 * otherwise skip part of the string. Differs when pattern is "^"
15661 * or "\<". */
15662 if (argvars[3].v_type != VAR_UNKNOWN)
15663 startcol = start;
15664 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015665 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015666 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015667 len -= start;
15668 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015669 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015670
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015671 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015672 nth = get_tv_number_chk(&argvars[3], &error);
15673 if (error)
15674 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 }
15676
15677 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15678 if (regmatch.regprog != NULL)
15679 {
15680 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015681
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015682 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015683 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015684 if (l != NULL)
15685 {
15686 if (li == NULL)
15687 {
15688 match = FALSE;
15689 break;
15690 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015691 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015692 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015693 if (str == NULL)
15694 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015695 }
15696
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015697 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015698
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015699 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015700 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015701 if (l == NULL && !match)
15702 break;
15703
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015704 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015705 if (l != NULL)
15706 {
15707 li = li->li_next;
15708 ++idx;
15709 }
15710 else
15711 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015712#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015713 startcol = (colnr_T)(regmatch.startp[0]
15714 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015715#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015716 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015717#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015718 if (startcol > (colnr_T)len
15719 || str + startcol <= regmatch.startp[0])
15720 {
15721 match = FALSE;
15722 break;
15723 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015724 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015725 }
15726
15727 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015729 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015730 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015731 int i;
15732
15733 /* return list with matched string and submatches */
15734 for (i = 0; i < NSUBEXP; ++i)
15735 {
15736 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015737 {
15738 if (list_append_string(rettv->vval.v_list,
15739 (char_u *)"", 0) == FAIL)
15740 break;
15741 }
15742 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015743 regmatch.startp[i],
15744 (int)(regmatch.endp[i] - regmatch.startp[i]))
15745 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015746 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015747 }
15748 }
15749 else if (type == 2)
15750 {
15751 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015752 if (l != NULL)
15753 copy_tv(&li->li_tv, rettv);
15754 else
15755 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015757 }
15758 else if (l != NULL)
15759 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 else
15761 {
15762 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015763 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 (varnumber_T)(regmatch.startp[0] - str);
15765 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015766 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015768 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 }
15770 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015771 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 }
15773
15774theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015775 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 p_cpo = save_cpo;
15777}
15778
15779/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780 * "match()" function
15781 */
15782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015783f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015784{
15785 find_some_match(argvars, rettv, 1);
15786}
15787
15788/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015789 * "matchadd()" function
15790 */
15791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015792f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015793{
15794#ifdef FEAT_SEARCH_EXTRA
15795 char_u buf[NUMBUFLEN];
15796 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15797 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15798 int prio = 10; /* default priority */
15799 int id = -1;
15800 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015801 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015802
15803 rettv->vval.v_number = -1;
15804
15805 if (grp == NULL || pat == NULL)
15806 return;
15807 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015808 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015809 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015810 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015811 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015812 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015813 if (argvars[4].v_type != VAR_UNKNOWN)
15814 {
15815 if (argvars[4].v_type != VAR_DICT)
15816 {
15817 EMSG(_(e_dictreq));
15818 return;
15819 }
15820 if (dict_find(argvars[4].vval.v_dict,
15821 (char_u *)"conceal", -1) != NULL)
15822 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15823 (char_u *)"conceal", FALSE);
15824 }
15825 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015826 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015827 if (error == TRUE)
15828 return;
15829 if (id >= 1 && id <= 3)
15830 {
15831 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15832 return;
15833 }
15834
Bram Moolenaar6561d522015-07-21 15:48:27 +020015835 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15836 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015837#endif
15838}
15839
15840/*
15841 * "matchaddpos()" function
15842 */
15843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015844f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015845{
15846#ifdef FEAT_SEARCH_EXTRA
15847 char_u buf[NUMBUFLEN];
15848 char_u *group;
15849 int prio = 10;
15850 int id = -1;
15851 int error = FALSE;
15852 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015853 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015854
15855 rettv->vval.v_number = -1;
15856
15857 group = get_tv_string_buf_chk(&argvars[0], buf);
15858 if (group == NULL)
15859 return;
15860
15861 if (argvars[1].v_type != VAR_LIST)
15862 {
15863 EMSG2(_(e_listarg), "matchaddpos()");
15864 return;
15865 }
15866 l = argvars[1].vval.v_list;
15867 if (l == NULL)
15868 return;
15869
15870 if (argvars[2].v_type != VAR_UNKNOWN)
15871 {
15872 prio = get_tv_number_chk(&argvars[2], &error);
15873 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015874 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015875 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015876 if (argvars[4].v_type != VAR_UNKNOWN)
15877 {
15878 if (argvars[4].v_type != VAR_DICT)
15879 {
15880 EMSG(_(e_dictreq));
15881 return;
15882 }
15883 if (dict_find(argvars[4].vval.v_dict,
15884 (char_u *)"conceal", -1) != NULL)
15885 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15886 (char_u *)"conceal", FALSE);
15887 }
15888 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015889 }
15890 if (error == TRUE)
15891 return;
15892
15893 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15894 if (id == 1 || id == 2)
15895 {
15896 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15897 return;
15898 }
15899
Bram Moolenaar6561d522015-07-21 15:48:27 +020015900 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15901 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015902#endif
15903}
15904
15905/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015906 * "matcharg()" function
15907 */
15908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015909f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015910{
15911 if (rettv_list_alloc(rettv) == OK)
15912 {
15913#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015914 int id = get_tv_number(&argvars[0]);
15915 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015916
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015917 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015918 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015919 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15920 {
15921 list_append_string(rettv->vval.v_list,
15922 syn_id2name(m->hlg_id), -1);
15923 list_append_string(rettv->vval.v_list, m->pattern, -1);
15924 }
15925 else
15926 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015927 list_append_string(rettv->vval.v_list, NULL, -1);
15928 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015929 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015930 }
15931#endif
15932 }
15933}
15934
15935/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015936 * "matchdelete()" function
15937 */
15938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015939f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015940{
15941#ifdef FEAT_SEARCH_EXTRA
15942 rettv->vval.v_number = match_delete(curwin,
15943 (int)get_tv_number(&argvars[0]), TRUE);
15944#endif
15945}
15946
15947/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015948 * "matchend()" function
15949 */
15950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015951f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015952{
15953 find_some_match(argvars, rettv, 0);
15954}
15955
15956/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015957 * "matchlist()" function
15958 */
15959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015960f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015961{
15962 find_some_match(argvars, rettv, 3);
15963}
15964
15965/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015966 * "matchstr()" function
15967 */
15968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015969f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015970{
15971 find_some_match(argvars, rettv, 2);
15972}
15973
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015974static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015975
15976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015977max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015978{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015979 long n = 0;
15980 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015981 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015982
15983 if (argvars[0].v_type == VAR_LIST)
15984 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015985 list_T *l;
15986 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015987
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015988 l = argvars[0].vval.v_list;
15989 if (l != NULL)
15990 {
15991 li = l->lv_first;
15992 if (li != NULL)
15993 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015994 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015995 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015996 {
15997 li = li->li_next;
15998 if (li == NULL)
15999 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016000 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016001 if (domax ? i > n : i < n)
16002 n = i;
16003 }
16004 }
16005 }
16006 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016007 else if (argvars[0].v_type == VAR_DICT)
16008 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016009 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016010 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016011 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016012 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016013
16014 d = argvars[0].vval.v_dict;
16015 if (d != NULL)
16016 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016017 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016018 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016019 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016020 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016021 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016022 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016023 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016024 if (first)
16025 {
16026 n = i;
16027 first = FALSE;
16028 }
16029 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016030 n = i;
16031 }
16032 }
16033 }
16034 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016035 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016036 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016037 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016038}
16039
16040/*
16041 * "max()" function
16042 */
16043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016044f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016045{
16046 max_min(argvars, rettv, TRUE);
16047}
16048
16049/*
16050 * "min()" function
16051 */
16052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016053f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016054{
16055 max_min(argvars, rettv, FALSE);
16056}
16057
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016058static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016059
16060/*
16061 * Create the directory in which "dir" is located, and higher levels when
16062 * needed.
16063 */
16064 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016065mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016066{
16067 char_u *p;
16068 char_u *updir;
16069 int r = FAIL;
16070
16071 /* Get end of directory name in "dir".
16072 * We're done when it's "/" or "c:/". */
16073 p = gettail_sep(dir);
16074 if (p <= get_past_head(dir))
16075 return OK;
16076
16077 /* If the directory exists we're done. Otherwise: create it.*/
16078 updir = vim_strnsave(dir, (int)(p - dir));
16079 if (updir == NULL)
16080 return FAIL;
16081 if (mch_isdir(updir))
16082 r = OK;
16083 else if (mkdir_recurse(updir, prot) == OK)
16084 r = vim_mkdir_emsg(updir, prot);
16085 vim_free(updir);
16086 return r;
16087}
16088
16089#ifdef vim_mkdir
16090/*
16091 * "mkdir()" function
16092 */
16093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016094f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016095{
16096 char_u *dir;
16097 char_u buf[NUMBUFLEN];
16098 int prot = 0755;
16099
16100 rettv->vval.v_number = FAIL;
16101 if (check_restricted() || check_secure())
16102 return;
16103
16104 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016105 if (*dir == NUL)
16106 rettv->vval.v_number = FAIL;
16107 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016108 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016109 if (*gettail(dir) == NUL)
16110 /* remove trailing slashes */
16111 *gettail_sep(dir) = NUL;
16112
16113 if (argvars[1].v_type != VAR_UNKNOWN)
16114 {
16115 if (argvars[2].v_type != VAR_UNKNOWN)
16116 prot = get_tv_number_chk(&argvars[2], NULL);
16117 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16118 mkdir_recurse(dir, prot);
16119 }
16120 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016121 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016122}
16123#endif
16124
Bram Moolenaar0d660222005-01-07 21:51:51 +000016125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 * "mode()" function
16127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016129f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016131 char_u buf[3];
16132
16133 buf[1] = NUL;
16134 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 if (VIsual_active)
16137 {
16138 if (VIsual_select)
16139 buf[0] = VIsual_mode + 's' - 'v';
16140 else
16141 buf[0] = VIsual_mode;
16142 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016143 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016144 || State == CONFIRM)
16145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016147 if (State == ASKMORE)
16148 buf[1] = 'm';
16149 else if (State == CONFIRM)
16150 buf[1] = '?';
16151 }
16152 else if (State == EXTERNCMD)
16153 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 else if (State & INSERT)
16155 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016156#ifdef FEAT_VREPLACE
16157 if (State & VREPLACE_FLAG)
16158 {
16159 buf[0] = 'R';
16160 buf[1] = 'v';
16161 }
16162 else
16163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 if (State & REPLACE_FLAG)
16165 buf[0] = 'R';
16166 else
16167 buf[0] = 'i';
16168 }
16169 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016172 if (exmode_active)
16173 buf[1] = 'v';
16174 }
16175 else if (exmode_active)
16176 {
16177 buf[0] = 'c';
16178 buf[1] = 'e';
16179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016183 if (finish_op)
16184 buf[1] = 'o';
16185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016187 /* Clear out the minor mode when the argument is not a non-zero number or
16188 * non-empty string. */
16189 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016190 buf[1] = NUL;
16191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016192 rettv->vval.v_string = vim_strsave(buf);
16193 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194}
16195
Bram Moolenaar429fa852013-04-15 12:27:36 +020016196#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016197/*
16198 * "mzeval()" function
16199 */
16200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016201f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016202{
16203 char_u *str;
16204 char_u buf[NUMBUFLEN];
16205
16206 str = get_tv_string_buf(&argvars[0], buf);
16207 do_mzeval(str, rettv);
16208}
Bram Moolenaar75676462013-01-30 14:55:42 +010016209
16210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016211mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016212{
16213 typval_T argvars[3];
16214
16215 argvars[0].v_type = VAR_STRING;
16216 argvars[0].vval.v_string = name;
16217 copy_tv(args, &argvars[1]);
16218 argvars[2].v_type = VAR_UNKNOWN;
16219 f_call(argvars, rettv);
16220 clear_tv(&argvars[1]);
16221}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016222#endif
16223
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016225 * "nextnonblank()" function
16226 */
16227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016228f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229{
16230 linenr_T lnum;
16231
16232 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016234 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016235 {
16236 lnum = 0;
16237 break;
16238 }
16239 if (*skipwhite(ml_get(lnum)) != NUL)
16240 break;
16241 }
16242 rettv->vval.v_number = lnum;
16243}
16244
16245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 * "nr2char()" function
16247 */
16248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016249f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250{
16251 char_u buf[NUMBUFLEN];
16252
16253#ifdef FEAT_MBYTE
16254 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016255 {
16256 int utf8 = 0;
16257
16258 if (argvars[1].v_type != VAR_UNKNOWN)
16259 utf8 = get_tv_number_chk(&argvars[1], NULL);
16260 if (utf8)
16261 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16262 else
16263 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265 else
16266#endif
16267 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 buf[1] = NUL;
16270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016271 rettv->v_type = VAR_STRING;
16272 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016273}
16274
16275/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016276 * "or(expr, expr)" function
16277 */
16278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016279f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016280{
16281 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16282 | get_tv_number_chk(&argvars[1], NULL);
16283}
16284
16285/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016286 * "pathshorten()" function
16287 */
16288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016289f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016290{
16291 char_u *p;
16292
16293 rettv->v_type = VAR_STRING;
16294 p = get_tv_string_chk(&argvars[0]);
16295 if (p == NULL)
16296 rettv->vval.v_string = NULL;
16297 else
16298 {
16299 p = vim_strsave(p);
16300 rettv->vval.v_string = p;
16301 if (p != NULL)
16302 shorten_dir(p);
16303 }
16304}
16305
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016306#ifdef FEAT_PERL
16307/*
16308 * "perleval()" function
16309 */
16310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016311f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016312{
16313 char_u *str;
16314 char_u buf[NUMBUFLEN];
16315
16316 str = get_tv_string_buf(&argvars[0], buf);
16317 do_perleval(str, rettv);
16318}
16319#endif
16320
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016321#ifdef FEAT_FLOAT
16322/*
16323 * "pow()" function
16324 */
16325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016326f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016327{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016328 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016329
16330 rettv->v_type = VAR_FLOAT;
16331 if (get_float_arg(argvars, &fx) == OK
16332 && get_float_arg(&argvars[1], &fy) == OK)
16333 rettv->vval.v_float = pow(fx, fy);
16334 else
16335 rettv->vval.v_float = 0.0;
16336}
16337#endif
16338
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016339/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016340 * "prevnonblank()" function
16341 */
16342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016343f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344{
16345 linenr_T lnum;
16346
16347 lnum = get_tv_lnum(argvars);
16348 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16349 lnum = 0;
16350 else
16351 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16352 --lnum;
16353 rettv->vval.v_number = lnum;
16354}
16355
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016356/* This dummy va_list is here because:
16357 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16358 * - locally in the function results in a "used before set" warning
16359 * - using va_start() to initialize it gives "function with fixed args" error */
16360static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016361
Bram Moolenaar8c711452005-01-14 21:53:12 +000016362/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016363 * "printf()" function
16364 */
16365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016366f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016367{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016368 char_u buf[NUMBUFLEN];
16369 int len;
16370 char_u *s;
16371 int saved_did_emsg = did_emsg;
16372 char *fmt;
16373
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016374 rettv->v_type = VAR_STRING;
16375 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016376
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016377 /* Get the required length, allocate the buffer and do it for real. */
16378 did_emsg = FALSE;
16379 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16380 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16381 if (!did_emsg)
16382 {
16383 s = alloc(len + 1);
16384 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016385 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016386 rettv->vval.v_string = s;
16387 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016388 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016389 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016390 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016391}
16392
16393/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016394 * "pumvisible()" function
16395 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016397f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016398{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016399#ifdef FEAT_INS_EXPAND
16400 if (pum_visible())
16401 rettv->vval.v_number = 1;
16402#endif
16403}
16404
Bram Moolenaardb913952012-06-29 12:54:53 +020016405#ifdef FEAT_PYTHON3
16406/*
16407 * "py3eval()" function
16408 */
16409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016410f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016411{
16412 char_u *str;
16413 char_u buf[NUMBUFLEN];
16414
16415 str = get_tv_string_buf(&argvars[0], buf);
16416 do_py3eval(str, rettv);
16417}
16418#endif
16419
16420#ifdef FEAT_PYTHON
16421/*
16422 * "pyeval()" function
16423 */
16424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016425f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016426{
16427 char_u *str;
16428 char_u buf[NUMBUFLEN];
16429
16430 str = get_tv_string_buf(&argvars[0], buf);
16431 do_pyeval(str, rettv);
16432}
16433#endif
16434
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016435/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016436 * "range()" function
16437 */
16438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016439f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016440{
16441 long start;
16442 long end;
16443 long stride = 1;
16444 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016445 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016447 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016448 if (argvars[1].v_type == VAR_UNKNOWN)
16449 {
16450 end = start - 1;
16451 start = 0;
16452 }
16453 else
16454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016455 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016456 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016457 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016458 }
16459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016460 if (error)
16461 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016462 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016463 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016464 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016465 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016466 else
16467 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016468 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016469 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016470 if (list_append_number(rettv->vval.v_list,
16471 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016472 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016473 }
16474}
16475
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016476/*
16477 * "readfile()" function
16478 */
16479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016480f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016481{
16482 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016483 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016484 char_u *fname;
16485 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016486 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16487 int io_size = sizeof(buf);
16488 int readlen; /* size of last fread() */
16489 char_u *prev = NULL; /* previously read bytes, if any */
16490 long prevlen = 0; /* length of data in prev */
16491 long prevsize = 0; /* size of prev buffer */
16492 long maxline = MAXLNUM;
16493 long cnt = 0;
16494 char_u *p; /* position in buf */
16495 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016496
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016497 if (argvars[1].v_type != VAR_UNKNOWN)
16498 {
16499 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16500 binary = TRUE;
16501 if (argvars[2].v_type != VAR_UNKNOWN)
16502 maxline = get_tv_number(&argvars[2]);
16503 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016504
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016505 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016506 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016507
16508 /* Always open the file in binary mode, library functions have a mind of
16509 * their own about CR-LF conversion. */
16510 fname = get_tv_string(&argvars[0]);
16511 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16512 {
16513 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16514 return;
16515 }
16516
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016517 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016518 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016519 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016520
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016521 /* This for loop processes what was read, but is also entered at end
16522 * of file so that either:
16523 * - an incomplete line gets written
16524 * - a "binary" file gets an empty line at the end if it ends in a
16525 * newline. */
16526 for (p = buf, start = buf;
16527 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16528 ++p)
16529 {
16530 if (*p == '\n' || readlen <= 0)
16531 {
16532 listitem_T *li;
16533 char_u *s = NULL;
16534 long_u len = p - start;
16535
16536 /* Finished a line. Remove CRs before NL. */
16537 if (readlen > 0 && !binary)
16538 {
16539 while (len > 0 && start[len - 1] == '\r')
16540 --len;
16541 /* removal may cross back to the "prev" string */
16542 if (len == 0)
16543 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16544 --prevlen;
16545 }
16546 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016547 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016548 else
16549 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016550 /* Change "prev" buffer to be the right size. This way
16551 * the bytes are only copied once, and very long lines are
16552 * allocated only once. */
16553 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016554 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016555 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016557 prev = NULL; /* the list will own the string */
16558 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016559 }
16560 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016561 if (s == NULL)
16562 {
16563 do_outofmem_msg((long_u) prevlen + len + 1);
16564 failed = TRUE;
16565 break;
16566 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016567
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016568 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016569 {
16570 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016571 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016572 break;
16573 }
16574 li->li_tv.v_type = VAR_STRING;
16575 li->li_tv.v_lock = 0;
16576 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016577 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016578
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016579 start = p + 1; /* step over newline */
16580 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016581 break;
16582 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016583 else if (*p == NUL)
16584 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016585#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016586 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16587 * when finding the BF and check the previous two bytes. */
16588 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016589 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016590 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16591 * + 1, these may be in the "prev" string. */
16592 char_u back1 = p >= buf + 1 ? p[-1]
16593 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16594 char_u back2 = p >= buf + 2 ? p[-2]
16595 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16596 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016597
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016598 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016599 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016600 char_u *dest = p - 2;
16601
16602 /* Usually a BOM is at the beginning of a file, and so at
16603 * the beginning of a line; then we can just step over it.
16604 */
16605 if (start == dest)
16606 start = p + 1;
16607 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016608 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016609 /* have to shuffle buf to close gap */
16610 int adjust_prevlen = 0;
16611
16612 if (dest < buf)
16613 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016614 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016615 dest = buf;
16616 }
16617 if (readlen > p - buf + 1)
16618 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16619 readlen -= 3 - adjust_prevlen;
16620 prevlen -= adjust_prevlen;
16621 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016622 }
16623 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016624 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016625#endif
16626 } /* for */
16627
16628 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16629 break;
16630 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016631 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016632 /* There's part of a line in buf, store it in "prev". */
16633 if (p - start + prevlen >= prevsize)
16634 {
16635 /* need bigger "prev" buffer */
16636 char_u *newprev;
16637
16638 /* A common use case is ordinary text files and "prev" gets a
16639 * fragment of a line, so the first allocation is made
16640 * small, to avoid repeatedly 'allocing' large and
16641 * 'reallocing' small. */
16642 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016643 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016644 else
16645 {
16646 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016647 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016648 prevsize = grow50pc > growmin ? grow50pc : growmin;
16649 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016650 newprev = prev == NULL ? alloc(prevsize)
16651 : vim_realloc(prev, prevsize);
16652 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016653 {
16654 do_outofmem_msg((long_u)prevsize);
16655 failed = TRUE;
16656 break;
16657 }
16658 prev = newprev;
16659 }
16660 /* Add the line part to end of "prev". */
16661 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016662 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016663 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016664 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016665
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016666 /*
16667 * For a negative line count use only the lines at the end of the file,
16668 * free the rest.
16669 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016670 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016671 while (cnt > -maxline)
16672 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016673 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016674 --cnt;
16675 }
16676
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016677 if (failed)
16678 {
16679 list_free(rettv->vval.v_list, TRUE);
16680 /* readfile doc says an empty list is returned on error */
16681 rettv->vval.v_list = list_alloc();
16682 }
16683
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016684 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016685 fclose(fd);
16686}
16687
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016688#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016689static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016690
16691/*
16692 * Convert a List to proftime_T.
16693 * Return FAIL when there is something wrong.
16694 */
16695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016696list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016697{
16698 long n1, n2;
16699 int error = FALSE;
16700
16701 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16702 || arg->vval.v_list->lv_len != 2)
16703 return FAIL;
16704 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16705 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16706# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016707 tm->HighPart = n1;
16708 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016709# else
16710 tm->tv_sec = n1;
16711 tm->tv_usec = n2;
16712# endif
16713 return error ? FAIL : OK;
16714}
16715#endif /* FEAT_RELTIME */
16716
16717/*
16718 * "reltime()" function
16719 */
16720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016721f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016722{
16723#ifdef FEAT_RELTIME
16724 proftime_T res;
16725 proftime_T start;
16726
16727 if (argvars[0].v_type == VAR_UNKNOWN)
16728 {
16729 /* No arguments: get current time. */
16730 profile_start(&res);
16731 }
16732 else if (argvars[1].v_type == VAR_UNKNOWN)
16733 {
16734 if (list2proftime(&argvars[0], &res) == FAIL)
16735 return;
16736 profile_end(&res);
16737 }
16738 else
16739 {
16740 /* Two arguments: compute the difference. */
16741 if (list2proftime(&argvars[0], &start) == FAIL
16742 || list2proftime(&argvars[1], &res) == FAIL)
16743 return;
16744 profile_sub(&res, &start);
16745 }
16746
16747 if (rettv_list_alloc(rettv) == OK)
16748 {
16749 long n1, n2;
16750
16751# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016752 n1 = res.HighPart;
16753 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016754# else
16755 n1 = res.tv_sec;
16756 n2 = res.tv_usec;
16757# endif
16758 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16759 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16760 }
16761#endif
16762}
16763
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016764#ifdef FEAT_FLOAT
16765/*
16766 * "reltimefloat()" function
16767 */
16768 static void
16769f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16770{
16771# ifdef FEAT_RELTIME
16772 proftime_T tm;
16773# endif
16774
16775 rettv->v_type = VAR_FLOAT;
16776 rettv->vval.v_float = 0;
16777# ifdef FEAT_RELTIME
16778 if (list2proftime(&argvars[0], &tm) == OK)
16779 rettv->vval.v_float = profile_float(&tm);
16780# endif
16781}
16782#endif
16783
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016784/*
16785 * "reltimestr()" function
16786 */
16787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016788f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016789{
16790#ifdef FEAT_RELTIME
16791 proftime_T tm;
16792#endif
16793
16794 rettv->v_type = VAR_STRING;
16795 rettv->vval.v_string = NULL;
16796#ifdef FEAT_RELTIME
16797 if (list2proftime(&argvars[0], &tm) == OK)
16798 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16799#endif
16800}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016801
Bram Moolenaar0d660222005-01-07 21:51:51 +000016802#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016803static void make_connection(void);
16804static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016805
16806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016807make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016808{
16809 if (X_DISPLAY == NULL
16810# ifdef FEAT_GUI
16811 && !gui.in_use
16812# endif
16813 )
16814 {
16815 x_force_connect = TRUE;
16816 setup_term_clip();
16817 x_force_connect = FALSE;
16818 }
16819}
16820
16821 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016822check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016823{
16824 make_connection();
16825 if (X_DISPLAY == NULL)
16826 {
16827 EMSG(_("E240: No connection to Vim server"));
16828 return FAIL;
16829 }
16830 return OK;
16831}
16832#endif
16833
16834#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016835static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016836
16837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016838remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016839{
16840 char_u *server_name;
16841 char_u *keys;
16842 char_u *r = NULL;
16843 char_u buf[NUMBUFLEN];
16844# ifdef WIN32
16845 HWND w;
16846# else
16847 Window w;
16848# endif
16849
16850 if (check_restricted() || check_secure())
16851 return;
16852
16853# ifdef FEAT_X11
16854 if (check_connection() == FAIL)
16855 return;
16856# endif
16857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016858 server_name = get_tv_string_chk(&argvars[0]);
16859 if (server_name == NULL)
16860 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016861 keys = get_tv_string_buf(&argvars[1], buf);
16862# ifdef WIN32
16863 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16864# else
16865 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16866 < 0)
16867# endif
16868 {
16869 if (r != NULL)
16870 EMSG(r); /* sending worked but evaluation failed */
16871 else
16872 EMSG2(_("E241: Unable to send to %s"), server_name);
16873 return;
16874 }
16875
16876 rettv->vval.v_string = r;
16877
16878 if (argvars[2].v_type != VAR_UNKNOWN)
16879 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016880 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016881 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016882 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016884 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016885 v.di_tv.v_type = VAR_STRING;
16886 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016887 idvar = get_tv_string_chk(&argvars[2]);
16888 if (idvar != NULL)
16889 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016890 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891 }
16892}
16893#endif
16894
16895/*
16896 * "remote_expr()" function
16897 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016899f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016900{
16901 rettv->v_type = VAR_STRING;
16902 rettv->vval.v_string = NULL;
16903#ifdef FEAT_CLIENTSERVER
16904 remote_common(argvars, rettv, TRUE);
16905#endif
16906}
16907
16908/*
16909 * "remote_foreground()" function
16910 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016912f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016913{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914#ifdef FEAT_CLIENTSERVER
16915# ifdef WIN32
16916 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016917 {
16918 char_u *server_name = get_tv_string_chk(&argvars[0]);
16919
16920 if (server_name != NULL)
16921 serverForeground(server_name);
16922 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923# else
16924 /* Send a foreground() expression to the server. */
16925 argvars[1].v_type = VAR_STRING;
16926 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16927 argvars[2].v_type = VAR_UNKNOWN;
16928 remote_common(argvars, rettv, TRUE);
16929 vim_free(argvars[1].vval.v_string);
16930# endif
16931#endif
16932}
16933
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016935f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936{
16937#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016938 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939 char_u *s = NULL;
16940# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016941 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016943 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944
16945 if (check_restricted() || check_secure())
16946 {
16947 rettv->vval.v_number = -1;
16948 return;
16949 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016950 serverid = get_tv_string_chk(&argvars[0]);
16951 if (serverid == NULL)
16952 {
16953 rettv->vval.v_number = -1;
16954 return; /* type error; errmsg already given */
16955 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016957 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958 if (n == 0)
16959 rettv->vval.v_number = -1;
16960 else
16961 {
16962 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16963 rettv->vval.v_number = (s != NULL);
16964 }
16965# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 if (check_connection() == FAIL)
16967 return;
16968
16969 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016970 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971# endif
16972
16973 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016975 char_u *retvar;
16976
Bram Moolenaar33570922005-01-25 22:26:29 +000016977 v.di_tv.v_type = VAR_STRING;
16978 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016979 retvar = get_tv_string_chk(&argvars[1]);
16980 if (retvar != NULL)
16981 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016982 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983 }
16984#else
16985 rettv->vval.v_number = -1;
16986#endif
16987}
16988
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016990f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991{
16992 char_u *r = NULL;
16993
16994#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016995 char_u *serverid = get_tv_string_chk(&argvars[0]);
16996
16997 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998 {
16999# ifdef WIN32
17000 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017001 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017003 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004 if (n != 0)
17005 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17006 if (r == NULL)
17007# else
17008 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017010# endif
17011 EMSG(_("E277: Unable to read a server reply"));
17012 }
17013#endif
17014 rettv->v_type = VAR_STRING;
17015 rettv->vval.v_string = r;
17016}
17017
17018/*
17019 * "remote_send()" function
17020 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017022f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023{
17024 rettv->v_type = VAR_STRING;
17025 rettv->vval.v_string = NULL;
17026#ifdef FEAT_CLIENTSERVER
17027 remote_common(argvars, rettv, FALSE);
17028#endif
17029}
17030
17031/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017032 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017033 */
17034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017035f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017036{
Bram Moolenaar33570922005-01-25 22:26:29 +000017037 list_T *l;
17038 listitem_T *item, *item2;
17039 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017040 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017041 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017042 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017043 dict_T *d;
17044 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017045 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017046
Bram Moolenaar8c711452005-01-14 21:53:12 +000017047 if (argvars[0].v_type == VAR_DICT)
17048 {
17049 if (argvars[2].v_type != VAR_UNKNOWN)
17050 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017051 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017052 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017054 key = get_tv_string_chk(&argvars[1]);
17055 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017056 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017057 di = dict_find(d, key, -1);
17058 if (di == NULL)
17059 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017060 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17061 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017062 {
17063 *rettv = di->di_tv;
17064 init_tv(&di->di_tv);
17065 dictitem_remove(d, di);
17066 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017068 }
17069 }
17070 else if (argvars[0].v_type != VAR_LIST)
17071 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017072 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017073 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017075 int error = FALSE;
17076
17077 idx = get_tv_number_chk(&argvars[1], &error);
17078 if (error)
17079 ; /* type error: do nothing, errmsg already given */
17080 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017081 EMSGN(_(e_listidx), idx);
17082 else
17083 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017084 if (argvars[2].v_type == VAR_UNKNOWN)
17085 {
17086 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017087 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017088 *rettv = item->li_tv;
17089 vim_free(item);
17090 }
17091 else
17092 {
17093 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017094 end = get_tv_number_chk(&argvars[2], &error);
17095 if (error)
17096 ; /* type error: do nothing */
17097 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017098 EMSGN(_(e_listidx), end);
17099 else
17100 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017101 int cnt = 0;
17102
17103 for (li = item; li != NULL; li = li->li_next)
17104 {
17105 ++cnt;
17106 if (li == item2)
17107 break;
17108 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017109 if (li == NULL) /* didn't find "item2" after "item" */
17110 EMSG(_(e_invrange));
17111 else
17112 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017113 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017114 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017115 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017116 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017117 l->lv_first = item;
17118 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017119 item->li_prev = NULL;
17120 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017121 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017122 }
17123 }
17124 }
17125 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017126 }
17127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128}
17129
17130/*
17131 * "rename({from}, {to})" function
17132 */
17133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017134f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135{
17136 char_u buf[NUMBUFLEN];
17137
17138 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017139 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017141 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17142 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143}
17144
17145/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017146 * "repeat()" function
17147 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017149f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017150{
17151 char_u *p;
17152 int n;
17153 int slen;
17154 int len;
17155 char_u *r;
17156 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017157
17158 n = get_tv_number(&argvars[1]);
17159 if (argvars[0].v_type == VAR_LIST)
17160 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017161 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017162 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017163 if (list_extend(rettv->vval.v_list,
17164 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017165 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017166 }
17167 else
17168 {
17169 p = get_tv_string(&argvars[0]);
17170 rettv->v_type = VAR_STRING;
17171 rettv->vval.v_string = NULL;
17172
17173 slen = (int)STRLEN(p);
17174 len = slen * n;
17175 if (len <= 0)
17176 return;
17177
17178 r = alloc(len + 1);
17179 if (r != NULL)
17180 {
17181 for (i = 0; i < n; i++)
17182 mch_memmove(r + i * slen, p, (size_t)slen);
17183 r[len] = NUL;
17184 }
17185
17186 rettv->vval.v_string = r;
17187 }
17188}
17189
17190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 * "resolve()" function
17192 */
17193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017194f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195{
17196 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017197#ifdef HAVE_READLINK
17198 char_u *buf = NULL;
17199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017201 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202#ifdef FEAT_SHORTCUT
17203 {
17204 char_u *v = NULL;
17205
17206 v = mch_resolve_shortcut(p);
17207 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017208 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017210 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 }
17212#else
17213# ifdef HAVE_READLINK
17214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 char_u *cpy;
17216 int len;
17217 char_u *remain = NULL;
17218 char_u *q;
17219 int is_relative_to_current = FALSE;
17220 int has_trailing_pathsep = FALSE;
17221 int limit = 100;
17222
17223 p = vim_strsave(p);
17224
17225 if (p[0] == '.' && (vim_ispathsep(p[1])
17226 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17227 is_relative_to_current = TRUE;
17228
17229 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017230 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017233 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235
17236 q = getnextcomp(p);
17237 if (*q != NUL)
17238 {
17239 /* Separate the first path component in "p", and keep the
17240 * remainder (beginning with the path separator). */
17241 remain = vim_strsave(q - 1);
17242 q[-1] = NUL;
17243 }
17244
Bram Moolenaard9462e32011-04-11 21:35:11 +020017245 buf = alloc(MAXPATHL + 1);
17246 if (buf == NULL)
17247 goto fail;
17248
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 for (;;)
17250 {
17251 for (;;)
17252 {
17253 len = readlink((char *)p, (char *)buf, MAXPATHL);
17254 if (len <= 0)
17255 break;
17256 buf[len] = NUL;
17257
17258 if (limit-- == 0)
17259 {
17260 vim_free(p);
17261 vim_free(remain);
17262 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017263 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 goto fail;
17265 }
17266
17267 /* Ensure that the result will have a trailing path separator
17268 * if the argument has one. */
17269 if (remain == NULL && has_trailing_pathsep)
17270 add_pathsep(buf);
17271
17272 /* Separate the first path component in the link value and
17273 * concatenate the remainders. */
17274 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17275 if (*q != NUL)
17276 {
17277 if (remain == NULL)
17278 remain = vim_strsave(q - 1);
17279 else
17280 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017281 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 if (cpy != NULL)
17283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284 vim_free(remain);
17285 remain = cpy;
17286 }
17287 }
17288 q[-1] = NUL;
17289 }
17290
17291 q = gettail(p);
17292 if (q > p && *q == NUL)
17293 {
17294 /* Ignore trailing path separator. */
17295 q[-1] = NUL;
17296 q = gettail(p);
17297 }
17298 if (q > p && !mch_isFullName(buf))
17299 {
17300 /* symlink is relative to directory of argument */
17301 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17302 if (cpy != NULL)
17303 {
17304 STRCPY(cpy, p);
17305 STRCPY(gettail(cpy), buf);
17306 vim_free(p);
17307 p = cpy;
17308 }
17309 }
17310 else
17311 {
17312 vim_free(p);
17313 p = vim_strsave(buf);
17314 }
17315 }
17316
17317 if (remain == NULL)
17318 break;
17319
17320 /* Append the first path component of "remain" to "p". */
17321 q = getnextcomp(remain + 1);
17322 len = q - remain - (*q != NUL);
17323 cpy = vim_strnsave(p, STRLEN(p) + len);
17324 if (cpy != NULL)
17325 {
17326 STRNCAT(cpy, remain, len);
17327 vim_free(p);
17328 p = cpy;
17329 }
17330 /* Shorten "remain". */
17331 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017332 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 else
17334 {
17335 vim_free(remain);
17336 remain = NULL;
17337 }
17338 }
17339
17340 /* If the result is a relative path name, make it explicitly relative to
17341 * the current directory if and only if the argument had this form. */
17342 if (!vim_ispathsep(*p))
17343 {
17344 if (is_relative_to_current
17345 && *p != NUL
17346 && !(p[0] == '.'
17347 && (p[1] == NUL
17348 || vim_ispathsep(p[1])
17349 || (p[1] == '.'
17350 && (p[2] == NUL
17351 || vim_ispathsep(p[2]))))))
17352 {
17353 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017354 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 if (cpy != NULL)
17356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 vim_free(p);
17358 p = cpy;
17359 }
17360 }
17361 else if (!is_relative_to_current)
17362 {
17363 /* Strip leading "./". */
17364 q = p;
17365 while (q[0] == '.' && vim_ispathsep(q[1]))
17366 q += 2;
17367 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017368 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 }
17370 }
17371
17372 /* Ensure that the result will have no trailing path separator
17373 * if the argument had none. But keep "/" or "//". */
17374 if (!has_trailing_pathsep)
17375 {
17376 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017377 if (after_pathsep(p, q))
17378 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 }
17380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017381 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 }
17383# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017384 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385# endif
17386#endif
17387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389
17390#ifdef HAVE_READLINK
17391fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017392 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017394 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395}
17396
17397/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017398 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 */
17400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017401f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402{
Bram Moolenaar33570922005-01-25 22:26:29 +000017403 list_T *l;
17404 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405
Bram Moolenaar0d660222005-01-07 21:51:51 +000017406 if (argvars[0].v_type != VAR_LIST)
17407 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017408 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017409 && !tv_check_lock(l->lv_lock,
17410 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017411 {
17412 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017413 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017414 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017415 while (li != NULL)
17416 {
17417 ni = li->li_prev;
17418 list_append(l, li);
17419 li = ni;
17420 }
17421 rettv->vval.v_list = l;
17422 rettv->v_type = VAR_LIST;
17423 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017424 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426}
17427
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017428#define SP_NOMOVE 0x01 /* don't move cursor */
17429#define SP_REPEAT 0x02 /* repeat to find outer pair */
17430#define SP_RETCOUNT 0x04 /* return matchcount */
17431#define SP_SETPCMARK 0x08 /* set previous context mark */
17432#define SP_START 0x10 /* accept match at start position */
17433#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17434#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017435#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017436
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017437static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017438
17439/*
17440 * Get flags for a search function.
17441 * Possibly sets "p_ws".
17442 * Returns BACKWARD, FORWARD or zero (for an error).
17443 */
17444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017445get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017446{
17447 int dir = FORWARD;
17448 char_u *flags;
17449 char_u nbuf[NUMBUFLEN];
17450 int mask;
17451
17452 if (varp->v_type != VAR_UNKNOWN)
17453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017454 flags = get_tv_string_buf_chk(varp, nbuf);
17455 if (flags == NULL)
17456 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017457 while (*flags != NUL)
17458 {
17459 switch (*flags)
17460 {
17461 case 'b': dir = BACKWARD; break;
17462 case 'w': p_ws = TRUE; break;
17463 case 'W': p_ws = FALSE; break;
17464 default: mask = 0;
17465 if (flagsp != NULL)
17466 switch (*flags)
17467 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017468 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017469 case 'e': mask = SP_END; break;
17470 case 'm': mask = SP_RETCOUNT; break;
17471 case 'n': mask = SP_NOMOVE; break;
17472 case 'p': mask = SP_SUBPAT; break;
17473 case 'r': mask = SP_REPEAT; break;
17474 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017475 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017476 }
17477 if (mask == 0)
17478 {
17479 EMSG2(_(e_invarg2), flags);
17480 dir = 0;
17481 }
17482 else
17483 *flagsp |= mask;
17484 }
17485 if (dir == 0)
17486 break;
17487 ++flags;
17488 }
17489 }
17490 return dir;
17491}
17492
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017494 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017496 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017497search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017499 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 char_u *pat;
17501 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017502 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 int save_p_ws = p_ws;
17504 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017505 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017506 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017507 proftime_T tm;
17508#ifdef FEAT_RELTIME
17509 long time_limit = 0;
17510#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017511 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017512 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017514 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017515 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017516 if (dir == 0)
17517 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017518 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017519 if (flags & SP_START)
17520 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017521 if (flags & SP_END)
17522 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017523 if (flags & SP_COLUMN)
17524 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017525
Bram Moolenaar76929292008-01-06 19:07:36 +000017526 /* Optional arguments: line number to stop searching and timeout. */
17527 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017528 {
17529 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17530 if (lnum_stop < 0)
17531 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017532#ifdef FEAT_RELTIME
17533 if (argvars[3].v_type != VAR_UNKNOWN)
17534 {
17535 time_limit = get_tv_number_chk(&argvars[3], NULL);
17536 if (time_limit < 0)
17537 goto theend;
17538 }
17539#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017540 }
17541
Bram Moolenaar76929292008-01-06 19:07:36 +000017542#ifdef FEAT_RELTIME
17543 /* Set the time limit, if there is one. */
17544 profile_setlimit(time_limit, &tm);
17545#endif
17546
Bram Moolenaar231334e2005-07-25 20:46:57 +000017547 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017548 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017549 * Check to make sure only those flags are set.
17550 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17551 * flags cannot be set. Check for that condition also.
17552 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017553 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017554 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017556 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017557 goto theend;
17558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017560 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017561 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017562 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017563 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017565 if (flags & SP_SUBPAT)
17566 retval = subpatnum;
17567 else
17568 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017569 if (flags & SP_SETPCMARK)
17570 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017572 if (match_pos != NULL)
17573 {
17574 /* Store the match cursor position */
17575 match_pos->lnum = pos.lnum;
17576 match_pos->col = pos.col + 1;
17577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 /* "/$" will put the cursor after the end of the line, may need to
17579 * correct that here */
17580 check_cursor();
17581 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017582
17583 /* If 'n' flag is used: restore cursor position. */
17584 if (flags & SP_NOMOVE)
17585 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017586 else
17587 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017588theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017590
17591 return retval;
17592}
17593
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017594#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017595
17596/*
17597 * round() is not in C90, use ceil() or floor() instead.
17598 */
17599 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017600vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017601{
17602 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17603}
17604
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017605/*
17606 * "round({float})" function
17607 */
17608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017609f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017610{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017611 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017612
17613 rettv->v_type = VAR_FLOAT;
17614 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017615 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017616 else
17617 rettv->vval.v_float = 0.0;
17618}
17619#endif
17620
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017621/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017622 * "screenattr()" function
17623 */
17624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017625f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017626{
17627 int row;
17628 int col;
17629 int c;
17630
17631 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17632 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17633 if (row < 0 || row >= screen_Rows
17634 || col < 0 || col >= screen_Columns)
17635 c = -1;
17636 else
17637 c = ScreenAttrs[LineOffset[row] + col];
17638 rettv->vval.v_number = c;
17639}
17640
17641/*
17642 * "screenchar()" function
17643 */
17644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017645f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017646{
17647 int row;
17648 int col;
17649 int off;
17650 int c;
17651
17652 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17653 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17654 if (row < 0 || row >= screen_Rows
17655 || col < 0 || col >= screen_Columns)
17656 c = -1;
17657 else
17658 {
17659 off = LineOffset[row] + col;
17660#ifdef FEAT_MBYTE
17661 if (enc_utf8 && ScreenLinesUC[off] != 0)
17662 c = ScreenLinesUC[off];
17663 else
17664#endif
17665 c = ScreenLines[off];
17666 }
17667 rettv->vval.v_number = c;
17668}
17669
17670/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017671 * "screencol()" function
17672 *
17673 * First column is 1 to be consistent with virtcol().
17674 */
17675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017676f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017677{
17678 rettv->vval.v_number = screen_screencol() + 1;
17679}
17680
17681/*
17682 * "screenrow()" function
17683 */
17684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017685f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017686{
17687 rettv->vval.v_number = screen_screenrow() + 1;
17688}
17689
17690/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017691 * "search()" function
17692 */
17693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017694f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017695{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017696 int flags = 0;
17697
17698 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699}
17700
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017702 * "searchdecl()" function
17703 */
17704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017705f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017706{
17707 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017708 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017709 int error = FALSE;
17710 char_u *name;
17711
17712 rettv->vval.v_number = 1; /* default: FAIL */
17713
17714 name = get_tv_string_chk(&argvars[0]);
17715 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017716 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017717 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017718 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17719 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17720 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017721 if (!error && name != NULL)
17722 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017723 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017724}
17725
17726/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017727 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017729 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017730searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731{
17732 char_u *spat, *mpat, *epat;
17733 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735 int dir;
17736 int flags = 0;
17737 char_u nbuf1[NUMBUFLEN];
17738 char_u nbuf2[NUMBUFLEN];
17739 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017740 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017741 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017742 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017745 spat = get_tv_string_chk(&argvars[0]);
17746 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17747 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17748 if (spat == NULL || mpat == NULL || epat == NULL)
17749 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 /* Handle the optional fourth argument: flags */
17752 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017753 if (dir == 0)
17754 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017755
17756 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017757 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17758 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017759 if ((flags & (SP_END | SP_SUBPAT)) != 0
17760 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017761 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017762 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017763 goto theend;
17764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017766 /* Using 'r' implies 'W', otherwise it doesn't work. */
17767 if (flags & SP_REPEAT)
17768 p_ws = FALSE;
17769
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017770 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017771 if (argvars[3].v_type == VAR_UNKNOWN
17772 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 skip = (char_u *)"";
17774 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017776 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017777 if (argvars[5].v_type != VAR_UNKNOWN)
17778 {
17779 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17780 if (lnum_stop < 0)
17781 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017782#ifdef FEAT_RELTIME
17783 if (argvars[6].v_type != VAR_UNKNOWN)
17784 {
17785 time_limit = get_tv_number_chk(&argvars[6], NULL);
17786 if (time_limit < 0)
17787 goto theend;
17788 }
17789#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017790 }
17791 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017792 if (skip == NULL)
17793 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017795 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017796 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017797
17798theend:
17799 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017800
17801 return retval;
17802}
17803
17804/*
17805 * "searchpair()" function
17806 */
17807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017808f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017809{
17810 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17811}
17812
17813/*
17814 * "searchpairpos()" function
17815 */
17816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017817f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017818{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017819 pos_T match_pos;
17820 int lnum = 0;
17821 int col = 0;
17822
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017823 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017824 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017825
17826 if (searchpair_cmn(argvars, &match_pos) > 0)
17827 {
17828 lnum = match_pos.lnum;
17829 col = match_pos.col;
17830 }
17831
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017832 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17833 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017834}
17835
17836/*
17837 * Search for a start/middle/end thing.
17838 * Used by searchpair(), see its documentation for the details.
17839 * Returns 0 or -1 for no match,
17840 */
17841 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017842do_searchpair(
17843 char_u *spat, /* start pattern */
17844 char_u *mpat, /* middle pattern */
17845 char_u *epat, /* end pattern */
17846 int dir, /* BACKWARD or FORWARD */
17847 char_u *skip, /* skip expression */
17848 int flags, /* SP_SETPCMARK and other SP_ values */
17849 pos_T *match_pos,
17850 linenr_T lnum_stop, /* stop at this line if not zero */
17851 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017852{
17853 char_u *save_cpo;
17854 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17855 long retval = 0;
17856 pos_T pos;
17857 pos_T firstpos;
17858 pos_T foundpos;
17859 pos_T save_cursor;
17860 pos_T save_pos;
17861 int n;
17862 int r;
17863 int nest = 1;
17864 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017865 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017866 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017867
17868 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17869 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017870 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017871
Bram Moolenaar76929292008-01-06 19:07:36 +000017872#ifdef FEAT_RELTIME
17873 /* Set the time limit, if there is one. */
17874 profile_setlimit(time_limit, &tm);
17875#endif
17876
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017877 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17878 * start/middle/end (pat3, for the top pair). */
17879 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17880 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17881 if (pat2 == NULL || pat3 == NULL)
17882 goto theend;
17883 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17884 if (*mpat == NUL)
17885 STRCPY(pat3, pat2);
17886 else
17887 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17888 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017889 if (flags & SP_START)
17890 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017891
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 save_cursor = curwin->w_cursor;
17893 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017894 clearpos(&firstpos);
17895 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 pat = pat3;
17897 for (;;)
17898 {
17899 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017900 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17902 /* didn't find it or found the first match again: FAIL */
17903 break;
17904
17905 if (firstpos.lnum == 0)
17906 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017907 if (equalpos(pos, foundpos))
17908 {
17909 /* Found the same position again. Can happen with a pattern that
17910 * has "\zs" at the end and searching backwards. Advance one
17911 * character and try again. */
17912 if (dir == BACKWARD)
17913 decl(&pos);
17914 else
17915 incl(&pos);
17916 }
17917 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017919 /* clear the start flag to avoid getting stuck here */
17920 options &= ~SEARCH_START;
17921
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 /* If the skip pattern matches, ignore this match. */
17923 if (*skip != NUL)
17924 {
17925 save_pos = curwin->w_cursor;
17926 curwin->w_cursor = pos;
17927 r = eval_to_bool(skip, &err, NULL, FALSE);
17928 curwin->w_cursor = save_pos;
17929 if (err)
17930 {
17931 /* Evaluating {skip} caused an error, break here. */
17932 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017933 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 break;
17935 }
17936 if (r)
17937 continue;
17938 }
17939
17940 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17941 {
17942 /* Found end when searching backwards or start when searching
17943 * forward: nested pair. */
17944 ++nest;
17945 pat = pat2; /* nested, don't search for middle */
17946 }
17947 else
17948 {
17949 /* Found end when searching forward or start when searching
17950 * backward: end of (nested) pair; or found middle in outer pair. */
17951 if (--nest == 1)
17952 pat = pat3; /* outer level, search for middle */
17953 }
17954
17955 if (nest == 0)
17956 {
17957 /* Found the match: return matchcount or line number. */
17958 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017959 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017961 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017962 if (flags & SP_SETPCMARK)
17963 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 curwin->w_cursor = pos;
17965 if (!(flags & SP_REPEAT))
17966 break;
17967 nest = 1; /* search for next unmatched */
17968 }
17969 }
17970
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017971 if (match_pos != NULL)
17972 {
17973 /* Store the match cursor position */
17974 match_pos->lnum = curwin->w_cursor.lnum;
17975 match_pos->col = curwin->w_cursor.col + 1;
17976 }
17977
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017979 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 curwin->w_cursor = save_cursor;
17981
17982theend:
17983 vim_free(pat2);
17984 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017985 if (p_cpo == empty_option)
17986 p_cpo = save_cpo;
17987 else
17988 /* Darn, evaluating the {skip} expression changed the value. */
17989 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017990
17991 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992}
17993
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017994/*
17995 * "searchpos()" function
17996 */
17997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017998f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017999{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018000 pos_T match_pos;
18001 int lnum = 0;
18002 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018003 int n;
18004 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018005
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018006 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018007 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018008
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018009 n = search_cmn(argvars, &match_pos, &flags);
18010 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018011 {
18012 lnum = match_pos.lnum;
18013 col = match_pos.col;
18014 }
18015
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018016 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18017 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018018 if (flags & SP_SUBPAT)
18019 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018020}
18021
Bram Moolenaar0d660222005-01-07 21:51:51 +000018022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018023f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018025#ifdef FEAT_CLIENTSERVER
18026 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018027 char_u *server = get_tv_string_chk(&argvars[0]);
18028 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029
Bram Moolenaar0d660222005-01-07 21:51:51 +000018030 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018031 if (server == NULL || reply == NULL)
18032 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018033 if (check_restricted() || check_secure())
18034 return;
18035# ifdef FEAT_X11
18036 if (check_connection() == FAIL)
18037 return;
18038# endif
18039
18040 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042 EMSG(_("E258: Unable to send to client"));
18043 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018045 rettv->vval.v_number = 0;
18046#else
18047 rettv->vval.v_number = -1;
18048#endif
18049}
18050
Bram Moolenaar0d660222005-01-07 21:51:51 +000018051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018052f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018053{
18054 char_u *r = NULL;
18055
18056#ifdef FEAT_CLIENTSERVER
18057# ifdef WIN32
18058 r = serverGetVimNames();
18059# else
18060 make_connection();
18061 if (X_DISPLAY != NULL)
18062 r = serverGetVimNames(X_DISPLAY);
18063# endif
18064#endif
18065 rettv->v_type = VAR_STRING;
18066 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067}
18068
18069/*
18070 * "setbufvar()" function
18071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018073f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074{
18075 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018078 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 char_u nbuf[NUMBUFLEN];
18080
18081 if (check_restricted() || check_secure())
18082 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018083 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18084 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018085 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 varp = &argvars[2];
18087
18088 if (buf != NULL && varname != NULL && varp != NULL)
18089 {
18090 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092
18093 if (*varname == '&')
18094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018095 long numval;
18096 char_u *strval;
18097 int error = FALSE;
18098
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018100 numval = get_tv_number_chk(varp, &error);
18101 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018102 if (!error && strval != NULL)
18103 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 }
18105 else
18106 {
18107 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18108 if (bufvarname != NULL)
18109 {
18110 STRCPY(bufvarname, "b:");
18111 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018112 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 vim_free(bufvarname);
18114 }
18115 }
18116
18117 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120}
18121
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018123f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018124{
18125 dict_T *d;
18126 dictitem_T *di;
18127 char_u *csearch;
18128
18129 if (argvars[0].v_type != VAR_DICT)
18130 {
18131 EMSG(_(e_dictreq));
18132 return;
18133 }
18134
18135 if ((d = argvars[0].vval.v_dict) != NULL)
18136 {
18137 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18138 if (csearch != NULL)
18139 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018140#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018141 if (enc_utf8)
18142 {
18143 int pcc[MAX_MCO];
18144 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018145
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018146 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18147 }
18148 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018149#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018150 set_last_csearch(PTR2CHAR(csearch),
18151 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018152 }
18153
18154 di = dict_find(d, (char_u *)"forward", -1);
18155 if (di != NULL)
18156 set_csearch_direction(get_tv_number(&di->di_tv)
18157 ? FORWARD : BACKWARD);
18158
18159 di = dict_find(d, (char_u *)"until", -1);
18160 if (di != NULL)
18161 set_csearch_until(!!get_tv_number(&di->di_tv));
18162 }
18163}
18164
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165/*
18166 * "setcmdpos()" function
18167 */
18168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018169f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018171 int pos = (int)get_tv_number(&argvars[0]) - 1;
18172
18173 if (pos >= 0)
18174 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175}
18176
18177/*
18178 * "setline()" function
18179 */
18180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018181f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182{
18183 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018184 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018185 list_T *l = NULL;
18186 listitem_T *li = NULL;
18187 long added = 0;
18188 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018190 lnum = get_tv_lnum(&argvars[0]);
18191 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018193 l = argvars[1].vval.v_list;
18194 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018196 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018197 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018198
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018199 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018200 for (;;)
18201 {
18202 if (l != NULL)
18203 {
18204 /* list argument, get next string */
18205 if (li == NULL)
18206 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018207 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018208 li = li->li_next;
18209 }
18210
18211 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018212 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018213 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018214
18215 /* When coming here from Insert mode, sync undo, so that this can be
18216 * undone separately from what was previously inserted. */
18217 if (u_sync_once == 2)
18218 {
18219 u_sync_once = 1; /* notify that u_sync() was called */
18220 u_sync(TRUE);
18221 }
18222
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018223 if (lnum <= curbuf->b_ml.ml_line_count)
18224 {
18225 /* existing line, replace it */
18226 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18227 {
18228 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018229 if (lnum == curwin->w_cursor.lnum)
18230 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018231 rettv->vval.v_number = 0; /* OK */
18232 }
18233 }
18234 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18235 {
18236 /* lnum is one past the last line, append the line */
18237 ++added;
18238 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18239 rettv->vval.v_number = 0; /* OK */
18240 }
18241
18242 if (l == NULL) /* only one string argument */
18243 break;
18244 ++lnum;
18245 }
18246
18247 if (added > 0)
18248 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249}
18250
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018251static 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 +000018252
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018254 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018255 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018257set_qf_ll_list(
18258 win_T *wp UNUSED,
18259 typval_T *list_arg UNUSED,
18260 typval_T *action_arg UNUSED,
18261 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018262{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018263#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018264 char_u *act;
18265 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018266#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018267
Bram Moolenaar2641f772005-03-25 21:58:17 +000018268 rettv->vval.v_number = -1;
18269
18270#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018271 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018272 EMSG(_(e_listreq));
18273 else
18274 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018275 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018276
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018277 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018278 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018279 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018280 if (act == NULL)
18281 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018282 if (*act == 'a' || *act == 'r')
18283 action = *act;
18284 }
18285
Bram Moolenaar81484f42012-12-05 15:16:47 +010018286 if (l != NULL && set_errorlist(wp, l, action,
18287 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018288 rettv->vval.v_number = 0;
18289 }
18290#endif
18291}
18292
18293/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018294 * "setloclist()" function
18295 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018297f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018298{
18299 win_T *win;
18300
18301 rettv->vval.v_number = -1;
18302
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018303 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018304 if (win != NULL)
18305 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18306}
18307
18308/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018309 * "setmatches()" function
18310 */
18311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018312f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018313{
18314#ifdef FEAT_SEARCH_EXTRA
18315 list_T *l;
18316 listitem_T *li;
18317 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018318 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018319
18320 rettv->vval.v_number = -1;
18321 if (argvars[0].v_type != VAR_LIST)
18322 {
18323 EMSG(_(e_listreq));
18324 return;
18325 }
18326 if ((l = argvars[0].vval.v_list) != NULL)
18327 {
18328
18329 /* To some extent make sure that we are dealing with a list from
18330 * "getmatches()". */
18331 li = l->lv_first;
18332 while (li != NULL)
18333 {
18334 if (li->li_tv.v_type != VAR_DICT
18335 || (d = li->li_tv.vval.v_dict) == NULL)
18336 {
18337 EMSG(_(e_invarg));
18338 return;
18339 }
18340 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018341 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18342 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018343 && dict_find(d, (char_u *)"priority", -1) != NULL
18344 && dict_find(d, (char_u *)"id", -1) != NULL))
18345 {
18346 EMSG(_(e_invarg));
18347 return;
18348 }
18349 li = li->li_next;
18350 }
18351
18352 clear_matches(curwin);
18353 li = l->lv_first;
18354 while (li != NULL)
18355 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018356 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018357 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018358 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018359 char_u *group;
18360 int priority;
18361 int id;
18362 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018363
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018364 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018365 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18366 {
18367 if (s == NULL)
18368 {
18369 s = list_alloc();
18370 if (s == NULL)
18371 return;
18372 }
18373
18374 /* match from matchaddpos() */
18375 for (i = 1; i < 9; i++)
18376 {
18377 sprintf((char *)buf, (char *)"pos%d", i);
18378 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18379 {
18380 if (di->di_tv.v_type != VAR_LIST)
18381 return;
18382
18383 list_append_tv(s, &di->di_tv);
18384 s->lv_refcount++;
18385 }
18386 else
18387 break;
18388 }
18389 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018390
18391 group = get_dict_string(d, (char_u *)"group", FALSE);
18392 priority = (int)get_dict_number(d, (char_u *)"priority");
18393 id = (int)get_dict_number(d, (char_u *)"id");
18394 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18395 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18396 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018397 if (i == 0)
18398 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018399 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018400 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018401 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018402 }
18403 else
18404 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018405 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018406 list_unref(s);
18407 s = NULL;
18408 }
18409
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018410 li = li->li_next;
18411 }
18412 rettv->vval.v_number = 0;
18413 }
18414#endif
18415}
18416
18417/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018418 * "setpos()" function
18419 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018421f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018422{
18423 pos_T pos;
18424 int fnum;
18425 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018426 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018427
Bram Moolenaar08250432008-02-13 11:42:46 +000018428 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018429 name = get_tv_string_chk(argvars);
18430 if (name != NULL)
18431 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018432 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018433 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018434 if (--pos.col < 0)
18435 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018436 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018437 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018438 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018439 if (fnum == curbuf->b_fnum)
18440 {
18441 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018442 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018443 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018444 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018445 curwin->w_set_curswant = FALSE;
18446 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018447 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018448 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018449 }
18450 else
18451 EMSG(_(e_invarg));
18452 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018453 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18454 {
18455 /* set mark */
18456 if (setmark_pos(name[1], &pos, fnum) == OK)
18457 rettv->vval.v_number = 0;
18458 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018459 else
18460 EMSG(_(e_invarg));
18461 }
18462 }
18463}
18464
18465/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018466 * "setqflist()" function
18467 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018469f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018470{
18471 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18472}
18473
18474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 * "setreg()" function
18476 */
18477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018478f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479{
18480 int regname;
18481 char_u *strregname;
18482 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018483 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 int append;
18485 char_u yank_type;
18486 long block_len;
18487
18488 block_len = -1;
18489 yank_type = MAUTO;
18490 append = FALSE;
18491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018492 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018493 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018495 if (strregname == NULL)
18496 return; /* type error; errmsg already given */
18497 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 if (regname == 0 || regname == '@')
18499 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018501 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018503 stropt = get_tv_string_chk(&argvars[2]);
18504 if (stropt == NULL)
18505 return; /* type error */
18506 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507 switch (*stropt)
18508 {
18509 case 'a': case 'A': /* append */
18510 append = TRUE;
18511 break;
18512 case 'v': case 'c': /* character-wise selection */
18513 yank_type = MCHAR;
18514 break;
18515 case 'V': case 'l': /* line-wise selection */
18516 yank_type = MLINE;
18517 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 case 'b': case Ctrl_V: /* block-wise selection */
18519 yank_type = MBLOCK;
18520 if (VIM_ISDIGIT(stropt[1]))
18521 {
18522 ++stropt;
18523 block_len = getdigits(&stropt) - 1;
18524 --stropt;
18525 }
18526 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 }
18528 }
18529
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018530 if (argvars[1].v_type == VAR_LIST)
18531 {
18532 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018533 char_u **allocval;
18534 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018535 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018536 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018537 int len = argvars[1].vval.v_list->lv_len;
18538 listitem_T *li;
18539
Bram Moolenaar7d647822014-04-05 21:28:56 +020018540 /* First half: use for pointers to result lines; second half: use for
18541 * pointers to allocated copies. */
18542 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018543 if (lstval == NULL)
18544 return;
18545 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018546 allocval = lstval + len + 2;
18547 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018548
18549 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18550 li = li->li_next)
18551 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018552 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018553 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018554 goto free_lstval;
18555 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018556 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018557 /* Need to make a copy, next get_tv_string_buf_chk() will
18558 * overwrite the string. */
18559 strval = vim_strsave(buf);
18560 if (strval == NULL)
18561 goto free_lstval;
18562 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018563 }
18564 *curval++ = strval;
18565 }
18566 *curval++ = NULL;
18567
18568 write_reg_contents_lst(regname, lstval, -1,
18569 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018570free_lstval:
18571 while (curallocval > allocval)
18572 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018573 vim_free(lstval);
18574 }
18575 else
18576 {
18577 strval = get_tv_string_chk(&argvars[1]);
18578 if (strval == NULL)
18579 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018580 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018582 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018583 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584}
18585
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018586/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018587 * "settabvar()" function
18588 */
18589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018590f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018591{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018592#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018593 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018594 tabpage_T *tp;
18595#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018596 char_u *varname, *tabvarname;
18597 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018598
18599 rettv->vval.v_number = 0;
18600
18601 if (check_restricted() || check_secure())
18602 return;
18603
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018604#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018605 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018606#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018607 varname = get_tv_string_chk(&argvars[1]);
18608 varp = &argvars[2];
18609
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018610 if (varname != NULL && varp != NULL
18611#ifdef FEAT_WINDOWS
18612 && tp != NULL
18613#endif
18614 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018615 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018616#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018617 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018618 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018619#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018620
18621 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18622 if (tabvarname != NULL)
18623 {
18624 STRCPY(tabvarname, "t:");
18625 STRCPY(tabvarname + 2, varname);
18626 set_var(tabvarname, varp, TRUE);
18627 vim_free(tabvarname);
18628 }
18629
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018630#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018631 /* Restore current tabpage */
18632 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018633 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018634#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018635 }
18636}
18637
18638/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018639 * "settabwinvar()" function
18640 */
18641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018642f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018643{
18644 setwinvar(argvars, rettv, 1);
18645}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646
18647/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018648 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018651f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018653 setwinvar(argvars, rettv, 0);
18654}
18655
18656/*
18657 * "setwinvar()" and "settabwinvar()" functions
18658 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018659
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018661setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018662{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 win_T *win;
18664#ifdef FEAT_WINDOWS
18665 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018666 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018667 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668#endif
18669 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018670 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018672 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673
18674 if (check_restricted() || check_secure())
18675 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018676
18677#ifdef FEAT_WINDOWS
18678 if (off == 1)
18679 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18680 else
18681 tp = curtab;
18682#endif
18683 win = find_win_by_nr(&argvars[off], tp);
18684 varname = get_tv_string_chk(&argvars[off + 1]);
18685 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686
18687 if (win != NULL && varname != NULL && varp != NULL)
18688 {
18689#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018690 need_switch_win = !(tp == curtab && win == curwin);
18691 if (!need_switch_win
18692 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018695 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018697 long numval;
18698 char_u *strval;
18699 int error = FALSE;
18700
18701 ++varname;
18702 numval = get_tv_number_chk(varp, &error);
18703 strval = get_tv_string_buf_chk(varp, nbuf);
18704 if (!error && strval != NULL)
18705 set_option_value(varname, numval, strval, OPT_LOCAL);
18706 }
18707 else
18708 {
18709 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18710 if (winvarname != NULL)
18711 {
18712 STRCPY(winvarname, "w:");
18713 STRCPY(winvarname + 2, varname);
18714 set_var(winvarname, varp, TRUE);
18715 vim_free(winvarname);
18716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717 }
18718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018720 if (need_switch_win)
18721 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722#endif
18723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724}
18725
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018726#ifdef FEAT_CRYPT
18727/*
18728 * "sha256({string})" function
18729 */
18730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018731f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018732{
18733 char_u *p;
18734
18735 p = get_tv_string(&argvars[0]);
18736 rettv->vval.v_string = vim_strsave(
18737 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18738 rettv->v_type = VAR_STRING;
18739}
18740#endif /* FEAT_CRYPT */
18741
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018743 * "shellescape({string})" function
18744 */
18745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018746f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018747{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018748 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018749 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018750 rettv->v_type = VAR_STRING;
18751}
18752
18753/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018754 * shiftwidth() function
18755 */
18756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018757f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018758{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018759 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018760}
18761
18762/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018763 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 */
18765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018766f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018768 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769
Bram Moolenaar0d660222005-01-07 21:51:51 +000018770 p = get_tv_string(&argvars[0]);
18771 rettv->vval.v_string = vim_strsave(p);
18772 simplify_filename(rettv->vval.v_string); /* simplify in place */
18773 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774}
18775
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018776#ifdef FEAT_FLOAT
18777/*
18778 * "sin()" function
18779 */
18780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018781f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018782{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018783 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018784
18785 rettv->v_type = VAR_FLOAT;
18786 if (get_float_arg(argvars, &f) == OK)
18787 rettv->vval.v_float = sin(f);
18788 else
18789 rettv->vval.v_float = 0.0;
18790}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018791
18792/*
18793 * "sinh()" function
18794 */
18795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018796f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018797{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018798 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018799
18800 rettv->v_type = VAR_FLOAT;
18801 if (get_float_arg(argvars, &f) == OK)
18802 rettv->vval.v_float = sinh(f);
18803 else
18804 rettv->vval.v_float = 0.0;
18805}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018806#endif
18807
Bram Moolenaar0d660222005-01-07 21:51:51 +000018808static int
18809#ifdef __BORLANDC__
18810 _RTLENTRYF
18811#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018812 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018813static int
18814#ifdef __BORLANDC__
18815 _RTLENTRYF
18816#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018817 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018818
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018819/* struct used in the array that's given to qsort() */
18820typedef struct
18821{
18822 listitem_T *item;
18823 int idx;
18824} sortItem_T;
18825
Bram Moolenaar0b962472016-02-22 22:51:33 +010018826/* struct storing information about current sort */
18827typedef struct
18828{
18829 int item_compare_ic;
18830 int item_compare_numeric;
18831 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018832#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018833 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018834#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018835 char_u *item_compare_func;
18836 dict_T *item_compare_selfdict;
18837 int item_compare_func_err;
18838 int item_compare_keep_zero;
18839} sortinfo_T;
18840static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018841static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018842#define ITEM_COMPARE_FAIL 999
18843
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018845 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018847 static int
18848#ifdef __BORLANDC__
18849_RTLENTRYF
18850#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018851item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018853 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018854 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018855 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018856 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018857 int res;
18858 char_u numbuf1[NUMBUFLEN];
18859 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018861 si1 = (sortItem_T *)s1;
18862 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018863 tv1 = &si1->item->li_tv;
18864 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018865
Bram Moolenaar0b962472016-02-22 22:51:33 +010018866 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018867 {
18868 long v1 = get_tv_number(tv1);
18869 long v2 = get_tv_number(tv2);
18870
18871 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18872 }
18873
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018874#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018875 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018876 {
18877 float_T v1 = get_tv_float(tv1);
18878 float_T v2 = get_tv_float(tv2);
18879
18880 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18881 }
18882#endif
18883
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018884 /* tv2string() puts quotes around a string and allocates memory. Don't do
18885 * that for string variables. Use a single quote when comparing with a
18886 * non-string to do what the docs promise. */
18887 if (tv1->v_type == VAR_STRING)
18888 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018889 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018890 p1 = (char_u *)"'";
18891 else
18892 p1 = tv1->vval.v_string;
18893 }
18894 else
18895 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18896 if (tv2->v_type == VAR_STRING)
18897 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018898 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018899 p2 = (char_u *)"'";
18900 else
18901 p2 = tv2->vval.v_string;
18902 }
18903 else
18904 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018905 if (p1 == NULL)
18906 p1 = (char_u *)"";
18907 if (p2 == NULL)
18908 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018909 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018910 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018911 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018912 res = STRICMP(p1, p2);
18913 else
18914 res = STRCMP(p1, p2);
18915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018917 {
18918 double n1, n2;
18919 n1 = strtod((char *)p1, (char **)&p1);
18920 n2 = strtod((char *)p2, (char **)&p2);
18921 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18922 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018923
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018924 /* When the result would be zero, compare the item indexes. Makes the
18925 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018926 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018927 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018928
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929 vim_free(tofree1);
18930 vim_free(tofree2);
18931 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932}
18933
18934 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018935#ifdef __BORLANDC__
18936_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018938item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018939{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018940 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018941 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018942 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018943 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018944 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018946 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018947 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018948 return 0;
18949
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018950 si1 = (sortItem_T *)s1;
18951 si2 = (sortItem_T *)s2;
18952
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018953 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018954 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018955 copy_tv(&si1->item->li_tv, &argv[0]);
18956 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018957
18958 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018959 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010018960 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018961 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010018962 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963 clear_tv(&argv[0]);
18964 clear_tv(&argv[1]);
18965
18966 if (res == FAIL)
18967 res = ITEM_COMPARE_FAIL;
18968 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018969 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18970 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018971 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018972 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018973
18974 /* When the result would be zero, compare the pointers themselves. Makes
18975 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018976 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018977 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018978
Bram Moolenaar0d660222005-01-07 21:51:51 +000018979 return res;
18980}
18981
18982/*
18983 * "sort({list})" function
18984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018986do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987{
Bram Moolenaar33570922005-01-25 22:26:29 +000018988 list_T *l;
18989 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018990 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018991 sortinfo_T *old_sortinfo;
18992 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018993 long len;
18994 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995
Bram Moolenaar0b962472016-02-22 22:51:33 +010018996 /* Pointer to current info struct used in compare function. Save and
18997 * restore the current one for nested calls. */
18998 old_sortinfo = sortinfo;
18999 sortinfo = &info;
19000
Bram Moolenaar0d660222005-01-07 21:51:51 +000019001 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019002 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 else
19004 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019005 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019006 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019007 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19008 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019009 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019010 rettv->vval.v_list = l;
19011 rettv->v_type = VAR_LIST;
19012 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013
Bram Moolenaar0d660222005-01-07 21:51:51 +000019014 len = list_len(l);
19015 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019016 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017
Bram Moolenaar0b962472016-02-22 22:51:33 +010019018 info.item_compare_ic = FALSE;
19019 info.item_compare_numeric = FALSE;
19020 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019021#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019022 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019023#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019024 info.item_compare_func = NULL;
19025 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019026 if (argvars[1].v_type != VAR_UNKNOWN)
19027 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019028 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019029 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019030 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019031 else
19032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019033 int error = FALSE;
19034
19035 i = get_tv_number_chk(&argvars[1], &error);
19036 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019037 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019038 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019039 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019040 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019041 info.item_compare_func = get_tv_string(&argvars[1]);
19042 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019043 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019044 if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019045 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019046 info.item_compare_func = NULL;
19047 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019048 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019049 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019050 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019051 info.item_compare_func = NULL;
19052 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019053 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019054#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019055 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019056 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019057 info.item_compare_func = NULL;
19058 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019059 }
19060#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019061 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019062 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019063 info.item_compare_func = NULL;
19064 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019065 }
19066 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019067 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019068
19069 if (argvars[2].v_type != VAR_UNKNOWN)
19070 {
19071 /* optional third argument: {dict} */
19072 if (argvars[2].v_type != VAR_DICT)
19073 {
19074 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019075 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019076 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019077 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019078 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080
Bram Moolenaar0d660222005-01-07 21:51:51 +000019081 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019082 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019083 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019084 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085
Bram Moolenaar327aa022014-03-25 18:24:23 +010019086 i = 0;
19087 if (sort)
19088 {
19089 /* sort(): ptrs will be the list to sort */
19090 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019091 {
19092 ptrs[i].item = li;
19093 ptrs[i].idx = i;
19094 ++i;
19095 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019096
Bram Moolenaar0b962472016-02-22 22:51:33 +010019097 info.item_compare_func_err = FALSE;
19098 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019099 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019100 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019101 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019102 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019103 EMSG(_("E702: Sort compare function failed"));
19104 else
19105 {
19106 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019107 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019108 info.item_compare_func == NULL
19109 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019110
Bram Moolenaar0b962472016-02-22 22:51:33 +010019111 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019112 {
19113 /* Clear the List and append the items in sorted order. */
19114 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19115 l->lv_len = 0;
19116 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019117 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019118 }
19119 }
19120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019122 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019123 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019124
19125 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019126 info.item_compare_func_err = FALSE;
19127 info.item_compare_keep_zero = TRUE;
19128 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019129 ? item_compare2 : item_compare;
19130
19131 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19132 li = li->li_next)
19133 {
19134 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19135 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019136 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019137 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019138 {
19139 EMSG(_("E882: Uniq compare function failed"));
19140 break;
19141 }
19142 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019143
Bram Moolenaar0b962472016-02-22 22:51:33 +010019144 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019145 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019146 while (--i >= 0)
19147 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019148 li = ptrs[i].item->li_next;
19149 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019150 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019151 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019152 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019153 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019154 list_fix_watch(l, li);
19155 listitem_free(li);
19156 l->lv_len--;
19157 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019158 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019159 }
19160
19161 vim_free(ptrs);
19162 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019163theend:
19164 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019165}
19166
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019167/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019168 * "sort({list})" function
19169 */
19170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019171f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019172{
19173 do_sort_uniq(argvars, rettv, TRUE);
19174}
19175
19176/*
19177 * "uniq({list})" function
19178 */
19179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019180f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019181{
19182 do_sort_uniq(argvars, rettv, FALSE);
19183}
19184
19185/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019186 * "soundfold({word})" function
19187 */
19188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019189f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019190{
19191 char_u *s;
19192
19193 rettv->v_type = VAR_STRING;
19194 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019195#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019196 rettv->vval.v_string = eval_soundfold(s);
19197#else
19198 rettv->vval.v_string = vim_strsave(s);
19199#endif
19200}
19201
19202/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019203 * "spellbadword()" function
19204 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019206f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019207{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019208 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019209 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019210 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019211
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019212 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019213 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019214
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019215#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019216 if (argvars[0].v_type == VAR_UNKNOWN)
19217 {
19218 /* Find the start and length of the badly spelled word. */
19219 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19220 if (len != 0)
19221 word = ml_get_cursor();
19222 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019223 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019224 {
19225 char_u *str = get_tv_string_chk(&argvars[0]);
19226 int capcol = -1;
19227
19228 if (str != NULL)
19229 {
19230 /* Check the argument for spelling. */
19231 while (*str != NUL)
19232 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019233 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019234 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019235 {
19236 word = str;
19237 break;
19238 }
19239 str += len;
19240 }
19241 }
19242 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019243#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019244
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019245 list_append_string(rettv->vval.v_list, word, len);
19246 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019247 attr == HLF_SPB ? "bad" :
19248 attr == HLF_SPR ? "rare" :
19249 attr == HLF_SPL ? "local" :
19250 attr == HLF_SPC ? "caps" :
19251 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019252}
19253
19254/*
19255 * "spellsuggest()" function
19256 */
19257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019258f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019259{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019260#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019261 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019262 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019263 int maxcount;
19264 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019265 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019266 listitem_T *li;
19267 int need_capital = FALSE;
19268#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019269
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019270 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019271 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019272
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019273#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019274 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019275 {
19276 str = get_tv_string(&argvars[0]);
19277 if (argvars[1].v_type != VAR_UNKNOWN)
19278 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019279 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019280 if (maxcount <= 0)
19281 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019282 if (argvars[2].v_type != VAR_UNKNOWN)
19283 {
19284 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19285 if (typeerr)
19286 return;
19287 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019288 }
19289 else
19290 maxcount = 25;
19291
Bram Moolenaar4770d092006-01-12 23:22:24 +000019292 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019293
19294 for (i = 0; i < ga.ga_len; ++i)
19295 {
19296 str = ((char_u **)ga.ga_data)[i];
19297
19298 li = listitem_alloc();
19299 if (li == NULL)
19300 vim_free(str);
19301 else
19302 {
19303 li->li_tv.v_type = VAR_STRING;
19304 li->li_tv.v_lock = 0;
19305 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019306 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019307 }
19308 }
19309 ga_clear(&ga);
19310 }
19311#endif
19312}
19313
Bram Moolenaar0d660222005-01-07 21:51:51 +000019314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019315f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019316{
19317 char_u *str;
19318 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019319 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019320 regmatch_T regmatch;
19321 char_u patbuf[NUMBUFLEN];
19322 char_u *save_cpo;
19323 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019324 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019325 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019326 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019327
19328 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19329 save_cpo = p_cpo;
19330 p_cpo = (char_u *)"";
19331
19332 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019333 if (argvars[1].v_type != VAR_UNKNOWN)
19334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019335 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19336 if (pat == NULL)
19337 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019338 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019339 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019340 }
19341 if (pat == NULL || *pat == NUL)
19342 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019343
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019344 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019346 if (typeerr)
19347 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348
Bram Moolenaar0d660222005-01-07 21:51:51 +000019349 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19350 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019352 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019353 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019354 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019355 if (*str == NUL)
19356 match = FALSE; /* empty item at the end */
19357 else
19358 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019359 if (match)
19360 end = regmatch.startp[0];
19361 else
19362 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019363 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19364 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019365 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019366 if (list_append_string(rettv->vval.v_list, str,
19367 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019368 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019369 }
19370 if (!match)
19371 break;
19372 /* Advance to just after the match. */
19373 if (regmatch.endp[0] > str)
19374 col = 0;
19375 else
19376 {
19377 /* Don't get stuck at the same match. */
19378#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019379 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019380#else
19381 col = 1;
19382#endif
19383 }
19384 str = regmatch.endp[0];
19385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386
Bram Moolenaar473de612013-06-08 18:19:48 +020019387 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389
Bram Moolenaar0d660222005-01-07 21:51:51 +000019390 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391}
19392
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019393#ifdef FEAT_FLOAT
19394/*
19395 * "sqrt()" function
19396 */
19397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019398f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019399{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019400 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019401
19402 rettv->v_type = VAR_FLOAT;
19403 if (get_float_arg(argvars, &f) == OK)
19404 rettv->vval.v_float = sqrt(f);
19405 else
19406 rettv->vval.v_float = 0.0;
19407}
19408
19409/*
19410 * "str2float()" function
19411 */
19412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019413f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019414{
19415 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19416
19417 if (*p == '+')
19418 p = skipwhite(p + 1);
19419 (void)string2float(p, &rettv->vval.v_float);
19420 rettv->v_type = VAR_FLOAT;
19421}
19422#endif
19423
Bram Moolenaar2c932302006-03-18 21:42:09 +000019424/*
19425 * "str2nr()" function
19426 */
19427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019428f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019429{
19430 int base = 10;
19431 char_u *p;
19432 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019433 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019434
19435 if (argvars[1].v_type != VAR_UNKNOWN)
19436 {
19437 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019438 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019439 {
19440 EMSG(_(e_invarg));
19441 return;
19442 }
19443 }
19444
19445 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019446 if (*p == '+')
19447 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019448 switch (base)
19449 {
19450 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19451 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19452 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19453 default: what = 0;
19454 }
19455 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019456 rettv->vval.v_number = n;
19457}
19458
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459#ifdef HAVE_STRFTIME
19460/*
19461 * "strftime({format}[, {time}])" function
19462 */
19463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019464f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465{
19466 char_u result_buf[256];
19467 struct tm *curtime;
19468 time_t seconds;
19469 char_u *p;
19470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019471 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019473 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019474 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475 seconds = time(NULL);
19476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019477 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 curtime = localtime(&seconds);
19479 /* MSVC returns NULL for an invalid value of seconds. */
19480 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 else
19483 {
19484# ifdef FEAT_MBYTE
19485 vimconv_T conv;
19486 char_u *enc;
19487
19488 conv.vc_type = CONV_NONE;
19489 enc = enc_locale();
19490 convert_setup(&conv, p_enc, enc);
19491 if (conv.vc_type != CONV_NONE)
19492 p = string_convert(&conv, p, NULL);
19493# endif
19494 if (p != NULL)
19495 (void)strftime((char *)result_buf, sizeof(result_buf),
19496 (char *)p, curtime);
19497 else
19498 result_buf[0] = NUL;
19499
19500# ifdef FEAT_MBYTE
19501 if (conv.vc_type != CONV_NONE)
19502 vim_free(p);
19503 convert_setup(&conv, enc, p_enc);
19504 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019505 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 else
19507# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019508 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509
19510# ifdef FEAT_MBYTE
19511 /* Release conversion descriptors */
19512 convert_setup(&conv, NULL, NULL);
19513 vim_free(enc);
19514# endif
19515 }
19516}
19517#endif
19518
19519/*
19520 * "stridx()" function
19521 */
19522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019523f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524{
19525 char_u buf[NUMBUFLEN];
19526 char_u *needle;
19527 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019528 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019530 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019532 needle = get_tv_string_chk(&argvars[1]);
19533 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019534 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019535 if (needle == NULL || haystack == NULL)
19536 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537
Bram Moolenaar33570922005-01-25 22:26:29 +000019538 if (argvars[2].v_type != VAR_UNKNOWN)
19539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019540 int error = FALSE;
19541
19542 start_idx = get_tv_number_chk(&argvars[2], &error);
19543 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019544 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019545 if (start_idx >= 0)
19546 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019547 }
19548
19549 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19550 if (pos != NULL)
19551 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552}
19553
19554/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019555 * "string()" function
19556 */
19557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019558f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019559{
19560 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019561 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019563 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019564 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019565 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019566 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019567 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568}
19569
19570/*
19571 * "strlen()" function
19572 */
19573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019574f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019576 rettv->vval.v_number = (varnumber_T)(STRLEN(
19577 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578}
19579
19580/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019581 * "strchars()" function
19582 */
19583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019584f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019585{
19586 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019587 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019588#ifdef FEAT_MBYTE
19589 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019590 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019591#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019592
19593 if (argvars[1].v_type != VAR_UNKNOWN)
19594 skipcc = get_tv_number_chk(&argvars[1], NULL);
19595 if (skipcc < 0 || skipcc > 1)
19596 EMSG(_(e_invarg));
19597 else
19598 {
19599#ifdef FEAT_MBYTE
19600 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19601 while (*s != NUL)
19602 {
19603 func_mb_ptr2char_adv(&s);
19604 ++len;
19605 }
19606 rettv->vval.v_number = len;
19607#else
19608 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19609#endif
19610 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019611}
19612
19613/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019614 * "strdisplaywidth()" function
19615 */
19616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019617f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019618{
19619 char_u *s = get_tv_string(&argvars[0]);
19620 int col = 0;
19621
19622 if (argvars[1].v_type != VAR_UNKNOWN)
19623 col = get_tv_number(&argvars[1]);
19624
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019625 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019626}
19627
19628/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019629 * "strwidth()" function
19630 */
19631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019632f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019633{
19634 char_u *s = get_tv_string(&argvars[0]);
19635
19636 rettv->vval.v_number = (varnumber_T)(
19637#ifdef FEAT_MBYTE
19638 mb_string2cells(s, -1)
19639#else
19640 STRLEN(s)
19641#endif
19642 );
19643}
19644
19645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 * "strpart()" function
19647 */
19648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019649f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650{
19651 char_u *p;
19652 int n;
19653 int len;
19654 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019655 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019657 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 slen = (int)STRLEN(p);
19659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019660 n = get_tv_number_chk(&argvars[1], &error);
19661 if (error)
19662 len = 0;
19663 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019664 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 else
19666 len = slen - n; /* default len: all bytes that are available. */
19667
19668 /*
19669 * Only return the overlap between the specified part and the actual
19670 * string.
19671 */
19672 if (n < 0)
19673 {
19674 len += n;
19675 n = 0;
19676 }
19677 else if (n > slen)
19678 n = slen;
19679 if (len < 0)
19680 len = 0;
19681 else if (n + len > slen)
19682 len = slen - n;
19683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019684 rettv->v_type = VAR_STRING;
19685 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686}
19687
19688/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019689 * "strridx()" function
19690 */
19691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019692f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019693{
19694 char_u buf[NUMBUFLEN];
19695 char_u *needle;
19696 char_u *haystack;
19697 char_u *rest;
19698 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019699 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019700
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019701 needle = get_tv_string_chk(&argvars[1]);
19702 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019703
19704 rettv->vval.v_number = -1;
19705 if (needle == NULL || haystack == NULL)
19706 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019707
19708 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019709 if (argvars[2].v_type != VAR_UNKNOWN)
19710 {
19711 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019712 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019713 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019714 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019715 }
19716 else
19717 end_idx = haystack_len;
19718
Bram Moolenaar0d660222005-01-07 21:51:51 +000019719 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019720 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019721 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019722 lastmatch = haystack + end_idx;
19723 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019724 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019725 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019726 for (rest = haystack; *rest != '\0'; ++rest)
19727 {
19728 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019729 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019730 break;
19731 lastmatch = rest;
19732 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019733 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019734
19735 if (lastmatch == NULL)
19736 rettv->vval.v_number = -1;
19737 else
19738 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19739}
19740
19741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742 * "strtrans()" function
19743 */
19744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019745f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019747 rettv->v_type = VAR_STRING;
19748 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749}
19750
19751/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019752 * "submatch()" function
19753 */
19754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019755f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019756{
Bram Moolenaar41571762014-04-02 19:00:58 +020019757 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019758 int no;
19759 int retList = 0;
19760
19761 no = (int)get_tv_number_chk(&argvars[0], &error);
19762 if (error)
19763 return;
19764 error = FALSE;
19765 if (argvars[1].v_type != VAR_UNKNOWN)
19766 retList = get_tv_number_chk(&argvars[1], &error);
19767 if (error)
19768 return;
19769
19770 if (retList == 0)
19771 {
19772 rettv->v_type = VAR_STRING;
19773 rettv->vval.v_string = reg_submatch(no);
19774 }
19775 else
19776 {
19777 rettv->v_type = VAR_LIST;
19778 rettv->vval.v_list = reg_submatch_list(no);
19779 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019780}
19781
19782/*
19783 * "substitute()" function
19784 */
19785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019786f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019787{
19788 char_u patbuf[NUMBUFLEN];
19789 char_u subbuf[NUMBUFLEN];
19790 char_u flagsbuf[NUMBUFLEN];
19791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019792 char_u *str = get_tv_string_chk(&argvars[0]);
19793 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19794 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19795 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19796
Bram Moolenaar0d660222005-01-07 21:51:51 +000019797 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019798 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19799 rettv->vval.v_string = NULL;
19800 else
19801 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019802}
19803
19804/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019805 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019808f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809{
19810 int id = 0;
19811#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019812 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 long col;
19814 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019815 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019817 lnum = get_tv_lnum(argvars); /* -1 on type error */
19818 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19819 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019821 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019822 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019823 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824#endif
19825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019826 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827}
19828
19829/*
19830 * "synIDattr(id, what [, mode])" function
19831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019833f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834{
19835 char_u *p = NULL;
19836#ifdef FEAT_SYN_HL
19837 int id;
19838 char_u *what;
19839 char_u *mode;
19840 char_u modebuf[NUMBUFLEN];
19841 int modec;
19842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019843 id = get_tv_number(&argvars[0]);
19844 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019845 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019847 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019849 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 modec = 0; /* replace invalid with current */
19851 }
19852 else
19853 {
19854#ifdef FEAT_GUI
19855 if (gui.in_use)
19856 modec = 'g';
19857 else
19858#endif
19859 if (t_colors > 1)
19860 modec = 'c';
19861 else
19862 modec = 't';
19863 }
19864
19865
19866 switch (TOLOWER_ASC(what[0]))
19867 {
19868 case 'b':
19869 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19870 p = highlight_color(id, what, modec);
19871 else /* bold */
19872 p = highlight_has_attr(id, HL_BOLD, modec);
19873 break;
19874
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019875 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 p = highlight_color(id, what, modec);
19877 break;
19878
19879 case 'i':
19880 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19881 p = highlight_has_attr(id, HL_INVERSE, modec);
19882 else /* italic */
19883 p = highlight_has_attr(id, HL_ITALIC, modec);
19884 break;
19885
19886 case 'n': /* name */
19887 p = get_highlight_name(NULL, id - 1);
19888 break;
19889
19890 case 'r': /* reverse */
19891 p = highlight_has_attr(id, HL_INVERSE, modec);
19892 break;
19893
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019894 case 's':
19895 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19896 p = highlight_color(id, what, modec);
19897 else /* standout */
19898 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 break;
19900
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019901 case 'u':
19902 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19903 /* underline */
19904 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19905 else
19906 /* undercurl */
19907 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 break;
19909 }
19910
19911 if (p != NULL)
19912 p = vim_strsave(p);
19913#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019914 rettv->v_type = VAR_STRING;
19915 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916}
19917
19918/*
19919 * "synIDtrans(id)" function
19920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019922f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923{
19924 int id;
19925
19926#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019927 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928
19929 if (id > 0)
19930 id = syn_get_final_id(id);
19931 else
19932#endif
19933 id = 0;
19934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019935 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936}
19937
19938/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019939 * "synconcealed(lnum, col)" function
19940 */
19941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019942f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019943{
19944#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19945 long lnum;
19946 long col;
19947 int syntax_flags = 0;
19948 int cchar;
19949 int matchid = 0;
19950 char_u str[NUMBUFLEN];
19951#endif
19952
19953 rettv->v_type = VAR_LIST;
19954 rettv->vval.v_list = NULL;
19955
19956#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19957 lnum = get_tv_lnum(argvars); /* -1 on type error */
19958 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19959
19960 vim_memset(str, NUL, sizeof(str));
19961
19962 if (rettv_list_alloc(rettv) != FAIL)
19963 {
19964 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19965 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19966 && curwin->w_p_cole > 0)
19967 {
19968 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19969 syntax_flags = get_syntax_info(&matchid);
19970
19971 /* get the conceal character */
19972 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19973 {
19974 cchar = syn_get_sub_char();
19975 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19976 cchar = lcs_conceal;
19977 if (cchar != NUL)
19978 {
19979# ifdef FEAT_MBYTE
19980 if (has_mbyte)
19981 (*mb_char2bytes)(cchar, str);
19982 else
19983# endif
19984 str[0] = cchar;
19985 }
19986 }
19987 }
19988
19989 list_append_number(rettv->vval.v_list,
19990 (syntax_flags & HL_CONCEAL) != 0);
19991 /* -1 to auto-determine strlen */
19992 list_append_string(rettv->vval.v_list, str, -1);
19993 list_append_number(rettv->vval.v_list, matchid);
19994 }
19995#endif
19996}
19997
19998/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019999 * "synstack(lnum, col)" function
20000 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020002f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020003{
20004#ifdef FEAT_SYN_HL
20005 long lnum;
20006 long col;
20007 int i;
20008 int id;
20009#endif
20010
20011 rettv->v_type = VAR_LIST;
20012 rettv->vval.v_list = NULL;
20013
20014#ifdef FEAT_SYN_HL
20015 lnum = get_tv_lnum(argvars); /* -1 on type error */
20016 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20017
20018 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020019 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020020 && rettv_list_alloc(rettv) != FAIL)
20021 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020022 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020023 for (i = 0; ; ++i)
20024 {
20025 id = syn_get_stack_item(i);
20026 if (id < 0)
20027 break;
20028 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20029 break;
20030 }
20031 }
20032#endif
20033}
20034
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020036get_cmd_output_as_rettv(
20037 typval_T *argvars,
20038 typval_T *rettv,
20039 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020041 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020043 char_u *infile = NULL;
20044 char_u buf[NUMBUFLEN];
20045 int err = FALSE;
20046 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020047 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020048 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020050 rettv->v_type = VAR_STRING;
20051 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020052 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020053 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020054
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020055 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020056 {
20057 /*
20058 * Write the string to a temp file, to be used for input of the shell
20059 * command.
20060 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020061 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020062 {
20063 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020064 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020065 }
20066
20067 fd = mch_fopen((char *)infile, WRITEBIN);
20068 if (fd == NULL)
20069 {
20070 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020071 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020072 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020073 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020074 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020075 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20076 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020077 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020078 else
20079 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020080 size_t len;
20081
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020082 p = get_tv_string_buf_chk(&argvars[1], buf);
20083 if (p == NULL)
20084 {
20085 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020086 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020087 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020088 len = STRLEN(p);
20089 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020090 err = TRUE;
20091 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020092 if (fclose(fd) != 0)
20093 err = TRUE;
20094 if (err)
20095 {
20096 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020097 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020098 }
20099 }
20100
Bram Moolenaar52a72462014-08-29 15:53:52 +020020101 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20102 * echoes typeahead, that messes up the display. */
20103 if (!msg_silent)
20104 flags += SHELL_COOKED;
20105
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020106 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020108 int len;
20109 listitem_T *li;
20110 char_u *s = NULL;
20111 char_u *start;
20112 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020113 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114
Bram Moolenaar52a72462014-08-29 15:53:52 +020020115 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020116 if (res == NULL)
20117 goto errret;
20118
20119 list = list_alloc();
20120 if (list == NULL)
20121 goto errret;
20122
20123 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020125 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020126 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020127 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020128 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020129
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020130 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020131 if (s == NULL)
20132 goto errret;
20133
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020134 for (p = s; start < end; ++p, ++start)
20135 *p = *start == NUL ? NL : *start;
20136 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020137
20138 li = listitem_alloc();
20139 if (li == NULL)
20140 {
20141 vim_free(s);
20142 goto errret;
20143 }
20144 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020145 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020146 li->li_tv.vval.v_string = s;
20147 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020149
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020150 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020151 rettv->v_type = VAR_LIST;
20152 rettv->vval.v_list = list;
20153 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020155 else
20156 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020157 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020158#ifdef USE_CR
20159 /* translate <CR> into <NL> */
20160 if (res != NULL)
20161 {
20162 char_u *s;
20163
20164 for (s = res; *s; ++s)
20165 {
20166 if (*s == CAR)
20167 *s = NL;
20168 }
20169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170#else
20171# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020172 /* translate <CR><NL> into <NL> */
20173 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020175 char_u *s, *d;
20176
20177 d = res;
20178 for (s = res; *s; ++s)
20179 {
20180 if (s[0] == CAR && s[1] == NL)
20181 ++s;
20182 *d++ = *s;
20183 }
20184 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186# endif
20187#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020188 rettv->vval.v_string = res;
20189 res = NULL;
20190 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020191
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020192errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020193 if (infile != NULL)
20194 {
20195 mch_remove(infile);
20196 vim_free(infile);
20197 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020198 if (res != NULL)
20199 vim_free(res);
20200 if (list != NULL)
20201 list_free(list, TRUE);
20202}
20203
20204/*
20205 * "system()" function
20206 */
20207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020208f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020209{
20210 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20211}
20212
20213/*
20214 * "systemlist()" function
20215 */
20216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020217f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020218{
20219 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220}
20221
20222/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020223 * "tabpagebuflist()" function
20224 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020226f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020227{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020228#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020229 tabpage_T *tp;
20230 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020231
20232 if (argvars[0].v_type == VAR_UNKNOWN)
20233 wp = firstwin;
20234 else
20235 {
20236 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20237 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020238 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020239 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020240 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020241 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020242 for (; wp != NULL; wp = wp->w_next)
20243 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020244 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020245 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020246 }
20247#endif
20248}
20249
20250
20251/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020252 * "tabpagenr()" function
20253 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020255f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020256{
20257 int nr = 1;
20258#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020259 char_u *arg;
20260
20261 if (argvars[0].v_type != VAR_UNKNOWN)
20262 {
20263 arg = get_tv_string_chk(&argvars[0]);
20264 nr = 0;
20265 if (arg != NULL)
20266 {
20267 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020268 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020269 else
20270 EMSG2(_(e_invexpr2), arg);
20271 }
20272 }
20273 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020274 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020275#endif
20276 rettv->vval.v_number = nr;
20277}
20278
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020279
20280#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020281static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020282
20283/*
20284 * Common code for tabpagewinnr() and winnr().
20285 */
20286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020287get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020288{
20289 win_T *twin;
20290 int nr = 1;
20291 win_T *wp;
20292 char_u *arg;
20293
20294 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20295 if (argvar->v_type != VAR_UNKNOWN)
20296 {
20297 arg = get_tv_string_chk(argvar);
20298 if (arg == NULL)
20299 nr = 0; /* type error; errmsg already given */
20300 else if (STRCMP(arg, "$") == 0)
20301 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20302 else if (STRCMP(arg, "#") == 0)
20303 {
20304 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20305 if (twin == NULL)
20306 nr = 0;
20307 }
20308 else
20309 {
20310 EMSG2(_(e_invexpr2), arg);
20311 nr = 0;
20312 }
20313 }
20314
20315 if (nr > 0)
20316 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20317 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020318 {
20319 if (wp == NULL)
20320 {
20321 /* didn't find it in this tabpage */
20322 nr = 0;
20323 break;
20324 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020325 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020326 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020327 return nr;
20328}
20329#endif
20330
20331/*
20332 * "tabpagewinnr()" function
20333 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020335f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020336{
20337 int nr = 1;
20338#ifdef FEAT_WINDOWS
20339 tabpage_T *tp;
20340
20341 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20342 if (tp == NULL)
20343 nr = 0;
20344 else
20345 nr = get_winnr(tp, &argvars[1]);
20346#endif
20347 rettv->vval.v_number = nr;
20348}
20349
20350
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020351/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020352 * "tagfiles()" function
20353 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020355f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020356{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020357 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020358 tagname_T tn;
20359 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020360
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020361 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020362 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020363 fname = alloc(MAXPATHL);
20364 if (fname == NULL)
20365 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020366
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020367 for (first = TRUE; ; first = FALSE)
20368 if (get_tagfname(&tn, first, fname) == FAIL
20369 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020370 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020371 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020372 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020373}
20374
20375/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020376 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020377 */
20378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020379f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020380{
20381 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020382
20383 tag_pattern = get_tv_string(&argvars[0]);
20384
20385 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020386 if (*tag_pattern == NUL)
20387 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020388
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020389 if (rettv_list_alloc(rettv) == OK)
20390 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020391}
20392
20393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 * "tempname()" function
20395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020397f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398{
20399 static int x = 'A';
20400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020401 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020402 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403
20404 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20405 * names. Skip 'I' and 'O', they are used for shell redirection. */
20406 do
20407 {
20408 if (x == 'Z')
20409 x = '0';
20410 else if (x == '9')
20411 x = 'A';
20412 else
20413 {
20414#ifdef EBCDIC
20415 if (x == 'I')
20416 x = 'J';
20417 else if (x == 'R')
20418 x = 'S';
20419 else
20420#endif
20421 ++x;
20422 }
20423 } while (x == 'I' || x == 'O');
20424}
20425
20426/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020427 * "test(list)" function: Just checking the walls...
20428 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020430f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020431{
20432 /* Used for unit testing. Change the code below to your liking. */
20433#if 0
20434 listitem_T *li;
20435 list_T *l;
20436 char_u *bad, *good;
20437
20438 if (argvars[0].v_type != VAR_LIST)
20439 return;
20440 l = argvars[0].vval.v_list;
20441 if (l == NULL)
20442 return;
20443 li = l->lv_first;
20444 if (li == NULL)
20445 return;
20446 bad = get_tv_string(&li->li_tv);
20447 li = li->li_next;
20448 if (li == NULL)
20449 return;
20450 good = get_tv_string(&li->li_tv);
20451 rettv->vval.v_number = test_edit_score(bad, good);
20452#endif
20453}
20454
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020455#ifdef FEAT_FLOAT
20456/*
20457 * "tan()" function
20458 */
20459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020460f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020461{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020462 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020463
20464 rettv->v_type = VAR_FLOAT;
20465 if (get_float_arg(argvars, &f) == OK)
20466 rettv->vval.v_float = tan(f);
20467 else
20468 rettv->vval.v_float = 0.0;
20469}
20470
20471/*
20472 * "tanh()" function
20473 */
20474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020475f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020476{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020477 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020478
20479 rettv->v_type = VAR_FLOAT;
20480 if (get_float_arg(argvars, &f) == OK)
20481 rettv->vval.v_float = tanh(f);
20482 else
20483 rettv->vval.v_float = 0.0;
20484}
20485#endif
20486
Bram Moolenaard52d9742005-08-21 22:20:28 +000020487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 * "tolower(string)" function
20489 */
20490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020491f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492{
20493 char_u *p;
20494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020495 p = vim_strsave(get_tv_string(&argvars[0]));
20496 rettv->v_type = VAR_STRING;
20497 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498
20499 if (p != NULL)
20500 while (*p != NUL)
20501 {
20502#ifdef FEAT_MBYTE
20503 int l;
20504
20505 if (enc_utf8)
20506 {
20507 int c, lc;
20508
20509 c = utf_ptr2char(p);
20510 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020511 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 /* TODO: reallocate string when byte count changes. */
20513 if (utf_char2len(lc) == l)
20514 utf_char2bytes(lc, p);
20515 p += l;
20516 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020517 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 p += l; /* skip multi-byte character */
20519 else
20520#endif
20521 {
20522 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20523 ++p;
20524 }
20525 }
20526}
20527
20528/*
20529 * "toupper(string)" function
20530 */
20531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020532f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020534 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020535 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536}
20537
20538/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020539 * "tr(string, fromstr, tostr)" function
20540 */
20541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020542f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020543{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020544 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020545 char_u *fromstr;
20546 char_u *tostr;
20547 char_u *p;
20548#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020549 int inlen;
20550 int fromlen;
20551 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020552 int idx;
20553 char_u *cpstr;
20554 int cplen;
20555 int first = TRUE;
20556#endif
20557 char_u buf[NUMBUFLEN];
20558 char_u buf2[NUMBUFLEN];
20559 garray_T ga;
20560
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020561 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020562 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20563 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020564
20565 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020566 rettv->v_type = VAR_STRING;
20567 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020568 if (fromstr == NULL || tostr == NULL)
20569 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020570 ga_init2(&ga, (int)sizeof(char), 80);
20571
20572#ifdef FEAT_MBYTE
20573 if (!has_mbyte)
20574#endif
20575 /* not multi-byte: fromstr and tostr must be the same length */
20576 if (STRLEN(fromstr) != STRLEN(tostr))
20577 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020578#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020579error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020580#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020581 EMSG2(_(e_invarg2), fromstr);
20582 ga_clear(&ga);
20583 return;
20584 }
20585
20586 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020587 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020588 {
20589#ifdef FEAT_MBYTE
20590 if (has_mbyte)
20591 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020592 inlen = (*mb_ptr2len)(in_str);
20593 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020594 cplen = inlen;
20595 idx = 0;
20596 for (p = fromstr; *p != NUL; p += fromlen)
20597 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020598 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020599 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020600 {
20601 for (p = tostr; *p != NUL; p += tolen)
20602 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020603 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020604 if (idx-- == 0)
20605 {
20606 cplen = tolen;
20607 cpstr = p;
20608 break;
20609 }
20610 }
20611 if (*p == NUL) /* tostr is shorter than fromstr */
20612 goto error;
20613 break;
20614 }
20615 ++idx;
20616 }
20617
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020618 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020619 {
20620 /* Check that fromstr and tostr have the same number of
20621 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020622 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020623 first = FALSE;
20624 for (p = tostr; *p != NUL; p += tolen)
20625 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020626 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020627 --idx;
20628 }
20629 if (idx != 0)
20630 goto error;
20631 }
20632
Bram Moolenaarcde88542015-08-11 19:14:00 +020020633 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020634 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020635 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020636
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020637 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020638 }
20639 else
20640#endif
20641 {
20642 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020643 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020644 if (p != NULL)
20645 ga_append(&ga, tostr[p - fromstr]);
20646 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020647 ga_append(&ga, *in_str);
20648 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020649 }
20650 }
20651
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020652 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020653 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020654 ga_append(&ga, NUL);
20655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020656 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020657}
20658
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020659#ifdef FEAT_FLOAT
20660/*
20661 * "trunc({float})" function
20662 */
20663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020664f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020665{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020666 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020667
20668 rettv->v_type = VAR_FLOAT;
20669 if (get_float_arg(argvars, &f) == OK)
20670 /* trunc() is not in C90, use floor() or ceil() instead. */
20671 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20672 else
20673 rettv->vval.v_float = 0.0;
20674}
20675#endif
20676
Bram Moolenaar8299df92004-07-10 09:47:34 +000020677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020678 * "type(expr)" function
20679 */
20680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020681f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020683 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020684
20685 switch (argvars[0].v_type)
20686 {
20687 case VAR_NUMBER: n = 0; break;
20688 case VAR_STRING: n = 1; break;
20689 case VAR_FUNC: n = 2; break;
20690 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020691 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020692 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020693 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020694 if (argvars[0].vval.v_number == VVAL_FALSE
20695 || argvars[0].vval.v_number == VVAL_TRUE)
20696 n = 6;
20697 else
20698 n = 7;
20699 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020700 case VAR_JOB: n = 8; break;
20701 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020702 case VAR_UNKNOWN:
20703 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20704 n = -1;
20705 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020706 }
20707 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708}
20709
20710/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020711 * "undofile(name)" function
20712 */
20713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020714f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020715{
20716 rettv->v_type = VAR_STRING;
20717#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020718 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020719 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020720
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020721 if (*fname == NUL)
20722 {
20723 /* If there is no file name there will be no undo file. */
20724 rettv->vval.v_string = NULL;
20725 }
20726 else
20727 {
20728 char_u *ffname = FullName_save(fname, FALSE);
20729
20730 if (ffname != NULL)
20731 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20732 vim_free(ffname);
20733 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020734 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020735#else
20736 rettv->vval.v_string = NULL;
20737#endif
20738}
20739
20740/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020741 * "undotree()" function
20742 */
20743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020744f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020745{
20746 if (rettv_dict_alloc(rettv) == OK)
20747 {
20748 dict_T *dict = rettv->vval.v_dict;
20749 list_T *list;
20750
Bram Moolenaar730cde92010-06-27 05:18:54 +020020751 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020752 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020753 dict_add_nr_str(dict, "save_last",
20754 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020755 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20756 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020757 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020758
20759 list = list_alloc();
20760 if (list != NULL)
20761 {
20762 u_eval_tree(curbuf->b_u_oldhead, list);
20763 dict_add_list(dict, "entries", list);
20764 }
20765 }
20766}
20767
20768/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020769 * "values(dict)" function
20770 */
20771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020772f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020773{
20774 dict_list(argvars, rettv, 1);
20775}
20776
20777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 * "virtcol(string)" function
20779 */
20780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020781f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782{
20783 colnr_T vcol = 0;
20784 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020785 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020787 fp = var2fpos(&argvars[0], FALSE, &fnum);
20788 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20789 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 {
20791 getvvcol(curwin, fp, NULL, NULL, &vcol);
20792 ++vcol;
20793 }
20794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020795 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796}
20797
20798/*
20799 * "visualmode()" function
20800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020802f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 char_u str[2];
20805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020806 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 str[0] = curbuf->b_visual_mode_eval;
20808 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020809 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810
20811 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020812 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814}
20815
20816/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020817 * "wildmenumode()" function
20818 */
20819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020820f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020821{
20822#ifdef FEAT_WILDMENU
20823 if (wild_menu_showing)
20824 rettv->vval.v_number = 1;
20825#endif
20826}
20827
20828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 * "winbufnr(nr)" function
20830 */
20831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020832f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833{
20834 win_T *wp;
20835
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020836 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020838 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020840 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841}
20842
20843/*
20844 * "wincol()" function
20845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020847f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848{
20849 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020850 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851}
20852
20853/*
20854 * "winheight(nr)" function
20855 */
20856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020857f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858{
20859 win_T *wp;
20860
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020861 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020863 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020865 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866}
20867
20868/*
20869 * "winline()" function
20870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020872f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873{
20874 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020875 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876}
20877
20878/*
20879 * "winnr()" function
20880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020882f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883{
20884 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020885
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020887 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020889 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890}
20891
20892/*
20893 * "winrestcmd()" function
20894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020896f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897{
20898#ifdef FEAT_WINDOWS
20899 win_T *wp;
20900 int winnr = 1;
20901 garray_T ga;
20902 char_u buf[50];
20903
20904 ga_init2(&ga, (int)sizeof(char), 70);
20905 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20906 {
20907 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20908 ga_concat(&ga, buf);
20909# ifdef FEAT_VERTSPLIT
20910 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20911 ga_concat(&ga, buf);
20912# endif
20913 ++winnr;
20914 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020915 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020917 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020919 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020921 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922}
20923
20924/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020925 * "winrestview()" function
20926 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020928f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020929{
20930 dict_T *dict;
20931
20932 if (argvars[0].v_type != VAR_DICT
20933 || (dict = argvars[0].vval.v_dict) == NULL)
20934 EMSG(_(e_invarg));
20935 else
20936 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020937 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20938 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20939 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20940 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020941#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020942 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20943 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020944#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020945 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20946 {
20947 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20948 curwin->w_set_curswant = FALSE;
20949 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020950
Bram Moolenaar82c25852014-05-28 16:47:16 +020020951 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20952 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020953#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020954 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20955 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020956#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020957 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20958 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20959 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20960 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020961
20962 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020963 win_new_height(curwin, curwin->w_height);
20964# ifdef FEAT_VERTSPLIT
20965 win_new_width(curwin, W_WIDTH(curwin));
20966# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020967 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020968
Bram Moolenaarb851a962014-10-31 15:45:52 +010020969 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020970 curwin->w_topline = 1;
20971 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20972 curwin->w_topline = curbuf->b_ml.ml_line_count;
20973#ifdef FEAT_DIFF
20974 check_topfill(curwin, TRUE);
20975#endif
20976 }
20977}
20978
20979/*
20980 * "winsaveview()" function
20981 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020983f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020984{
20985 dict_T *dict;
20986
Bram Moolenaara800b422010-06-27 01:15:55 +020020987 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020988 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020989 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020990
20991 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20992 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20993#ifdef FEAT_VIRTUALEDIT
20994 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20995#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020996 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020997 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20998
20999 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21000#ifdef FEAT_DIFF
21001 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21002#endif
21003 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21004 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21005}
21006
21007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 * "winwidth(nr)" function
21009 */
21010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021011f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012{
21013 win_T *wp;
21014
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021015 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021017 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 else
21019#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021020 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021022 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023#endif
21024}
21025
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021027 * "wordcount()" function
21028 */
21029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021030f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021031{
21032 if (rettv_dict_alloc(rettv) == FAIL)
21033 return;
21034 cursor_pos_info(rettv->vval.v_dict);
21035}
21036
21037/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021038 * Write list of strings to file
21039 */
21040 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021041write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021042{
21043 listitem_T *li;
21044 int c;
21045 int ret = OK;
21046 char_u *s;
21047
21048 for (li = list->lv_first; li != NULL; li = li->li_next)
21049 {
21050 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21051 {
21052 if (*s == '\n')
21053 c = putc(NUL, fd);
21054 else
21055 c = putc(*s, fd);
21056 if (c == EOF)
21057 {
21058 ret = FAIL;
21059 break;
21060 }
21061 }
21062 if (!binary || li->li_next != NULL)
21063 if (putc('\n', fd) == EOF)
21064 {
21065 ret = FAIL;
21066 break;
21067 }
21068 if (ret == FAIL)
21069 {
21070 EMSG(_(e_write));
21071 break;
21072 }
21073 }
21074 return ret;
21075}
21076
21077/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021078 * "writefile()" function
21079 */
21080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021081f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021082{
21083 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021084 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021085 char_u *fname;
21086 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021087 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021088
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021089 if (check_restricted() || check_secure())
21090 return;
21091
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021092 if (argvars[0].v_type != VAR_LIST)
21093 {
21094 EMSG2(_(e_listarg), "writefile()");
21095 return;
21096 }
21097 if (argvars[0].vval.v_list == NULL)
21098 return;
21099
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021100 if (argvars[2].v_type != VAR_UNKNOWN)
21101 {
21102 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21103 binary = TRUE;
21104 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21105 append = TRUE;
21106 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021107
21108 /* Always open the file in binary mode, library functions have a mind of
21109 * their own about CR-LF conversion. */
21110 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021111 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21112 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021113 {
21114 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21115 ret = -1;
21116 }
21117 else
21118 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021119 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21120 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021121 fclose(fd);
21122 }
21123
21124 rettv->vval.v_number = ret;
21125}
21126
21127/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021128 * "xor(expr, expr)" function
21129 */
21130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021131f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021132{
21133 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21134 ^ get_tv_number_chk(&argvars[1], NULL);
21135}
21136
21137
21138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021140 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 */
21142 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021143var2fpos(
21144 typval_T *varp,
21145 int dollar_lnum, /* TRUE when $ is last line */
21146 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021148 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021150 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151
Bram Moolenaara5525202006-03-02 22:52:09 +000021152 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021153 if (varp->v_type == VAR_LIST)
21154 {
21155 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021156 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021157 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021158 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021159
21160 l = varp->vval.v_list;
21161 if (l == NULL)
21162 return NULL;
21163
21164 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021165 pos.lnum = list_find_nr(l, 0L, &error);
21166 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021167 return NULL; /* invalid line number */
21168
21169 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021170 pos.col = list_find_nr(l, 1L, &error);
21171 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021172 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021173 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021174
21175 /* We accept "$" for the column number: last column. */
21176 li = list_find(l, 1L);
21177 if (li != NULL && li->li_tv.v_type == VAR_STRING
21178 && li->li_tv.vval.v_string != NULL
21179 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21180 pos.col = len + 1;
21181
Bram Moolenaara5525202006-03-02 22:52:09 +000021182 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021183 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021184 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021185 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021186
Bram Moolenaara5525202006-03-02 22:52:09 +000021187#ifdef FEAT_VIRTUALEDIT
21188 /* Get the virtual offset. Defaults to zero. */
21189 pos.coladd = list_find_nr(l, 2L, &error);
21190 if (error)
21191 pos.coladd = 0;
21192#endif
21193
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021194 return &pos;
21195 }
21196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021197 name = get_tv_string_chk(varp);
21198 if (name == NULL)
21199 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021200 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021202 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21203 {
21204 if (VIsual_active)
21205 return &VIsual;
21206 return &curwin->w_cursor;
21207 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021208 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021210 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21212 return NULL;
21213 return pp;
21214 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021215
21216#ifdef FEAT_VIRTUALEDIT
21217 pos.coladd = 0;
21218#endif
21219
Bram Moolenaar477933c2007-07-17 14:32:23 +000021220 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021221 {
21222 pos.col = 0;
21223 if (name[1] == '0') /* "w0": first visible line */
21224 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021225 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021226 pos.lnum = curwin->w_topline;
21227 return &pos;
21228 }
21229 else if (name[1] == '$') /* "w$": last visible line */
21230 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021231 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021232 pos.lnum = curwin->w_botline - 1;
21233 return &pos;
21234 }
21235 }
21236 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021238 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 {
21240 pos.lnum = curbuf->b_ml.ml_line_count;
21241 pos.col = 0;
21242 }
21243 else
21244 {
21245 pos.lnum = curwin->w_cursor.lnum;
21246 pos.col = (colnr_T)STRLEN(ml_get_curline());
21247 }
21248 return &pos;
21249 }
21250 return NULL;
21251}
21252
21253/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021254 * Convert list in "arg" into a position and optional file number.
21255 * When "fnump" is NULL there is no file number, only 3 items.
21256 * Note that the column is passed on as-is, the caller may want to decrement
21257 * it to use 1 for the first column.
21258 * Return FAIL when conversion is not possible, doesn't check the position for
21259 * validity.
21260 */
21261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021262list2fpos(
21263 typval_T *arg,
21264 pos_T *posp,
21265 int *fnump,
21266 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021267{
21268 list_T *l = arg->vval.v_list;
21269 long i = 0;
21270 long n;
21271
Bram Moolenaar493c1782014-05-28 14:34:46 +020021272 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21273 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021274 if (arg->v_type != VAR_LIST
21275 || l == NULL
21276 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021277 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021278 return FAIL;
21279
21280 if (fnump != NULL)
21281 {
21282 n = list_find_nr(l, i++, NULL); /* fnum */
21283 if (n < 0)
21284 return FAIL;
21285 if (n == 0)
21286 n = curbuf->b_fnum; /* current buffer */
21287 *fnump = n;
21288 }
21289
21290 n = list_find_nr(l, i++, NULL); /* lnum */
21291 if (n < 0)
21292 return FAIL;
21293 posp->lnum = n;
21294
21295 n = list_find_nr(l, i++, NULL); /* col */
21296 if (n < 0)
21297 return FAIL;
21298 posp->col = n;
21299
21300#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021301 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021302 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021303 posp->coladd = 0;
21304 else
21305 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021306#endif
21307
Bram Moolenaar493c1782014-05-28 14:34:46 +020021308 if (curswantp != NULL)
21309 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21310
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021311 return OK;
21312}
21313
21314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 * Get the length of an environment variable name.
21316 * Advance "arg" to the first character after the name.
21317 * Return 0 for error.
21318 */
21319 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021320get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321{
21322 char_u *p;
21323 int len;
21324
21325 for (p = *arg; vim_isIDc(*p); ++p)
21326 ;
21327 if (p == *arg) /* no name found */
21328 return 0;
21329
21330 len = (int)(p - *arg);
21331 *arg = p;
21332 return len;
21333}
21334
21335/*
21336 * Get the length of the name of a function or internal variable.
21337 * "arg" is advanced to the first non-white character after the name.
21338 * Return 0 if something is wrong.
21339 */
21340 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021341get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342{
21343 char_u *p;
21344 int len;
21345
21346 /* Find the end of the name. */
21347 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021348 {
21349 if (*p == ':')
21350 {
21351 /* "s:" is start of "s:var", but "n:" is not and can be used in
21352 * slice "[n:]". Also "xx:" is not a namespace. */
21353 len = (int)(p - *arg);
21354 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21355 || len > 1)
21356 break;
21357 }
21358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 if (p == *arg) /* no name found */
21360 return 0;
21361
21362 len = (int)(p - *arg);
21363 *arg = skipwhite(p);
21364
21365 return len;
21366}
21367
21368/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021369 * Get the length of the name of a variable or function.
21370 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021372 * Return -1 if curly braces expansion failed.
21373 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 * If the name contains 'magic' {}'s, expand them and return the
21375 * expanded name in an allocated string via 'alias' - caller must free.
21376 */
21377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021378get_name_len(
21379 char_u **arg,
21380 char_u **alias,
21381 int evaluate,
21382 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383{
21384 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 char_u *p;
21386 char_u *expr_start;
21387 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388
21389 *alias = NULL; /* default to no alias */
21390
21391 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21392 && (*arg)[2] == (int)KE_SNR)
21393 {
21394 /* hard coded <SNR>, already translated */
21395 *arg += 3;
21396 return get_id_len(arg) + 3;
21397 }
21398 len = eval_fname_script(*arg);
21399 if (len > 0)
21400 {
21401 /* literal "<SID>", "s:" or "<SNR>" */
21402 *arg += len;
21403 }
21404
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021406 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021408 p = find_name_end(*arg, &expr_start, &expr_end,
21409 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 if (expr_start != NULL)
21411 {
21412 char_u *temp_string;
21413
21414 if (!evaluate)
21415 {
21416 len += (int)(p - *arg);
21417 *arg = skipwhite(p);
21418 return len;
21419 }
21420
21421 /*
21422 * Include any <SID> etc in the expanded string:
21423 * Thus the -len here.
21424 */
21425 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21426 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021427 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428 *alias = temp_string;
21429 *arg = skipwhite(p);
21430 return (int)STRLEN(temp_string);
21431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432
21433 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021434 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 EMSG2(_(e_invexpr2), *arg);
21436
21437 return len;
21438}
21439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021440/*
21441 * Find the end of a variable or function name, taking care of magic braces.
21442 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21443 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021444 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021445 * Return a pointer to just after the name. Equal to "arg" if there is no
21446 * valid name.
21447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021449find_name_end(
21450 char_u *arg,
21451 char_u **expr_start,
21452 char_u **expr_end,
21453 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021455 int mb_nest = 0;
21456 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021458 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021460 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021462 *expr_start = NULL;
21463 *expr_end = NULL;
21464 }
21465
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021466 /* Quick check for valid starting character. */
21467 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21468 return arg;
21469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021470 for (p = arg; *p != NUL
21471 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021472 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021473 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021474 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021475 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021476 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021477 if (*p == '\'')
21478 {
21479 /* skip over 'string' to avoid counting [ and ] inside it. */
21480 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21481 ;
21482 if (*p == NUL)
21483 break;
21484 }
21485 else if (*p == '"')
21486 {
21487 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21488 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21489 if (*p == '\\' && p[1] != NUL)
21490 ++p;
21491 if (*p == NUL)
21492 break;
21493 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021494 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21495 {
21496 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021497 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021498 len = (int)(p - arg);
21499 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021500 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021501 break;
21502 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021504 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021506 if (*p == '[')
21507 ++br_nest;
21508 else if (*p == ']')
21509 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021512 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021514 if (*p == '{')
21515 {
21516 mb_nest++;
21517 if (expr_start != NULL && *expr_start == NULL)
21518 *expr_start = p;
21519 }
21520 else if (*p == '}')
21521 {
21522 mb_nest--;
21523 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21524 *expr_end = p;
21525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527 }
21528
21529 return p;
21530}
21531
21532/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021533 * Expands out the 'magic' {}'s in a variable/function name.
21534 * Note that this can call itself recursively, to deal with
21535 * constructs like foo{bar}{baz}{bam}
21536 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21537 * "in_start" ^
21538 * "expr_start" ^
21539 * "expr_end" ^
21540 * "in_end" ^
21541 *
21542 * Returns a new allocated string, which the caller must free.
21543 * Returns NULL for failure.
21544 */
21545 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021546make_expanded_name(
21547 char_u *in_start,
21548 char_u *expr_start,
21549 char_u *expr_end,
21550 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021551{
21552 char_u c1;
21553 char_u *retval = NULL;
21554 char_u *temp_result;
21555 char_u *nextcmd = NULL;
21556
21557 if (expr_end == NULL || in_end == NULL)
21558 return NULL;
21559 *expr_start = NUL;
21560 *expr_end = NUL;
21561 c1 = *in_end;
21562 *in_end = NUL;
21563
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021564 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021565 if (temp_result != NULL && nextcmd == NULL)
21566 {
21567 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21568 + (in_end - expr_end) + 1));
21569 if (retval != NULL)
21570 {
21571 STRCPY(retval, in_start);
21572 STRCAT(retval, temp_result);
21573 STRCAT(retval, expr_end + 1);
21574 }
21575 }
21576 vim_free(temp_result);
21577
21578 *in_end = c1; /* put char back for error messages */
21579 *expr_start = '{';
21580 *expr_end = '}';
21581
21582 if (retval != NULL)
21583 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021584 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021585 if (expr_start != NULL)
21586 {
21587 /* Further expansion! */
21588 temp_result = make_expanded_name(retval, expr_start,
21589 expr_end, temp_result);
21590 vim_free(retval);
21591 retval = temp_result;
21592 }
21593 }
21594
21595 return retval;
21596}
21597
21598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021600 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 */
21602 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021603eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021605 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21606}
21607
21608/*
21609 * Return TRUE if character "c" can be used as the first character in a
21610 * variable or function name (excluding '{' and '}').
21611 */
21612 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021613eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021614{
21615 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616}
21617
21618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 * Set number v: variable to "val".
21620 */
21621 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021622set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021624 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625}
21626
21627/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021628 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 */
21630 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021631get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021633 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634}
21635
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021636/*
21637 * Get string v: variable value. Uses a static buffer, can only be used once.
21638 */
21639 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021640get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021641{
21642 return get_tv_string(&vimvars[idx].vv_tv);
21643}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021644
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021646 * Get List v: variable value. Caller must take care of reference count when
21647 * needed.
21648 */
21649 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021650get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021651{
21652 return vimvars[idx].vv_list;
21653}
21654
21655/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021656 * Set v:char to character "c".
21657 */
21658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021659set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021660{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021661 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021662
21663#ifdef FEAT_MBYTE
21664 if (has_mbyte)
21665 buf[(*mb_char2bytes)(c, buf)] = NUL;
21666 else
21667#endif
21668 {
21669 buf[0] = c;
21670 buf[1] = NUL;
21671 }
21672 set_vim_var_string(VV_CHAR, buf, -1);
21673}
21674
21675/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021676 * Set v:count to "count" and v:count1 to "count1".
21677 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678 */
21679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021680set_vcount(
21681 long count,
21682 long count1,
21683 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021685 if (set_prevcount)
21686 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021687 vimvars[VV_COUNT].vv_nr = count;
21688 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689}
21690
21691/*
21692 * Set string v: variable to a copy of "val".
21693 */
21694 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021695set_vim_var_string(
21696 int idx,
21697 char_u *val,
21698 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021699{
Bram Moolenaara542c682016-01-31 16:28:04 +010021700 clear_tv(&vimvars[idx].vv_di.di_tv);
21701 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021703 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021705 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021707 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708}
21709
21710/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021711 * Set List v: variable to "val".
21712 */
21713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021714set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021715{
Bram Moolenaara542c682016-01-31 16:28:04 +010021716 clear_tv(&vimvars[idx].vv_di.di_tv);
21717 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021718 vimvars[idx].vv_list = val;
21719 if (val != NULL)
21720 ++val->lv_refcount;
21721}
21722
21723/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021724 * Set Dictionary v: variable to "val".
21725 */
21726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021727set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021728{
21729 int todo;
21730 hashitem_T *hi;
21731
Bram Moolenaara542c682016-01-31 16:28:04 +010021732 clear_tv(&vimvars[idx].vv_di.di_tv);
21733 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021734 vimvars[idx].vv_dict = val;
21735 if (val != NULL)
21736 {
21737 ++val->dv_refcount;
21738
21739 /* Set readonly */
21740 todo = (int)val->dv_hashtab.ht_used;
21741 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21742 {
21743 if (HASHITEM_EMPTY(hi))
21744 continue;
21745 --todo;
21746 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21747 }
21748 }
21749}
21750
21751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 * Set v:register if needed.
21753 */
21754 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021755set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756{
21757 char_u regname;
21758
21759 if (c == 0 || c == ' ')
21760 regname = '"';
21761 else
21762 regname = c;
21763 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021764 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 set_vim_var_string(VV_REG, &regname, 1);
21766}
21767
21768/*
21769 * Get or set v:exception. If "oldval" == NULL, return the current value.
21770 * Otherwise, restore the value to "oldval" and return NULL.
21771 * Must always be called in pairs to save and restore v:exception! Does not
21772 * take care of memory allocations.
21773 */
21774 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021775v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776{
21777 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021778 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779
Bram Moolenaare9a41262005-01-15 22:18:47 +000021780 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 return NULL;
21782}
21783
21784/*
21785 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21786 * Otherwise, restore the value to "oldval" and return NULL.
21787 * Must always be called in pairs to save and restore v:throwpoint! Does not
21788 * take care of memory allocations.
21789 */
21790 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021791v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792{
21793 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021794 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795
Bram Moolenaare9a41262005-01-15 22:18:47 +000021796 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797 return NULL;
21798}
21799
21800#if defined(FEAT_AUTOCMD) || defined(PROTO)
21801/*
21802 * Set v:cmdarg.
21803 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21804 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21805 * Must always be called in pairs!
21806 */
21807 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021808set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809{
21810 char_u *oldval;
21811 char_u *newval;
21812 unsigned len;
21813
Bram Moolenaare9a41262005-01-15 22:18:47 +000021814 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021815 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021817 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021818 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021819 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 }
21821
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021822 if (eap->force_bin == FORCE_BIN)
21823 len = 6;
21824 else if (eap->force_bin == FORCE_NOBIN)
21825 len = 8;
21826 else
21827 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021828
21829 if (eap->read_edit)
21830 len += 7;
21831
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021832 if (eap->force_ff != 0)
21833 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21834# ifdef FEAT_MBYTE
21835 if (eap->force_enc != 0)
21836 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021837 if (eap->bad_char != 0)
21838 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021839# endif
21840
21841 newval = alloc(len + 1);
21842 if (newval == NULL)
21843 return NULL;
21844
21845 if (eap->force_bin == FORCE_BIN)
21846 sprintf((char *)newval, " ++bin");
21847 else if (eap->force_bin == FORCE_NOBIN)
21848 sprintf((char *)newval, " ++nobin");
21849 else
21850 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021851
21852 if (eap->read_edit)
21853 STRCAT(newval, " ++edit");
21854
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021855 if (eap->force_ff != 0)
21856 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21857 eap->cmd + eap->force_ff);
21858# ifdef FEAT_MBYTE
21859 if (eap->force_enc != 0)
21860 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21861 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021862 if (eap->bad_char == BAD_KEEP)
21863 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21864 else if (eap->bad_char == BAD_DROP)
21865 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21866 else if (eap->bad_char != 0)
21867 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021868# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021869 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021870 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871}
21872#endif
21873
21874/*
21875 * Get the value of internal variable "name".
21876 * Return OK or FAIL.
21877 */
21878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021879get_var_tv(
21880 char_u *name,
21881 int len, /* length of "name" */
21882 typval_T *rettv, /* NULL when only checking existence */
21883 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21884 int verbose, /* may give error message */
21885 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886{
21887 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021888 typval_T *tv = NULL;
21889 typval_T atv;
21890 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892
21893 /* truncate the name, so that we can use strcmp() */
21894 cc = name[len];
21895 name[len] = NUL;
21896
21897 /*
21898 * Check for "b:changedtick".
21899 */
21900 if (STRCMP(name, "b:changedtick") == 0)
21901 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021902 atv.v_type = VAR_NUMBER;
21903 atv.vval.v_number = curbuf->b_changedtick;
21904 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 }
21906
21907 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 * Check for user-defined variables.
21909 */
21910 else
21911 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021912 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021914 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021915 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021916 if (dip != NULL)
21917 *dip = v;
21918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 }
21920
Bram Moolenaare9a41262005-01-15 22:18:47 +000021921 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021923 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021924 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 ret = FAIL;
21926 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021927 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021928 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929
21930 name[len] = cc;
21931
21932 return ret;
21933}
21934
21935/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021936 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21937 * Also handle function call with Funcref variable: func(expr)
21938 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21939 */
21940 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021941handle_subscript(
21942 char_u **arg,
21943 typval_T *rettv,
21944 int evaluate, /* do more than finding the end */
21945 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021946{
21947 int ret = OK;
21948 dict_T *selfdict = NULL;
21949 char_u *s;
21950 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021951 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021952
21953 while (ret == OK
21954 && (**arg == '['
21955 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021956 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021957 && !vim_iswhite(*(*arg - 1)))
21958 {
21959 if (**arg == '(')
21960 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021961 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021962 if (evaluate)
21963 {
21964 functv = *rettv;
21965 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021966
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021967 /* Invoke the function. Recursive! */
21968 s = functv.vval.v_string;
21969 }
21970 else
21971 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021972 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021973 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21974 &len, evaluate, selfdict);
21975
21976 /* Clear the funcref afterwards, so that deleting it while
21977 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021978 if (evaluate)
21979 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021980
21981 /* Stop the expression evaluation when immediately aborting on
21982 * error, or when an interrupt occurred or an exception was thrown
21983 * but not caught. */
21984 if (aborting())
21985 {
21986 if (ret == OK)
21987 clear_tv(rettv);
21988 ret = FAIL;
21989 }
21990 dict_unref(selfdict);
21991 selfdict = NULL;
21992 }
21993 else /* **arg == '[' || **arg == '.' */
21994 {
21995 dict_unref(selfdict);
21996 if (rettv->v_type == VAR_DICT)
21997 {
21998 selfdict = rettv->vval.v_dict;
21999 if (selfdict != NULL)
22000 ++selfdict->dv_refcount;
22001 }
22002 else
22003 selfdict = NULL;
22004 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22005 {
22006 clear_tv(rettv);
22007 ret = FAIL;
22008 }
22009 }
22010 }
22011 dict_unref(selfdict);
22012 return ret;
22013}
22014
22015/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022016 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022017 * value).
22018 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022019 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022020alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022021{
Bram Moolenaar33570922005-01-25 22:26:29 +000022022 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022023}
22024
22025/*
22026 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 * The string "s" must have been allocated, it is consumed.
22028 * Return NULL for out of memory, the variable otherwise.
22029 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022030 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022031alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032{
Bram Moolenaar33570922005-01-25 22:26:29 +000022033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022035 rettv = alloc_tv();
22036 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022038 rettv->v_type = VAR_STRING;
22039 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 }
22041 else
22042 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022043 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044}
22045
22046/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022047 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022050free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051{
22052 if (varp != NULL)
22053 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022054 switch (varp->v_type)
22055 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022056 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022057 func_unref(varp->vval.v_string);
22058 /*FALLTHROUGH*/
22059 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022060 vim_free(varp->vval.v_string);
22061 break;
22062 case VAR_LIST:
22063 list_unref(varp->vval.v_list);
22064 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022065 case VAR_DICT:
22066 dict_unref(varp->vval.v_dict);
22067 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022068 case VAR_JOB:
22069#ifdef FEAT_JOB
22070 job_unref(varp->vval.v_job);
22071 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022072#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022073 case VAR_CHANNEL:
22074#ifdef FEAT_CHANNEL
22075 channel_unref(varp->vval.v_channel);
22076 break;
22077#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022078 case VAR_NUMBER:
22079 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022080 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022081 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022082 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 vim_free(varp);
22085 }
22086}
22087
22088/*
22089 * Free the memory for a variable value and set the value to NULL or 0.
22090 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022091 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022092clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093{
22094 if (varp != NULL)
22095 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022096 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022098 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022099 func_unref(varp->vval.v_string);
22100 /*FALLTHROUGH*/
22101 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022102 vim_free(varp->vval.v_string);
22103 varp->vval.v_string = NULL;
22104 break;
22105 case VAR_LIST:
22106 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022107 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022108 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022109 case VAR_DICT:
22110 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022111 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022112 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022113 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022114 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022115 varp->vval.v_number = 0;
22116 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022117 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022118#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022119 varp->vval.v_float = 0.0;
22120 break;
22121#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022122 case VAR_JOB:
22123#ifdef FEAT_JOB
22124 job_unref(varp->vval.v_job);
22125 varp->vval.v_job = NULL;
22126#endif
22127 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022128 case VAR_CHANNEL:
22129#ifdef FEAT_CHANNEL
22130 channel_unref(varp->vval.v_channel);
22131 varp->vval.v_channel = NULL;
22132#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022133 case VAR_UNKNOWN:
22134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022136 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 }
22138}
22139
22140/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022141 * Set the value of a variable to NULL without freeing items.
22142 */
22143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022144init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022145{
22146 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022147 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022148}
22149
22150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 * Get the number value of a variable.
22152 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022153 * For incompatible types, return 0.
22154 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22155 * caller of incompatible types: it sets *denote to TRUE if "denote"
22156 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 */
22158 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022159get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022161 int error = FALSE;
22162
22163 return get_tv_number_chk(varp, &error); /* return 0L on error */
22164}
22165
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022166 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022167get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022168{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022169 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022171 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022173 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022174 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022175 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022176#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022177 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022178 break;
22179#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022180 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022181 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022182 break;
22183 case VAR_STRING:
22184 if (varp->vval.v_string != NULL)
22185 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022186 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022187 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022188 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022189 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022190 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022191 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022192 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022193 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022194 case VAR_SPECIAL:
22195 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22196 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022197 case VAR_JOB:
22198#ifdef FEAT_JOB
22199 EMSG(_("E910: Using a Job as a Number"));
22200 break;
22201#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022202 case VAR_CHANNEL:
22203#ifdef FEAT_CHANNEL
22204 EMSG(_("E913: Using a Channel as a Number"));
22205 break;
22206#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022207 case VAR_UNKNOWN:
22208 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022211 if (denote == NULL) /* useful for values that must be unsigned */
22212 n = -1;
22213 else
22214 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022215 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216}
22217
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022218#ifdef FEAT_FLOAT
22219 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022220get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022221{
22222 switch (varp->v_type)
22223 {
22224 case VAR_NUMBER:
22225 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022226 case VAR_FLOAT:
22227 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022228 case VAR_FUNC:
22229 EMSG(_("E891: Using a Funcref as a Float"));
22230 break;
22231 case VAR_STRING:
22232 EMSG(_("E892: Using a String as a Float"));
22233 break;
22234 case VAR_LIST:
22235 EMSG(_("E893: Using a List as a Float"));
22236 break;
22237 case VAR_DICT:
22238 EMSG(_("E894: Using a Dictionary as a Float"));
22239 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022240 case VAR_SPECIAL:
22241 EMSG(_("E907: Using a special value as a Float"));
22242 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022243 case VAR_JOB:
22244# ifdef FEAT_JOB
22245 EMSG(_("E911: Using a Job as a Float"));
22246 break;
22247# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022248 case VAR_CHANNEL:
22249# ifdef FEAT_CHANNEL
22250 EMSG(_("E914: Using a Channel as a Float"));
22251 break;
22252# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022253 case VAR_UNKNOWN:
22254 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022255 break;
22256 }
22257 return 0;
22258}
22259#endif
22260
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022262 * Get the lnum from the first argument.
22263 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022264 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265 */
22266 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022267get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268{
Bram Moolenaar33570922005-01-25 22:26:29 +000022269 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270 linenr_T lnum;
22271
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022272 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273 if (lnum == 0) /* no valid number, try using line() */
22274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022275 rettv.v_type = VAR_NUMBER;
22276 f_line(argvars, &rettv);
22277 lnum = rettv.vval.v_number;
22278 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279 }
22280 return lnum;
22281}
22282
22283/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022284 * Get the lnum from the first argument.
22285 * Also accepts "$", then "buf" is used.
22286 * Returns 0 on error.
22287 */
22288 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022289get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022290{
22291 if (argvars[0].v_type == VAR_STRING
22292 && argvars[0].vval.v_string != NULL
22293 && argvars[0].vval.v_string[0] == '$'
22294 && buf != NULL)
22295 return buf->b_ml.ml_line_count;
22296 return get_tv_number_chk(&argvars[0], NULL);
22297}
22298
22299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300 * Get the string value of a variable.
22301 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022302 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22303 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 * If the String variable has never been set, return an empty string.
22305 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022306 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22307 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308 */
22309 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022310get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311{
22312 static char_u mybuf[NUMBUFLEN];
22313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022314 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315}
22316
22317 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022318get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022320 char_u *res = get_tv_string_buf_chk(varp, buf);
22321
22322 return res != NULL ? res : (char_u *)"";
22323}
22324
Bram Moolenaar7d647822014-04-05 21:28:56 +020022325/*
22326 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22327 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022329get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022330{
22331 static char_u mybuf[NUMBUFLEN];
22332
22333 return get_tv_string_buf_chk(varp, mybuf);
22334}
22335
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022336 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022337get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022338{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022339 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022341 case VAR_NUMBER:
22342 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22343 return buf;
22344 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022345 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022346 break;
22347 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022348 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022349 break;
22350 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022351 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022352 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022353 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022354#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022355 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022356 break;
22357#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022358 case VAR_STRING:
22359 if (varp->vval.v_string != NULL)
22360 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022361 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022362 case VAR_SPECIAL:
22363 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22364 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022365 case VAR_JOB:
22366#ifdef FEAT_JOB
22367 {
22368 job_T *job = varp->vval.v_job;
22369 char *status = job->jv_status == JOB_FAILED ? "fail"
22370 : job->jv_status == JOB_ENDED ? "dead"
22371 : "run";
22372# ifdef UNIX
22373 vim_snprintf((char *)buf, NUMBUFLEN,
22374 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022375# elif defined(WIN32)
22376 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022377 "process %ld %s",
22378 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022379 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022380# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022381 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022382 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22383# endif
22384 return buf;
22385 }
22386#endif
22387 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022388 case VAR_CHANNEL:
22389#ifdef FEAT_CHANNEL
22390 {
22391 channel_T *channel = varp->vval.v_channel;
22392 char *status = channel_status(channel);
22393
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022394 if (channel == NULL)
22395 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22396 else
22397 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022398 "channel %d %s", channel->ch_id, status);
22399 return buf;
22400 }
22401#endif
22402 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022403 case VAR_UNKNOWN:
22404 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022405 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022407 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408}
22409
22410/*
22411 * Find variable "name" in the list of variables.
22412 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022413 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022414 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022415 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022417 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022418find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022421 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422
Bram Moolenaara7043832005-01-21 11:56:39 +000022423 ht = find_var_ht(name, &varname);
22424 if (htp != NULL)
22425 *htp = ht;
22426 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022428 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429}
22430
22431/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022432 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022433 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022435 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022436find_var_in_ht(
22437 hashtab_T *ht,
22438 int htname,
22439 char_u *varname,
22440 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022441{
Bram Moolenaar33570922005-01-25 22:26:29 +000022442 hashitem_T *hi;
22443
22444 if (*varname == NUL)
22445 {
22446 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022447 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022448 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022449 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022450 case 'g': return &globvars_var;
22451 case 'v': return &vimvars_var;
22452 case 'b': return &curbuf->b_bufvar;
22453 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022454#ifdef FEAT_WINDOWS
22455 case 't': return &curtab->tp_winvar;
22456#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022457 case 'l': return current_funccal == NULL
22458 ? NULL : &current_funccal->l_vars_var;
22459 case 'a': return current_funccal == NULL
22460 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022461 }
22462 return NULL;
22463 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022464
22465 hi = hash_find(ht, varname);
22466 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022467 {
22468 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022469 * worked find the variable again. Don't auto-load a script if it was
22470 * loaded already, otherwise it would be loaded every time when
22471 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022472 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022473 {
22474 /* Note: script_autoload() may make "hi" invalid. It must either
22475 * be obtained again or not used. */
22476 if (!script_autoload(varname, FALSE) || aborting())
22477 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022478 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022479 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022480 if (HASHITEM_EMPTY(hi))
22481 return NULL;
22482 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022483 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022484}
22485
22486/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022487 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022488 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022489 * Set "varname" to the start of name without ':'.
22490 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022491 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022492find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022494 hashitem_T *hi;
22495
Bram Moolenaar73627d02015-08-11 15:46:09 +020022496 if (name[0] == NUL)
22497 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 if (name[1] != ':')
22499 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022500 /* The name must not start with a colon or #. */
22501 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 return NULL;
22503 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022504
22505 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022506 hi = hash_find(&compat_hashtab, name);
22507 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022508 return &compat_hashtab;
22509
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022511 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022512 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 }
22514 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022515 if (*name == 'g') /* global variable */
22516 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022517 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22518 */
22519 if (vim_strchr(name + 2, ':') != NULL
22520 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022521 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022523 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022525 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022526#ifdef FEAT_WINDOWS
22527 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022528 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022529#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022530 if (*name == 'v') /* v: variable */
22531 return &vimvarht;
22532 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022533 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022534 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022535 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536 if (*name == 's' /* script variable */
22537 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22538 return &SCRIPT_VARS(current_SID);
22539 return NULL;
22540}
22541
22542/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022543 * Get function call environment based on bactrace debug level
22544 */
22545 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022546get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022547{
22548 int i;
22549 funccall_T *funccal;
22550 funccall_T *temp_funccal;
22551
22552 funccal = current_funccal;
22553 if (debug_backtrace_level > 0)
22554 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022555 for (i = 0; i < debug_backtrace_level; i++)
22556 {
22557 temp_funccal = funccal->caller;
22558 if (temp_funccal)
22559 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022560 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022561 /* backtrace level overflow. reset to max */
22562 debug_backtrace_level = i;
22563 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022564 }
22565 return funccal;
22566}
22567
22568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022570 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 * Returns NULL when it doesn't exist.
22572 */
22573 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022574get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575{
Bram Moolenaar33570922005-01-25 22:26:29 +000022576 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022578 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 if (v == NULL)
22580 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022581 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582}
22583
22584/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022585 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 * sourcing this script and when executing functions defined in the script.
22587 */
22588 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022589new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590{
Bram Moolenaara7043832005-01-21 11:56:39 +000022591 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 hashtab_T *ht;
22593 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022594
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22596 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022597 /* Re-allocating ga_data means that an ht_array pointing to
22598 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022599 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022600 for (i = 1; i <= ga_scripts.ga_len; ++i)
22601 {
22602 ht = &SCRIPT_VARS(i);
22603 if (ht->ht_mask == HT_INIT_SIZE - 1)
22604 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022605 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022606 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022607 }
22608
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 while (ga_scripts.ga_len < id)
22610 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022611 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022612 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022613 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 }
22616 }
22617}
22618
22619/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22621 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 */
22623 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022624init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625{
Bram Moolenaar33570922005-01-25 22:26:29 +000022626 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022627 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022628 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022629 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022630 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022631 dict_var->di_tv.vval.v_dict = dict;
22632 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022633 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022634 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22635 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636}
22637
22638/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022639 * Unreference a dictionary initialized by init_var_dict().
22640 */
22641 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022642unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022643{
22644 /* Now the dict needs to be freed if no one else is using it, go back to
22645 * normal reference counting. */
22646 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22647 dict_unref(dict);
22648}
22649
22650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022652 * Frees all allocated variables and the value they contain.
22653 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 */
22655 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022656vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022657{
22658 vars_clear_ext(ht, TRUE);
22659}
22660
22661/*
22662 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22663 */
22664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022665vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666{
Bram Moolenaara7043832005-01-21 11:56:39 +000022667 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022668 hashitem_T *hi;
22669 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670
Bram Moolenaar33570922005-01-25 22:26:29 +000022671 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022672 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022673 for (hi = ht->ht_array; todo > 0; ++hi)
22674 {
22675 if (!HASHITEM_EMPTY(hi))
22676 {
22677 --todo;
22678
Bram Moolenaar33570922005-01-25 22:26:29 +000022679 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022680 * ht_array might change then. hash_clear() takes care of it
22681 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022682 v = HI2DI(hi);
22683 if (free_val)
22684 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022685 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022686 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022687 }
22688 }
22689 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022690 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691}
22692
Bram Moolenaara7043832005-01-21 11:56:39 +000022693/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022694 * Delete a variable from hashtab "ht" at item "hi".
22695 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022698delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699{
Bram Moolenaar33570922005-01-25 22:26:29 +000022700 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022701
22702 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022703 clear_tv(&di->di_tv);
22704 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705}
22706
22707/*
22708 * List the value of one internal variable.
22709 */
22710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022711list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022713 char_u *tofree;
22714 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022715 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022716
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022717 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022718 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022719 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022720 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721}
22722
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022724list_one_var_a(
22725 char_u *prefix,
22726 char_u *name,
22727 int type,
22728 char_u *string,
22729 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730{
Bram Moolenaar31859182007-08-14 20:41:13 +000022731 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22732 msg_start();
22733 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 if (name != NULL) /* "a:" vars don't have a name stored */
22735 msg_puts(name);
22736 msg_putchar(' ');
22737 msg_advance(22);
22738 if (type == VAR_NUMBER)
22739 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022740 else if (type == VAR_FUNC)
22741 msg_putchar('*');
22742 else if (type == VAR_LIST)
22743 {
22744 msg_putchar('[');
22745 if (*string == '[')
22746 ++string;
22747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022748 else if (type == VAR_DICT)
22749 {
22750 msg_putchar('{');
22751 if (*string == '{')
22752 ++string;
22753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 else
22755 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022756
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022758
22759 if (type == VAR_FUNC)
22760 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022761 if (*first)
22762 {
22763 msg_clr_eos();
22764 *first = FALSE;
22765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766}
22767
22768/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022769 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 * If the variable already exists, the value is updated.
22771 * Otherwise the variable is created.
22772 */
22773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022774set_var(
22775 char_u *name,
22776 typval_T *tv,
22777 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778{
Bram Moolenaar33570922005-01-25 22:26:29 +000022779 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022781 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022783 ht = find_var_ht(name, &varname);
22784 if (ht == NULL || *varname == NUL)
22785 {
22786 EMSG2(_(e_illvar), name);
22787 return;
22788 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022789 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022790
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022791 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22792 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022793
Bram Moolenaar33570922005-01-25 22:26:29 +000022794 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022796 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022797 if (var_check_ro(v->di_flags, name, FALSE)
22798 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022799 return;
22800 if (v->di_tv.v_type != tv->v_type
22801 && !((v->di_tv.v_type == VAR_STRING
22802 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022803 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022804 || tv->v_type == VAR_NUMBER))
22805#ifdef FEAT_FLOAT
22806 && !((v->di_tv.v_type == VAR_NUMBER
22807 || v->di_tv.v_type == VAR_FLOAT)
22808 && (tv->v_type == VAR_NUMBER
22809 || tv->v_type == VAR_FLOAT))
22810#endif
22811 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022812 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022813 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022814 return;
22815 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022816
22817 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022818 * Handle setting internal v: variables separately where needed to
22819 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022820 */
22821 if (ht == &vimvarht)
22822 {
22823 if (v->di_tv.v_type == VAR_STRING)
22824 {
22825 vim_free(v->di_tv.vval.v_string);
22826 if (copy || tv->v_type != VAR_STRING)
22827 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22828 else
22829 {
22830 /* Take over the string to avoid an extra alloc/free. */
22831 v->di_tv.vval.v_string = tv->vval.v_string;
22832 tv->vval.v_string = NULL;
22833 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022834 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022835 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022836 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022837 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022838 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022839 if (STRCMP(varname, "searchforward") == 0)
22840 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022841#ifdef FEAT_SEARCH_EXTRA
22842 else if (STRCMP(varname, "hlsearch") == 0)
22843 {
22844 no_hlsearch = !v->di_tv.vval.v_number;
22845 redraw_all_later(SOME_VALID);
22846 }
22847#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022848 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022849 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022850 else if (v->di_tv.v_type != tv->v_type)
22851 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022852 }
22853
22854 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 }
22856 else /* add a new variable */
22857 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022858 /* Can't add "v:" variable. */
22859 if (ht == &vimvarht)
22860 {
22861 EMSG2(_(e_illvar), name);
22862 return;
22863 }
22864
Bram Moolenaar92124a32005-06-17 22:03:40 +000022865 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022866 if (!valid_varname(varname))
22867 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022868
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022869 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22870 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022871 if (v == NULL)
22872 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022873 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022874 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022876 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 return;
22878 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022879 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022881
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022882 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022883 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022884 else
22885 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022886 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022887 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022888 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890}
22891
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022892/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022893 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022894 * Also give an error message.
22895 */
22896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022897var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022898{
22899 if (flags & DI_FLAGS_RO)
22900 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022901 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022902 return TRUE;
22903 }
22904 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22905 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022906 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022907 return TRUE;
22908 }
22909 return FALSE;
22910}
22911
22912/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022913 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22914 * Also give an error message.
22915 */
22916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022917var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022918{
22919 if (flags & DI_FLAGS_FIX)
22920 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022921 EMSG2(_("E795: Cannot delete variable %s"),
22922 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022923 return TRUE;
22924 }
22925 return FALSE;
22926}
22927
22928/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022929 * Check if a funcref is assigned to a valid variable name.
22930 * Return TRUE and give an error if not.
22931 */
22932 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022933var_check_func_name(
22934 char_u *name, /* points to start of variable name */
22935 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022936{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022937 /* Allow for w: b: s: and t:. */
22938 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022939 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22940 ? name[2] : name[0]))
22941 {
22942 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22943 name);
22944 return TRUE;
22945 }
22946 /* Don't allow hiding a function. When "v" is not NULL we might be
22947 * assigning another function to the same var, the type is checked
22948 * below. */
22949 if (new_var && function_exists(name))
22950 {
22951 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22952 name);
22953 return TRUE;
22954 }
22955 return FALSE;
22956}
22957
22958/*
22959 * Check if a variable name is valid.
22960 * Return FALSE and give an error if not.
22961 */
22962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022963valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022964{
22965 char_u *p;
22966
22967 for (p = varname; *p != NUL; ++p)
22968 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22969 && *p != AUTOLOAD_CHAR)
22970 {
22971 EMSG2(_(e_illvar), varname);
22972 return FALSE;
22973 }
22974 return TRUE;
22975}
22976
22977/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022978 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022979 * Also give an error message, using "name" or _("name") when use_gettext is
22980 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022981 */
22982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022983tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022984{
22985 if (lock & VAR_LOCKED)
22986 {
22987 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022988 name == NULL ? (char_u *)_("Unknown")
22989 : use_gettext ? (char_u *)_(name)
22990 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022991 return TRUE;
22992 }
22993 if (lock & VAR_FIXED)
22994 {
22995 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022996 name == NULL ? (char_u *)_("Unknown")
22997 : use_gettext ? (char_u *)_(name)
22998 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022999 return TRUE;
23000 }
23001 return FALSE;
23002}
23003
23004/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023005 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023006 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023007 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023008 * It is OK for "from" and "to" to point to the same item. This is used to
23009 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023010 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023012copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023014 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023015 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023016 switch (from->v_type)
23017 {
23018 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023019 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023020 to->vval.v_number = from->vval.v_number;
23021 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023022 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023023#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023024 to->vval.v_float = from->vval.v_float;
23025 break;
23026#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023027 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023028#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023029 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023030 if (to->vval.v_job != NULL)
23031 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023032 break;
23033#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023034 case VAR_CHANNEL:
23035#ifdef FEAT_CHANNEL
23036 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023037 if (to->vval.v_channel != NULL)
23038 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023039 break;
23040#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023041 case VAR_STRING:
23042 case VAR_FUNC:
23043 if (from->vval.v_string == NULL)
23044 to->vval.v_string = NULL;
23045 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023046 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023047 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023048 if (from->v_type == VAR_FUNC)
23049 func_ref(to->vval.v_string);
23050 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023051 break;
23052 case VAR_LIST:
23053 if (from->vval.v_list == NULL)
23054 to->vval.v_list = NULL;
23055 else
23056 {
23057 to->vval.v_list = from->vval.v_list;
23058 ++to->vval.v_list->lv_refcount;
23059 }
23060 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023061 case VAR_DICT:
23062 if (from->vval.v_dict == NULL)
23063 to->vval.v_dict = NULL;
23064 else
23065 {
23066 to->vval.v_dict = from->vval.v_dict;
23067 ++to->vval.v_dict->dv_refcount;
23068 }
23069 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023070 case VAR_UNKNOWN:
23071 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023072 break;
23073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074}
23075
23076/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023077 * Make a copy of an item.
23078 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023079 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23080 * reference to an already copied list/dict can be used.
23081 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023082 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023083 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023084item_copy(
23085 typval_T *from,
23086 typval_T *to,
23087 int deep,
23088 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023089{
23090 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023091 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023092
Bram Moolenaar33570922005-01-25 22:26:29 +000023093 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023094 {
23095 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023096 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023097 }
23098 ++recurse;
23099
23100 switch (from->v_type)
23101 {
23102 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023103 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023104 case VAR_STRING:
23105 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023106 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023107 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023108 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023109 copy_tv(from, to);
23110 break;
23111 case VAR_LIST:
23112 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023113 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023114 if (from->vval.v_list == NULL)
23115 to->vval.v_list = NULL;
23116 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23117 {
23118 /* use the copy made earlier */
23119 to->vval.v_list = from->vval.v_list->lv_copylist;
23120 ++to->vval.v_list->lv_refcount;
23121 }
23122 else
23123 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23124 if (to->vval.v_list == NULL)
23125 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023126 break;
23127 case VAR_DICT:
23128 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023129 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023130 if (from->vval.v_dict == NULL)
23131 to->vval.v_dict = NULL;
23132 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23133 {
23134 /* use the copy made earlier */
23135 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23136 ++to->vval.v_dict->dv_refcount;
23137 }
23138 else
23139 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23140 if (to->vval.v_dict == NULL)
23141 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023142 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023143 case VAR_UNKNOWN:
23144 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023145 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023146 }
23147 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023148 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023149}
23150
23151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 * ":echo expr1 ..." print each argument separated with a space, add a
23153 * newline at the end.
23154 * ":echon expr1 ..." print each argument plain.
23155 */
23156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023157ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158{
23159 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023160 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023161 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 char_u *p;
23163 int needclr = TRUE;
23164 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023165 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166
23167 if (eap->skip)
23168 ++emsg_skip;
23169 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23170 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023171 /* If eval1() causes an error message the text from the command may
23172 * still need to be cleared. E.g., "echo 22,44". */
23173 need_clr_eos = needclr;
23174
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023176 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177 {
23178 /*
23179 * Report the invalid expression unless the expression evaluation
23180 * has been cancelled due to an aborting error, an interrupt, or an
23181 * exception.
23182 */
23183 if (!aborting())
23184 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023185 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 break;
23187 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023188 need_clr_eos = FALSE;
23189
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 if (!eap->skip)
23191 {
23192 if (atstart)
23193 {
23194 atstart = FALSE;
23195 /* Call msg_start() after eval1(), evaluating the expression
23196 * may cause a message to appear. */
23197 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023198 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023199 /* Mark the saved text as finishing the line, so that what
23200 * follows is displayed on a new line when scrolling back
23201 * at the more prompt. */
23202 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 }
23206 else if (eap->cmdidx == CMD_echo)
23207 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023208 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023209 if (p != NULL)
23210 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023212 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023214 if (*p != TAB && needclr)
23215 {
23216 /* remove any text still there from the command */
23217 msg_clr_eos();
23218 needclr = FALSE;
23219 }
23220 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 }
23222 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023223 {
23224#ifdef FEAT_MBYTE
23225 if (has_mbyte)
23226 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023227 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023228
23229 (void)msg_outtrans_len_attr(p, i, echo_attr);
23230 p += i - 1;
23231 }
23232 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023234 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023237 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023239 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 arg = skipwhite(arg);
23241 }
23242 eap->nextcmd = check_nextcmd(arg);
23243
23244 if (eap->skip)
23245 --emsg_skip;
23246 else
23247 {
23248 /* remove text that may still be there from the command */
23249 if (needclr)
23250 msg_clr_eos();
23251 if (eap->cmdidx == CMD_echo)
23252 msg_end();
23253 }
23254}
23255
23256/*
23257 * ":echohl {name}".
23258 */
23259 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023260ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261{
23262 int id;
23263
23264 id = syn_name2id(eap->arg);
23265 if (id == 0)
23266 echo_attr = 0;
23267 else
23268 echo_attr = syn_id2attr(id);
23269}
23270
23271/*
23272 * ":execute expr1 ..." execute the result of an expression.
23273 * ":echomsg expr1 ..." Print a message
23274 * ":echoerr expr1 ..." Print an error
23275 * Each gets spaces around each argument and a newline at the end for
23276 * echo commands
23277 */
23278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023279ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280{
23281 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023282 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283 int ret = OK;
23284 char_u *p;
23285 garray_T ga;
23286 int len;
23287 int save_did_emsg;
23288
23289 ga_init2(&ga, 1, 80);
23290
23291 if (eap->skip)
23292 ++emsg_skip;
23293 while (*arg != NUL && *arg != '|' && *arg != '\n')
23294 {
23295 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023296 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297 {
23298 /*
23299 * Report the invalid expression unless the expression evaluation
23300 * has been cancelled due to an aborting error, an interrupt, or an
23301 * exception.
23302 */
23303 if (!aborting())
23304 EMSG2(_(e_invexpr2), p);
23305 ret = FAIL;
23306 break;
23307 }
23308
23309 if (!eap->skip)
23310 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023311 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312 len = (int)STRLEN(p);
23313 if (ga_grow(&ga, len + 2) == FAIL)
23314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023315 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 ret = FAIL;
23317 break;
23318 }
23319 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023320 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023322 ga.ga_len += len;
23323 }
23324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023325 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 arg = skipwhite(arg);
23327 }
23328
23329 if (ret != FAIL && ga.ga_data != NULL)
23330 {
23331 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023334 out_flush();
23335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023336 else if (eap->cmdidx == CMD_echoerr)
23337 {
23338 /* We don't want to abort following commands, restore did_emsg. */
23339 save_did_emsg = did_emsg;
23340 EMSG((char_u *)ga.ga_data);
23341 if (!force_abort)
23342 did_emsg = save_did_emsg;
23343 }
23344 else if (eap->cmdidx == CMD_execute)
23345 do_cmdline((char_u *)ga.ga_data,
23346 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23347 }
23348
23349 ga_clear(&ga);
23350
23351 if (eap->skip)
23352 --emsg_skip;
23353
23354 eap->nextcmd = check_nextcmd(arg);
23355}
23356
23357/*
23358 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23359 * "arg" points to the "&" or '+' when called, to "option" when returning.
23360 * Returns NULL when no option name found. Otherwise pointer to the char
23361 * after the option name.
23362 */
23363 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023364find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023365{
23366 char_u *p = *arg;
23367
23368 ++p;
23369 if (*p == 'g' && p[1] == ':')
23370 {
23371 *opt_flags = OPT_GLOBAL;
23372 p += 2;
23373 }
23374 else if (*p == 'l' && p[1] == ':')
23375 {
23376 *opt_flags = OPT_LOCAL;
23377 p += 2;
23378 }
23379 else
23380 *opt_flags = 0;
23381
23382 if (!ASCII_ISALPHA(*p))
23383 return NULL;
23384 *arg = p;
23385
23386 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23387 p += 4; /* termcap option */
23388 else
23389 while (ASCII_ISALPHA(*p))
23390 ++p;
23391 return p;
23392}
23393
23394/*
23395 * ":function"
23396 */
23397 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023398ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399{
23400 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023401 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 int j;
23403 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023405 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406 char_u *name = NULL;
23407 char_u *p;
23408 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023409 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410 garray_T newargs;
23411 garray_T newlines;
23412 int varargs = FALSE;
23413 int mustend = FALSE;
23414 int flags = 0;
23415 ufunc_T *fp;
23416 int indent;
23417 int nesting;
23418 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023419 dictitem_T *v;
23420 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023421 static int func_nr = 0; /* number for nameless function */
23422 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023423 hashtab_T *ht;
23424 int todo;
23425 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023426 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023427
23428 /*
23429 * ":function" without argument: list functions.
23430 */
23431 if (ends_excmd(*eap->arg))
23432 {
23433 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023434 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023435 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023436 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023437 {
23438 if (!HASHITEM_EMPTY(hi))
23439 {
23440 --todo;
23441 fp = HI2UF(hi);
23442 if (!isdigit(*fp->uf_name))
23443 list_func_head(fp, FALSE);
23444 }
23445 }
23446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023447 eap->nextcmd = check_nextcmd(eap->arg);
23448 return;
23449 }
23450
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023451 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023452 * ":function /pat": list functions matching pattern.
23453 */
23454 if (*eap->arg == '/')
23455 {
23456 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23457 if (!eap->skip)
23458 {
23459 regmatch_T regmatch;
23460
23461 c = *p;
23462 *p = NUL;
23463 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23464 *p = c;
23465 if (regmatch.regprog != NULL)
23466 {
23467 regmatch.rm_ic = p_ic;
23468
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023469 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023470 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23471 {
23472 if (!HASHITEM_EMPTY(hi))
23473 {
23474 --todo;
23475 fp = HI2UF(hi);
23476 if (!isdigit(*fp->uf_name)
23477 && vim_regexec(&regmatch, fp->uf_name, 0))
23478 list_func_head(fp, FALSE);
23479 }
23480 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023481 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023482 }
23483 }
23484 if (*p == '/')
23485 ++p;
23486 eap->nextcmd = check_nextcmd(p);
23487 return;
23488 }
23489
23490 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023491 * Get the function name. There are these situations:
23492 * func normal function name
23493 * "name" == func, "fudi.fd_dict" == NULL
23494 * dict.func new dictionary entry
23495 * "name" == NULL, "fudi.fd_dict" set,
23496 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23497 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023498 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023499 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23500 * dict.func existing dict entry that's not a Funcref
23501 * "name" == NULL, "fudi.fd_dict" set,
23502 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023503 * s:func script-local function name
23504 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023507 name = trans_function_name(&p, eap->skip, 0, &fudi);
23508 paren = (vim_strchr(p, '(') != NULL);
23509 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 {
23511 /*
23512 * Return on an invalid expression in braces, unless the expression
23513 * evaluation has been cancelled due to an aborting error, an
23514 * interrupt, or an exception.
23515 */
23516 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023517 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023518 if (!eap->skip && fudi.fd_newkey != NULL)
23519 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023520 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523 else
23524 eap->skip = TRUE;
23525 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023526
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 /* An error in a function call during evaluation of an expression in magic
23528 * braces should not cause the function not to be defined. */
23529 saved_did_emsg = did_emsg;
23530 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531
23532 /*
23533 * ":function func" with only function name: list function.
23534 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023535 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 {
23537 if (!ends_excmd(*skipwhite(p)))
23538 {
23539 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023540 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 }
23542 eap->nextcmd = check_nextcmd(p);
23543 if (eap->nextcmd != NULL)
23544 *p = NUL;
23545 if (!eap->skip && !got_int)
23546 {
23547 fp = find_func(name);
23548 if (fp != NULL)
23549 {
23550 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023551 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023553 if (FUNCLINE(fp, j) == NULL)
23554 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 msg_putchar('\n');
23556 msg_outnum((long)(j + 1));
23557 if (j < 9)
23558 msg_putchar(' ');
23559 if (j < 99)
23560 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023561 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 out_flush(); /* show a line at a time */
23563 ui_breakcheck();
23564 }
23565 if (!got_int)
23566 {
23567 msg_putchar('\n');
23568 msg_puts((char_u *)" endfunction");
23569 }
23570 }
23571 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023572 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023574 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 }
23576
23577 /*
23578 * ":function name(arg1, arg2)" Define function.
23579 */
23580 p = skipwhite(p);
23581 if (*p != '(')
23582 {
23583 if (!eap->skip)
23584 {
23585 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023586 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 }
23588 /* attempt to continue by skipping some text */
23589 if (vim_strchr(p, '(') != NULL)
23590 p = vim_strchr(p, '(');
23591 }
23592 p = skipwhite(p + 1);
23593
23594 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23595 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23596
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023597 if (!eap->skip)
23598 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023599 /* Check the name of the function. Unless it's a dictionary function
23600 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023601 if (name != NULL)
23602 arg = name;
23603 else
23604 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023605 if (arg != NULL && (fudi.fd_di == NULL
23606 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023607 {
23608 if (*arg == K_SPECIAL)
23609 j = 3;
23610 else
23611 j = 0;
23612 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23613 : eval_isnamec(arg[j])))
23614 ++j;
23615 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023616 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023617 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023618 /* Disallow using the g: dict. */
23619 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23620 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023621 }
23622
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623 /*
23624 * Isolate the arguments: "arg1, arg2, ...)"
23625 */
23626 while (*p != ')')
23627 {
23628 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23629 {
23630 varargs = TRUE;
23631 p += 3;
23632 mustend = TRUE;
23633 }
23634 else
23635 {
23636 arg = p;
23637 while (ASCII_ISALNUM(*p) || *p == '_')
23638 ++p;
23639 if (arg == p || isdigit(*arg)
23640 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23641 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23642 {
23643 if (!eap->skip)
23644 EMSG2(_("E125: Illegal argument: %s"), arg);
23645 break;
23646 }
23647 if (ga_grow(&newargs, 1) == FAIL)
23648 goto erret;
23649 c = *p;
23650 *p = NUL;
23651 arg = vim_strsave(arg);
23652 if (arg == NULL)
23653 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023654
23655 /* Check for duplicate argument name. */
23656 for (i = 0; i < newargs.ga_len; ++i)
23657 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23658 {
23659 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023660 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023661 goto erret;
23662 }
23663
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23665 *p = c;
23666 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 if (*p == ',')
23668 ++p;
23669 else
23670 mustend = TRUE;
23671 }
23672 p = skipwhite(p);
23673 if (mustend && *p != ')')
23674 {
23675 if (!eap->skip)
23676 EMSG2(_(e_invarg2), eap->arg);
23677 break;
23678 }
23679 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023680 if (*p != ')')
23681 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023682 ++p; /* skip the ')' */
23683
Bram Moolenaare9a41262005-01-15 22:18:47 +000023684 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023685 for (;;)
23686 {
23687 p = skipwhite(p);
23688 if (STRNCMP(p, "range", 5) == 0)
23689 {
23690 flags |= FC_RANGE;
23691 p += 5;
23692 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023693 else if (STRNCMP(p, "dict", 4) == 0)
23694 {
23695 flags |= FC_DICT;
23696 p += 4;
23697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698 else if (STRNCMP(p, "abort", 5) == 0)
23699 {
23700 flags |= FC_ABORT;
23701 p += 5;
23702 }
23703 else
23704 break;
23705 }
23706
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023707 /* When there is a line break use what follows for the function body.
23708 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23709 if (*p == '\n')
23710 line_arg = p + 1;
23711 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712 EMSG(_(e_trailing));
23713
23714 /*
23715 * Read the body of the function, until ":endfunction" is found.
23716 */
23717 if (KeyTyped)
23718 {
23719 /* Check if the function already exists, don't let the user type the
23720 * whole function before telling him it doesn't work! For a script we
23721 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023722 if (!eap->skip && !eap->forceit)
23723 {
23724 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23725 EMSG(_(e_funcdict));
23726 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023727 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023730 if (!eap->skip && did_emsg)
23731 goto erret;
23732
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733 msg_putchar('\n'); /* don't overwrite the function name */
23734 cmdline_row = msg_row;
23735 }
23736
23737 indent = 2;
23738 nesting = 0;
23739 for (;;)
23740 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023741 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023742 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023743 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023744 saved_wait_return = FALSE;
23745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023747 sourcing_lnum_off = sourcing_lnum;
23748
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023749 if (line_arg != NULL)
23750 {
23751 /* Use eap->arg, split up in parts by line breaks. */
23752 theline = line_arg;
23753 p = vim_strchr(theline, '\n');
23754 if (p == NULL)
23755 line_arg += STRLEN(line_arg);
23756 else
23757 {
23758 *p = NUL;
23759 line_arg = p + 1;
23760 }
23761 }
23762 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763 theline = getcmdline(':', 0L, indent);
23764 else
23765 theline = eap->getline(':', eap->cookie, indent);
23766 if (KeyTyped)
23767 lines_left = Rows - 1;
23768 if (theline == NULL)
23769 {
23770 EMSG(_("E126: Missing :endfunction"));
23771 goto erret;
23772 }
23773
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023774 /* Detect line continuation: sourcing_lnum increased more than one. */
23775 if (sourcing_lnum > sourcing_lnum_off + 1)
23776 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23777 else
23778 sourcing_lnum_off = 0;
23779
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780 if (skip_until != NULL)
23781 {
23782 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23783 * don't check for ":endfunc". */
23784 if (STRCMP(theline, skip_until) == 0)
23785 {
23786 vim_free(skip_until);
23787 skip_until = NULL;
23788 }
23789 }
23790 else
23791 {
23792 /* skip ':' and blanks*/
23793 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23794 ;
23795
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023796 /* Check for "endfunction". */
23797 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023799 if (line_arg == NULL)
23800 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023801 break;
23802 }
23803
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023804 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 * at "end". */
23806 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23807 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023808 else if (STRNCMP(p, "if", 2) == 0
23809 || STRNCMP(p, "wh", 2) == 0
23810 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811 || STRNCMP(p, "try", 3) == 0)
23812 indent += 2;
23813
23814 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023815 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023817 if (*p == '!')
23818 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023820 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23821 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023823 ++nesting;
23824 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 }
23826 }
23827
23828 /* Check for ":append" or ":insert". */
23829 p = skip_range(p, NULL);
23830 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23831 || (p[0] == 'i'
23832 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23833 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23834 skip_until = vim_strsave((char_u *)".");
23835
23836 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23837 arg = skipwhite(skiptowhite(p));
23838 if (arg[0] == '<' && arg[1] =='<'
23839 && ((p[0] == 'p' && p[1] == 'y'
23840 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23841 || (p[0] == 'p' && p[1] == 'e'
23842 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23843 || (p[0] == 't' && p[1] == 'c'
23844 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023845 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23846 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23848 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023849 || (p[0] == 'm' && p[1] == 'z'
23850 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 ))
23852 {
23853 /* ":python <<" continues until a dot, like ":append" */
23854 p = skipwhite(arg + 2);
23855 if (*p == NUL)
23856 skip_until = vim_strsave((char_u *)".");
23857 else
23858 skip_until = vim_strsave(p);
23859 }
23860 }
23861
23862 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023863 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023864 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023865 if (line_arg == NULL)
23866 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023868 }
23869
23870 /* Copy the line to newly allocated memory. get_one_sourceline()
23871 * allocates 250 bytes per line, this saves 80% on average. The cost
23872 * is an extra alloc/free. */
23873 p = vim_strsave(theline);
23874 if (p != NULL)
23875 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023876 if (line_arg == NULL)
23877 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023878 theline = p;
23879 }
23880
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023881 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23882
23883 /* Add NULL lines for continuation lines, so that the line count is
23884 * equal to the index in the growarray. */
23885 while (sourcing_lnum_off-- > 0)
23886 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023887
23888 /* Check for end of eap->arg. */
23889 if (line_arg != NULL && *line_arg == NUL)
23890 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 }
23892
23893 /* Don't define the function when skipping commands or when an error was
23894 * detected. */
23895 if (eap->skip || did_emsg)
23896 goto erret;
23897
23898 /*
23899 * If there are no errors, add the function
23900 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023901 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023902 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023903 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023904 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023905 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023906 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023907 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023908 goto erret;
23909 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023910
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023911 fp = find_func(name);
23912 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023913 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023914 if (!eap->forceit)
23915 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023916 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023917 goto erret;
23918 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023919 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023920 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023921 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023922 name);
23923 goto erret;
23924 }
23925 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023926 ga_clear_strings(&(fp->uf_args));
23927 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023928 vim_free(name);
23929 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931 }
23932 else
23933 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023934 char numbuf[20];
23935
23936 fp = NULL;
23937 if (fudi.fd_newkey == NULL && !eap->forceit)
23938 {
23939 EMSG(_(e_funcdict));
23940 goto erret;
23941 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023942 if (fudi.fd_di == NULL)
23943 {
23944 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023945 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023946 goto erret;
23947 }
23948 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023949 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023950 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023951
23952 /* Give the function a sequential number. Can only be used with a
23953 * Funcref! */
23954 vim_free(name);
23955 sprintf(numbuf, "%d", ++func_nr);
23956 name = vim_strsave((char_u *)numbuf);
23957 if (name == NULL)
23958 goto erret;
23959 }
23960
23961 if (fp == NULL)
23962 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023963 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023964 {
23965 int slen, plen;
23966 char_u *scriptname;
23967
23968 /* Check that the autoload name matches the script name. */
23969 j = FAIL;
23970 if (sourcing_name != NULL)
23971 {
23972 scriptname = autoload_name(name);
23973 if (scriptname != NULL)
23974 {
23975 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023976 plen = (int)STRLEN(p);
23977 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023978 if (slen > plen && fnamecmp(p,
23979 sourcing_name + slen - plen) == 0)
23980 j = OK;
23981 vim_free(scriptname);
23982 }
23983 }
23984 if (j == FAIL)
23985 {
23986 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23987 goto erret;
23988 }
23989 }
23990
23991 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 if (fp == NULL)
23993 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023994
23995 if (fudi.fd_dict != NULL)
23996 {
23997 if (fudi.fd_di == NULL)
23998 {
23999 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024000 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024001 if (fudi.fd_di == NULL)
24002 {
24003 vim_free(fp);
24004 goto erret;
24005 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024006 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24007 {
24008 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024009 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024010 goto erret;
24011 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024012 }
24013 else
24014 /* overwrite existing dict entry */
24015 clear_tv(&fudi.fd_di->di_tv);
24016 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024017 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024018 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024019 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024020
24021 /* behave like "dict" was used */
24022 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024023 }
24024
Bram Moolenaar071d4272004-06-13 20:20:40 +000024025 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024026 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024027 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24028 {
24029 vim_free(fp);
24030 goto erret;
24031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024033 fp->uf_args = newargs;
24034 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024035#ifdef FEAT_PROFILE
24036 fp->uf_tml_count = NULL;
24037 fp->uf_tml_total = NULL;
24038 fp->uf_tml_self = NULL;
24039 fp->uf_profiling = FALSE;
24040 if (prof_def_func())
24041 func_do_profile(fp);
24042#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024043 fp->uf_varargs = varargs;
24044 fp->uf_flags = flags;
24045 fp->uf_calls = 0;
24046 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024047 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048
24049erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 ga_clear_strings(&newargs);
24051 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024052ret_free:
24053 vim_free(skip_until);
24054 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024056 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024057 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058}
24059
24060/*
24061 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024062 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024064 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024065 * TFN_INT: internal function name OK
24066 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024067 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024068 * Advances "pp" to just after the function name (if no error).
24069 */
24070 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024071trans_function_name(
24072 char_u **pp,
24073 int skip, /* only find the end, don't evaluate */
24074 int flags,
24075 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024077 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 char_u *start;
24079 char_u *end;
24080 int lead;
24081 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024083 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024084
24085 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024086 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024088
24089 /* Check for hard coded <SNR>: already translated function ID (from a user
24090 * command). */
24091 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24092 && (*pp)[2] == (int)KE_SNR)
24093 {
24094 *pp += 3;
24095 len = get_id_len(pp) + 3;
24096 return vim_strnsave(start, len);
24097 }
24098
24099 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24100 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024102 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024104
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024105 /* Note that TFN_ flags use the same values as GLV_ flags. */
24106 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024107 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024108 if (end == start)
24109 {
24110 if (!skip)
24111 EMSG(_("E129: Function name required"));
24112 goto theend;
24113 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024114 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024115 {
24116 /*
24117 * Report an invalid expression in braces, unless the expression
24118 * evaluation has been cancelled due to an aborting error, an
24119 * interrupt, or an exception.
24120 */
24121 if (!aborting())
24122 {
24123 if (end != NULL)
24124 EMSG2(_(e_invarg2), start);
24125 }
24126 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024127 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024128 goto theend;
24129 }
24130
24131 if (lv.ll_tv != NULL)
24132 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024133 if (fdp != NULL)
24134 {
24135 fdp->fd_dict = lv.ll_dict;
24136 fdp->fd_newkey = lv.ll_newkey;
24137 lv.ll_newkey = NULL;
24138 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024139 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024140 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24141 {
24142 name = vim_strsave(lv.ll_tv->vval.v_string);
24143 *pp = end;
24144 }
24145 else
24146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024147 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24148 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024149 EMSG(_(e_funcref));
24150 else
24151 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024152 name = NULL;
24153 }
24154 goto theend;
24155 }
24156
24157 if (lv.ll_name == NULL)
24158 {
24159 /* Error found, but continue after the function name. */
24160 *pp = end;
24161 goto theend;
24162 }
24163
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024164 /* Check if the name is a Funcref. If so, use the value. */
24165 if (lv.ll_exp_name != NULL)
24166 {
24167 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024168 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024169 if (name == lv.ll_exp_name)
24170 name = NULL;
24171 }
24172 else
24173 {
24174 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024175 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024176 if (name == *pp)
24177 name = NULL;
24178 }
24179 if (name != NULL)
24180 {
24181 name = vim_strsave(name);
24182 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024183 if (STRNCMP(name, "<SNR>", 5) == 0)
24184 {
24185 /* Change "<SNR>" to the byte sequence. */
24186 name[0] = K_SPECIAL;
24187 name[1] = KS_EXTRA;
24188 name[2] = (int)KE_SNR;
24189 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24190 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024191 goto theend;
24192 }
24193
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024194 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024195 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024196 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024197 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24198 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24199 {
24200 /* When there was "s:" already or the name expanded to get a
24201 * leading "s:" then remove it. */
24202 lv.ll_name += 2;
24203 len -= 2;
24204 lead = 2;
24205 }
24206 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024207 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024208 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024209 /* skip over "s:" and "g:" */
24210 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024211 lv.ll_name += 2;
24212 len = (int)(end - lv.ll_name);
24213 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024214
24215 /*
24216 * Copy the function name to allocated memory.
24217 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24218 * Accept <SNR>123_name() outside a script.
24219 */
24220 if (skip)
24221 lead = 0; /* do nothing */
24222 else if (lead > 0)
24223 {
24224 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024225 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24226 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024227 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024228 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024229 if (current_SID <= 0)
24230 {
24231 EMSG(_(e_usingsid));
24232 goto theend;
24233 }
24234 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24235 lead += (int)STRLEN(sid_buf);
24236 }
24237 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024238 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024239 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024240 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024241 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024242 goto theend;
24243 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024244 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024245 {
24246 char_u *cp = vim_strchr(lv.ll_name, ':');
24247
24248 if (cp != NULL && cp < end)
24249 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024250 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024251 goto theend;
24252 }
24253 }
24254
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024255 name = alloc((unsigned)(len + lead + 1));
24256 if (name != NULL)
24257 {
24258 if (lead > 0)
24259 {
24260 name[0] = K_SPECIAL;
24261 name[1] = KS_EXTRA;
24262 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024263 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024264 STRCPY(name + 3, sid_buf);
24265 }
24266 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024267 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024268 }
24269 *pp = end;
24270
24271theend:
24272 clear_lval(&lv);
24273 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274}
24275
24276/*
24277 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24278 * Return 2 if "p" starts with "s:".
24279 * Return 0 otherwise.
24280 */
24281 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024282eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024284 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24285 * the standard library function. */
24286 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24287 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288 return 5;
24289 if (p[0] == 's' && p[1] == ':')
24290 return 2;
24291 return 0;
24292}
24293
24294/*
24295 * Return TRUE if "p" starts with "<SID>" or "s:".
24296 * Only works if eval_fname_script() returned non-zero for "p"!
24297 */
24298 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024299eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300{
24301 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24302}
24303
24304/*
24305 * List the head of the function: "name(arg1, arg2)".
24306 */
24307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024308list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309{
24310 int j;
24311
24312 msg_start();
24313 if (indent)
24314 MSG_PUTS(" ");
24315 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024316 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317 {
24318 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024319 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024320 }
24321 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024322 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024323 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024324 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 {
24326 if (j)
24327 MSG_PUTS(", ");
24328 msg_puts(FUNCARG(fp, j));
24329 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024330 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 {
24332 if (j)
24333 MSG_PUTS(", ");
24334 MSG_PUTS("...");
24335 }
24336 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024337 if (fp->uf_flags & FC_ABORT)
24338 MSG_PUTS(" abort");
24339 if (fp->uf_flags & FC_RANGE)
24340 MSG_PUTS(" range");
24341 if (fp->uf_flags & FC_DICT)
24342 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024343 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024344 if (p_verbose > 0)
24345 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346}
24347
24348/*
24349 * Find a function by name, return pointer to it in ufuncs.
24350 * Return NULL for unknown function.
24351 */
24352 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024353find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024355 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024356
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024357 hi = hash_find(&func_hashtab, name);
24358 if (!HASHITEM_EMPTY(hi))
24359 return HI2UF(hi);
24360 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361}
24362
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024363#if defined(EXITFREE) || defined(PROTO)
24364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024365free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024366{
24367 hashitem_T *hi;
24368
24369 /* Need to start all over every time, because func_free() may change the
24370 * hash table. */
24371 while (func_hashtab.ht_used > 0)
24372 for (hi = func_hashtab.ht_array; ; ++hi)
24373 if (!HASHITEM_EMPTY(hi))
24374 {
24375 func_free(HI2UF(hi));
24376 break;
24377 }
24378}
24379#endif
24380
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024381 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024382translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024383{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024384 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024385 return find_internal_func(name) >= 0;
24386 return find_func(name) != NULL;
24387}
24388
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024389/*
24390 * Return TRUE if a function "name" exists.
24391 */
24392 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024393function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024394{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024395 char_u *nm = name;
24396 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024397 int n = FALSE;
24398
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024399 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24400 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024401 nm = skipwhite(nm);
24402
24403 /* Only accept "funcname", "funcname ", "funcname (..." and
24404 * "funcname(...", not "funcname!...". */
24405 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024406 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024407 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024408 return n;
24409}
24410
Bram Moolenaara1544c02013-05-30 12:35:52 +020024411 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024412get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024413{
24414 char_u *nm = name;
24415 char_u *p;
24416
24417 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24418
24419 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024420 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024421 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024422
Bram Moolenaara1544c02013-05-30 12:35:52 +020024423 vim_free(p);
24424 return NULL;
24425}
24426
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024427/*
24428 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024429 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24430 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024431 */
24432 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024433builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024434{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024435 char_u *p;
24436
24437 if (!ASCII_ISLOWER(name[0]))
24438 return FALSE;
24439 p = vim_strchr(name, AUTOLOAD_CHAR);
24440 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024441}
24442
Bram Moolenaar05159a02005-02-26 23:04:13 +000024443#if defined(FEAT_PROFILE) || defined(PROTO)
24444/*
24445 * Start profiling function "fp".
24446 */
24447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024448func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024449{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024450 int len = fp->uf_lines.ga_len;
24451
24452 if (len == 0)
24453 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024454 fp->uf_tm_count = 0;
24455 profile_zero(&fp->uf_tm_self);
24456 profile_zero(&fp->uf_tm_total);
24457 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024458 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024459 if (fp->uf_tml_total == NULL)
24460 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024461 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024462 if (fp->uf_tml_self == NULL)
24463 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024464 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024465 fp->uf_tml_idx = -1;
24466 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24467 || fp->uf_tml_self == NULL)
24468 return; /* out of memory */
24469
24470 fp->uf_profiling = TRUE;
24471}
24472
24473/*
24474 * Dump the profiling results for all functions in file "fd".
24475 */
24476 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024477func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024478{
24479 hashitem_T *hi;
24480 int todo;
24481 ufunc_T *fp;
24482 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024483 ufunc_T **sorttab;
24484 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024485
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024486 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024487 if (todo == 0)
24488 return; /* nothing to dump */
24489
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024490 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024491
Bram Moolenaar05159a02005-02-26 23:04:13 +000024492 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24493 {
24494 if (!HASHITEM_EMPTY(hi))
24495 {
24496 --todo;
24497 fp = HI2UF(hi);
24498 if (fp->uf_profiling)
24499 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024500 if (sorttab != NULL)
24501 sorttab[st_len++] = fp;
24502
Bram Moolenaar05159a02005-02-26 23:04:13 +000024503 if (fp->uf_name[0] == K_SPECIAL)
24504 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24505 else
24506 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24507 if (fp->uf_tm_count == 1)
24508 fprintf(fd, "Called 1 time\n");
24509 else
24510 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24511 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24512 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24513 fprintf(fd, "\n");
24514 fprintf(fd, "count total (s) self (s)\n");
24515
24516 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24517 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024518 if (FUNCLINE(fp, i) == NULL)
24519 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024520 prof_func_line(fd, fp->uf_tml_count[i],
24521 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024522 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24523 }
24524 fprintf(fd, "\n");
24525 }
24526 }
24527 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024528
24529 if (sorttab != NULL && st_len > 0)
24530 {
24531 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24532 prof_total_cmp);
24533 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24534 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24535 prof_self_cmp);
24536 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24537 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024538
24539 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024540}
Bram Moolenaar73830342005-02-28 22:48:19 +000024541
24542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024543prof_sort_list(
24544 FILE *fd,
24545 ufunc_T **sorttab,
24546 int st_len,
24547 char *title,
24548 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024549{
24550 int i;
24551 ufunc_T *fp;
24552
24553 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24554 fprintf(fd, "count total (s) self (s) function\n");
24555 for (i = 0; i < 20 && i < st_len; ++i)
24556 {
24557 fp = sorttab[i];
24558 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24559 prefer_self);
24560 if (fp->uf_name[0] == K_SPECIAL)
24561 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24562 else
24563 fprintf(fd, " %s()\n", fp->uf_name);
24564 }
24565 fprintf(fd, "\n");
24566}
24567
24568/*
24569 * Print the count and times for one function or function line.
24570 */
24571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024572prof_func_line(
24573 FILE *fd,
24574 int count,
24575 proftime_T *total,
24576 proftime_T *self,
24577 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024578{
24579 if (count > 0)
24580 {
24581 fprintf(fd, "%5d ", count);
24582 if (prefer_self && profile_equal(total, self))
24583 fprintf(fd, " ");
24584 else
24585 fprintf(fd, "%s ", profile_msg(total));
24586 if (!prefer_self && profile_equal(total, self))
24587 fprintf(fd, " ");
24588 else
24589 fprintf(fd, "%s ", profile_msg(self));
24590 }
24591 else
24592 fprintf(fd, " ");
24593}
24594
24595/*
24596 * Compare function for total time sorting.
24597 */
24598 static int
24599#ifdef __BORLANDC__
24600_RTLENTRYF
24601#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024602prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024603{
24604 ufunc_T *p1, *p2;
24605
24606 p1 = *(ufunc_T **)s1;
24607 p2 = *(ufunc_T **)s2;
24608 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24609}
24610
24611/*
24612 * Compare function for self time sorting.
24613 */
24614 static int
24615#ifdef __BORLANDC__
24616_RTLENTRYF
24617#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024618prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024619{
24620 ufunc_T *p1, *p2;
24621
24622 p1 = *(ufunc_T **)s1;
24623 p2 = *(ufunc_T **)s2;
24624 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24625}
24626
Bram Moolenaar05159a02005-02-26 23:04:13 +000024627#endif
24628
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024629/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024630 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024631 * Return TRUE if a package was loaded.
24632 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024633 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024634script_autoload(
24635 char_u *name,
24636 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024637{
24638 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024639 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024640 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024641 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024642
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024643 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024644 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024645 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024646 return FALSE;
24647
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024648 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024649
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024650 /* Find the name in the list of previously loaded package names. Skip
24651 * "autoload/", it's always the same. */
24652 for (i = 0; i < ga_loaded.ga_len; ++i)
24653 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24654 break;
24655 if (!reload && i < ga_loaded.ga_len)
24656 ret = FALSE; /* was loaded already */
24657 else
24658 {
24659 /* Remember the name if it wasn't loaded already. */
24660 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24661 {
24662 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24663 tofree = NULL;
24664 }
24665
24666 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024667 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024668 ret = TRUE;
24669 }
24670
24671 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024672 return ret;
24673}
24674
24675/*
24676 * Return the autoload script name for a function or variable name.
24677 * Returns NULL when out of memory.
24678 */
24679 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024680autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024681{
24682 char_u *p;
24683 char_u *scriptname;
24684
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024685 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024686 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24687 if (scriptname == NULL)
24688 return FALSE;
24689 STRCPY(scriptname, "autoload/");
24690 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024691 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024692 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024693 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024694 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024695 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024696}
24697
Bram Moolenaar071d4272004-06-13 20:20:40 +000024698#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24699
24700/*
24701 * Function given to ExpandGeneric() to obtain the list of user defined
24702 * function names.
24703 */
24704 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024705get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024707 static long_u done;
24708 static hashitem_T *hi;
24709 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710
24711 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024712 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024713 done = 0;
24714 hi = func_hashtab.ht_array;
24715 }
24716 if (done < func_hashtab.ht_used)
24717 {
24718 if (done++ > 0)
24719 ++hi;
24720 while (HASHITEM_EMPTY(hi))
24721 ++hi;
24722 fp = HI2UF(hi);
24723
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024724 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024725 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024726
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024727 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24728 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729
24730 cat_func_name(IObuff, fp);
24731 if (xp->xp_context != EXPAND_USER_FUNC)
24732 {
24733 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024734 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024735 STRCAT(IObuff, ")");
24736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024737 return IObuff;
24738 }
24739 return NULL;
24740}
24741
24742#endif /* FEAT_CMDL_COMPL */
24743
24744/*
24745 * Copy the function name of "fp" to buffer "buf".
24746 * "buf" must be able to hold the function name plus three bytes.
24747 * Takes care of script-local function names.
24748 */
24749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024750cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024752 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753 {
24754 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024755 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024756 }
24757 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024758 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024759}
24760
24761/*
24762 * ":delfunction {name}"
24763 */
24764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024765ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024767 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768 char_u *p;
24769 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024770 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771
24772 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024773 name = trans_function_name(&p, eap->skip, 0, &fudi);
24774 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024776 {
24777 if (fudi.fd_dict != NULL && !eap->skip)
24778 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 if (!ends_excmd(*skipwhite(p)))
24782 {
24783 vim_free(name);
24784 EMSG(_(e_trailing));
24785 return;
24786 }
24787 eap->nextcmd = check_nextcmd(p);
24788 if (eap->nextcmd != NULL)
24789 *p = NUL;
24790
24791 if (!eap->skip)
24792 fp = find_func(name);
24793 vim_free(name);
24794
24795 if (!eap->skip)
24796 {
24797 if (fp == NULL)
24798 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024799 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800 return;
24801 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024802 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024803 {
24804 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24805 return;
24806 }
24807
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024808 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024810 /* Delete the dict item that refers to the function, it will
24811 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024812 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024813 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024814 else
24815 func_free(fp);
24816 }
24817}
24818
24819/*
24820 * Free a function and remove it from the list of functions.
24821 */
24822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024823func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024824{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024825 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024826
24827 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024828 ga_clear_strings(&(fp->uf_args));
24829 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024830#ifdef FEAT_PROFILE
24831 vim_free(fp->uf_tml_count);
24832 vim_free(fp->uf_tml_total);
24833 vim_free(fp->uf_tml_self);
24834#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024835
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024836 /* remove the function from the function hashtable */
24837 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24838 if (HASHITEM_EMPTY(hi))
24839 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024840 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024841 hash_remove(&func_hashtab, hi);
24842
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024843 vim_free(fp);
24844}
24845
24846/*
24847 * Unreference a Function: decrement the reference count and free it when it
24848 * becomes zero. Only for numbered functions.
24849 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024851func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024852{
24853 ufunc_T *fp;
24854
24855 if (name != NULL && isdigit(*name))
24856 {
24857 fp = find_func(name);
24858 if (fp == NULL)
24859 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024860 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024861 {
24862 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024863 * when "uf_calls" becomes zero. */
24864 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024865 func_free(fp);
24866 }
24867 }
24868}
24869
24870/*
24871 * Count a reference to a Function.
24872 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024873 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024874func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024875{
24876 ufunc_T *fp;
24877
24878 if (name != NULL && isdigit(*name))
24879 {
24880 fp = find_func(name);
24881 if (fp == NULL)
24882 EMSG2(_(e_intern2), "func_ref()");
24883 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024884 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024885 }
24886}
24887
24888/*
24889 * Call a user function.
24890 */
24891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024892call_user_func(
24893 ufunc_T *fp, /* pointer to function */
24894 int argcount, /* nr of args */
24895 typval_T *argvars, /* arguments */
24896 typval_T *rettv, /* return value */
24897 linenr_T firstline, /* first line of range */
24898 linenr_T lastline, /* last line of range */
24899 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900{
Bram Moolenaar33570922005-01-25 22:26:29 +000024901 char_u *save_sourcing_name;
24902 linenr_T save_sourcing_lnum;
24903 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024904 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024905 int save_did_emsg;
24906 static int depth = 0;
24907 dictitem_T *v;
24908 int fixvar_idx = 0; /* index in fixvar[] */
24909 int i;
24910 int ai;
24911 char_u numbuf[NUMBUFLEN];
24912 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024913 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024914#ifdef FEAT_PROFILE
24915 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024916 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918
24919 /* If depth of calling is getting too high, don't execute the function */
24920 if (depth >= p_mfd)
24921 {
24922 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024923 rettv->v_type = VAR_NUMBER;
24924 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 return;
24926 }
24927 ++depth;
24928
24929 line_breakcheck(); /* check for CTRL-C hit */
24930
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024931 fc = (funccall_T *)alloc(sizeof(funccall_T));
24932 fc->caller = current_funccal;
24933 current_funccal = fc;
24934 fc->func = fp;
24935 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024936 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024937 fc->linenr = 0;
24938 fc->returned = FALSE;
24939 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024941 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24942 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943
Bram Moolenaar33570922005-01-25 22:26:29 +000024944 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024945 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024946 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24947 * each argument variable and saves a lot of time.
24948 */
24949 /*
24950 * Init l: variables.
24951 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024952 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024953 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024954 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024955 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24956 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024957 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024958 name = v->di_key;
24959 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024960 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024961 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024962 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024963 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024964 v->di_tv.vval.v_dict = selfdict;
24965 ++selfdict->dv_refcount;
24966 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024967
Bram Moolenaar33570922005-01-25 22:26:29 +000024968 /*
24969 * Init a: variables.
24970 * Set a:0 to "argcount".
24971 * Set a:000 to a list with room for the "..." arguments.
24972 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024973 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024974 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024975 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024976 /* Use "name" to avoid a warning from some compiler that checks the
24977 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024978 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024979 name = v->di_key;
24980 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024981 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024982 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024983 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024984 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024985 v->di_tv.vval.v_list = &fc->l_varlist;
24986 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24987 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24988 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024989
24990 /*
24991 * Set a:firstline to "firstline" and a:lastline to "lastline".
24992 * Set a:name to named arguments.
24993 * Set a:N to the "..." arguments.
24994 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024995 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024996 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024997 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024998 (varnumber_T)lastline);
24999 for (i = 0; i < argcount; ++i)
25000 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025001 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025002 if (ai < 0)
25003 /* named argument a:name */
25004 name = FUNCARG(fp, i);
25005 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025006 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025007 /* "..." argument a:1, a:2, etc. */
25008 sprintf((char *)numbuf, "%d", ai + 1);
25009 name = numbuf;
25010 }
25011 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25012 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025013 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025014 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25015 }
25016 else
25017 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025018 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25019 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025020 if (v == NULL)
25021 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025022 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025023 }
25024 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025025 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025026
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025027 /* Note: the values are copied directly to avoid alloc/free.
25028 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025029 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025030 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025031
25032 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25033 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025034 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25035 fc->l_listitems[ai].li_tv = argvars[i];
25036 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025037 }
25038 }
25039
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040 /* Don't redraw while executing the function. */
25041 ++RedrawingDisabled;
25042 save_sourcing_name = sourcing_name;
25043 save_sourcing_lnum = sourcing_lnum;
25044 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025045 /* need space for function name + ("function " + 3) or "[number]" */
25046 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25047 + STRLEN(fp->uf_name) + 20;
25048 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049 if (sourcing_name != NULL)
25050 {
25051 if (save_sourcing_name != NULL
25052 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025053 sprintf((char *)sourcing_name, "%s[%d]..",
25054 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025055 else
25056 STRCPY(sourcing_name, "function ");
25057 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25058
25059 if (p_verbose >= 12)
25060 {
25061 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025062 verbose_enter_scroll();
25063
Bram Moolenaar555b2802005-05-19 21:08:39 +000025064 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065 if (p_verbose >= 14)
25066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025067 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025068 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025069 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025070 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071
25072 msg_puts((char_u *)"(");
25073 for (i = 0; i < argcount; ++i)
25074 {
25075 if (i > 0)
25076 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025077 if (argvars[i].v_type == VAR_NUMBER)
25078 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079 else
25080 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025081 /* Do not want errors such as E724 here. */
25082 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025083 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025084 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025085 if (s != NULL)
25086 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025087 if (vim_strsize(s) > MSG_BUF_CLEN)
25088 {
25089 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25090 s = buf;
25091 }
25092 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025093 vim_free(tofree);
25094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025095 }
25096 }
25097 msg_puts((char_u *)")");
25098 }
25099 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025100
25101 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025102 --no_wait_return;
25103 }
25104 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025105#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025106 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025107 {
25108 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25109 func_do_profile(fp);
25110 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025111 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025112 {
25113 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025114 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025115 profile_zero(&fp->uf_tm_children);
25116 }
25117 script_prof_save(&wait_start);
25118 }
25119#endif
25120
Bram Moolenaar071d4272004-06-13 20:20:40 +000025121 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025122 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025123 save_did_emsg = did_emsg;
25124 did_emsg = FALSE;
25125
25126 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025127 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25129
25130 --RedrawingDisabled;
25131
25132 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025133 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025134 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025135 clear_tv(rettv);
25136 rettv->v_type = VAR_NUMBER;
25137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 }
25139
Bram Moolenaar05159a02005-02-26 23:04:13 +000025140#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025141 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025142 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025143 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025144 profile_end(&call_start);
25145 profile_sub_wait(&wait_start, &call_start);
25146 profile_add(&fp->uf_tm_total, &call_start);
25147 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025148 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025149 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025150 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25151 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025152 }
25153 }
25154#endif
25155
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156 /* when being verbose, mention the return value */
25157 if (p_verbose >= 12)
25158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025160 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025161
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025163 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025164 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025165 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025166 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025167 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025168 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025169 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025170 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025171 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025172 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025173
Bram Moolenaar555b2802005-05-19 21:08:39 +000025174 /* The value may be very long. Skip the middle part, so that we
25175 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025176 * truncate it at the end. Don't want errors such as E724 here. */
25177 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025178 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025179 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025180 if (s != NULL)
25181 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025182 if (vim_strsize(s) > MSG_BUF_CLEN)
25183 {
25184 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25185 s = buf;
25186 }
25187 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025188 vim_free(tofree);
25189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025190 }
25191 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025192
25193 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194 --no_wait_return;
25195 }
25196
25197 vim_free(sourcing_name);
25198 sourcing_name = save_sourcing_name;
25199 sourcing_lnum = save_sourcing_lnum;
25200 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025201#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025202 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025203 script_prof_restore(&wait_start);
25204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025205
25206 if (p_verbose >= 12 && sourcing_name != NULL)
25207 {
25208 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025209 verbose_enter_scroll();
25210
Bram Moolenaar555b2802005-05-19 21:08:39 +000025211 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025213
25214 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215 --no_wait_return;
25216 }
25217
25218 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025219 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025221
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025222 /* If the a:000 list and the l: and a: dicts are not referenced we can
25223 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025224 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25225 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25226 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25227 {
25228 free_funccal(fc, FALSE);
25229 }
25230 else
25231 {
25232 hashitem_T *hi;
25233 listitem_T *li;
25234 int todo;
25235
25236 /* "fc" is still in use. This can happen when returning "a:000" or
25237 * assigning "l:" to a global variable.
25238 * Link "fc" in the list for garbage collection later. */
25239 fc->caller = previous_funccal;
25240 previous_funccal = fc;
25241
25242 /* Make a copy of the a: variables, since we didn't do that above. */
25243 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25244 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25245 {
25246 if (!HASHITEM_EMPTY(hi))
25247 {
25248 --todo;
25249 v = HI2DI(hi);
25250 copy_tv(&v->di_tv, &v->di_tv);
25251 }
25252 }
25253
25254 /* Make a copy of the a:000 items, since we didn't do that above. */
25255 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25256 copy_tv(&li->li_tv, &li->li_tv);
25257 }
25258}
25259
25260/*
25261 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025262 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025263 */
25264 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025265can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025266{
25267 return (fc->l_varlist.lv_copyID != copyID
25268 && fc->l_vars.dv_copyID != copyID
25269 && fc->l_avars.dv_copyID != copyID);
25270}
25271
25272/*
25273 * Free "fc" and what it contains.
25274 */
25275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025276free_funccal(
25277 funccall_T *fc,
25278 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025279{
25280 listitem_T *li;
25281
25282 /* The a: variables typevals may not have been allocated, only free the
25283 * allocated variables. */
25284 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25285
25286 /* free all l: variables */
25287 vars_clear(&fc->l_vars.dv_hashtab);
25288
25289 /* Free the a:000 variables if they were allocated. */
25290 if (free_val)
25291 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25292 clear_tv(&li->li_tv);
25293
25294 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295}
25296
25297/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025298 * Add a number variable "name" to dict "dp" with value "nr".
25299 */
25300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025301add_nr_var(
25302 dict_T *dp,
25303 dictitem_T *v,
25304 char *name,
25305 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025306{
25307 STRCPY(v->di_key, name);
25308 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25309 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25310 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025311 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025312 v->di_tv.vval.v_number = nr;
25313}
25314
25315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316 * ":return [expr]"
25317 */
25318 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025319ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320{
25321 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025322 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323 int returning = FALSE;
25324
25325 if (current_funccal == NULL)
25326 {
25327 EMSG(_("E133: :return not inside a function"));
25328 return;
25329 }
25330
25331 if (eap->skip)
25332 ++emsg_skip;
25333
25334 eap->nextcmd = NULL;
25335 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025336 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337 {
25338 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025339 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025340 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025341 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025342 }
25343 /* It's safer to return also on error. */
25344 else if (!eap->skip)
25345 {
25346 /*
25347 * Return unless the expression evaluation has been cancelled due to an
25348 * aborting error, an interrupt, or an exception.
25349 */
25350 if (!aborting())
25351 returning = do_return(eap, FALSE, TRUE, NULL);
25352 }
25353
25354 /* When skipping or the return gets pending, advance to the next command
25355 * in this line (!returning). Otherwise, ignore the rest of the line.
25356 * Following lines will be ignored by get_func_line(). */
25357 if (returning)
25358 eap->nextcmd = NULL;
25359 else if (eap->nextcmd == NULL) /* no argument */
25360 eap->nextcmd = check_nextcmd(arg);
25361
25362 if (eap->skip)
25363 --emsg_skip;
25364}
25365
25366/*
25367 * Return from a function. Possibly makes the return pending. Also called
25368 * for a pending return at the ":endtry" or after returning from an extra
25369 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025370 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025371 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372 * FALSE when the return gets pending.
25373 */
25374 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025375do_return(
25376 exarg_T *eap,
25377 int reanimate,
25378 int is_cmd,
25379 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380{
25381 int idx;
25382 struct condstack *cstack = eap->cstack;
25383
25384 if (reanimate)
25385 /* Undo the return. */
25386 current_funccal->returned = FALSE;
25387
25388 /*
25389 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25390 * not in its finally clause (which then is to be executed next) is found.
25391 * In this case, make the ":return" pending for execution at the ":endtry".
25392 * Otherwise, return normally.
25393 */
25394 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25395 if (idx >= 0)
25396 {
25397 cstack->cs_pending[idx] = CSTP_RETURN;
25398
25399 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025400 /* A pending return again gets pending. "rettv" points to an
25401 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025402 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025403 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404 else
25405 {
25406 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025407 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025408 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025409 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025411 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412 {
25413 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025414 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025415 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416 else
25417 EMSG(_(e_outofmem));
25418 }
25419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025420 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421
25422 if (reanimate)
25423 {
25424 /* The pending return value could be overwritten by a ":return"
25425 * without argument in a finally clause; reset the default
25426 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025427 current_funccal->rettv->v_type = VAR_NUMBER;
25428 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429 }
25430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025431 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025432 }
25433 else
25434 {
25435 current_funccal->returned = TRUE;
25436
25437 /* If the return is carried out now, store the return value. For
25438 * a return immediately after reanimation, the value is already
25439 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025440 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025442 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025443 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025445 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446 }
25447 }
25448
25449 return idx < 0;
25450}
25451
25452/*
25453 * Free the variable with a pending return value.
25454 */
25455 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025456discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457{
Bram Moolenaar33570922005-01-25 22:26:29 +000025458 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459}
25460
25461/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025462 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463 * is an allocated string. Used by report_pending() for verbose messages.
25464 */
25465 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025466get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025468 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025469 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025470 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025472 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025473 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025474 if (s == NULL)
25475 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025476
25477 STRCPY(IObuff, ":return ");
25478 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25479 if (STRLEN(s) + 8 >= IOSIZE)
25480 STRCPY(IObuff + IOSIZE - 4, "...");
25481 vim_free(tofree);
25482 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025483}
25484
25485/*
25486 * Get next function line.
25487 * Called by do_cmdline() to get the next line.
25488 * Returns allocated string, or NULL for end of function.
25489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025491get_func_line(
25492 int c UNUSED,
25493 void *cookie,
25494 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025495{
Bram Moolenaar33570922005-01-25 22:26:29 +000025496 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025497 ufunc_T *fp = fcp->func;
25498 char_u *retval;
25499 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500
25501 /* If breakpoints have been added/deleted need to check for it. */
25502 if (fcp->dbg_tick != debug_tick)
25503 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025504 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025505 sourcing_lnum);
25506 fcp->dbg_tick = debug_tick;
25507 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025508#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025509 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025510 func_line_end(cookie);
25511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512
Bram Moolenaar05159a02005-02-26 23:04:13 +000025513 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025514 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25515 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516 retval = NULL;
25517 else
25518 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025519 /* Skip NULL lines (continuation lines). */
25520 while (fcp->linenr < gap->ga_len
25521 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25522 ++fcp->linenr;
25523 if (fcp->linenr >= gap->ga_len)
25524 retval = NULL;
25525 else
25526 {
25527 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25528 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025529#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025530 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025531 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025532#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025534 }
25535
25536 /* Did we encounter a breakpoint? */
25537 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25538 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025539 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025540 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025541 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025542 sourcing_lnum);
25543 fcp->dbg_tick = debug_tick;
25544 }
25545
25546 return retval;
25547}
25548
Bram Moolenaar05159a02005-02-26 23:04:13 +000025549#if defined(FEAT_PROFILE) || defined(PROTO)
25550/*
25551 * Called when starting to read a function line.
25552 * "sourcing_lnum" must be correct!
25553 * When skipping lines it may not actually be executed, but we won't find out
25554 * until later and we need to store the time now.
25555 */
25556 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025557func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025558{
25559 funccall_T *fcp = (funccall_T *)cookie;
25560 ufunc_T *fp = fcp->func;
25561
25562 if (fp->uf_profiling && sourcing_lnum >= 1
25563 && sourcing_lnum <= fp->uf_lines.ga_len)
25564 {
25565 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025566 /* Skip continuation lines. */
25567 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25568 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025569 fp->uf_tml_execed = FALSE;
25570 profile_start(&fp->uf_tml_start);
25571 profile_zero(&fp->uf_tml_children);
25572 profile_get_wait(&fp->uf_tml_wait);
25573 }
25574}
25575
25576/*
25577 * Called when actually executing a function line.
25578 */
25579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025580func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025581{
25582 funccall_T *fcp = (funccall_T *)cookie;
25583 ufunc_T *fp = fcp->func;
25584
25585 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25586 fp->uf_tml_execed = TRUE;
25587}
25588
25589/*
25590 * Called when done with a function line.
25591 */
25592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025593func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025594{
25595 funccall_T *fcp = (funccall_T *)cookie;
25596 ufunc_T *fp = fcp->func;
25597
25598 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25599 {
25600 if (fp->uf_tml_execed)
25601 {
25602 ++fp->uf_tml_count[fp->uf_tml_idx];
25603 profile_end(&fp->uf_tml_start);
25604 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025605 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025606 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25607 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025608 }
25609 fp->uf_tml_idx = -1;
25610 }
25611}
25612#endif
25613
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614/*
25615 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025616 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617 */
25618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025619func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620{
Bram Moolenaar33570922005-01-25 22:26:29 +000025621 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025622
25623 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25624 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025625 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025626 || fcp->returned);
25627}
25628
25629/*
25630 * return TRUE if cookie indicates a function which "abort"s on errors.
25631 */
25632 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025633func_has_abort(
25634 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025635{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025636 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025637}
25638
25639#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25640typedef enum
25641{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025642 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25643 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25644 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645} var_flavour_T;
25646
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025647static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648
25649 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025650var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025651{
25652 char_u *p = varname;
25653
25654 if (ASCII_ISUPPER(*p))
25655 {
25656 while (*(++p))
25657 if (ASCII_ISLOWER(*p))
25658 return VAR_FLAVOUR_SESSION;
25659 return VAR_FLAVOUR_VIMINFO;
25660 }
25661 else
25662 return VAR_FLAVOUR_DEFAULT;
25663}
25664#endif
25665
25666#if defined(FEAT_VIMINFO) || defined(PROTO)
25667/*
25668 * Restore global vars that start with a capital from the viminfo file
25669 */
25670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025671read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025672{
25673 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025674 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025675 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025676 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025677
25678 if (!writing && (find_viminfo_parameter('!') != NULL))
25679 {
25680 tab = vim_strchr(virp->vir_line + 1, '\t');
25681 if (tab != NULL)
25682 {
25683 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025684 switch (*tab)
25685 {
25686 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025687#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025688 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025689#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025690 case 'D': type = VAR_DICT; break;
25691 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025692 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025694
25695 tab = vim_strchr(tab, '\t');
25696 if (tab != NULL)
25697 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025698 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025699 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025700 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025701 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025702#ifdef FEAT_FLOAT
25703 else if (type == VAR_FLOAT)
25704 (void)string2float(tab + 1, &tv.vval.v_float);
25705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025706 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025707 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025708 if (type == VAR_DICT || type == VAR_LIST)
25709 {
25710 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25711
25712 if (etv == NULL)
25713 /* Failed to parse back the dict or list, use it as a
25714 * string. */
25715 tv.v_type = VAR_STRING;
25716 else
25717 {
25718 vim_free(tv.vval.v_string);
25719 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025720 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025721 }
25722 }
25723
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025724 /* when in a function use global variables */
25725 save_funccal = current_funccal;
25726 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025727 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025728 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025729
25730 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025731 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025732 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25733 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025734 }
25735 }
25736 }
25737
25738 return viminfo_readline(virp);
25739}
25740
25741/*
25742 * Write global vars that start with a capital to the viminfo file
25743 */
25744 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025745write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746{
Bram Moolenaar33570922005-01-25 22:26:29 +000025747 hashitem_T *hi;
25748 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025749 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025750 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025751 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025752 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025753 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754
25755 if (find_viminfo_parameter('!') == NULL)
25756 return;
25757
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025758 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025759
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025760 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025761 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025763 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025764 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025765 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025766 this_var = HI2DI(hi);
25767 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025768 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025769 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025770 {
25771 case VAR_STRING: s = "STR"; break;
25772 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025773 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025774 case VAR_DICT: s = "DIC"; break;
25775 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025776 case VAR_SPECIAL: s = "XPL"; break;
25777
25778 case VAR_UNKNOWN:
25779 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025780 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025781 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025782 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025783 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025784 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025785 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025786 if (p != NULL)
25787 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025788 vim_free(tofree);
25789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025790 }
25791 }
25792}
25793#endif
25794
25795#if defined(FEAT_SESSION) || defined(PROTO)
25796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025797store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025798{
Bram Moolenaar33570922005-01-25 22:26:29 +000025799 hashitem_T *hi;
25800 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025801 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802 char_u *p, *t;
25803
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025804 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025805 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025807 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025809 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025810 this_var = HI2DI(hi);
25811 if ((this_var->di_tv.v_type == VAR_NUMBER
25812 || this_var->di_tv.v_type == VAR_STRING)
25813 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025814 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025815 /* Escape special characters with a backslash. Turn a LF and
25816 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025817 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025818 (char_u *)"\\\"\n\r");
25819 if (p == NULL) /* out of memory */
25820 break;
25821 for (t = p; *t != NUL; ++t)
25822 if (*t == '\n')
25823 *t = 'n';
25824 else if (*t == '\r')
25825 *t = 'r';
25826 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025827 this_var->di_key,
25828 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25829 : ' ',
25830 p,
25831 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25832 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025833 || put_eol(fd) == FAIL)
25834 {
25835 vim_free(p);
25836 return FAIL;
25837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 vim_free(p);
25839 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025840#ifdef FEAT_FLOAT
25841 else if (this_var->di_tv.v_type == VAR_FLOAT
25842 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25843 {
25844 float_T f = this_var->di_tv.vval.v_float;
25845 int sign = ' ';
25846
25847 if (f < 0)
25848 {
25849 f = -f;
25850 sign = '-';
25851 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025852 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025853 this_var->di_key, sign, f) < 0)
25854 || put_eol(fd) == FAIL)
25855 return FAIL;
25856 }
25857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 }
25859 }
25860 return OK;
25861}
25862#endif
25863
Bram Moolenaar661b1822005-07-28 22:36:45 +000025864/*
25865 * Display script name where an item was last set.
25866 * Should only be invoked when 'verbose' is non-zero.
25867 */
25868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025869last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025870{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025871 char_u *p;
25872
Bram Moolenaar661b1822005-07-28 22:36:45 +000025873 if (scriptID != 0)
25874 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025875 p = home_replace_save(NULL, get_scriptname(scriptID));
25876 if (p != NULL)
25877 {
25878 verbose_enter();
25879 MSG_PUTS(_("\n\tLast set from "));
25880 MSG_PUTS(p);
25881 vim_free(p);
25882 verbose_leave();
25883 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025884 }
25885}
25886
Bram Moolenaard812df62008-11-09 12:46:09 +000025887/*
25888 * List v:oldfiles in a nice way.
25889 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025891ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025892{
25893 list_T *l = vimvars[VV_OLDFILES].vv_list;
25894 listitem_T *li;
25895 int nr = 0;
25896
25897 if (l == NULL)
25898 msg((char_u *)_("No old files"));
25899 else
25900 {
25901 msg_start();
25902 msg_scroll = TRUE;
25903 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25904 {
25905 msg_outnum((long)++nr);
25906 MSG_PUTS(": ");
25907 msg_outtrans(get_tv_string(&li->li_tv));
25908 msg_putchar('\n');
25909 out_flush(); /* output one line at a time */
25910 ui_breakcheck();
25911 }
25912 /* Assume "got_int" was set to truncate the listing. */
25913 got_int = FALSE;
25914
25915#ifdef FEAT_BROWSE_CMD
25916 if (cmdmod.browse)
25917 {
25918 quit_more = FALSE;
25919 nr = prompt_for_number(FALSE);
25920 msg_starthere();
25921 if (nr > 0)
25922 {
25923 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25924 (long)nr);
25925
25926 if (p != NULL)
25927 {
25928 p = expand_env_save(p);
25929 eap->arg = p;
25930 eap->cmdidx = CMD_edit;
25931 cmdmod.browse = FALSE;
25932 do_exedit(eap, NULL);
25933 vim_free(p);
25934 }
25935 }
25936 }
25937#endif
25938 }
25939}
25940
Bram Moolenaar53744302015-07-17 17:38:22 +020025941/* reset v:option_new, v:option_old and v:option_type */
25942 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025943reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025944{
25945 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25946 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25947 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25948}
25949
25950
Bram Moolenaar071d4272004-06-13 20:20:40 +000025951#endif /* FEAT_EVAL */
25952
Bram Moolenaar071d4272004-06-13 20:20:40 +000025953
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025954#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025955
25956#ifdef WIN3264
25957/*
25958 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25959 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025960static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25961static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25962static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025963
25964/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025965 * Get the short path (8.3) for the filename in "fnamep".
25966 * Only works for a valid file name.
25967 * When the path gets longer "fnamep" is changed and the allocated buffer
25968 * is put in "bufp".
25969 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25970 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025971 */
25972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025973get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025974{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025975 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025976 char_u *newbuf;
25977
25978 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025979 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980 if (l > len - 1)
25981 {
25982 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025983 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025984 newbuf = vim_strnsave(*fnamep, l);
25985 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025986 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987
25988 vim_free(*bufp);
25989 *fnamep = *bufp = newbuf;
25990
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025991 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025992 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993 }
25994
25995 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025996 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997}
25998
25999/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026000 * Get the short path (8.3) for the filename in "fname". The converted
26001 * path is returned in "bufp".
26002 *
26003 * Some of the directories specified in "fname" may not exist. This function
26004 * will shorten the existing directories at the beginning of the path and then
26005 * append the remaining non-existing path.
26006 *
26007 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026008 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026009 * bufp - Pointer to an allocated buffer for the filename.
26010 * fnamelen - Length of the filename pointed to by fname
26011 *
26012 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013 */
26014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026015shortpath_for_invalid_fname(
26016 char_u **fname,
26017 char_u **bufp,
26018 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026019{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026020 char_u *short_fname, *save_fname, *pbuf_unused;
26021 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026022 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026023 int old_len, len;
26024 int new_len, sfx_len;
26025 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026
26027 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026028 old_len = *fnamelen;
26029 save_fname = vim_strnsave(*fname, old_len);
26030 pbuf_unused = NULL;
26031 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026032
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026033 endp = save_fname + old_len - 1; /* Find the end of the copy */
26034 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026036 /*
26037 * Try shortening the supplied path till it succeeds by removing one
26038 * directory at a time from the tail of the path.
26039 */
26040 len = 0;
26041 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026042 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026043 /* go back one path-separator */
26044 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26045 --endp;
26046 if (endp <= save_fname)
26047 break; /* processed the complete path */
26048
26049 /*
26050 * Replace the path separator with a NUL and try to shorten the
26051 * resulting path.
26052 */
26053 ch = *endp;
26054 *endp = 0;
26055 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026056 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026057 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26058 {
26059 retval = FAIL;
26060 goto theend;
26061 }
26062 *endp = ch; /* preserve the string */
26063
26064 if (len > 0)
26065 break; /* successfully shortened the path */
26066
26067 /* failed to shorten the path. Skip the path separator */
26068 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026069 }
26070
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026071 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026072 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026073 /*
26074 * Succeeded in shortening the path. Now concatenate the shortened
26075 * path with the remaining path at the tail.
26076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026078 /* Compute the length of the new path. */
26079 sfx_len = (int)(save_endp - endp) + 1;
26080 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026082 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026083 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026084 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026085 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026086 /* There is not enough space in the currently allocated string,
26087 * copy it to a buffer big enough. */
26088 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026089 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026090 {
26091 retval = FAIL;
26092 goto theend;
26093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094 }
26095 else
26096 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026097 /* Transfer short_fname to the main buffer (it's big enough),
26098 * unless get_short_pathname() did its work in-place. */
26099 *fname = *bufp = save_fname;
26100 if (short_fname != save_fname)
26101 vim_strncpy(save_fname, short_fname, len);
26102 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026104
26105 /* concat the not-shortened part of the path */
26106 vim_strncpy(*fname + len, endp, sfx_len);
26107 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026108 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026109
26110theend:
26111 vim_free(pbuf_unused);
26112 vim_free(save_fname);
26113
26114 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026115}
26116
26117/*
26118 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026119 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026120 */
26121 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026122shortpath_for_partial(
26123 char_u **fnamep,
26124 char_u **bufp,
26125 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026126{
26127 int sepcount, len, tflen;
26128 char_u *p;
26129 char_u *pbuf, *tfname;
26130 int hasTilde;
26131
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026132 /* Count up the path separators from the RHS.. so we know which part
26133 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026134 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026135 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026136 if (vim_ispathsep(*p))
26137 ++sepcount;
26138
26139 /* Need full path first (use expand_env() to remove a "~/") */
26140 hasTilde = (**fnamep == '~');
26141 if (hasTilde)
26142 pbuf = tfname = expand_env_save(*fnamep);
26143 else
26144 pbuf = tfname = FullName_save(*fnamep, FALSE);
26145
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026146 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026147
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026148 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26149 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026150
26151 if (len == 0)
26152 {
26153 /* Don't have a valid filename, so shorten the rest of the
26154 * path if we can. This CAN give us invalid 8.3 filenames, but
26155 * there's not a lot of point in guessing what it might be.
26156 */
26157 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026158 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26159 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160 }
26161
26162 /* Count the paths backward to find the beginning of the desired string. */
26163 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026164 {
26165#ifdef FEAT_MBYTE
26166 if (has_mbyte)
26167 p -= mb_head_off(tfname, p);
26168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169 if (vim_ispathsep(*p))
26170 {
26171 if (sepcount == 0 || (hasTilde && sepcount == 1))
26172 break;
26173 else
26174 sepcount --;
26175 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026177 if (hasTilde)
26178 {
26179 --p;
26180 if (p >= tfname)
26181 *p = '~';
26182 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026184 }
26185 else
26186 ++p;
26187
26188 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26189 vim_free(*bufp);
26190 *fnamelen = (int)STRLEN(p);
26191 *bufp = pbuf;
26192 *fnamep = p;
26193
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026194 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026195}
26196#endif /* WIN3264 */
26197
26198/*
26199 * Adjust a filename, according to a string of modifiers.
26200 * *fnamep must be NUL terminated when called. When returning, the length is
26201 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026202 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026203 * When there is an error, *fnamep is set to NULL.
26204 */
26205 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026206modify_fname(
26207 char_u *src, /* string with modifiers */
26208 int *usedlen, /* characters after src that are used */
26209 char_u **fnamep, /* file name so far */
26210 char_u **bufp, /* buffer for allocated file name or NULL */
26211 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026212{
26213 int valid = 0;
26214 char_u *tail;
26215 char_u *s, *p, *pbuf;
26216 char_u dirname[MAXPATHL];
26217 int c;
26218 int has_fullname = 0;
26219#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026220 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026221 int has_shortname = 0;
26222#endif
26223
26224repeat:
26225 /* ":p" - full path/file_name */
26226 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26227 {
26228 has_fullname = 1;
26229
26230 valid |= VALID_PATH;
26231 *usedlen += 2;
26232
26233 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26234 if ((*fnamep)[0] == '~'
26235#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26236 && ((*fnamep)[1] == '/'
26237# ifdef BACKSLASH_IN_FILENAME
26238 || (*fnamep)[1] == '\\'
26239# endif
26240 || (*fnamep)[1] == NUL)
26241
26242#endif
26243 )
26244 {
26245 *fnamep = expand_env_save(*fnamep);
26246 vim_free(*bufp); /* free any allocated file name */
26247 *bufp = *fnamep;
26248 if (*fnamep == NULL)
26249 return -1;
26250 }
26251
26252 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026253 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026254 {
26255 if (vim_ispathsep(*p)
26256 && p[1] == '.'
26257 && (p[2] == NUL
26258 || vim_ispathsep(p[2])
26259 || (p[2] == '.'
26260 && (p[3] == NUL || vim_ispathsep(p[3])))))
26261 break;
26262 }
26263
26264 /* FullName_save() is slow, don't use it when not needed. */
26265 if (*p != NUL || !vim_isAbsName(*fnamep))
26266 {
26267 *fnamep = FullName_save(*fnamep, *p != NUL);
26268 vim_free(*bufp); /* free any allocated file name */
26269 *bufp = *fnamep;
26270 if (*fnamep == NULL)
26271 return -1;
26272 }
26273
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026274#ifdef WIN3264
26275# if _WIN32_WINNT >= 0x0500
26276 if (vim_strchr(*fnamep, '~') != NULL)
26277 {
26278 /* Expand 8.3 filename to full path. Needed to make sure the same
26279 * file does not have two different names.
26280 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26281 p = alloc(_MAX_PATH + 1);
26282 if (p != NULL)
26283 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026284 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026285 {
26286 vim_free(*bufp);
26287 *bufp = *fnamep = p;
26288 }
26289 else
26290 vim_free(p);
26291 }
26292 }
26293# endif
26294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026295 /* Append a path separator to a directory. */
26296 if (mch_isdir(*fnamep))
26297 {
26298 /* Make room for one or two extra characters. */
26299 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26300 vim_free(*bufp); /* free any allocated file name */
26301 *bufp = *fnamep;
26302 if (*fnamep == NULL)
26303 return -1;
26304 add_pathsep(*fnamep);
26305 }
26306 }
26307
26308 /* ":." - path relative to the current directory */
26309 /* ":~" - path relative to the home directory */
26310 /* ":8" - shortname path - postponed till after */
26311 while (src[*usedlen] == ':'
26312 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26313 {
26314 *usedlen += 2;
26315 if (c == '8')
26316 {
26317#ifdef WIN3264
26318 has_shortname = 1; /* Postpone this. */
26319#endif
26320 continue;
26321 }
26322 pbuf = NULL;
26323 /* Need full path first (use expand_env() to remove a "~/") */
26324 if (!has_fullname)
26325 {
26326 if (c == '.' && **fnamep == '~')
26327 p = pbuf = expand_env_save(*fnamep);
26328 else
26329 p = pbuf = FullName_save(*fnamep, FALSE);
26330 }
26331 else
26332 p = *fnamep;
26333
26334 has_fullname = 0;
26335
26336 if (p != NULL)
26337 {
26338 if (c == '.')
26339 {
26340 mch_dirname(dirname, MAXPATHL);
26341 s = shorten_fname(p, dirname);
26342 if (s != NULL)
26343 {
26344 *fnamep = s;
26345 if (pbuf != NULL)
26346 {
26347 vim_free(*bufp); /* free any allocated file name */
26348 *bufp = pbuf;
26349 pbuf = NULL;
26350 }
26351 }
26352 }
26353 else
26354 {
26355 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26356 /* Only replace it when it starts with '~' */
26357 if (*dirname == '~')
26358 {
26359 s = vim_strsave(dirname);
26360 if (s != NULL)
26361 {
26362 *fnamep = s;
26363 vim_free(*bufp);
26364 *bufp = s;
26365 }
26366 }
26367 }
26368 vim_free(pbuf);
26369 }
26370 }
26371
26372 tail = gettail(*fnamep);
26373 *fnamelen = (int)STRLEN(*fnamep);
26374
26375 /* ":h" - head, remove "/file_name", can be repeated */
26376 /* Don't remove the first "/" or "c:\" */
26377 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26378 {
26379 valid |= VALID_HEAD;
26380 *usedlen += 2;
26381 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026382 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026383 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026384 *fnamelen = (int)(tail - *fnamep);
26385#ifdef VMS
26386 if (*fnamelen > 0)
26387 *fnamelen += 1; /* the path separator is part of the path */
26388#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026389 if (*fnamelen == 0)
26390 {
26391 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26392 p = vim_strsave((char_u *)".");
26393 if (p == NULL)
26394 return -1;
26395 vim_free(*bufp);
26396 *bufp = *fnamep = tail = p;
26397 *fnamelen = 1;
26398 }
26399 else
26400 {
26401 while (tail > s && !after_pathsep(s, tail))
26402 mb_ptr_back(*fnamep, tail);
26403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026404 }
26405
26406 /* ":8" - shortname */
26407 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26408 {
26409 *usedlen += 2;
26410#ifdef WIN3264
26411 has_shortname = 1;
26412#endif
26413 }
26414
26415#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026416 /*
26417 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026418 */
26419 if (has_shortname)
26420 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026421 /* Copy the string if it is shortened by :h and when it wasn't copied
26422 * yet, because we are going to change it in place. Avoids changing
26423 * the buffer name for "%:8". */
26424 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026425 {
26426 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026427 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026428 return -1;
26429 vim_free(*bufp);
26430 *bufp = *fnamep = p;
26431 }
26432
26433 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026434 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026435 if (!has_fullname && !vim_isAbsName(*fnamep))
26436 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026437 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026438 return -1;
26439 }
26440 else
26441 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026442 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026443
Bram Moolenaardc935552011-08-17 15:23:23 +020026444 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026445 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026446 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026447 return -1;
26448
26449 if (l == 0)
26450 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026451 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026453 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026454 return -1;
26455 }
26456 *fnamelen = l;
26457 }
26458 }
26459#endif /* WIN3264 */
26460
26461 /* ":t" - tail, just the basename */
26462 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26463 {
26464 *usedlen += 2;
26465 *fnamelen -= (int)(tail - *fnamep);
26466 *fnamep = tail;
26467 }
26468
26469 /* ":e" - extension, can be repeated */
26470 /* ":r" - root, without extension, can be repeated */
26471 while (src[*usedlen] == ':'
26472 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26473 {
26474 /* find a '.' in the tail:
26475 * - for second :e: before the current fname
26476 * - otherwise: The last '.'
26477 */
26478 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26479 s = *fnamep - 2;
26480 else
26481 s = *fnamep + *fnamelen - 1;
26482 for ( ; s > tail; --s)
26483 if (s[0] == '.')
26484 break;
26485 if (src[*usedlen + 1] == 'e') /* :e */
26486 {
26487 if (s > tail)
26488 {
26489 *fnamelen += (int)(*fnamep - (s + 1));
26490 *fnamep = s + 1;
26491#ifdef VMS
26492 /* cut version from the extension */
26493 s = *fnamep + *fnamelen - 1;
26494 for ( ; s > *fnamep; --s)
26495 if (s[0] == ';')
26496 break;
26497 if (s > *fnamep)
26498 *fnamelen = s - *fnamep;
26499#endif
26500 }
26501 else if (*fnamep <= tail)
26502 *fnamelen = 0;
26503 }
26504 else /* :r */
26505 {
26506 if (s > tail) /* remove one extension */
26507 *fnamelen = (int)(s - *fnamep);
26508 }
26509 *usedlen += 2;
26510 }
26511
26512 /* ":s?pat?foo?" - substitute */
26513 /* ":gs?pat?foo?" - global substitute */
26514 if (src[*usedlen] == ':'
26515 && (src[*usedlen + 1] == 's'
26516 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26517 {
26518 char_u *str;
26519 char_u *pat;
26520 char_u *sub;
26521 int sep;
26522 char_u *flags;
26523 int didit = FALSE;
26524
26525 flags = (char_u *)"";
26526 s = src + *usedlen + 2;
26527 if (src[*usedlen + 1] == 'g')
26528 {
26529 flags = (char_u *)"g";
26530 ++s;
26531 }
26532
26533 sep = *s++;
26534 if (sep)
26535 {
26536 /* find end of pattern */
26537 p = vim_strchr(s, sep);
26538 if (p != NULL)
26539 {
26540 pat = vim_strnsave(s, (int)(p - s));
26541 if (pat != NULL)
26542 {
26543 s = p + 1;
26544 /* find end of substitution */
26545 p = vim_strchr(s, sep);
26546 if (p != NULL)
26547 {
26548 sub = vim_strnsave(s, (int)(p - s));
26549 str = vim_strnsave(*fnamep, *fnamelen);
26550 if (sub != NULL && str != NULL)
26551 {
26552 *usedlen = (int)(p + 1 - src);
26553 s = do_string_sub(str, pat, sub, flags);
26554 if (s != NULL)
26555 {
26556 *fnamep = s;
26557 *fnamelen = (int)STRLEN(s);
26558 vim_free(*bufp);
26559 *bufp = s;
26560 didit = TRUE;
26561 }
26562 }
26563 vim_free(sub);
26564 vim_free(str);
26565 }
26566 vim_free(pat);
26567 }
26568 }
26569 /* after using ":s", repeat all the modifiers */
26570 if (didit)
26571 goto repeat;
26572 }
26573 }
26574
Bram Moolenaar26df0922014-02-23 23:39:13 +010026575 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26576 {
26577 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26578 if (p == NULL)
26579 return -1;
26580 vim_free(*bufp);
26581 *bufp = *fnamep = p;
26582 *fnamelen = (int)STRLEN(p);
26583 *usedlen += 2;
26584 }
26585
Bram Moolenaar071d4272004-06-13 20:20:40 +000026586 return valid;
26587}
26588
26589/*
26590 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26591 * "flags" can be "g" to do a global substitute.
26592 * Returns an allocated string, NULL for error.
26593 */
26594 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026595do_string_sub(
26596 char_u *str,
26597 char_u *pat,
26598 char_u *sub,
26599 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026600{
26601 int sublen;
26602 regmatch_T regmatch;
26603 int i;
26604 int do_all;
26605 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026606 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026607 garray_T ga;
26608 char_u *ret;
26609 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026610 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026611
26612 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26613 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026614 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026615
26616 ga_init2(&ga, 1, 200);
26617
26618 do_all = (flags[0] == 'g');
26619
26620 regmatch.rm_ic = p_ic;
26621 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26622 if (regmatch.regprog != NULL)
26623 {
26624 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026625 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026626 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26627 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026628 /* Skip empty match except for first match. */
26629 if (regmatch.startp[0] == regmatch.endp[0])
26630 {
26631 if (zero_width == regmatch.startp[0])
26632 {
26633 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026634 i = MB_PTR2LEN(tail);
26635 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26636 (size_t)i);
26637 ga.ga_len += i;
26638 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026639 continue;
26640 }
26641 zero_width = regmatch.startp[0];
26642 }
26643
Bram Moolenaar071d4272004-06-13 20:20:40 +000026644 /*
26645 * Get some space for a temporary buffer to do the substitution
26646 * into. It will contain:
26647 * - The text up to where the match is.
26648 * - The substituted text.
26649 * - The text after the match.
26650 */
26651 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026652 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026653 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26654 {
26655 ga_clear(&ga);
26656 break;
26657 }
26658
26659 /* copy the text up to where the match is */
26660 i = (int)(regmatch.startp[0] - tail);
26661 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26662 /* add the substituted text */
26663 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26664 + ga.ga_len + i, TRUE, TRUE, FALSE);
26665 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026666 tail = regmatch.endp[0];
26667 if (*tail == NUL)
26668 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026669 if (!do_all)
26670 break;
26671 }
26672
26673 if (ga.ga_data != NULL)
26674 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26675
Bram Moolenaar473de612013-06-08 18:19:48 +020026676 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026677 }
26678
26679 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26680 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026681 if (p_cpo == empty_option)
26682 p_cpo = save_cpo;
26683 else
26684 /* Darn, evaluating {sub} expression changed the value. */
26685 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026686
26687 return ret;
26688}
26689
26690#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */