blob: a36f3dd51d0eef34c7a68ac12d2b4ea7ec4cab81 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100454#ifdef FEAT_JOB
455static void job_free(job_T *job);
456#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
458static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
462static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
463static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_abs(typval_T *argvars, typval_T *rettv);
469static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_add(typval_T *argvars, typval_T *rettv);
472static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
473static void f_and(typval_T *argvars, typval_T *rettv);
474static void f_append(typval_T *argvars, typval_T *rettv);
475static void f_argc(typval_T *argvars, typval_T *rettv);
476static void f_argidx(typval_T *argvars, typval_T *rettv);
477static void f_arglistid(typval_T *argvars, typval_T *rettv);
478static void f_argv(typval_T *argvars, typval_T *rettv);
479static void f_assert_equal(typval_T *argvars, typval_T *rettv);
480static void f_assert_exception(typval_T *argvars, typval_T *rettv);
481static void f_assert_fails(typval_T *argvars, typval_T *rettv);
482static void f_assert_false(typval_T *argvars, typval_T *rettv);
483static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100485static void f_asin(typval_T *argvars, typval_T *rettv);
486static void f_atan(typval_T *argvars, typval_T *rettv);
487static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_browse(typval_T *argvars, typval_T *rettv);
490static void f_browsedir(typval_T *argvars, typval_T *rettv);
491static void f_bufexists(typval_T *argvars, typval_T *rettv);
492static void f_buflisted(typval_T *argvars, typval_T *rettv);
493static void f_bufloaded(typval_T *argvars, typval_T *rettv);
494static void f_bufname(typval_T *argvars, typval_T *rettv);
495static void f_bufnr(typval_T *argvars, typval_T *rettv);
496static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
497static void f_byte2line(typval_T *argvars, typval_T *rettv);
498static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
499static void f_byteidx(typval_T *argvars, typval_T *rettv);
500static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
501static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100503static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100505#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100506static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100507static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
508static void f_ch_open(typval_T *argvars, typval_T *rettv);
509static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100512static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100513#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_changenr(typval_T *argvars, typval_T *rettv);
515static void f_char2nr(typval_T *argvars, typval_T *rettv);
516static void f_cindent(typval_T *argvars, typval_T *rettv);
517static void f_clearmatches(typval_T *argvars, typval_T *rettv);
518static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000519#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100520static void f_complete(typval_T *argvars, typval_T *rettv);
521static void f_complete_add(typval_T *argvars, typval_T *rettv);
522static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_confirm(typval_T *argvars, typval_T *rettv);
525static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_cos(typval_T *argvars, typval_T *rettv);
528static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_count(typval_T *argvars, typval_T *rettv);
531static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
532static void f_cursor(typval_T *argsvars, typval_T *rettv);
533static void f_deepcopy(typval_T *argvars, typval_T *rettv);
534static void f_delete(typval_T *argvars, typval_T *rettv);
535static void f_did_filetype(typval_T *argvars, typval_T *rettv);
536static void f_diff_filler(typval_T *argvars, typval_T *rettv);
537static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100538static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_empty(typval_T *argvars, typval_T *rettv);
540static void f_escape(typval_T *argvars, typval_T *rettv);
541static void f_eval(typval_T *argvars, typval_T *rettv);
542static void f_eventhandler(typval_T *argvars, typval_T *rettv);
543static void f_executable(typval_T *argvars, typval_T *rettv);
544static void f_exepath(typval_T *argvars, typval_T *rettv);
545static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_expand(typval_T *argvars, typval_T *rettv);
550static void f_extend(typval_T *argvars, typval_T *rettv);
551static void f_feedkeys(typval_T *argvars, typval_T *rettv);
552static void f_filereadable(typval_T *argvars, typval_T *rettv);
553static void f_filewritable(typval_T *argvars, typval_T *rettv);
554static void f_filter(typval_T *argvars, typval_T *rettv);
555static void f_finddir(typval_T *argvars, typval_T *rettv);
556static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000557#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_float2nr(typval_T *argvars, typval_T *rettv);
559static void f_floor(typval_T *argvars, typval_T *rettv);
560static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_fnameescape(typval_T *argvars, typval_T *rettv);
563static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
564static void f_foldclosed(typval_T *argvars, typval_T *rettv);
565static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
566static void f_foldlevel(typval_T *argvars, typval_T *rettv);
567static void f_foldtext(typval_T *argvars, typval_T *rettv);
568static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
569static void f_foreground(typval_T *argvars, typval_T *rettv);
570static void f_function(typval_T *argvars, typval_T *rettv);
571static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
572static void f_get(typval_T *argvars, typval_T *rettv);
573static void f_getbufline(typval_T *argvars, typval_T *rettv);
574static void f_getbufvar(typval_T *argvars, typval_T *rettv);
575static void f_getchar(typval_T *argvars, typval_T *rettv);
576static void f_getcharmod(typval_T *argvars, typval_T *rettv);
577static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
578static void f_getcmdline(typval_T *argvars, typval_T *rettv);
579static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
580static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
581static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
582static void f_getcwd(typval_T *argvars, typval_T *rettv);
583static void f_getfontname(typval_T *argvars, typval_T *rettv);
584static void f_getfperm(typval_T *argvars, typval_T *rettv);
585static void f_getfsize(typval_T *argvars, typval_T *rettv);
586static void f_getftime(typval_T *argvars, typval_T *rettv);
587static void f_getftype(typval_T *argvars, typval_T *rettv);
588static void f_getline(typval_T *argvars, typval_T *rettv);
589static void f_getmatches(typval_T *argvars, typval_T *rettv);
590static void f_getpid(typval_T *argvars, typval_T *rettv);
591static void f_getcurpos(typval_T *argvars, typval_T *rettv);
592static void f_getpos(typval_T *argvars, typval_T *rettv);
593static void f_getqflist(typval_T *argvars, typval_T *rettv);
594static void f_getreg(typval_T *argvars, typval_T *rettv);
595static void f_getregtype(typval_T *argvars, typval_T *rettv);
596static void f_gettabvar(typval_T *argvars, typval_T *rettv);
597static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
598static void f_getwinposx(typval_T *argvars, typval_T *rettv);
599static void f_getwinposy(typval_T *argvars, typval_T *rettv);
600static void f_getwinvar(typval_T *argvars, typval_T *rettv);
601static void f_glob(typval_T *argvars, typval_T *rettv);
602static void f_globpath(typval_T *argvars, typval_T *rettv);
603static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
604static void f_has(typval_T *argvars, typval_T *rettv);
605static void f_has_key(typval_T *argvars, typval_T *rettv);
606static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
607static void f_hasmapto(typval_T *argvars, typval_T *rettv);
608static void f_histadd(typval_T *argvars, typval_T *rettv);
609static void f_histdel(typval_T *argvars, typval_T *rettv);
610static void f_histget(typval_T *argvars, typval_T *rettv);
611static void f_histnr(typval_T *argvars, typval_T *rettv);
612static void f_hlID(typval_T *argvars, typval_T *rettv);
613static void f_hlexists(typval_T *argvars, typval_T *rettv);
614static void f_hostname(typval_T *argvars, typval_T *rettv);
615static void f_iconv(typval_T *argvars, typval_T *rettv);
616static void f_indent(typval_T *argvars, typval_T *rettv);
617static void f_index(typval_T *argvars, typval_T *rettv);
618static void f_input(typval_T *argvars, typval_T *rettv);
619static void f_inputdialog(typval_T *argvars, typval_T *rettv);
620static void f_inputlist(typval_T *argvars, typval_T *rettv);
621static void f_inputrestore(typval_T *argvars, typval_T *rettv);
622static void f_inputsave(typval_T *argvars, typval_T *rettv);
623static void f_inputsecret(typval_T *argvars, typval_T *rettv);
624static void f_insert(typval_T *argvars, typval_T *rettv);
625static void f_invert(typval_T *argvars, typval_T *rettv);
626static void f_isdirectory(typval_T *argvars, typval_T *rettv);
627static void f_islocked(typval_T *argvars, typval_T *rettv);
628static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100629#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100630# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100631static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100632# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +0100633static void f_job_start(typval_T *argvars, typval_T *rettv);
634static void f_job_stop(typval_T *argvars, typval_T *rettv);
635static void f_job_status(typval_T *argvars, typval_T *rettv);
636#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100637static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100638static void f_js_decode(typval_T *argvars, typval_T *rettv);
639static void f_js_encode(typval_T *argvars, typval_T *rettv);
640static void f_json_decode(typval_T *argvars, typval_T *rettv);
641static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100642static void f_keys(typval_T *argvars, typval_T *rettv);
643static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
644static void f_len(typval_T *argvars, typval_T *rettv);
645static void f_libcall(typval_T *argvars, typval_T *rettv);
646static void f_libcallnr(typval_T *argvars, typval_T *rettv);
647static void f_line(typval_T *argvars, typval_T *rettv);
648static void f_line2byte(typval_T *argvars, typval_T *rettv);
649static void f_lispindent(typval_T *argvars, typval_T *rettv);
650static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100652static void f_log(typval_T *argvars, typval_T *rettv);
653static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200655#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200657#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_map(typval_T *argvars, typval_T *rettv);
659static void f_maparg(typval_T *argvars, typval_T *rettv);
660static void f_mapcheck(typval_T *argvars, typval_T *rettv);
661static void f_match(typval_T *argvars, typval_T *rettv);
662static void f_matchadd(typval_T *argvars, typval_T *rettv);
663static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
664static void f_matcharg(typval_T *argvars, typval_T *rettv);
665static void f_matchdelete(typval_T *argvars, typval_T *rettv);
666static void f_matchend(typval_T *argvars, typval_T *rettv);
667static void f_matchlist(typval_T *argvars, typval_T *rettv);
668static void f_matchstr(typval_T *argvars, typval_T *rettv);
669static void f_max(typval_T *argvars, typval_T *rettv);
670static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000671#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000673#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100675#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100677#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
679static void f_nr2char(typval_T *argvars, typval_T *rettv);
680static void f_or(typval_T *argvars, typval_T *rettv);
681static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100682#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100684#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000685#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000687#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
689static void f_printf(typval_T *argvars, typval_T *rettv);
690static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200691#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200693#endif
694#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_range(typval_T *argvars, typval_T *rettv);
698static void f_readfile(typval_T *argvars, typval_T *rettv);
699static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100700#ifdef FEAT_FLOAT
701static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
702#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_reltimestr(typval_T *argvars, typval_T *rettv);
704static void f_remote_expr(typval_T *argvars, typval_T *rettv);
705static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
706static void f_remote_peek(typval_T *argvars, typval_T *rettv);
707static void f_remote_read(typval_T *argvars, typval_T *rettv);
708static void f_remote_send(typval_T *argvars, typval_T *rettv);
709static void f_remove(typval_T *argvars, typval_T *rettv);
710static void f_rename(typval_T *argvars, typval_T *rettv);
711static void f_repeat(typval_T *argvars, typval_T *rettv);
712static void f_resolve(typval_T *argvars, typval_T *rettv);
713static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000714#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100715static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100717static void f_screenattr(typval_T *argvars, typval_T *rettv);
718static void f_screenchar(typval_T *argvars, typval_T *rettv);
719static void f_screencol(typval_T *argvars, typval_T *rettv);
720static void f_screenrow(typval_T *argvars, typval_T *rettv);
721static void f_search(typval_T *argvars, typval_T *rettv);
722static void f_searchdecl(typval_T *argvars, typval_T *rettv);
723static void f_searchpair(typval_T *argvars, typval_T *rettv);
724static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
725static void f_searchpos(typval_T *argvars, typval_T *rettv);
726static void f_server2client(typval_T *argvars, typval_T *rettv);
727static void f_serverlist(typval_T *argvars, typval_T *rettv);
728static void f_setbufvar(typval_T *argvars, typval_T *rettv);
729static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
730static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
731static void f_setline(typval_T *argvars, typval_T *rettv);
732static void f_setloclist(typval_T *argvars, typval_T *rettv);
733static void f_setmatches(typval_T *argvars, typval_T *rettv);
734static void f_setpos(typval_T *argvars, typval_T *rettv);
735static void f_setqflist(typval_T *argvars, typval_T *rettv);
736static void f_setreg(typval_T *argvars, typval_T *rettv);
737static void f_settabvar(typval_T *argvars, typval_T *rettv);
738static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
739static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100740#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100741static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100742#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_shellescape(typval_T *argvars, typval_T *rettv);
744static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
745static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000746#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_sin(typval_T *argvars, typval_T *rettv);
748static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000749#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_sort(typval_T *argvars, typval_T *rettv);
751static void f_soundfold(typval_T *argvars, typval_T *rettv);
752static void f_spellbadword(typval_T *argvars, typval_T *rettv);
753static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
754static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000755#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_sqrt(typval_T *argvars, typval_T *rettv);
757static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000758#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_str2nr(typval_T *argvars, typval_T *rettv);
760static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000761#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000763#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_stridx(typval_T *argvars, typval_T *rettv);
765static void f_string(typval_T *argvars, typval_T *rettv);
766static void f_strlen(typval_T *argvars, typval_T *rettv);
767static void f_strpart(typval_T *argvars, typval_T *rettv);
768static void f_strridx(typval_T *argvars, typval_T *rettv);
769static void f_strtrans(typval_T *argvars, typval_T *rettv);
770static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
771static void f_strwidth(typval_T *argvars, typval_T *rettv);
772static void f_submatch(typval_T *argvars, typval_T *rettv);
773static void f_substitute(typval_T *argvars, typval_T *rettv);
774static void f_synID(typval_T *argvars, typval_T *rettv);
775static void f_synIDattr(typval_T *argvars, typval_T *rettv);
776static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
777static void f_synstack(typval_T *argvars, typval_T *rettv);
778static void f_synconcealed(typval_T *argvars, typval_T *rettv);
779static void f_system(typval_T *argvars, typval_T *rettv);
780static void f_systemlist(typval_T *argvars, typval_T *rettv);
781static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
782static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
783static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
784static void f_taglist(typval_T *argvars, typval_T *rettv);
785static void f_tagfiles(typval_T *argvars, typval_T *rettv);
786static void f_tempname(typval_T *argvars, typval_T *rettv);
787static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200788#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100789static void f_tan(typval_T *argvars, typval_T *rettv);
790static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200791#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100792static void f_tolower(typval_T *argvars, typval_T *rettv);
793static void f_toupper(typval_T *argvars, typval_T *rettv);
794static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000795#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000797#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100798static void f_type(typval_T *argvars, typval_T *rettv);
799static void f_undofile(typval_T *argvars, typval_T *rettv);
800static void f_undotree(typval_T *argvars, typval_T *rettv);
801static void f_uniq(typval_T *argvars, typval_T *rettv);
802static void f_values(typval_T *argvars, typval_T *rettv);
803static void f_virtcol(typval_T *argvars, typval_T *rettv);
804static void f_visualmode(typval_T *argvars, typval_T *rettv);
805static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
806static void f_winbufnr(typval_T *argvars, typval_T *rettv);
807static void f_wincol(typval_T *argvars, typval_T *rettv);
808static void f_winheight(typval_T *argvars, typval_T *rettv);
809static void f_winline(typval_T *argvars, typval_T *rettv);
810static void f_winnr(typval_T *argvars, typval_T *rettv);
811static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
812static void f_winrestview(typval_T *argvars, typval_T *rettv);
813static void f_winsaveview(typval_T *argvars, typval_T *rettv);
814static void f_winwidth(typval_T *argvars, typval_T *rettv);
815static void f_writefile(typval_T *argvars, typval_T *rettv);
816static void f_wordcount(typval_T *argvars, typval_T *rettv);
817static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000818
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100819static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
820static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
821static int get_env_len(char_u **arg);
822static int get_id_len(char_u **arg);
823static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
824static 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 +0000825#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
826#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
827 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100828static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
829static int eval_isnamec(int c);
830static int eval_isnamec1(int c);
831static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
832static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static typval_T *alloc_string_tv(char_u *string);
834static void init_tv(typval_T *varp);
835static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100836#ifdef FEAT_FLOAT
837static float_T get_tv_float(typval_T *varp);
838#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100839static linenr_T get_tv_lnum(typval_T *argvars);
840static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
841static char_u *get_tv_string(typval_T *varp);
842static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
843static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
844static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
845static hashtab_T *find_var_ht(char_u *name, char_u **varname);
846static funccall_T *get_funccal(void);
847static void vars_clear_ext(hashtab_T *ht, int free_val);
848static void delete_var(hashtab_T *ht, hashitem_T *hi);
849static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
850static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
851static void set_var(char_u *name, typval_T *varp, int copy);
852static int var_check_ro(int flags, char_u *name, int use_gettext);
853static int var_check_fixed(int flags, char_u *name, int use_gettext);
854static int var_check_func_name(char_u *name, int new_var);
855static int valid_varname(char_u *varname);
856static int tv_check_lock(int lock, char_u *name, int use_gettext);
857static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
858static char_u *find_option_end(char_u **arg, int *opt_flags);
859static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
860static int eval_fname_script(char_u *p);
861static int eval_fname_sid(char_u *p);
862static void list_func_head(ufunc_T *fp, int indent);
863static ufunc_T *find_func(char_u *name);
864static int function_exists(char_u *name);
865static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000866#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100867static void func_do_profile(ufunc_T *fp);
868static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
869static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000870static int
871# ifdef __BORLANDC__
872 _RTLENTRYF
873# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000875static int
876# ifdef __BORLANDC__
877 _RTLENTRYF
878# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000880#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881static int script_autoload(char_u *name, int reload);
882static char_u *autoload_name(char_u *name);
883static void cat_func_name(char_u *buf, ufunc_T *fp);
884static void func_free(ufunc_T *fp);
885static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
886static int can_free_funccal(funccall_T *fc, int copyID) ;
887static void free_funccal(funccall_T *fc, int free_val);
888static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
889static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
890static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
891static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
892static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
893static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
894static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
895static int write_list(FILE *fd, list_T *list, int binary);
896static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000897
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200898
899#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100900static int compare_func_name(const void *s1, const void *s2);
901static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200902#endif
903
Bram Moolenaar33570922005-01-25 22:26:29 +0000904/*
905 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000906 */
907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100908eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000909{
Bram Moolenaar33570922005-01-25 22:26:29 +0000910 int i;
911 struct vimvar *p;
912
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200913 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
914 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200915 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000916 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000917 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000918
919 for (i = 0; i < VV_LEN; ++i)
920 {
921 p = &vimvars[i];
922 STRCPY(p->vv_di.di_key, p->vv_name);
923 if (p->vv_flags & VV_RO)
924 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
925 else if (p->vv_flags & VV_RO_SBX)
926 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
927 else
928 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000929
930 /* add to v: scope dict, unless the value is not always available */
931 if (p->vv_type != VAR_UNKNOWN)
932 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000933 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000934 /* add to compat scope dict */
935 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000936 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100937 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
938
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000939 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100940 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200941 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100942 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100943
944 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
945 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
946 set_vim_var_nr(VV_NONE, VVAL_NONE);
947 set_vim_var_nr(VV_NULL, VVAL_NULL);
948
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200949 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200950
951#ifdef EBCDIC
952 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100953 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200954 */
955 sortFunctions();
956#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000957}
958
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000959#if defined(EXITFREE) || defined(PROTO)
960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100961eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962{
963 int i;
964 struct vimvar *p;
965
966 for (i = 0; i < VV_LEN; ++i)
967 {
968 p = &vimvars[i];
969 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000970 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000971 vim_free(p->vv_str);
972 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000973 }
974 else if (p->vv_di.di_tv.v_type == VAR_LIST)
975 {
976 list_unref(p->vv_list);
977 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000978 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000979 }
980 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000981 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000982 hash_clear(&compat_hashtab);
983
Bram Moolenaard9fba312005-06-26 22:34:35 +0000984 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100985# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200986 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100987# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000988
989 /* global variables */
990 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000991
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000992 /* autoloaded script names */
993 ga_clear_strings(&ga_loaded);
994
Bram Moolenaarcca74132013-09-25 21:00:28 +0200995 /* Script-local variables. First clear all the variables and in a second
996 * loop free the scriptvar_T, because a variable in one script might hold
997 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200998 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200999 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001000 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001001 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001002 ga_clear(&ga_scripts);
1003
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001004 /* unreferenced lists and dicts */
1005 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001006
1007 /* functions */
1008 free_all_functions();
1009 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001010}
1011#endif
1012
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 * Return the name of the executed function.
1015 */
1016 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001017func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020}
1021
1022/*
1023 * Return the address holding the next breakpoint line for a funccall cookie.
1024 */
1025 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001026func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
Bram Moolenaar33570922005-01-25 22:26:29 +00001028 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029}
1030
1031/*
1032 * Return the address holding the debug tick for a funccall cookie.
1033 */
1034 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001035func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
Bram Moolenaar33570922005-01-25 22:26:29 +00001037 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038}
1039
1040/*
1041 * Return the nesting level for a funccall cookie.
1042 */
1043 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001044func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
Bram Moolenaar33570922005-01-25 22:26:29 +00001046 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047}
1048
1049/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001050funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001052/* pointer to list of previously used funccal, still around because some
1053 * item in it is still being used. */
1054funccall_T *previous_funccal = NULL;
1055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056/*
1057 * Return TRUE when a function was ended by a ":return" command.
1058 */
1059 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001060current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
1062 return current_funccal->returned;
1063}
1064
1065
1066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 * Set an internal variable to a string value. Creates the variable if it does
1068 * not already exist.
1069 */
1070 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001071set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001073 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001074 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 val = vim_strsave(value);
1077 if (val != NULL)
1078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001079 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001080 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001082 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001083 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 }
1086}
1087
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001089static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090static char_u *redir_endp = NULL;
1091static char_u *redir_varname = NULL;
1092
1093/*
1094 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001095 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 * Returns OK if successfully completed the setup. FAIL otherwise.
1097 */
1098 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001099var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100{
1101 int save_emsg;
1102 int err;
1103 typval_T tv;
1104
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001105 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001106 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 {
1108 EMSG(_(e_invarg));
1109 return FAIL;
1110 }
1111
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001112 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 redir_varname = vim_strsave(name);
1114 if (redir_varname == NULL)
1115 return FAIL;
1116
1117 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1118 if (redir_lval == NULL)
1119 {
1120 var_redir_stop();
1121 return FAIL;
1122 }
1123
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 /* The output is stored in growarray "redir_ga" until redirection ends. */
1125 ga_init2(&redir_ga, (int)sizeof(char), 500);
1126
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001128 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001129 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1131 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001132 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 if (redir_endp != NULL && *redir_endp != NUL)
1134 /* Trailing characters are present after the variable name */
1135 EMSG(_(e_trailing));
1136 else
1137 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 var_redir_stop();
1140 return FAIL;
1141 }
1142
1143 /* check if we can write to the variable: set it to or append an empty
1144 * string */
1145 save_emsg = did_emsg;
1146 did_emsg = FALSE;
1147 tv.v_type = VAR_STRING;
1148 tv.vval.v_string = (char_u *)"";
1149 if (append)
1150 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1151 else
1152 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001153 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001155 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 if (err)
1157 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 var_redir_stop();
1160 return FAIL;
1161 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162
1163 return OK;
1164}
1165
1166/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 * Append "value[value_len]" to the variable set by var_redir_start().
1168 * The actual appending is postponed until redirection ends, because the value
1169 * appended may in fact be the string we write to, changing it may cause freed
1170 * memory to be used:
1171 * :redir => foo
1172 * :let foo
1173 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 */
1175 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001176var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001178 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179
1180 if (redir_lval == NULL)
1181 return;
1182
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001183 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001184 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001186 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189 {
1190 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001192 }
1193 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195}
1196
1197/*
1198 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001199 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200 */
1201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001202var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001204 typval_T tv;
1205
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206 if (redir_lval != NULL)
1207 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001208 /* If there was no error: assign the text to the variable. */
1209 if (redir_endp != NULL)
1210 {
1211 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1212 tv.v_type = VAR_STRING;
1213 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001214 /* Call get_lval() again, if it's inside a Dict or List it may
1215 * have changed. */
1216 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001217 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001218 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1219 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1220 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001221 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001222
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001223 /* free the collected output */
1224 vim_free(redir_ga.ga_data);
1225 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001226
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 vim_free(redir_lval);
1228 redir_lval = NULL;
1229 }
1230 vim_free(redir_varname);
1231 redir_varname = NULL;
1232}
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234# if defined(FEAT_MBYTE) || defined(PROTO)
1235 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001236eval_charconvert(
1237 char_u *enc_from,
1238 char_u *enc_to,
1239 char_u *fname_from,
1240 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241{
1242 int err = FALSE;
1243
1244 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1245 set_vim_var_string(VV_CC_TO, enc_to, -1);
1246 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1247 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1248 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1249 err = TRUE;
1250 set_vim_var_string(VV_CC_FROM, NULL, -1);
1251 set_vim_var_string(VV_CC_TO, NULL, -1);
1252 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1253 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1254
1255 if (err)
1256 return FAIL;
1257 return OK;
1258}
1259# endif
1260
1261# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1262 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001263eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 int err = FALSE;
1266
1267 set_vim_var_string(VV_FNAME_IN, fname, -1);
1268 set_vim_var_string(VV_CMDARG, args, -1);
1269 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1270 err = TRUE;
1271 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1272 set_vim_var_string(VV_CMDARG, NULL, -1);
1273
1274 if (err)
1275 {
1276 mch_remove(fname);
1277 return FAIL;
1278 }
1279 return OK;
1280}
1281# endif
1282
1283# if defined(FEAT_DIFF) || defined(PROTO)
1284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001285eval_diff(
1286 char_u *origfile,
1287 char_u *newfile,
1288 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
1290 int err = FALSE;
1291
1292 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1293 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1294 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1295 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1296 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1297 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1298 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1299}
1300
1301 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001302eval_patch(
1303 char_u *origfile,
1304 char_u *difffile,
1305 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 int err;
1308
1309 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1310 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1311 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1312 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1313 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1314 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1315 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1316}
1317# endif
1318
1319/*
1320 * Top level evaluation function, returning a boolean.
1321 * Sets "error" to TRUE if there was an error.
1322 * Return TRUE or FALSE.
1323 */
1324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001325eval_to_bool(
1326 char_u *arg,
1327 int *error,
1328 char_u **nextcmd,
1329 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
Bram Moolenaar33570922005-01-25 22:26:29 +00001331 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 int retval = FALSE;
1333
1334 if (skip)
1335 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 else
1339 {
1340 *error = FALSE;
1341 if (!skip)
1342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001343 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346 }
1347 if (skip)
1348 --emsg_skip;
1349
1350 return retval;
1351}
1352
1353/*
1354 * Top level evaluation function, returning a string. If "skip" is TRUE,
1355 * only parsing to "nextcmd" is done, without reporting errors. Return
1356 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1357 */
1358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001359eval_to_string_skip(
1360 char_u *arg,
1361 char_u **nextcmd,
1362 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
Bram Moolenaar33570922005-01-25 22:26:29 +00001364 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 char_u *retval;
1366
1367 if (skip)
1368 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 retval = NULL;
1371 else
1372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 retval = vim_strsave(get_tv_string(&tv));
1374 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376 if (skip)
1377 --emsg_skip;
1378
1379 return retval;
1380}
1381
1382/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001383 * Skip over an expression at "*pp".
1384 * Return FAIL for an error, OK otherwise.
1385 */
1386 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001387skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001388{
Bram Moolenaar33570922005-01-25 22:26:29 +00001389 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390
1391 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001393}
1394
1395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001397 * When "convert" is TRUE convert a List into a sequence of lines and convert
1398 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 * Return pointer to allocated memory, or NULL for failure.
1400 */
1401 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402eval_to_string(
1403 char_u *arg,
1404 char_u **nextcmd,
1405 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001410#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001411 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001414 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 retval = NULL;
1416 else
1417 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001418 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001419 {
1420 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001421 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001422 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001423 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001424 if (tv.vval.v_list->lv_len > 0)
1425 ga_append(&ga, NL);
1426 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001427 ga_append(&ga, NUL);
1428 retval = (char_u *)ga.ga_data;
1429 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001430#ifdef FEAT_FLOAT
1431 else if (convert && tv.v_type == VAR_FLOAT)
1432 {
1433 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1434 retval = vim_strsave(numbuf);
1435 }
1436#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001437 else
1438 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 }
1441
1442 return retval;
1443}
1444
1445/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001446 * Call eval_to_string() without using current local variables and using
1447 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 */
1449 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001450eval_to_string_safe(
1451 char_u *arg,
1452 char_u **nextcmd,
1453 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 char_u *retval;
1456 void *save_funccalp;
1457
1458 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001459 if (use_sandbox)
1460 ++sandbox;
1461 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001462 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001463 if (use_sandbox)
1464 --sandbox;
1465 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 restore_funccal(save_funccalp);
1467 return retval;
1468}
1469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470/*
1471 * Top level evaluation function, returning a number.
1472 * Evaluates "expr" silently.
1473 * Returns -1 for an error.
1474 */
1475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001476eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
Bram Moolenaar33570922005-01-25 22:26:29 +00001478 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001480 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481
1482 ++emsg_off;
1483
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001484 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 retval = -1;
1486 else
1487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001488 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491 --emsg_off;
1492
1493 return retval;
1494}
1495
Bram Moolenaara40058a2005-07-11 22:42:07 +00001496/*
1497 * Prepare v: variable "idx" to be used.
1498 * Save the current typeval in "save_tv".
1499 * When not used yet add the variable to the v: hashtable.
1500 */
1501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001503{
1504 *save_tv = vimvars[idx].vv_tv;
1505 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1506 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1507}
1508
1509/*
1510 * Restore v: variable "idx" to typeval "save_tv".
1511 * When no longer defined, remove the variable from the v: hashtable.
1512 */
1513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001514restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001515{
1516 hashitem_T *hi;
1517
Bram Moolenaara40058a2005-07-11 22:42:07 +00001518 vimvars[idx].vv_tv = *save_tv;
1519 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1520 {
1521 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1522 if (HASHITEM_EMPTY(hi))
1523 EMSG2(_(e_intern2), "restore_vimvar()");
1524 else
1525 hash_remove(&vimvarht, hi);
1526 }
1527}
1528
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001529#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001530/*
1531 * Evaluate an expression to a list with suggestions.
1532 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001533 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001534 */
1535 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537{
1538 typval_T save_val;
1539 typval_T rettv;
1540 list_T *list = NULL;
1541 char_u *p = skipwhite(expr);
1542
1543 /* Set "v:val" to the bad word. */
1544 prepare_vimvar(VV_VAL, &save_val);
1545 vimvars[VV_VAL].vv_type = VAR_STRING;
1546 vimvars[VV_VAL].vv_str = badword;
1547 if (p_verbose == 0)
1548 ++emsg_off;
1549
1550 if (eval1(&p, &rettv, TRUE) == OK)
1551 {
1552 if (rettv.v_type != VAR_LIST)
1553 clear_tv(&rettv);
1554 else
1555 list = rettv.vval.v_list;
1556 }
1557
1558 if (p_verbose == 0)
1559 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001560 restore_vimvar(VV_VAL, &save_val);
1561
1562 return list;
1563}
1564
1565/*
1566 * "list" is supposed to contain two items: a word and a number. Return the
1567 * word in "pp" and the number as the return value.
1568 * Return -1 if anything isn't right.
1569 * Used to get the good word and score from the eval_spell_expr() result.
1570 */
1571 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001572get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001573{
1574 listitem_T *li;
1575
1576 li = list->lv_first;
1577 if (li == NULL)
1578 return -1;
1579 *pp = get_tv_string(&li->li_tv);
1580
1581 li = li->li_next;
1582 if (li == NULL)
1583 return -1;
1584 return get_tv_number(&li->li_tv);
1585}
1586#endif
1587
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001588/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001589 * Top level evaluation function.
1590 * Returns an allocated typval_T with the result.
1591 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001592 */
1593 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001594eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001595{
1596 typval_T *tv;
1597
1598 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001599 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001600 {
1601 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001602 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001603 }
1604
1605 return tv;
1606}
1607
1608
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001611 * Uses argv[argc] for the function arguments. Only Number and String
1612 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001616call_vim_function(
1617 char_u *func,
1618 int argc,
1619 char_u **argv,
1620 int safe, /* use the sandbox */
1621 int str_arg_only, /* all arguments are strings */
1622 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
Bram Moolenaar33570922005-01-25 22:26:29 +00001624 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 long n;
1626 int len;
1627 int i;
1628 int doesrange;
1629 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001632 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636 for (i = 0; i < argc; i++)
1637 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001638 /* Pass a NULL or empty argument as an empty string */
1639 if (argv[i] == NULL || *argv[i] == NUL)
1640 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001641 argvars[i].v_type = VAR_STRING;
1642 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001643 continue;
1644 }
1645
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001646 if (str_arg_only)
1647 len = 0;
1648 else
1649 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001650 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (len != 0 && len == (int)STRLEN(argv[i]))
1652 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001653 argvars[i].v_type = VAR_NUMBER;
1654 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 }
1656 else
1657 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001658 argvars[i].v_type = VAR_STRING;
1659 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 }
1662
1663 if (safe)
1664 {
1665 save_funccalp = save_funccal();
1666 ++sandbox;
1667 }
1668
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1670 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (safe)
1674 {
1675 --sandbox;
1676 restore_funccal(save_funccalp);
1677 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678 vim_free(argvars);
1679
1680 if (ret == FAIL)
1681 clear_tv(rettv);
1682
1683 return ret;
1684}
1685
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001686/*
1687 * Call vimL function "func" and return the result as a number.
1688 * Returns -1 when calling the function fails.
1689 * Uses argv[argc] for the function arguments.
1690 */
1691 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001692call_func_retnr(
1693 char_u *func,
1694 int argc,
1695 char_u **argv,
1696 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001697{
1698 typval_T rettv;
1699 long retval;
1700
1701 /* All arguments are passed as strings, no conversion to number. */
1702 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1703 return -1;
1704
1705 retval = get_tv_number_chk(&rettv, NULL);
1706 clear_tv(&rettv);
1707 return retval;
1708}
1709
1710#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1711 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1712
Bram Moolenaar4f688582007-07-24 12:34:30 +00001713# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001715 * Call vimL function "func" and return the result as a string.
1716 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001717 * Uses argv[argc] for the function arguments.
1718 */
1719 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001720call_func_retstr(
1721 char_u *func,
1722 int argc,
1723 char_u **argv,
1724 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725{
1726 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001727 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001729 /* All arguments are passed as strings, no conversion to number. */
1730 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731 return NULL;
1732
1733 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 return retval;
1736}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001737# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001739/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001740 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001742 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743 */
1744 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001745call_func_retlist(
1746 char_u *func,
1747 int argc,
1748 char_u **argv,
1749 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750{
1751 typval_T rettv;
1752
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001753 /* All arguments are passed as strings, no conversion to number. */
1754 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001755 return NULL;
1756
1757 if (rettv.v_type != VAR_LIST)
1758 {
1759 clear_tv(&rettv);
1760 return NULL;
1761 }
1762
1763 return rettv.vval.v_list;
1764}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
1766
1767/*
1768 * Save the current function call pointer, and set it to NULL.
1769 * Used when executing autocommands and for ":source".
1770 */
1771 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001772save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001774 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 current_funccal = NULL;
1777 return (void *)fc;
1778}
1779
1780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001781restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001783 funccall_T *fc = (funccall_T *)vfc;
1784
1785 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786}
1787
Bram Moolenaar05159a02005-02-26 23:04:13 +00001788#if defined(FEAT_PROFILE) || defined(PROTO)
1789/*
1790 * Prepare profiling for entering a child or something else that is not
1791 * counted for the script/function itself.
1792 * Should always be called in pair with prof_child_exit().
1793 */
1794 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001795prof_child_enter(
1796 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001797{
1798 funccall_T *fc = current_funccal;
1799
1800 if (fc != NULL && fc->func->uf_profiling)
1801 profile_start(&fc->prof_child);
1802 script_prof_save(tm);
1803}
1804
1805/*
1806 * Take care of time spent in a child.
1807 * Should always be called after prof_child_enter().
1808 */
1809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001810prof_child_exit(
1811 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001812{
1813 funccall_T *fc = current_funccal;
1814
1815 if (fc != NULL && fc->func->uf_profiling)
1816 {
1817 profile_end(&fc->prof_child);
1818 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1819 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1820 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1821 }
1822 script_prof_restore(tm);
1823}
1824#endif
1825
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#ifdef FEAT_FOLDING
1828/*
1829 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1830 * it in "*cp". Doesn't give error messages.
1831 */
1832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001833eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
Bram Moolenaar33570922005-01-25 22:26:29 +00001835 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 int retval;
1837 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001838 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1839 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001842 if (use_sandbox)
1843 ++sandbox;
1844 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 retval = 0;
1848 else
1849 {
1850 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 if (tv.v_type == VAR_NUMBER)
1852 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001853 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 retval = 0;
1855 else
1856 {
1857 /* If the result is a string, check if there is a non-digit before
1858 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (!VIM_ISDIGIT(*s) && *s != '-')
1861 *cp = *s++;
1862 retval = atol((char *)s);
1863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001867 if (use_sandbox)
1868 --sandbox;
1869 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870
1871 return retval;
1872}
1873#endif
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 * ":let" list all variable values
1877 * ":let var1 var2" list variable values
1878 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 * ":let var += expr" assignment command.
1880 * ":let var -= expr" assignment command.
1881 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 */
1884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001885ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886{
1887 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001889 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 int var_count = 0;
1892 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001894 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001895 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
Bram Moolenaardb552d602006-03-23 22:59:57 +00001897 argend = skip_var_list(arg, &var_count, &semicolon);
1898 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001900 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1901 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 expr = skipwhite(argend);
1903 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1904 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001906 /*
1907 * ":let" without "=": list variables
1908 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 if (*arg == '[')
1910 EMSG(_(e_invarg));
1911 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001913 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001917 list_glob_vars(&first);
1918 list_buf_vars(&first);
1919 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001920#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001921 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001922#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 list_script_vars(&first);
1924 list_func_vars(&first);
1925 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 eap->nextcmd = check_nextcmd(arg);
1928 }
1929 else
1930 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 op[0] = '=';
1932 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001933 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001935 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1936 op[0] = *expr; /* +=, -= or .= */
1937 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001939 else
1940 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if (eap->skip)
1943 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 if (eap->skip)
1946 {
1947 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 --emsg_skip;
1950 }
1951 else if (i != FAIL)
1952 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001954 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 }
1958}
1959
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960/*
1961 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1962 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 * When "nextchars" is not NULL it points to a string with characters that
1964 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1965 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 * Returns OK or FAIL;
1967 */
1968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001969ex_let_vars(
1970 char_u *arg_start,
1971 typval_T *tv,
1972 int copy, /* copy values from "tv", don't move */
1973 int semicolon, /* from skip_var_list() */
1974 int var_count, /* from skip_var_list() */
1975 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976{
1977 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001978 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001980 listitem_T *item;
1981 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982
1983 if (*arg != '[')
1984 {
1985 /*
1986 * ":let var = expr" or ":for var in list"
1987 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001988 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 return FAIL;
1990 return OK;
1991 }
1992
1993 /*
1994 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1995 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001996 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 {
1998 EMSG(_(e_listreq));
1999 return FAIL;
2000 }
2001
2002 i = list_len(l);
2003 if (semicolon == 0 && var_count < i)
2004 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002005 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 return FAIL;
2007 }
2008 if (var_count - semicolon > i)
2009 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002010 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 return FAIL;
2012 }
2013
2014 item = l->lv_first;
2015 while (*arg != ']')
2016 {
2017 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002018 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 item = item->li_next;
2020 if (arg == NULL)
2021 return FAIL;
2022
2023 arg = skipwhite(arg);
2024 if (*arg == ';')
2025 {
2026 /* Put the rest of the list (may be empty) in the var after ';'.
2027 * Create a new list for this. */
2028 l = list_alloc();
2029 if (l == NULL)
2030 return FAIL;
2031 while (item != NULL)
2032 {
2033 list_append_tv(l, &item->li_tv);
2034 item = item->li_next;
2035 }
2036
2037 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002038 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 ltv.vval.v_list = l;
2040 l->lv_refcount = 1;
2041
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2043 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 clear_tv(&ltv);
2045 if (arg == NULL)
2046 return FAIL;
2047 break;
2048 }
2049 else if (*arg != ',' && *arg != ']')
2050 {
2051 EMSG2(_(e_intern2), "ex_let_vars()");
2052 return FAIL;
2053 }
2054 }
2055
2056 return OK;
2057}
2058
2059/*
2060 * Skip over assignable variable "var" or list of variables "[var, var]".
2061 * Used for ":let varvar = expr" and ":for varvar in expr".
2062 * For "[var, var]" increment "*var_count" for each variable.
2063 * for "[var, var; var]" set "semicolon".
2064 * Return NULL for an error.
2065 */
2066 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002067skip_var_list(
2068 char_u *arg,
2069 int *var_count,
2070 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071{
2072 char_u *p, *s;
2073
2074 if (*arg == '[')
2075 {
2076 /* "[var, var]": find the matching ']'. */
2077 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002078 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079 {
2080 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2081 s = skip_var_one(p);
2082 if (s == p)
2083 {
2084 EMSG2(_(e_invarg2), p);
2085 return NULL;
2086 }
2087 ++*var_count;
2088
2089 p = skipwhite(s);
2090 if (*p == ']')
2091 break;
2092 else if (*p == ';')
2093 {
2094 if (*semicolon == 1)
2095 {
2096 EMSG(_("Double ; in list of variables"));
2097 return NULL;
2098 }
2099 *semicolon = 1;
2100 }
2101 else if (*p != ',')
2102 {
2103 EMSG2(_(e_invarg2), p);
2104 return NULL;
2105 }
2106 }
2107 return p + 1;
2108 }
2109 else
2110 return skip_var_one(arg);
2111}
2112
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002114 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002115 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002117 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002118skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002119{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002120 if (*arg == '@' && arg[1] != NUL)
2121 return arg + 2;
2122 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2123 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124}
2125
Bram Moolenaara7043832005-01-21 11:56:39 +00002126/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002127 * List variables for hashtab "ht" with prefix "prefix".
2128 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002129 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002131list_hashtable_vars(
2132 hashtab_T *ht,
2133 char_u *prefix,
2134 int empty,
2135 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002136{
Bram Moolenaar33570922005-01-25 22:26:29 +00002137 hashitem_T *hi;
2138 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002139 int todo;
2140
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002141 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002142 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2143 {
2144 if (!HASHITEM_EMPTY(hi))
2145 {
2146 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002147 di = HI2DI(hi);
2148 if (empty || di->di_tv.v_type != VAR_STRING
2149 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002151 }
2152 }
2153}
2154
2155/*
2156 * List global variables.
2157 */
2158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002159list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002160{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002162}
2163
2164/*
2165 * List buffer variables.
2166 */
2167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002168list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002169{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002170 char_u numbuf[NUMBUFLEN];
2171
Bram Moolenaar429fa852013-04-15 12:27:36 +02002172 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002174
2175 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2177 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002178}
2179
2180/*
2181 * List window variables.
2182 */
2183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002184list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002185{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002186 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002188}
2189
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002190#ifdef FEAT_WINDOWS
2191/*
2192 * List tab page variables.
2193 */
2194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002196{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002197 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002199}
2200#endif
2201
Bram Moolenaara7043832005-01-21 11:56:39 +00002202/*
2203 * List Vim variables.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209}
2210
2211/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212 * List script-local variables, if there is a script.
2213 */
2214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002215list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216{
2217 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2219 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220}
2221
2222/*
2223 * List function variables, if there is a function.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227{
2228 if (current_funccal != NULL)
2229 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002231}
2232
2233/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 * List variables in "arg".
2235 */
2236 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238{
2239 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 char_u *name_start;
2243 char_u *arg_subsc;
2244 char_u *tofree;
2245 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246
2247 while (!ends_excmd(*arg) && !got_int)
2248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002251 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2253 {
2254 emsg_severe = TRUE;
2255 EMSG(_(e_trailing));
2256 break;
2257 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 /* get_name_len() takes care of expanding curly braces */
2262 name_start = name = arg;
2263 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2264 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 /* This is mainly to keep test 49 working: when expanding
2267 * curly braces fails overrule the exception error message. */
2268 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 emsg_severe = TRUE;
2271 EMSG2(_(e_invarg2), arg);
2272 break;
2273 }
2274 error = TRUE;
2275 }
2276 else
2277 {
2278 if (tofree != NULL)
2279 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002280 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 else
2283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 /* handle d.key, l[idx], f(expr) */
2285 arg_subsc = arg;
2286 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002294 case 'g': list_glob_vars(first); break;
2295 case 'b': list_buf_vars(first); break;
2296 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002297#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002298 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002299#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002300 case 'v': list_vim_vars(first); break;
2301 case 's': list_script_vars(first); break;
2302 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 default:
2304 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002305 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
2307 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 {
2309 char_u numbuf[NUMBUFLEN];
2310 char_u *tf;
2311 int c;
2312 char_u *s;
2313
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002314 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 c = *arg;
2316 *arg = NUL;
2317 list_one_var_a((char_u *)"",
2318 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002319 tv.v_type,
2320 s == NULL ? (char_u *)"" : s,
2321 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322 *arg = c;
2323 vim_free(tf);
2324 }
2325 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002326 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
2328 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329
2330 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332
2333 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 }
2335
2336 return arg;
2337}
2338
2339/*
2340 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2341 * Returns a pointer to the char just after the var name.
2342 * Returns NULL if there is an error.
2343 */
2344 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002345ex_let_one(
2346 char_u *arg, /* points to variable name */
2347 typval_T *tv, /* value to assign to variable */
2348 int copy, /* copy value from "tv" */
2349 char_u *endchars, /* valid chars after variable name or NULL */
2350 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351{
2352 int c1;
2353 char_u *name;
2354 char_u *p;
2355 char_u *arg_end = NULL;
2356 int len;
2357 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359
2360 /*
2361 * ":let $VAR = expr": Set environment variable.
2362 */
2363 if (*arg == '$')
2364 {
2365 /* Find the end of the name. */
2366 ++arg;
2367 name = arg;
2368 len = get_env_len(&arg);
2369 if (len == 0)
2370 EMSG2(_(e_invarg2), name - 1);
2371 else
2372 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373 if (op != NULL && (*op == '+' || *op == '-'))
2374 EMSG2(_(e_letwrong), op);
2375 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002376 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002378 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 {
2380 c1 = name[len];
2381 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 p = get_tv_string_chk(tv);
2383 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 {
2385 int mustfree = FALSE;
2386 char_u *s = vim_getenv(name, &mustfree);
2387
2388 if (s != NULL)
2389 {
2390 p = tofree = concat_str(s, p);
2391 if (mustfree)
2392 vim_free(s);
2393 }
2394 }
2395 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002398 if (STRICMP(name, "HOME") == 0)
2399 init_homedir();
2400 else if (didset_vim && STRICMP(name, "VIM") == 0)
2401 didset_vim = FALSE;
2402 else if (didset_vimruntime
2403 && STRICMP(name, "VIMRUNTIME") == 0)
2404 didset_vimruntime = FALSE;
2405 arg_end = arg;
2406 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409 }
2410 }
2411 }
2412
2413 /*
2414 * ":let &option = expr": Set option value.
2415 * ":let &l:option = expr": Set local option value.
2416 * ":let &g:option = expr": Set global option value.
2417 */
2418 else if (*arg == '&')
2419 {
2420 /* Find the end of the name. */
2421 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002422 if (p == NULL || (endchars != NULL
2423 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 EMSG(_(e_letunexp));
2425 else
2426 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 long n;
2428 int opt_type;
2429 long numval;
2430 char_u *stringval = NULL;
2431 char_u *s;
2432
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 c1 = *p;
2434 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435
2436 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002437 s = get_tv_string_chk(tv); /* != NULL if number or string */
2438 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 {
2440 opt_type = get_option_value(arg, &numval,
2441 &stringval, opt_flags);
2442 if ((opt_type == 1 && *op == '.')
2443 || (opt_type == 0 && *op != '.'))
2444 EMSG2(_(e_letwrong), op);
2445 else
2446 {
2447 if (opt_type == 1) /* number */
2448 {
2449 if (*op == '+')
2450 n = numval + n;
2451 else
2452 n = numval - n;
2453 }
2454 else if (opt_type == 0 && stringval != NULL) /* string */
2455 {
2456 s = concat_str(stringval, s);
2457 vim_free(stringval);
2458 stringval = s;
2459 }
2460 }
2461 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002462 if (s != NULL)
2463 {
2464 set_option_value(arg, n, s, opt_flags);
2465 arg_end = p;
2466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 }
2470 }
2471
2472 /*
2473 * ":let @r = expr": Set register contents.
2474 */
2475 else if (*arg == '@')
2476 {
2477 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 if (op != NULL && (*op == '+' || *op == '-'))
2479 EMSG2(_(e_letwrong), op);
2480 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002481 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 EMSG(_(e_letunexp));
2483 else
2484 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002485 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 char_u *s;
2487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002488 p = get_tv_string_chk(tv);
2489 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002491 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 if (s != NULL)
2493 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002494 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 vim_free(s);
2496 }
2497 }
2498 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002499 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 arg_end = arg + 1;
2502 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002503 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 }
2505 }
2506
2507 /*
2508 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002511 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002513 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002515 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2519 EMSG(_(e_letunexp));
2520 else
2521 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002522 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 arg_end = p;
2524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 }
2528
2529 else
2530 EMSG2(_(e_invarg2), arg);
2531
2532 return arg_end;
2533}
2534
2535/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2537 */
2538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002539check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540{
2541 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2542 {
2543 EMSG2(_(e_readonlyvar), arg);
2544 return TRUE;
2545 }
2546 return FALSE;
2547}
2548
2549/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 * Get an lval: variable, Dict item or List item that can be assigned a value
2551 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2552 * "name.key", "name.key[expr]" etc.
2553 * Indexing only works if "name" is an existing List or Dictionary.
2554 * "name" points to the start of the name.
2555 * If "rettv" is not NULL it points to the value to be assigned.
2556 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2557 * wrong; must end in space or cmd separator.
2558 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002559 * flags:
2560 * GLV_QUIET: do not give error messages
2561 * GLV_NO_AUTOLOAD: do not use script autoloading
2562 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002564 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 */
2567 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002568get_lval(
2569 char_u *name,
2570 typval_T *rettv,
2571 lval_T *lp,
2572 int unlet,
2573 int skip,
2574 int flags, /* GLV_ values */
2575 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 char_u *expr_start, *expr_end;
2579 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 dictitem_T *v;
2581 typval_T var1;
2582 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002588 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592
2593 if (skip)
2594 {
2595 /* When skipping just find the end of the name. */
2596 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002597 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 }
2599
2600 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002601 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (expr_start != NULL)
2603 {
2604 /* Don't expand the name when we already know there is an error. */
2605 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2606 && *p != '[' && *p != '.')
2607 {
2608 EMSG(_(e_trailing));
2609 return NULL;
2610 }
2611
2612 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2613 if (lp->ll_exp_name == NULL)
2614 {
2615 /* Report an invalid expression in braces, unless the
2616 * expression evaluation has been cancelled due to an
2617 * aborting error, an interrupt, or an exception. */
2618 if (!aborting() && !quiet)
2619 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002620 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 EMSG2(_(e_invarg2), name);
2622 return NULL;
2623 }
2624 }
2625 lp->ll_name = lp->ll_exp_name;
2626 }
2627 else
2628 lp->ll_name = name;
2629
2630 /* Without [idx] or .key we are done. */
2631 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2632 return p;
2633
2634 cc = *p;
2635 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002636 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (v == NULL && !quiet)
2638 EMSG2(_(e_undefvar), lp->ll_name);
2639 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002640 if (v == NULL)
2641 return NULL;
2642
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 /*
2644 * Loop until no more [idx] or .key is following.
2645 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2650 && !(lp->ll_tv->v_type == VAR_DICT
2651 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_("E689: Can only index a List or Dictionary"));
2655 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E708: [:] must come last"));
2661 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 len = -1;
2665 if (*p == '.')
2666 {
2667 key = p + 1;
2668 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2669 ;
2670 if (len == 0)
2671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (!quiet)
2673 EMSG(_(e_emptykey));
2674 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676 p = key + len;
2677 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 else
2679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 if (*p == ':')
2683 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 else
2685 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 empty1 = FALSE;
2687 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002689 if (get_tv_string_chk(&var1) == NULL)
2690 {
2691 /* not a number or string */
2692 clear_tv(&var1);
2693 return NULL;
2694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 }
2696
2697 /* Optionally get the second index [ :expr]. */
2698 if (*p == ':')
2699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002703 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 if (!empty1)
2705 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (rettv != NULL && (rettv->v_type != VAR_LIST
2709 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (!quiet)
2712 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (!empty1)
2714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 }
2717 p = skipwhite(p + 1);
2718 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 else
2721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 if (!empty1)
2726 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002729 if (get_tv_string_chk(&var2) == NULL)
2730 {
2731 /* not a number or string */
2732 if (!empty1)
2733 clear_tv(&var1);
2734 clear_tv(&var2);
2735 return NULL;
2736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002742
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (!quiet)
2746 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (!empty1)
2748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 }
2753
2754 /* Skip to past ']'. */
2755 ++p;
2756 }
2757
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 {
2760 if (len == -1)
2761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002763 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 if (*key == NUL)
2765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!quiet)
2767 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
2771 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 lp->ll_list = NULL;
2773 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002774 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002775
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002776 /* When assigning to a scope dictionary check that a function and
2777 * variable name is valid (only variable name unless it is l: or
2778 * g: dictionary). Disallow overwriting a builtin function. */
2779 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002780 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 int prevval;
2782 int wrong;
2783
2784 if (len != -1)
2785 {
2786 prevval = key[len];
2787 key[len] = NUL;
2788 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002789 else
2790 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2792 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002794 || !valid_varname(key);
2795 if (len != -1)
2796 key[len] = prevval;
2797 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002798 return NULL;
2799 }
2800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 /* Can't add "v:" variable. */
2804 if (lp->ll_dict == &vimvardict)
2805 {
2806 EMSG2(_(e_illvar), name);
2807 return NULL;
2808 }
2809
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002810 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 if (len == -1)
2816 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 }
2819 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 if (len == -1)
2824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 p = NULL;
2827 break;
2828 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002829 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002830 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002831 return NULL;
2832
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 if (len == -1)
2834 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 }
2837 else
2838 {
2839 /*
2840 * Get the number and item for the only or first index of the List.
2841 */
2842 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 else
2845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002846 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 clear_tv(&var1);
2848 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002849 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_list = lp->ll_tv->vval.v_list;
2851 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2852 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002854 if (lp->ll_n1 < 0)
2855 {
2856 lp->ll_n1 = 0;
2857 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2858 }
2859 }
2860 if (lp->ll_li == NULL)
2861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 if (!quiet)
2865 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 }
2868
2869 /*
2870 * May need to find the item or absolute index for the second
2871 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 * When no index given: "lp->ll_empty2" is TRUE.
2873 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002877 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002883 {
2884 if (!quiet)
2885 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002887 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 }
2890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2892 if (lp->ll_n1 < 0)
2893 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2894 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 {
2896 if (!quiet)
2897 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002899 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002900 }
2901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002904 }
2905
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return p;
2907}
2908
2909/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 */
2912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002913clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914{
2915 vim_free(lp->ll_exp_name);
2916 vim_free(lp->ll_newkey);
2917}
2918
2919/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002920 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 */
2924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002925set_var_lval(
2926 lval_T *lp,
2927 char_u *endp,
2928 typval_T *rettv,
2929 int copy,
2930 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931{
2932 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002933 listitem_T *ri;
2934 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935
2936 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 cc = *endp;
2941 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 if (op != NULL && *op != '=')
2943 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002946 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002947 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002948 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002949 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002951 if ((di == NULL
2952 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2953 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2954 FALSE)))
2955 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 set_var(lp->ll_name, &tv, FALSE);
2957 clear_tv(&tv);
2958 }
2959 }
2960 else
2961 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002963 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002965 else if (tv_check_lock(lp->ll_newkey == NULL
2966 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002967 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002968 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 else if (lp->ll_range)
2970 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002971 listitem_T *ll_li = lp->ll_li;
2972 int ll_n1 = lp->ll_n1;
2973
2974 /*
2975 * Check whether any of the list items is locked
2976 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002977 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002979 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002980 return;
2981 ri = ri->li_next;
2982 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2983 break;
2984 ll_li = ll_li->li_next;
2985 ++ll_n1;
2986 }
2987
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /*
2989 * Assign the List values to the list items.
2990 */
2991 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002992 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 if (op != NULL && *op != '=')
2994 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2995 else
2996 {
2997 clear_tv(&lp->ll_li->li_tv);
2998 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2999 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 ri = ri->li_next;
3001 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3002 break;
3003 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003006 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 ri = NULL;
3009 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003010 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 lp->ll_li = lp->ll_li->li_next;
3013 ++lp->ll_n1;
3014 }
3015 if (ri != NULL)
3016 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003017 else if (lp->ll_empty2
3018 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 : lp->ll_n1 != lp->ll_n2)
3020 EMSG(_("E711: List value has not enough items"));
3021 }
3022 else
3023 {
3024 /*
3025 * Assign to a List or Dictionary item.
3026 */
3027 if (lp->ll_newkey != NULL)
3028 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 if (op != NULL && *op != '=')
3030 {
3031 EMSG2(_(e_letwrong), op);
3032 return;
3033 }
3034
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003036 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 if (di == NULL)
3038 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003039 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3040 {
3041 vim_free(di);
3042 return;
3043 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003045 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 else if (op != NULL && *op != '=')
3047 {
3048 tv_op(lp->ll_tv, rettv, op);
3049 return;
3050 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003051 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003053
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 /*
3055 * Assign the value to the variable or list item.
3056 */
3057 if (copy)
3058 copy_tv(rettv, lp->ll_tv);
3059 else
3060 {
3061 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003062 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003063 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064 }
3065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066}
3067
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003068/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3070 * Returns OK or FAIL.
3071 */
3072 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003073tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074{
3075 long n;
3076 char_u numbuf[NUMBUFLEN];
3077 char_u *s;
3078
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003079 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3080 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3081 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 {
3083 switch (tv1->v_type)
3084 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003085 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 case VAR_DICT:
3087 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003088 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003089 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003090 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 break;
3092
3093 case VAR_LIST:
3094 if (*op != '+' || tv2->v_type != VAR_LIST)
3095 break;
3096 /* List += List */
3097 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3098 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3099 return OK;
3100
3101 case VAR_NUMBER:
3102 case VAR_STRING:
3103 if (tv2->v_type == VAR_LIST)
3104 break;
3105 if (*op == '+' || *op == '-')
3106 {
3107 /* nr += nr or nr -= nr*/
3108 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003109#ifdef FEAT_FLOAT
3110 if (tv2->v_type == VAR_FLOAT)
3111 {
3112 float_T f = n;
3113
3114 if (*op == '+')
3115 f += tv2->vval.v_float;
3116 else
3117 f -= tv2->vval.v_float;
3118 clear_tv(tv1);
3119 tv1->v_type = VAR_FLOAT;
3120 tv1->vval.v_float = f;
3121 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123#endif
3124 {
3125 if (*op == '+')
3126 n += get_tv_number(tv2);
3127 else
3128 n -= get_tv_number(tv2);
3129 clear_tv(tv1);
3130 tv1->v_type = VAR_NUMBER;
3131 tv1->vval.v_number = n;
3132 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 }
3134 else
3135 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136 if (tv2->v_type == VAR_FLOAT)
3137 break;
3138
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 /* str .= str */
3140 s = get_tv_string(tv1);
3141 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3142 clear_tv(tv1);
3143 tv1->v_type = VAR_STRING;
3144 tv1->vval.v_string = s;
3145 }
3146 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147
3148#ifdef FEAT_FLOAT
3149 case VAR_FLOAT:
3150 {
3151 float_T f;
3152
3153 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3154 && tv2->v_type != VAR_NUMBER
3155 && tv2->v_type != VAR_STRING))
3156 break;
3157 if (tv2->v_type == VAR_FLOAT)
3158 f = tv2->vval.v_float;
3159 else
3160 f = get_tv_number(tv2);
3161 if (*op == '+')
3162 tv1->vval.v_float += f;
3163 else
3164 tv1->vval.v_float -= f;
3165 }
3166 return OK;
3167#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003168 }
3169 }
3170
3171 EMSG2(_(e_letwrong), op);
3172 return FAIL;
3173}
3174
3175/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176 * Add a watcher to a list.
3177 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003179list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180{
3181 lw->lw_next = l->lv_watch;
3182 l->lv_watch = lw;
3183}
3184
3185/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003186 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187 * No warning when it isn't found...
3188 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003190list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191{
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193
3194 lwp = &l->lv_watch;
3195 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3196 {
3197 if (lw == lwrem)
3198 {
3199 *lwp = lw->lw_next;
3200 break;
3201 }
3202 lwp = &lw->lw_next;
3203 }
3204}
3205
3206/*
3207 * Just before removing an item from a list: advance watchers to the next
3208 * item.
3209 */
3210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3216 if (lw->lw_item == item)
3217 lw->lw_item = item->li_next;
3218}
3219
3220/*
3221 * Evaluate the expression used in a ":for var in expr" command.
3222 * "arg" points to "var".
3223 * Set "*errp" to TRUE for an error, FALSE otherwise;
3224 * Return a pointer that holds the info. Null when there is an error.
3225 */
3226 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003227eval_for_line(
3228 char_u *arg,
3229 int *errp,
3230 char_u **nextcmdp,
3231 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232{
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 typval_T tv;
3236 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237
3238 *errp = TRUE; /* default: there is an error */
3239
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 if (fi == NULL)
3242 return NULL;
3243
3244 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3245 if (expr == NULL)
3246 return fi;
3247
3248 expr = skipwhite(expr);
3249 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3250 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003251 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 return fi;
3253 }
3254
3255 if (skip)
3256 ++emsg_skip;
3257 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3258 {
3259 *errp = FALSE;
3260 if (!skip)
3261 {
3262 l = tv.vval.v_list;
3263 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 clear_tv(&tv);
3267 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 else
3269 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003270 /* No need to increment the refcount, it's already set for the
3271 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 fi->fi_list = l;
3273 list_add_watch(l, &fi->fi_lw);
3274 fi->fi_lw.lw_item = l->lv_first;
3275 }
3276 }
3277 }
3278 if (skip)
3279 --emsg_skip;
3280
3281 return fi;
3282}
3283
3284/*
3285 * Use the first item in a ":for" list. Advance to the next.
3286 * Assign the values to the variable (list). "arg" points to the first one.
3287 * Return TRUE when a valid item was found, FALSE when at end of list or
3288 * something wrong.
3289 */
3290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003291next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003293 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296
3297 item = fi->fi_lw.lw_item;
3298 if (item == NULL)
3299 result = FALSE;
3300 else
3301 {
3302 fi->fi_lw.lw_item = item->li_next;
3303 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3304 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3305 }
3306 return result;
3307}
3308
3309/*
3310 * Free the structure used to store info used by ":for".
3311 */
3312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003313free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314{
Bram Moolenaar33570922005-01-25 22:26:29 +00003315 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003317 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003318 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320 list_unref(fi->fi_list);
3321 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322 vim_free(fi);
3323}
3324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3326
3327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003328set_context_for_expression(
3329 expand_T *xp,
3330 char_u *arg,
3331 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 int got_eq = FALSE;
3334 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003335 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 if (cmdidx == CMD_let)
3338 {
3339 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003340 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 {
3342 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003343 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 {
3345 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003346 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347 if (vim_iswhite(*p))
3348 break;
3349 }
3350 return;
3351 }
3352 }
3353 else
3354 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3355 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 while ((xp->xp_pattern = vim_strpbrk(arg,
3357 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3358 {
3359 c = *xp->xp_pattern;
3360 if (c == '&')
3361 {
3362 c = xp->xp_pattern[1];
3363 if (c == '&')
3364 {
3365 ++xp->xp_pattern;
3366 xp->xp_context = cmdidx != CMD_let || got_eq
3367 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3368 }
3369 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003372 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3373 xp->xp_pattern += 2;
3374
3375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377 else if (c == '$')
3378 {
3379 /* environment variable */
3380 xp->xp_context = EXPAND_ENV_VARS;
3381 }
3382 else if (c == '=')
3383 {
3384 got_eq = TRUE;
3385 xp->xp_context = EXPAND_EXPRESSION;
3386 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003387 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 && xp->xp_context == EXPAND_FUNCTIONS
3389 && vim_strchr(xp->xp_pattern, '(') == NULL)
3390 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003391 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 break;
3393 }
3394 else if (cmdidx != CMD_let || got_eq)
3395 {
3396 if (c == '"') /* string */
3397 {
3398 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3399 if (c == '\\' && xp->xp_pattern[1] != NUL)
3400 ++xp->xp_pattern;
3401 xp->xp_context = EXPAND_NOTHING;
3402 }
3403 else if (c == '\'') /* literal string */
3404 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003405 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3407 /* skip */ ;
3408 xp->xp_context = EXPAND_NOTHING;
3409 }
3410 else if (c == '|')
3411 {
3412 if (xp->xp_pattern[1] == '|')
3413 {
3414 ++xp->xp_pattern;
3415 xp->xp_context = EXPAND_EXPRESSION;
3416 }
3417 else
3418 xp->xp_context = EXPAND_COMMANDS;
3419 }
3420 else
3421 xp->xp_context = EXPAND_EXPRESSION;
3422 }
3423 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003424 /* Doesn't look like something valid, expand as an expression
3425 * anyway. */
3426 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 arg = xp->xp_pattern;
3428 if (*arg != NUL)
3429 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3430 /* skip */ ;
3431 }
3432 xp->xp_pattern = arg;
3433}
3434
3435#endif /* FEAT_CMDL_COMPL */
3436
3437/*
3438 * ":1,25call func(arg1, arg2)" function call.
3439 */
3440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003441ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 char_u *arg = eap->arg;
3444 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003446 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 linenr_T lnum;
3450 int doesrange;
3451 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003452 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003454 if (eap->skip)
3455 {
3456 /* trans_function_name() doesn't work well when skipping, use eval0()
3457 * instead to skip to any following command, e.g. for:
3458 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003459 ++emsg_skip;
3460 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3461 clear_tv(&rettv);
3462 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003463 return;
3464 }
3465
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003466 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003467 if (fudi.fd_newkey != NULL)
3468 {
3469 /* Still need to give an error message for missing key. */
3470 EMSG2(_(e_dictkey), fudi.fd_newkey);
3471 vim_free(fudi.fd_newkey);
3472 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003473 if (tofree == NULL)
3474 return;
3475
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003476 /* Increase refcount on dictionary, it could get deleted when evaluating
3477 * the arguments. */
3478 if (fudi.fd_dict != NULL)
3479 ++fudi.fd_dict->dv_refcount;
3480
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003481 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003482 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003483 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
Bram Moolenaar532c7802005-01-27 14:44:31 +00003485 /* Skip white space to allow ":call func ()". Not good, but required for
3486 * backward compatibility. */
3487 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003488 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 if (*startarg != '(')
3491 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003492 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 goto end;
3494 }
3495
3496 /*
3497 * When skipping, evaluate the function once, to find the end of the
3498 * arguments.
3499 * When the function takes a range, this is discovered after the first
3500 * call, and the loop is broken.
3501 */
3502 if (eap->skip)
3503 {
3504 ++emsg_skip;
3505 lnum = eap->line2; /* do it once, also with an invalid range */
3506 }
3507 else
3508 lnum = eap->line1;
3509 for ( ; lnum <= eap->line2; ++lnum)
3510 {
3511 if (!eap->skip && eap->addr_count > 0)
3512 {
3513 curwin->w_cursor.lnum = lnum;
3514 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003515#ifdef FEAT_VIRTUALEDIT
3516 curwin->w_cursor.coladd = 0;
3517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003520 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003521 eap->line1, eap->line2, &doesrange,
3522 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 failed = TRUE;
3525 break;
3526 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003527
3528 /* Handle a function returning a Funcref, Dictionary or List. */
3529 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3530 {
3531 failed = TRUE;
3532 break;
3533 }
3534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003535 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (doesrange || eap->skip)
3537 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003540 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003542 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (aborting())
3544 break;
3545 }
3546 if (eap->skip)
3547 --emsg_skip;
3548
3549 if (!failed)
3550 {
3551 /* Check for trailing illegal characters and a following command. */
3552 if (!ends_excmd(*arg))
3553 {
3554 emsg_severe = TRUE;
3555 EMSG(_(e_trailing));
3556 }
3557 else
3558 eap->nextcmd = check_nextcmd(arg);
3559 }
3560
3561end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003562 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003563 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564}
3565
3566/*
3567 * ":unlet[!] var1 ... " command.
3568 */
3569 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003570ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572 ex_unletlock(eap, eap->arg, 0);
3573}
3574
3575/*
3576 * ":lockvar" and ":unlockvar" commands
3577 */
3578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582 int deep = 2;
3583
3584 if (eap->forceit)
3585 deep = -1;
3586 else if (vim_isdigit(*arg))
3587 {
3588 deep = getdigits(&arg);
3589 arg = skipwhite(arg);
3590 }
3591
3592 ex_unletlock(eap, arg, deep);
3593}
3594
3595/*
3596 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3597 */
3598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003599ex_unletlock(
3600 exarg_T *eap,
3601 char_u *argstart,
3602 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603{
3604 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 do
3610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003612 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003613 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 if (lv.ll_name == NULL)
3615 error = TRUE; /* error but continue parsing */
3616 if (name_end == NULL || (!vim_iswhite(*name_end)
3617 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (name_end != NULL)
3620 {
3621 emsg_severe = TRUE;
3622 EMSG(_(e_trailing));
3623 }
3624 if (!(eap->skip || error))
3625 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 break;
3627 }
3628
3629 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 {
3631 if (eap->cmdidx == CMD_unlet)
3632 {
3633 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3634 error = TRUE;
3635 }
3636 else
3637 {
3638 if (do_lock_var(&lv, name_end, deep,
3639 eap->cmdidx == CMD_lockvar) == FAIL)
3640 error = TRUE;
3641 }
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (!eap->skip)
3645 clear_lval(&lv);
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 arg = skipwhite(name_end);
3648 } while (!ends_excmd(*arg));
3649
3650 eap->nextcmd = check_nextcmd(arg);
3651}
3652
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654do_unlet_var(
3655 lval_T *lp,
3656 char_u *name_end,
3657 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 int ret = OK;
3660 int cc;
3661
3662 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 cc = *name_end;
3665 *name_end = NUL;
3666
3667 /* Normal name or expanded name. */
3668 if (check_changedtick(lp->ll_name))
3669 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003674 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003676 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003677 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 else if (lp->ll_range)
3680 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003682 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003683 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003684
3685 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3686 {
3687 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003688 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 return FAIL;
3690 ll_li = li;
3691 ++ll_n1;
3692 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693
3694 /* Delete a range of List items. */
3695 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3696 {
3697 li = lp->ll_li->li_next;
3698 listitem_remove(lp->ll_list, lp->ll_li);
3699 lp->ll_li = li;
3700 ++lp->ll_n1;
3701 }
3702 }
3703 else
3704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003705 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 /* unlet a List item. */
3707 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003710 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 }
3712
3713 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714}
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716/*
3717 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003718 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
3720 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003721do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 hashtab_T *ht;
3724 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003725 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003726 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003727 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
Bram Moolenaar33570922005-01-25 22:26:29 +00003729 ht = find_var_ht(name, &varname);
3730 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003732 if (ht == &globvarht)
3733 d = &globvardict;
3734 else if (current_funccal != NULL
3735 && ht == &current_funccal->l_vars.dv_hashtab)
3736 d = &current_funccal->l_vars;
3737 else if (ht == &compat_hashtab)
3738 d = &vimvardict;
3739 else
3740 {
3741 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3742 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3743 }
3744 if (d == NULL)
3745 {
3746 EMSG2(_(e_intern2), "do_unlet()");
3747 return FAIL;
3748 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003749 hi = hash_find(ht, varname);
3750 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003751 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003752 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003753 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003754 || var_check_ro(di->di_flags, name, FALSE)
3755 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003756 return FAIL;
3757
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003758 delete_var(ht, hi);
3759 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762 if (forceit)
3763 return OK;
3764 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 return FAIL;
3766}
3767
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003768/*
3769 * Lock or unlock variable indicated by "lp".
3770 * "deep" is the levels to go (-1 for unlimited);
3771 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3772 */
3773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003774do_lock_var(
3775 lval_T *lp,
3776 char_u *name_end,
3777 int deep,
3778 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003779{
3780 int ret = OK;
3781 int cc;
3782 dictitem_T *di;
3783
3784 if (deep == 0) /* nothing to do */
3785 return OK;
3786
3787 if (lp->ll_tv == NULL)
3788 {
3789 cc = *name_end;
3790 *name_end = NUL;
3791
3792 /* Normal name or expanded name. */
3793 if (check_changedtick(lp->ll_name))
3794 ret = FAIL;
3795 else
3796 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003797 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003798 if (di == NULL)
3799 ret = FAIL;
3800 else
3801 {
3802 if (lock)
3803 di->di_flags |= DI_FLAGS_LOCK;
3804 else
3805 di->di_flags &= ~DI_FLAGS_LOCK;
3806 item_lock(&di->di_tv, deep, lock);
3807 }
3808 }
3809 *name_end = cc;
3810 }
3811 else if (lp->ll_range)
3812 {
3813 listitem_T *li = lp->ll_li;
3814
3815 /* (un)lock a range of List items. */
3816 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3817 {
3818 item_lock(&li->li_tv, deep, lock);
3819 li = li->li_next;
3820 ++lp->ll_n1;
3821 }
3822 }
3823 else if (lp->ll_list != NULL)
3824 /* (un)lock a List item. */
3825 item_lock(&lp->ll_li->li_tv, deep, lock);
3826 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003827 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003828 item_lock(&lp->ll_di->di_tv, deep, lock);
3829
3830 return ret;
3831}
3832
3833/*
3834 * Lock or unlock an item. "deep" is nr of levels to go.
3835 */
3836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003837item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003838{
3839 static int recurse = 0;
3840 list_T *l;
3841 listitem_T *li;
3842 dict_T *d;
3843 hashitem_T *hi;
3844 int todo;
3845
3846 if (recurse >= DICT_MAXNEST)
3847 {
3848 EMSG(_("E743: variable nested too deep for (un)lock"));
3849 return;
3850 }
3851 if (deep == 0)
3852 return;
3853 ++recurse;
3854
3855 /* lock/unlock the item itself */
3856 if (lock)
3857 tv->v_lock |= VAR_LOCKED;
3858 else
3859 tv->v_lock &= ~VAR_LOCKED;
3860
3861 switch (tv->v_type)
3862 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003863 case VAR_UNKNOWN:
3864 case VAR_NUMBER:
3865 case VAR_STRING:
3866 case VAR_FUNC:
3867 case VAR_FLOAT:
3868 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003869 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003870 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003871 break;
3872
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003873 case VAR_LIST:
3874 if ((l = tv->vval.v_list) != NULL)
3875 {
3876 if (lock)
3877 l->lv_lock |= VAR_LOCKED;
3878 else
3879 l->lv_lock &= ~VAR_LOCKED;
3880 if (deep < 0 || deep > 1)
3881 /* recursive: lock/unlock the items the List contains */
3882 for (li = l->lv_first; li != NULL; li = li->li_next)
3883 item_lock(&li->li_tv, deep - 1, lock);
3884 }
3885 break;
3886 case VAR_DICT:
3887 if ((d = tv->vval.v_dict) != NULL)
3888 {
3889 if (lock)
3890 d->dv_lock |= VAR_LOCKED;
3891 else
3892 d->dv_lock &= ~VAR_LOCKED;
3893 if (deep < 0 || deep > 1)
3894 {
3895 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003896 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003897 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3898 {
3899 if (!HASHITEM_EMPTY(hi))
3900 {
3901 --todo;
3902 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3903 }
3904 }
3905 }
3906 }
3907 }
3908 --recurse;
3909}
3910
Bram Moolenaara40058a2005-07-11 22:42:07 +00003911/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003912 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3913 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003914 */
3915 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003916tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003917{
3918 return (tv->v_lock & VAR_LOCKED)
3919 || (tv->v_type == VAR_LIST
3920 && tv->vval.v_list != NULL
3921 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3922 || (tv->v_type == VAR_DICT
3923 && tv->vval.v_dict != NULL
3924 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3925}
3926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3928/*
3929 * Delete all "menutrans_" variables.
3930 */
3931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003932del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003935 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003938 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 {
3941 if (!HASHITEM_EMPTY(hi))
3942 {
3943 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3945 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 }
3947 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949}
3950#endif
3951
3952#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3953
3954/*
3955 * Local string buffer for the next two functions to store a variable name
3956 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3957 * get_user_var_name().
3958 */
3959
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003960static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962static char_u *varnamebuf = NULL;
3963static int varnamebuflen = 0;
3964
3965/*
3966 * Function to concatenate a prefix and a variable name.
3967 */
3968 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003969cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
3971 int len;
3972
3973 len = (int)STRLEN(name) + 3;
3974 if (len > varnamebuflen)
3975 {
3976 vim_free(varnamebuf);
3977 len += 10; /* some additional space */
3978 varnamebuf = alloc(len);
3979 if (varnamebuf == NULL)
3980 {
3981 varnamebuflen = 0;
3982 return NULL;
3983 }
3984 varnamebuflen = len;
3985 }
3986 *varnamebuf = prefix;
3987 varnamebuf[1] = ':';
3988 STRCPY(varnamebuf + 2, name);
3989 return varnamebuf;
3990}
3991
3992/*
3993 * Function given to ExpandGeneric() to obtain the list of user defined
3994 * (global/buffer/window/built-in) variable names.
3995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003997get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003999 static long_u gdone;
4000 static long_u bdone;
4001 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002#ifdef FEAT_WINDOWS
4003 static long_u tdone;
4004#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004005 static int vidx;
4006 static hashitem_T *hi;
4007 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004011 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004012#ifdef FEAT_WINDOWS
4013 tdone = 0;
4014#endif
4015 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004016
4017 /* Global variables */
4018 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004020 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004021 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004022 else
4023 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 while (HASHITEM_EMPTY(hi))
4025 ++hi;
4026 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4027 return cat_prefix_varname('g', hi->hi_key);
4028 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004030
4031 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004032 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004033 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004035 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004036 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004037 else
4038 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004039 while (HASHITEM_EMPTY(hi))
4040 ++hi;
4041 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004045 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 return (char_u *)"b:changedtick";
4047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048
4049 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004050 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004053 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004054 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004055 else
4056 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004057 while (HASHITEM_EMPTY(hi))
4058 ++hi;
4059 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004061
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004062#ifdef FEAT_WINDOWS
4063 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004064 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004065 if (tdone < ht->ht_used)
4066 {
4067 if (tdone++ == 0)
4068 hi = ht->ht_array;
4069 else
4070 ++hi;
4071 while (HASHITEM_EMPTY(hi))
4072 ++hi;
4073 return cat_prefix_varname('t', hi->hi_key);
4074 }
4075#endif
4076
Bram Moolenaar33570922005-01-25 22:26:29 +00004077 /* v: variables */
4078 if (vidx < VV_LEN)
4079 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 vim_free(varnamebuf);
4082 varnamebuf = NULL;
4083 varnamebuflen = 0;
4084 return NULL;
4085}
4086
4087#endif /* FEAT_CMDL_COMPL */
4088
4089/*
4090 * types for expressions.
4091 */
4092typedef enum
4093{
4094 TYPE_UNKNOWN = 0
4095 , TYPE_EQUAL /* == */
4096 , TYPE_NEQUAL /* != */
4097 , TYPE_GREATER /* > */
4098 , TYPE_GEQUAL /* >= */
4099 , TYPE_SMALLER /* < */
4100 , TYPE_SEQUAL /* <= */
4101 , TYPE_MATCH /* =~ */
4102 , TYPE_NOMATCH /* !~ */
4103} exptype_T;
4104
4105/*
4106 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4109 */
4110
4111/*
4112 * Handle zero level expression.
4113 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004115 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * Return OK or FAIL.
4117 */
4118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004119eval0(
4120 char_u *arg,
4121 typval_T *rettv,
4122 char_u **nextcmd,
4123 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
4125 int ret;
4126 char_u *p;
4127
4128 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (ret == FAIL || !ends_excmd(*p))
4131 {
4132 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /*
4135 * Report the invalid expression unless the expression evaluation has
4136 * been cancelled due to an aborting error, an interrupt, or an
4137 * exception.
4138 */
4139 if (!aborting())
4140 EMSG2(_(e_invexpr2), arg);
4141 ret = FAIL;
4142 }
4143 if (nextcmd != NULL)
4144 *nextcmd = check_nextcmd(p);
4145
4146 return ret;
4147}
4148
4149/*
4150 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004151 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 *
4153 * "arg" must point to the first non-white of the expression.
4154 * "arg" is advanced to the next non-white after the recognized expression.
4155 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004156 * Note: "rettv.v_lock" is not set.
4157 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 * Return OK or FAIL.
4159 */
4160 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004161eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
4163 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004164 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 /*
4167 * Get the first variable.
4168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 return FAIL;
4171
4172 if ((*arg)[0] == '?')
4173 {
4174 result = FALSE;
4175 if (evaluate)
4176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 int error = FALSE;
4178
4179 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (error)
4183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185
4186 /*
4187 * Get the second variable.
4188 */
4189 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192
4193 /*
4194 * Check for the ":".
4195 */
4196 if ((*arg)[0] != ':')
4197 {
4198 EMSG(_("E109: Missing ':' after '?'"));
4199 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return FAIL;
4202 }
4203
4204 /*
4205 * Get the third variable.
4206 */
4207 *arg = skipwhite(*arg + 1);
4208 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4209 {
4210 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213 }
4214 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217
4218 return OK;
4219}
4220
4221/*
4222 * Handle first level expression:
4223 * expr2 || expr2 || expr2 logical OR
4224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004231eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 long result;
4235 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 /*
4239 * Get the first variable.
4240 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 return FAIL;
4243
4244 /*
4245 * Repeat until there is no following "||".
4246 */
4247 first = TRUE;
4248 result = FALSE;
4249 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4250 {
4251 if (evaluate && first)
4252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (error)
4257 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 first = FALSE;
4259 }
4260
4261 /*
4262 * Get the second variable.
4263 */
4264 *arg = skipwhite(*arg + 2);
4265 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4266 return FAIL;
4267
4268 /*
4269 * Compute the result.
4270 */
4271 if (evaluate && !result)
4272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 if (error)
4277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 if (evaluate)
4280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 rettv->v_type = VAR_NUMBER;
4282 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 }
4285
4286 return OK;
4287}
4288
4289/*
4290 * Handle second level expression:
4291 * expr3 && expr3 && expr3 logical AND
4292 *
4293 * "arg" must point to the first non-white of the expression.
4294 * "arg" is advanced to the next non-white after the recognized expression.
4295 *
4296 * Return OK or FAIL.
4297 */
4298 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004299eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
Bram Moolenaar33570922005-01-25 22:26:29 +00004301 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 long result;
4303 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 /*
4307 * Get the first variable.
4308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return FAIL;
4311
4312 /*
4313 * Repeat until there is no following "&&".
4314 */
4315 first = TRUE;
4316 result = TRUE;
4317 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4318 {
4319 if (evaluate && first)
4320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 if (error)
4325 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 first = FALSE;
4327 }
4328
4329 /*
4330 * Get the second variable.
4331 */
4332 *arg = skipwhite(*arg + 2);
4333 if (eval4(arg, &var2, evaluate && result) == FAIL)
4334 return FAIL;
4335
4336 /*
4337 * Compute the result.
4338 */
4339 if (evaluate && result)
4340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 if (error)
4345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 if (evaluate)
4348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 rettv->v_type = VAR_NUMBER;
4350 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 }
4353
4354 return OK;
4355}
4356
4357/*
4358 * Handle third level expression:
4359 * var1 == var2
4360 * var1 =~ var2
4361 * var1 != var2
4362 * var1 !~ var2
4363 * var1 > var2
4364 * var1 >= var2
4365 * var1 < var2
4366 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 * var1 is var2
4368 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 *
4370 * "arg" must point to the first non-white of the expression.
4371 * "arg" is advanced to the next non-white after the recognized expression.
4372 *
4373 * Return OK or FAIL.
4374 */
4375 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004376eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377{
Bram Moolenaar33570922005-01-25 22:26:29 +00004378 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 char_u *p;
4380 int i;
4381 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004382 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 int len = 2;
4384 long n1, n2;
4385 char_u *s1, *s2;
4386 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4387 regmatch_T regmatch;
4388 int ic;
4389 char_u *save_cpo;
4390
4391 /*
4392 * Get the first variable.
4393 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 return FAIL;
4396
4397 p = *arg;
4398 switch (p[0])
4399 {
4400 case '=': if (p[1] == '=')
4401 type = TYPE_EQUAL;
4402 else if (p[1] == '~')
4403 type = TYPE_MATCH;
4404 break;
4405 case '!': if (p[1] == '=')
4406 type = TYPE_NEQUAL;
4407 else if (p[1] == '~')
4408 type = TYPE_NOMATCH;
4409 break;
4410 case '>': if (p[1] != '=')
4411 {
4412 type = TYPE_GREATER;
4413 len = 1;
4414 }
4415 else
4416 type = TYPE_GEQUAL;
4417 break;
4418 case '<': if (p[1] != '=')
4419 {
4420 type = TYPE_SMALLER;
4421 len = 1;
4422 }
4423 else
4424 type = TYPE_SEQUAL;
4425 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004426 case 'i': if (p[1] == 's')
4427 {
4428 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4429 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004430 i = p[len];
4431 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 {
4433 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4434 type_is = TRUE;
4435 }
4436 }
4437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439
4440 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004441 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 */
4443 if (type != TYPE_UNKNOWN)
4444 {
4445 /* extra question mark appended: ignore case */
4446 if (p[len] == '?')
4447 {
4448 ic = TRUE;
4449 ++len;
4450 }
4451 /* extra '#' appended: match case */
4452 else if (p[len] == '#')
4453 {
4454 ic = FALSE;
4455 ++len;
4456 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004457 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 else
4459 ic = p_ic;
4460
4461 /*
4462 * Get the second variable.
4463 */
4464 *arg = skipwhite(p + len);
4465 if (eval5(arg, &var2, evaluate) == FAIL)
4466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 return FAIL;
4469 }
4470
4471 if (evaluate)
4472 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004473 if (type_is && rettv->v_type != var2.v_type)
4474 {
4475 /* For "is" a different type always means FALSE, for "notis"
4476 * it means TRUE. */
4477 n1 = (type == TYPE_NEQUAL);
4478 }
4479 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4480 {
4481 if (type_is)
4482 {
4483 n1 = (rettv->v_type == var2.v_type
4484 && rettv->vval.v_list == var2.vval.v_list);
4485 if (type == TYPE_NEQUAL)
4486 n1 = !n1;
4487 }
4488 else if (rettv->v_type != var2.v_type
4489 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4490 {
4491 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004492 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004494 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 clear_tv(rettv);
4496 clear_tv(&var2);
4497 return FAIL;
4498 }
4499 else
4500 {
4501 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004502 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4503 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004504 if (type == TYPE_NEQUAL)
4505 n1 = !n1;
4506 }
4507 }
4508
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004509 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4510 {
4511 if (type_is)
4512 {
4513 n1 = (rettv->v_type == var2.v_type
4514 && rettv->vval.v_dict == var2.vval.v_dict);
4515 if (type == TYPE_NEQUAL)
4516 n1 = !n1;
4517 }
4518 else if (rettv->v_type != var2.v_type
4519 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4520 {
4521 if (rettv->v_type != var2.v_type)
4522 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4523 else
4524 EMSG(_("E736: Invalid operation for Dictionary"));
4525 clear_tv(rettv);
4526 clear_tv(&var2);
4527 return FAIL;
4528 }
4529 else
4530 {
4531 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004532 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4533 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004534 if (type == TYPE_NEQUAL)
4535 n1 = !n1;
4536 }
4537 }
4538
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004539 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4540 {
4541 if (rettv->v_type != var2.v_type
4542 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4543 {
4544 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004545 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004546 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004547 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004548 clear_tv(rettv);
4549 clear_tv(&var2);
4550 return FAIL;
4551 }
4552 else
4553 {
4554 /* Compare two Funcrefs for being equal or unequal. */
4555 if (rettv->vval.v_string == NULL
4556 || var2.vval.v_string == NULL)
4557 n1 = FALSE;
4558 else
4559 n1 = STRCMP(rettv->vval.v_string,
4560 var2.vval.v_string) == 0;
4561 if (type == TYPE_NEQUAL)
4562 n1 = !n1;
4563 }
4564 }
4565
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004566#ifdef FEAT_FLOAT
4567 /*
4568 * If one of the two variables is a float, compare as a float.
4569 * When using "=~" or "!~", always compare as string.
4570 */
4571 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4572 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4573 {
4574 float_T f1, f2;
4575
4576 if (rettv->v_type == VAR_FLOAT)
4577 f1 = rettv->vval.v_float;
4578 else
4579 f1 = get_tv_number(rettv);
4580 if (var2.v_type == VAR_FLOAT)
4581 f2 = var2.vval.v_float;
4582 else
4583 f2 = get_tv_number(&var2);
4584 n1 = FALSE;
4585 switch (type)
4586 {
4587 case TYPE_EQUAL: n1 = (f1 == f2); break;
4588 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4589 case TYPE_GREATER: n1 = (f1 > f2); break;
4590 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4591 case TYPE_SMALLER: n1 = (f1 < f2); break;
4592 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4593 case TYPE_UNKNOWN:
4594 case TYPE_MATCH:
4595 case TYPE_NOMATCH: break; /* avoid gcc warning */
4596 }
4597 }
4598#endif
4599
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 /*
4601 * If one of the two variables is a number, compare as a number.
4602 * When using "=~" or "!~", always compare as string.
4603 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004604 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004607 n1 = get_tv_number(rettv);
4608 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 switch (type)
4610 {
4611 case TYPE_EQUAL: n1 = (n1 == n2); break;
4612 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4613 case TYPE_GREATER: n1 = (n1 > n2); break;
4614 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4615 case TYPE_SMALLER: n1 = (n1 < n2); break;
4616 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4617 case TYPE_UNKNOWN:
4618 case TYPE_MATCH:
4619 case TYPE_NOMATCH: break; /* avoid gcc warning */
4620 }
4621 }
4622 else
4623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004624 s1 = get_tv_string_buf(rettv, buf1);
4625 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4627 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4628 else
4629 i = 0;
4630 n1 = FALSE;
4631 switch (type)
4632 {
4633 case TYPE_EQUAL: n1 = (i == 0); break;
4634 case TYPE_NEQUAL: n1 = (i != 0); break;
4635 case TYPE_GREATER: n1 = (i > 0); break;
4636 case TYPE_GEQUAL: n1 = (i >= 0); break;
4637 case TYPE_SMALLER: n1 = (i < 0); break;
4638 case TYPE_SEQUAL: n1 = (i <= 0); break;
4639
4640 case TYPE_MATCH:
4641 case TYPE_NOMATCH:
4642 /* avoid 'l' flag in 'cpoptions' */
4643 save_cpo = p_cpo;
4644 p_cpo = (char_u *)"";
4645 regmatch.regprog = vim_regcomp(s2,
4646 RE_MAGIC + RE_STRING);
4647 regmatch.rm_ic = ic;
4648 if (regmatch.regprog != NULL)
4649 {
4650 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004651 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (type == TYPE_NOMATCH)
4653 n1 = !n1;
4654 }
4655 p_cpo = save_cpo;
4656 break;
4657
4658 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4659 }
4660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 clear_tv(rettv);
4662 clear_tv(&var2);
4663 rettv->v_type = VAR_NUMBER;
4664 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 }
4666 }
4667
4668 return OK;
4669}
4670
4671/*
4672 * Handle fourth level expression:
4673 * + number addition
4674 * - number subtraction
4675 * . string concatenation
4676 *
4677 * "arg" must point to the first non-white of the expression.
4678 * "arg" is advanced to the next non-white after the recognized expression.
4679 *
4680 * Return OK or FAIL.
4681 */
4682 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004683eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
Bram Moolenaar33570922005-01-25 22:26:29 +00004685 typval_T var2;
4686 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 int op;
4688 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004689#ifdef FEAT_FLOAT
4690 float_T f1 = 0, f2 = 0;
4691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 char_u *s1, *s2;
4693 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4694 char_u *p;
4695
4696 /*
4697 * Get the first variable.
4698 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004699 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 return FAIL;
4701
4702 /*
4703 * Repeat computing, until no '+', '-' or '.' is following.
4704 */
4705 for (;;)
4706 {
4707 op = **arg;
4708 if (op != '+' && op != '-' && op != '.')
4709 break;
4710
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711 if ((op != '+' || rettv->v_type != VAR_LIST)
4712#ifdef FEAT_FLOAT
4713 && (op == '.' || rettv->v_type != VAR_FLOAT)
4714#endif
4715 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 {
4717 /* For "list + ...", an illegal use of the first operand as
4718 * a number cannot be determined before evaluating the 2nd
4719 * operand: if this is also a list, all is ok.
4720 * For "something . ...", "something - ..." or "non-list + ...",
4721 * we know that the first operand needs to be a string or number
4722 * without evaluating the 2nd operand. So check before to avoid
4723 * side effects after an error. */
4724 if (evaluate && get_tv_string_chk(rettv) == NULL)
4725 {
4726 clear_tv(rettv);
4727 return FAIL;
4728 }
4729 }
4730
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 /*
4732 * Get the second variable.
4733 */
4734 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004735 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004737 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 return FAIL;
4739 }
4740
4741 if (evaluate)
4742 {
4743 /*
4744 * Compute the result.
4745 */
4746 if (op == '.')
4747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4749 s2 = get_tv_string_buf_chk(&var2, buf2);
4750 if (s2 == NULL) /* type error ? */
4751 {
4752 clear_tv(rettv);
4753 clear_tv(&var2);
4754 return FAIL;
4755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004756 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 clear_tv(rettv);
4758 rettv->v_type = VAR_STRING;
4759 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004761 else if (op == '+' && rettv->v_type == VAR_LIST
4762 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004763 {
4764 /* concatenate Lists */
4765 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4766 &var3) == FAIL)
4767 {
4768 clear_tv(rettv);
4769 clear_tv(&var2);
4770 return FAIL;
4771 }
4772 clear_tv(rettv);
4773 *rettv = var3;
4774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 else
4776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 int error = FALSE;
4778
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004779#ifdef FEAT_FLOAT
4780 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004782 f1 = rettv->vval.v_float;
4783 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004785 else
4786#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788 n1 = get_tv_number_chk(rettv, &error);
4789 if (error)
4790 {
4791 /* This can only happen for "list + non-list". For
4792 * "non-list + ..." or "something - ...", we returned
4793 * before evaluating the 2nd operand. */
4794 clear_tv(rettv);
4795 return FAIL;
4796 }
4797#ifdef FEAT_FLOAT
4798 if (var2.v_type == VAR_FLOAT)
4799 f1 = n1;
4800#endif
4801 }
4802#ifdef FEAT_FLOAT
4803 if (var2.v_type == VAR_FLOAT)
4804 {
4805 f2 = var2.vval.v_float;
4806 n2 = 0;
4807 }
4808 else
4809#endif
4810 {
4811 n2 = get_tv_number_chk(&var2, &error);
4812 if (error)
4813 {
4814 clear_tv(rettv);
4815 clear_tv(&var2);
4816 return FAIL;
4817 }
4818#ifdef FEAT_FLOAT
4819 if (rettv->v_type == VAR_FLOAT)
4820 f2 = n2;
4821#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824
4825#ifdef FEAT_FLOAT
4826 /* If there is a float on either side the result is a float. */
4827 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4828 {
4829 if (op == '+')
4830 f1 = f1 + f2;
4831 else
4832 f1 = f1 - f2;
4833 rettv->v_type = VAR_FLOAT;
4834 rettv->vval.v_float = f1;
4835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837#endif
4838 {
4839 if (op == '+')
4840 n1 = n1 + n2;
4841 else
4842 n1 = n1 - n2;
4843 rettv->v_type = VAR_NUMBER;
4844 rettv->vval.v_number = n1;
4845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 }
4850 return OK;
4851}
4852
4853/*
4854 * Handle fifth level expression:
4855 * * number multiplication
4856 * / number division
4857 * % number modulo
4858 *
4859 * "arg" must point to the first non-white of the expression.
4860 * "arg" is advanced to the next non-white after the recognized expression.
4861 *
4862 * Return OK or FAIL.
4863 */
4864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004865eval6(
4866 char_u **arg,
4867 typval_T *rettv,
4868 int evaluate,
4869 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
Bram Moolenaar33570922005-01-25 22:26:29 +00004871 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 int op;
4873 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874#ifdef FEAT_FLOAT
4875 int use_float = FALSE;
4876 float_T f1 = 0, f2;
4877#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004878 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 /*
4881 * Get the first variable.
4882 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004883 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 return FAIL;
4885
4886 /*
4887 * Repeat computing, until no '*', '/' or '%' is following.
4888 */
4889 for (;;)
4890 {
4891 op = **arg;
4892 if (op != '*' && op != '/' && op != '%')
4893 break;
4894
4895 if (evaluate)
4896 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897#ifdef FEAT_FLOAT
4898 if (rettv->v_type == VAR_FLOAT)
4899 {
4900 f1 = rettv->vval.v_float;
4901 use_float = TRUE;
4902 n1 = 0;
4903 }
4904 else
4905#endif
4906 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004907 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004908 if (error)
4909 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911 else
4912 n1 = 0;
4913
4914 /*
4915 * Get the second variable.
4916 */
4917 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004918 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 return FAIL;
4920
4921 if (evaluate)
4922 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923#ifdef FEAT_FLOAT
4924 if (var2.v_type == VAR_FLOAT)
4925 {
4926 if (!use_float)
4927 {
4928 f1 = n1;
4929 use_float = TRUE;
4930 }
4931 f2 = var2.vval.v_float;
4932 n2 = 0;
4933 }
4934 else
4935#endif
4936 {
4937 n2 = get_tv_number_chk(&var2, &error);
4938 clear_tv(&var2);
4939 if (error)
4940 return FAIL;
4941#ifdef FEAT_FLOAT
4942 if (use_float)
4943 f2 = n2;
4944#endif
4945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
4947 /*
4948 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004951#ifdef FEAT_FLOAT
4952 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954 if (op == '*')
4955 f1 = f1 * f2;
4956 else if (op == '/')
4957 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004958# ifdef VMS
4959 /* VMS crashes on divide by zero, work around it */
4960 if (f2 == 0.0)
4961 {
4962 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004963 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004964 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004965 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 }
4969 else
4970 f1 = f1 / f2;
4971# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 /* We rely on the floating point library to handle divide
4973 * by zero to result in "inf" and not a crash. */
4974 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004979 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 return FAIL;
4981 }
4982 rettv->v_type = VAR_FLOAT;
4983 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 if (op == '*')
4989 n1 = n1 * n2;
4990 else if (op == '/')
4991 {
4992 if (n2 == 0) /* give an error message? */
4993 {
4994 if (n1 == 0)
4995 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4996 else if (n1 < 0)
4997 n1 = -0x7fffffffL;
4998 else
4999 n1 = 0x7fffffffL;
5000 }
5001 else
5002 n1 = n1 / n2;
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005005 {
5006 if (n2 == 0) /* give an error message? */
5007 n1 = 0;
5008 else
5009 n1 = n1 % n2;
5010 }
5011 rettv->v_type = VAR_NUMBER;
5012 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015 }
5016
5017 return OK;
5018}
5019
5020/*
5021 * Handle sixth level expression:
5022 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005023 * "string" string constant
5024 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 * &option-name option value
5026 * @r register contents
5027 * identifier variable value
5028 * function() function call
5029 * $VAR environment variable
5030 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005031 * [expr, expr] List
5032 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 *
5034 * Also handle:
5035 * ! in front logical NOT
5036 * - in front unary minus
5037 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 * trailing [] subscript in String or List
5039 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * "arg" must point to the first non-white of the expression.
5042 * "arg" is advanced to the next non-white after the recognized expression.
5043 *
5044 * Return OK or FAIL.
5045 */
5046 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005047eval7(
5048 char_u **arg,
5049 typval_T *rettv,
5050 int evaluate,
5051 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 long n;
5054 int len;
5055 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 char_u *start_leader, *end_leader;
5057 int ret = OK;
5058 char_u *alias;
5059
5060 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005062 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
5066 /*
5067 * Skip '!' and '-' characters. They are handled later.
5068 */
5069 start_leader = *arg;
5070 while (**arg == '!' || **arg == '-' || **arg == '+')
5071 *arg = skipwhite(*arg + 1);
5072 end_leader = *arg;
5073
5074 switch (**arg)
5075 {
5076 /*
5077 * Number constant.
5078 */
5079 case '0':
5080 case '1':
5081 case '2':
5082 case '3':
5083 case '4':
5084 case '5':
5085 case '6':
5086 case '7':
5087 case '8':
5088 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005089 {
5090#ifdef FEAT_FLOAT
5091 char_u *p = skipdigits(*arg + 1);
5092 int get_float = FALSE;
5093
5094 /* We accept a float when the format matches
5095 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005096 * strict to avoid backwards compatibility problems.
5097 * Don't look for a float after the "." operator, so that
5098 * ":let vers = 1.2.3" doesn't fail. */
5099 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005101 get_float = TRUE;
5102 p = skipdigits(p + 2);
5103 if (*p == 'e' || *p == 'E')
5104 {
5105 ++p;
5106 if (*p == '-' || *p == '+')
5107 ++p;
5108 if (!vim_isdigit(*p))
5109 get_float = FALSE;
5110 else
5111 p = skipdigits(p + 1);
5112 }
5113 if (ASCII_ISALPHA(*p) || *p == '.')
5114 get_float = FALSE;
5115 }
5116 if (get_float)
5117 {
5118 float_T f;
5119
5120 *arg += string2float(*arg, &f);
5121 if (evaluate)
5122 {
5123 rettv->v_type = VAR_FLOAT;
5124 rettv->vval.v_float = f;
5125 }
5126 }
5127 else
5128#endif
5129 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005130 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005131 *arg += len;
5132 if (evaluate)
5133 {
5134 rettv->v_type = VAR_NUMBER;
5135 rettv->vval.v_number = n;
5136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
5138 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140
5141 /*
5142 * String constant: "string".
5143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 break;
5146
5147 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151 break;
5152
5153 /*
5154 * List: [expr, expr]
5155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
5158
5159 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 * Dictionary: {key: val, key: val}
5161 */
5162 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5163 break;
5164
5165 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005168 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 break;
5170
5171 /*
5172 * Environment variable: $VAR.
5173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 break;
5176
5177 /*
5178 * Register contents: @r.
5179 */
5180 case '@': ++*arg;
5181 if (evaluate)
5182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005184 rettv->vval.v_string = get_reg_contents(**arg,
5185 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187 if (**arg != NUL)
5188 ++*arg;
5189 break;
5190
5191 /*
5192 * nested expression: (expression).
5193 */
5194 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005195 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 if (**arg == ')')
5197 ++*arg;
5198 else if (ret == OK)
5199 {
5200 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 ret = FAIL;
5203 }
5204 break;
5205
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 break;
5208 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209
5210 if (ret == NOTDONE)
5211 {
5212 /*
5213 * Must be a variable or function name.
5214 * Can also be a curly-braces kind of name: {expr}.
5215 */
5216 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005217 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 if (alias != NULL)
5219 s = alias;
5220
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 ret = FAIL;
5223 else
5224 {
5225 if (**arg == '(') /* recursive! */
5226 {
5227 /* If "s" is the name of a variable of type VAR_FUNC
5228 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005229 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230
5231 /* Invoke the function. */
5232 ret = get_func_tv(s, len, rettv, arg,
5233 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005234 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005235
5236 /* If evaluate is FALSE rettv->v_type was not set in
5237 * get_func_tv, but it's needed in handle_subscript() to parse
5238 * what follows. So set it here. */
5239 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5240 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005241 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005242 rettv->v_type = VAR_FUNC;
5243 }
5244
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 /* Stop the expression evaluation when immediately
5246 * aborting on error, or when an interrupt occurred or
5247 * an exception was thrown but not caught. */
5248 if (aborting())
5249 {
5250 if (ret == OK)
5251 clear_tv(rettv);
5252 ret = FAIL;
5253 }
5254 }
5255 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005256 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005257 else
5258 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005260 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 }
5262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 *arg = skipwhite(*arg);
5264
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5266 * expr(expr). */
5267 if (ret == OK)
5268 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 /*
5271 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5272 */
5273 if (ret == OK && evaluate && end_leader > start_leader)
5274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005275 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005276 int val = 0;
5277#ifdef FEAT_FLOAT
5278 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005279
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005280 if (rettv->v_type == VAR_FLOAT)
5281 f = rettv->vval.v_float;
5282 else
5283#endif
5284 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287 clear_tv(rettv);
5288 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005290 else
5291 {
5292 while (end_leader > start_leader)
5293 {
5294 --end_leader;
5295 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005296 {
5297#ifdef FEAT_FLOAT
5298 if (rettv->v_type == VAR_FLOAT)
5299 f = !f;
5300 else
5301#endif
5302 val = !val;
5303 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005305 {
5306#ifdef FEAT_FLOAT
5307 if (rettv->v_type == VAR_FLOAT)
5308 f = -f;
5309 else
5310#endif
5311 val = -val;
5312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005314#ifdef FEAT_FLOAT
5315 if (rettv->v_type == VAR_FLOAT)
5316 {
5317 clear_tv(rettv);
5318 rettv->vval.v_float = f;
5319 }
5320 else
5321#endif
5322 {
5323 clear_tv(rettv);
5324 rettv->v_type = VAR_NUMBER;
5325 rettv->vval.v_number = val;
5326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329
5330 return ret;
5331}
5332
5333/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005334 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5335 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5337 */
5338 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005339eval_index(
5340 char_u **arg,
5341 typval_T *rettv,
5342 int evaluate,
5343 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344{
5345 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005346 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 long len = -1;
5349 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352
Bram Moolenaara03f2332016-02-06 18:09:59 +01005353 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005355 case VAR_FUNC:
5356 if (verbose)
5357 EMSG(_("E695: Cannot index a Funcref"));
5358 return FAIL;
5359 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005360#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005361 if (verbose)
5362 EMSG(_(e_float_as_string));
5363 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005364#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005365 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005366 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005367 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 if (verbose)
5369 EMSG(_("E909: Cannot index a special variable"));
5370 return FAIL;
5371 case VAR_UNKNOWN:
5372 if (evaluate)
5373 return FAIL;
5374 /* FALLTHROUGH */
5375
5376 case VAR_STRING:
5377 case VAR_NUMBER:
5378 case VAR_LIST:
5379 case VAR_DICT:
5380 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005381 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005383 init_tv(&var1);
5384 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 /*
5388 * dict.name
5389 */
5390 key = *arg + 1;
5391 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5392 ;
5393 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 }
5397 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399 /*
5400 * something[idx]
5401 *
5402 * Get the (first) variable from inside the [].
5403 */
5404 *arg = skipwhite(*arg + 1);
5405 if (**arg == ':')
5406 empty1 = TRUE;
5407 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5408 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005409 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5410 {
5411 /* not a number or string */
5412 clear_tv(&var1);
5413 return FAIL;
5414 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415
5416 /*
5417 * Get the second variable from inside the [:].
5418 */
5419 if (**arg == ':')
5420 {
5421 range = TRUE;
5422 *arg = skipwhite(*arg + 1);
5423 if (**arg == ']')
5424 empty2 = TRUE;
5425 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005427 if (!empty1)
5428 clear_tv(&var1);
5429 return FAIL;
5430 }
5431 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5432 {
5433 /* not a number or string */
5434 if (!empty1)
5435 clear_tv(&var1);
5436 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437 return FAIL;
5438 }
5439 }
5440
5441 /* Check for the ']'. */
5442 if (**arg != ']')
5443 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005444 if (verbose)
5445 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 clear_tv(&var1);
5447 if (range)
5448 clear_tv(&var2);
5449 return FAIL;
5450 }
5451 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 }
5453
5454 if (evaluate)
5455 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 n1 = 0;
5457 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 n1 = get_tv_number(&var1);
5460 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462 if (range)
5463 {
5464 if (empty2)
5465 n2 = -1;
5466 else
5467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 n2 = get_tv_number(&var2);
5469 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 }
5471 }
5472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005473 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005475 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005476 case VAR_FUNC:
5477 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005478 case VAR_SPECIAL:
5479 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005480 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005481 break; /* not evaluating, skipping over subscript */
5482
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 case VAR_NUMBER:
5484 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 len = (long)STRLEN(s);
5487 if (range)
5488 {
5489 /* The resulting variable is a substring. If the indexes
5490 * are out of range the result is empty. */
5491 if (n1 < 0)
5492 {
5493 n1 = len + n1;
5494 if (n1 < 0)
5495 n1 = 0;
5496 }
5497 if (n2 < 0)
5498 n2 = len + n2;
5499 else if (n2 >= len)
5500 n2 = len;
5501 if (n1 >= len || n2 < 0 || n1 > n2)
5502 s = NULL;
5503 else
5504 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5505 }
5506 else
5507 {
5508 /* The resulting variable is a string of a single
5509 * character. If the index is too big or negative the
5510 * result is empty. */
5511 if (n1 >= len || n1 < 0)
5512 s = NULL;
5513 else
5514 s = vim_strnsave(s + n1, 1);
5515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 clear_tv(rettv);
5517 rettv->v_type = VAR_STRING;
5518 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 break;
5520
5521 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 if (n1 < 0)
5524 n1 = len + n1;
5525 if (!empty1 && (n1 < 0 || n1 >= len))
5526 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005527 /* For a range we allow invalid values and return an empty
5528 * list. A list index out of range is an error. */
5529 if (!range)
5530 {
5531 if (verbose)
5532 EMSGN(_(e_listidx), n1);
5533 return FAIL;
5534 }
5535 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 }
5537 if (range)
5538 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005539 list_T *l;
5540 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541
5542 if (n2 < 0)
5543 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005544 else if (n2 >= len)
5545 n2 = len - 1;
5546 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005547 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 l = list_alloc();
5549 if (l == NULL)
5550 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 n1 <= n2; ++n1)
5553 {
5554 if (list_append_tv(l, &item->li_tv) == FAIL)
5555 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005556 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 return FAIL;
5558 }
5559 item = item->li_next;
5560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 clear_tv(rettv);
5562 rettv->v_type = VAR_LIST;
5563 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005564 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 else
5567 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005568 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 clear_tv(rettv);
5570 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 }
5572 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573
5574 case VAR_DICT:
5575 if (range)
5576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005577 if (verbose)
5578 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 if (len == -1)
5580 clear_tv(&var1);
5581 return FAIL;
5582 }
5583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005584 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585
5586 if (len == -1)
5587 {
5588 key = get_tv_string(&var1);
5589 if (*key == NUL)
5590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005591 if (verbose)
5592 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 clear_tv(&var1);
5594 return FAIL;
5595 }
5596 }
5597
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005598 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005600 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005601 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 if (len == -1)
5603 clear_tv(&var1);
5604 if (item == NULL)
5605 return FAIL;
5606
5607 copy_tv(&item->di_tv, &var1);
5608 clear_tv(rettv);
5609 *rettv = var1;
5610 }
5611 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 }
5613 }
5614
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 return OK;
5616}
5617
5618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 * Get an option value.
5620 * "arg" points to the '&' or '+' before the option name.
5621 * "arg" is advanced to character after the option name.
5622 * Return OK or FAIL.
5623 */
5624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005625get_option_tv(
5626 char_u **arg,
5627 typval_T *rettv, /* when NULL, only check if option exists */
5628 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
5630 char_u *option_end;
5631 long numval;
5632 char_u *stringval;
5633 int opt_type;
5634 int c;
5635 int working = (**arg == '+'); /* has("+option") */
5636 int ret = OK;
5637 int opt_flags;
5638
5639 /*
5640 * Isolate the option name and find its value.
5641 */
5642 option_end = find_option_end(arg, &opt_flags);
5643 if (option_end == NULL)
5644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005645 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 EMSG2(_("E112: Option name missing: %s"), *arg);
5647 return FAIL;
5648 }
5649
5650 if (!evaluate)
5651 {
5652 *arg = option_end;
5653 return OK;
5654 }
5655
5656 c = *option_end;
5657 *option_end = NUL;
5658 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 if (opt_type == -3) /* invalid name */
5662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 EMSG2(_("E113: Unknown option: %s"), *arg);
5665 ret = FAIL;
5666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 if (opt_type == -2) /* hidden string option */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 rettv->v_type = VAR_STRING;
5672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 else if (opt_type == -1) /* hidden number option */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_NUMBER;
5677 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 else if (opt_type == 1) /* number option */
5680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 rettv->v_type = VAR_NUMBER;
5682 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else /* string option */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_STRING;
5687 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 }
5690 else if (working && (opt_type == -2 || opt_type == -1))
5691 ret = FAIL;
5692
5693 *option_end = c; /* put back for error messages */
5694 *arg = option_end;
5695
5696 return ret;
5697}
5698
5699/*
5700 * Allocate a variable for a string constant.
5701 * Return OK or FAIL.
5702 */
5703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005704get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705{
5706 char_u *p;
5707 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 int extra = 0;
5709
5710 /*
5711 * Find the end of the string, skipping backslashed characters.
5712 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005713 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
5715 if (*p == '\\' && p[1] != NUL)
5716 {
5717 ++p;
5718 /* A "\<x>" form occupies at least 4 characters, and produces up
5719 * to 6 characters: reserve space for 2 extra */
5720 if (*p == '<')
5721 extra += 2;
5722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724
5725 if (*p != '"')
5726 {
5727 EMSG2(_("E114: Missing quote: %s"), *arg);
5728 return FAIL;
5729 }
5730
5731 /* If only parsing, set *arg and return here */
5732 if (!evaluate)
5733 {
5734 *arg = p + 1;
5735 return OK;
5736 }
5737
5738 /*
5739 * Copy the string into allocated memory, handling backslashed
5740 * characters.
5741 */
5742 name = alloc((unsigned)(p - *arg + extra));
5743 if (name == NULL)
5744 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 rettv->v_type = VAR_STRING;
5746 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 {
5750 if (*p == '\\')
5751 {
5752 switch (*++p)
5753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 case 'b': *name++ = BS; ++p; break;
5755 case 'e': *name++ = ESC; ++p; break;
5756 case 'f': *name++ = FF; ++p; break;
5757 case 'n': *name++ = NL; ++p; break;
5758 case 'r': *name++ = CAR; ++p; break;
5759 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761 case 'X': /* hex: "\x1", "\x12" */
5762 case 'x':
5763 case 'u': /* Unicode: "\u0023" */
5764 case 'U':
5765 if (vim_isxdigit(p[1]))
5766 {
5767 int n, nr;
5768 int c = toupper(*p);
5769
5770 if (c == 'X')
5771 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005772 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005774 else
5775 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 nr = 0;
5777 while (--n >= 0 && vim_isxdigit(p[1]))
5778 {
5779 ++p;
5780 nr = (nr << 4) + hex2nr(*p);
5781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783#ifdef FEAT_MBYTE
5784 /* For "\u" store the number according to
5785 * 'encoding'. */
5786 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 else
5789#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 break;
5793
5794 /* octal: "\1", "\12", "\123" */
5795 case '0':
5796 case '1':
5797 case '2':
5798 case '3':
5799 case '4':
5800 case '5':
5801 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 case '7': *name = *p++ - '0';
5803 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 *name = (*name << 3) + *p++ - '0';
5806 if (*p >= '0' && *p <= '7')
5807 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 break;
5811
5812 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 if (extra != 0)
5815 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 break;
5818 }
5819 /* FALLTHROUGH */
5820
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 break;
5823 }
5824 }
5825 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 *arg = p + 1;
5831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 return OK;
5833}
5834
5835/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 * Return OK or FAIL.
5838 */
5839 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005840get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
5842 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 int reduce = 0;
5845
5846 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 */
5849 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5850 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 break;
5855 ++reduce;
5856 ++p;
5857 }
5858 }
5859
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 return FAIL;
5864 }
5865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 if (!evaluate)
5868 {
5869 *arg = p + 1;
5870 return OK;
5871 }
5872
5873 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 */
5876 str = alloc((unsigned)((p - *arg) - reduce));
5877 if (str == NULL)
5878 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 rettv->v_type = VAR_STRING;
5880 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 break;
5888 ++p;
5889 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 *arg = p + 1;
5894
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 return OK;
5896}
5897
5898/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899 * Allocate a variable for a List and fill it from "*arg".
5900 * Return OK or FAIL.
5901 */
5902 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005903get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904{
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 list_T *l = NULL;
5906 typval_T tv;
5907 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908
5909 if (evaluate)
5910 {
5911 l = list_alloc();
5912 if (l == NULL)
5913 return FAIL;
5914 }
5915
5916 *arg = skipwhite(*arg + 1);
5917 while (**arg != ']' && **arg != NUL)
5918 {
5919 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5920 goto failret;
5921 if (evaluate)
5922 {
5923 item = listitem_alloc();
5924 if (item != NULL)
5925 {
5926 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005927 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 list_append(l, item);
5929 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005930 else
5931 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 }
5933
5934 if (**arg == ']')
5935 break;
5936 if (**arg != ',')
5937 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 goto failret;
5940 }
5941 *arg = skipwhite(*arg + 1);
5942 }
5943
5944 if (**arg != ']')
5945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947failret:
5948 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005949 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 return FAIL;
5951 }
5952
5953 *arg = skipwhite(*arg + 1);
5954 if (evaluate)
5955 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005956 rettv->v_type = VAR_LIST;
5957 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 ++l->lv_refcount;
5959 }
5960
5961 return OK;
5962}
5963
5964/*
5965 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005966 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005968 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005969list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005971 list_T *l;
5972
5973 l = (list_T *)alloc_clear(sizeof(list_T));
5974 if (l != NULL)
5975 {
5976 /* Prepend the list to the list of lists for garbage collection. */
5977 if (first_list != NULL)
5978 first_list->lv_used_prev = l;
5979 l->lv_used_prev = NULL;
5980 l->lv_used_next = first_list;
5981 first_list = l;
5982 }
5983 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984}
5985
5986/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005987 * Allocate an empty list for a return value.
5988 * Returns OK or FAIL.
5989 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005990 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005991rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005992{
5993 list_T *l = list_alloc();
5994
5995 if (l == NULL)
5996 return FAIL;
5997
5998 rettv->vval.v_list = l;
5999 rettv->v_type = VAR_LIST;
6000 ++l->lv_refcount;
6001 return OK;
6002}
6003
6004/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005 * Unreference a list: decrement the reference count and free it when it
6006 * becomes zero.
6007 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006009list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006011 if (l != NULL && --l->lv_refcount <= 0)
6012 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013}
6014
6015/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006016 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 * Ignores the reference count.
6018 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006019 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006020list_free(
6021 list_T *l,
6022 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023{
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006026 /* Remove the list from the list of lists for garbage collection. */
6027 if (l->lv_used_prev == NULL)
6028 first_list = l->lv_used_next;
6029 else
6030 l->lv_used_prev->lv_used_next = l->lv_used_next;
6031 if (l->lv_used_next != NULL)
6032 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6033
Bram Moolenaard9fba312005-06-26 22:34:35 +00006034 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006036 /* Remove the item before deleting it. */
6037 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006038 if (recurse || (item->li_tv.v_type != VAR_LIST
6039 && item->li_tv.v_type != VAR_DICT))
6040 clear_tv(&item->li_tv);
6041 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 }
6043 vim_free(l);
6044}
6045
6046/*
6047 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006048 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006050 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006051listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052{
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054}
6055
6056/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006057 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006059 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006060listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006062 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 vim_free(item);
6064}
6065
6066/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006067 * Remove a list item from a List and free it. Also clears the value.
6068 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006069 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006070listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006071{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006072 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006073 listitem_free(item);
6074}
6075
6076/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 * Get the number of items in a list.
6078 */
6079 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082 if (l == NULL)
6083 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006084 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085}
6086
6087/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006088 * Return TRUE when two lists have exactly the same values.
6089 */
6090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006091list_equal(
6092 list_T *l1,
6093 list_T *l2,
6094 int ic, /* ignore case for strings */
6095 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096{
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006099 if (l1 == NULL || l2 == NULL)
6100 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006101 if (l1 == l2)
6102 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006103 if (list_len(l1) != list_len(l2))
6104 return FALSE;
6105
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106 for (item1 = l1->lv_first, item2 = l2->lv_first;
6107 item1 != NULL && item2 != NULL;
6108 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006109 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 return FALSE;
6111 return item1 == NULL && item2 == NULL;
6112}
6113
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006114/*
6115 * Return the dictitem that an entry in a hashtable points to.
6116 */
6117 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006119{
6120 return HI2DI(hi);
6121}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006122
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006124 * Return TRUE when two dictionaries have exactly the same key/values.
6125 */
6126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006127dict_equal(
6128 dict_T *d1,
6129 dict_T *d2,
6130 int ic, /* ignore case for strings */
6131 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132{
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 hashitem_T *hi;
6134 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006135 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006137 if (d1 == NULL || d2 == NULL)
6138 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006139 if (d1 == d2)
6140 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141 if (dict_len(d1) != dict_len(d2))
6142 return FALSE;
6143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006144 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006147 if (!HASHITEM_EMPTY(hi))
6148 {
6149 item2 = dict_find(d2, hi->hi_key, -1);
6150 if (item2 == NULL)
6151 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006152 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006153 return FALSE;
6154 --todo;
6155 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006156 }
6157 return TRUE;
6158}
6159
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160static int tv_equal_recurse_limit;
6161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006162/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006163 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006164 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006165 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166 */
6167 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006168tv_equal(
6169 typval_T *tv1,
6170 typval_T *tv2,
6171 int ic, /* ignore case */
6172 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173{
6174 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006175 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006176 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006177 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006179 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006180 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006182 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 * recursiveness to a limit. We guess they are equal then.
6184 * A fixed limit has the problem of still taking an awful long time.
6185 * Reduce the limit every time running into it. That should work fine for
6186 * deeply linked structures that are not recursively linked and catch
6187 * recursiveness quickly. */
6188 if (!recursive)
6189 tv_equal_recurse_limit = 1000;
6190 if (recursive_cnt >= tv_equal_recurse_limit)
6191 {
6192 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006193 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006195
6196 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006199 ++recursive_cnt;
6200 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6201 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006202 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203
6204 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 ++recursive_cnt;
6206 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6207 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006208 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006209
6210 case VAR_FUNC:
6211 return (tv1->vval.v_string != NULL
6212 && tv2->vval.v_string != NULL
6213 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6214
6215 case VAR_NUMBER:
6216 return tv1->vval.v_number == tv2->vval.v_number;
6217
6218 case VAR_STRING:
6219 s1 = get_tv_string_buf(tv1, buf1);
6220 s2 = get_tv_string_buf(tv2, buf2);
6221 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006222
6223 case VAR_SPECIAL:
6224 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006225
6226 case VAR_FLOAT:
6227#ifdef FEAT_FLOAT
6228 return tv1->vval.v_float == tv2->vval.v_float;
6229#endif
6230 case VAR_JOB:
6231#ifdef FEAT_JOB
6232 return tv1->vval.v_job == tv2->vval.v_job;
6233#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006234 case VAR_CHANNEL:
6235#ifdef FEAT_CHANNEL
6236 return tv1->vval.v_channel == tv2->vval.v_channel;
6237#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006238 case VAR_UNKNOWN:
6239 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006240 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006241
Bram Moolenaara03f2332016-02-06 18:09:59 +01006242 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6243 * does not equal anything, not even itself. */
6244 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245}
6246
6247/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 * Locate item with index "n" in list "l" and return it.
6249 * A negative index is counted from the end; -1 is the last item.
6250 * Returns NULL when "n" is out of range.
6251 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006252 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006253list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254{
Bram Moolenaar33570922005-01-25 22:26:29 +00006255 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 long idx;
6257
6258 if (l == NULL)
6259 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006260
6261 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006263 n = l->lv_len + n;
6264
6265 /* Check for index out of range. */
6266 if (n < 0 || n >= l->lv_len)
6267 return NULL;
6268
6269 /* When there is a cached index may start search from there. */
6270 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006272 if (n < l->lv_idx / 2)
6273 {
6274 /* closest to the start of the list */
6275 item = l->lv_first;
6276 idx = 0;
6277 }
6278 else if (n > (l->lv_idx + l->lv_len) / 2)
6279 {
6280 /* closest to the end of the list */
6281 item = l->lv_last;
6282 idx = l->lv_len - 1;
6283 }
6284 else
6285 {
6286 /* closest to the cached index */
6287 item = l->lv_idx_item;
6288 idx = l->lv_idx;
6289 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 }
6291 else
6292 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006293 if (n < l->lv_len / 2)
6294 {
6295 /* closest to the start of the list */
6296 item = l->lv_first;
6297 idx = 0;
6298 }
6299 else
6300 {
6301 /* closest to the end of the list */
6302 item = l->lv_last;
6303 idx = l->lv_len - 1;
6304 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006306
6307 while (n > idx)
6308 {
6309 /* search forward */
6310 item = item->li_next;
6311 ++idx;
6312 }
6313 while (n < idx)
6314 {
6315 /* search backward */
6316 item = item->li_prev;
6317 --idx;
6318 }
6319
6320 /* cache the used index */
6321 l->lv_idx = idx;
6322 l->lv_idx_item = item;
6323
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 return item;
6325}
6326
6327/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006328 * Get list item "l[idx]" as a number.
6329 */
6330 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006331list_find_nr(
6332 list_T *l,
6333 long idx,
6334 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006335{
6336 listitem_T *li;
6337
6338 li = list_find(l, idx);
6339 if (li == NULL)
6340 {
6341 if (errorp != NULL)
6342 *errorp = TRUE;
6343 return -1L;
6344 }
6345 return get_tv_number_chk(&li->li_tv, errorp);
6346}
6347
6348/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006349 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6350 */
6351 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006352list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006353{
6354 listitem_T *li;
6355
6356 li = list_find(l, idx - 1);
6357 if (li == NULL)
6358 {
6359 EMSGN(_(e_listidx), idx);
6360 return NULL;
6361 }
6362 return get_tv_string(&li->li_tv);
6363}
6364
6365/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006366 * Locate "item" list "l" and return its index.
6367 * Returns -1 when "item" is not in the list.
6368 */
6369 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006370list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006371{
6372 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006374
6375 if (l == NULL)
6376 return -1;
6377 idx = 0;
6378 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6379 ++idx;
6380 if (li == NULL)
6381 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006382 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006383}
6384
6385/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006386 * Append item "item" to the end of list "l".
6387 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006388 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006389list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390{
6391 if (l->lv_last == NULL)
6392 {
6393 /* empty list */
6394 l->lv_first = item;
6395 l->lv_last = item;
6396 item->li_prev = NULL;
6397 }
6398 else
6399 {
6400 l->lv_last->li_next = item;
6401 item->li_prev = l->lv_last;
6402 l->lv_last = item;
6403 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006404 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405 item->li_next = NULL;
6406}
6407
6408/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006413list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006415 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416
Bram Moolenaar05159a02005-02-26 23:04:13 +00006417 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419 copy_tv(tv, &li->li_tv);
6420 list_append(l, li);
6421 return OK;
6422}
6423
6424/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006425 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006426 * Return FAIL when out of memory.
6427 */
6428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006429list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430{
6431 listitem_T *li = listitem_alloc();
6432
6433 if (li == NULL)
6434 return FAIL;
6435 li->li_tv.v_type = VAR_DICT;
6436 li->li_tv.v_lock = 0;
6437 li->li_tv.vval.v_dict = dict;
6438 list_append(list, li);
6439 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006440 return OK;
6441}
6442
6443/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006444 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006445 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006446 * Returns FAIL when out of memory.
6447 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006448 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006449list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006450{
6451 listitem_T *li = listitem_alloc();
6452
6453 if (li == NULL)
6454 return FAIL;
6455 list_append(l, li);
6456 li->li_tv.v_type = VAR_STRING;
6457 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006458 if (str == NULL)
6459 li->li_tv.vval.v_string = NULL;
6460 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006461 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006462 return FAIL;
6463 return OK;
6464}
6465
6466/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006467 * Append "n" to list "l".
6468 * Returns FAIL when out of memory.
6469 */
6470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006471list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006472{
6473 listitem_T *li;
6474
6475 li = listitem_alloc();
6476 if (li == NULL)
6477 return FAIL;
6478 li->li_tv.v_type = VAR_NUMBER;
6479 li->li_tv.v_lock = 0;
6480 li->li_tv.vval.v_number = n;
6481 list_append(l, li);
6482 return OK;
6483}
6484
6485/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006487 * If "item" is NULL append at the end.
6488 * Return FAIL when out of memory.
6489 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006491list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492{
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494
6495 if (ni == NULL)
6496 return FAIL;
6497 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006498 list_insert(l, ni, item);
6499 return OK;
6500}
6501
6502 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006503list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006504{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505 if (item == NULL)
6506 /* Append new item at end of list. */
6507 list_append(l, ni);
6508 else
6509 {
6510 /* Insert new item before existing item. */
6511 ni->li_prev = item->li_prev;
6512 ni->li_next = item;
6513 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006514 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006516 ++l->lv_idx;
6517 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006519 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 l->lv_idx_item = NULL;
6522 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006524 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526}
6527
6528/*
6529 * Extend "l1" with "l2".
6530 * If "bef" is NULL append at the end, otherwise insert before this item.
6531 * Returns FAIL when out of memory.
6532 */
6533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535{
Bram Moolenaar33570922005-01-25 22:26:29 +00006536 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006537 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006539 /* We also quit the loop when we have inserted the original item count of
6540 * the list, avoid a hang when we extend a list with itself. */
6541 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6543 return FAIL;
6544 return OK;
6545}
6546
6547/*
6548 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6549 * Return FAIL when out of memory.
6550 */
6551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006552list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553{
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006556 if (l1 == NULL || l2 == NULL)
6557 return FAIL;
6558
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 if (l == NULL)
6562 return FAIL;
6563 tv->v_type = VAR_LIST;
6564 tv->vval.v_list = l;
6565
6566 /* append all items from the second list */
6567 return list_extend(l, l2, NULL);
6568}
6569
6570/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 * Returns NULL when out of memory.
6575 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578{
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 list_T *copy;
6580 listitem_T *item;
6581 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582
6583 if (orig == NULL)
6584 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585
6586 copy = list_alloc();
6587 if (copy != NULL)
6588 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006589 if (copyID != 0)
6590 {
6591 /* Do this before adding the items, because one of the items may
6592 * refer back to this list. */
6593 orig->lv_copyID = copyID;
6594 orig->lv_copylist = copy;
6595 }
6596 for (item = orig->lv_first; item != NULL && !got_int;
6597 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 {
6599 ni = listitem_alloc();
6600 if (ni == NULL)
6601 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006602 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006603 {
6604 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6605 {
6606 vim_free(ni);
6607 break;
6608 }
6609 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006611 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 list_append(copy, ni);
6613 }
6614 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 if (item != NULL)
6616 {
6617 list_unref(copy);
6618 copy = NULL;
6619 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 }
6621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 return copy;
6623}
6624
6625/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006627 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006628 * This used to be called list_remove, but that conflicts with a Sun header
6629 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006633{
Bram Moolenaar33570922005-01-25 22:26:29 +00006634 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006636 /* notify watchers */
6637 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006639 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006640 list_fix_watch(l, ip);
6641 if (ip == item2)
6642 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644
6645 if (item2->li_next == NULL)
6646 l->lv_last = item->li_prev;
6647 else
6648 item2->li_next->li_prev = item->li_prev;
6649 if (item->li_prev == NULL)
6650 l->lv_first = item2->li_next;
6651 else
6652 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006653 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654}
6655
6656/*
6657 * Return an allocated string with the string representation of a list.
6658 * May return NULL.
6659 */
6660 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006661list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662{
6663 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664
6665 if (tv->vval.v_list == NULL)
6666 return NULL;
6667 ga_init2(&ga, (int)sizeof(char), 80);
6668 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006669 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006670 {
6671 vim_free(ga.ga_data);
6672 return NULL;
6673 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674 ga_append(&ga, ']');
6675 ga_append(&ga, NUL);
6676 return (char_u *)ga.ga_data;
6677}
6678
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006679typedef struct join_S {
6680 char_u *s;
6681 char_u *tofree;
6682} join_T;
6683
6684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685list_join_inner(
6686 garray_T *gap, /* to store the result in */
6687 list_T *l,
6688 char_u *sep,
6689 int echo_style,
6690 int copyID,
6691 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692{
6693 int i;
6694 join_T *p;
6695 int len;
6696 int sumlen = 0;
6697 int first = TRUE;
6698 char_u *tofree;
6699 char_u numbuf[NUMBUFLEN];
6700 listitem_T *item;
6701 char_u *s;
6702
6703 /* Stringify each item in the list. */
6704 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6705 {
6706 if (echo_style)
6707 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6708 else
6709 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6710 if (s == NULL)
6711 return FAIL;
6712
6713 len = (int)STRLEN(s);
6714 sumlen += len;
6715
Bram Moolenaarcde88542015-08-11 19:14:00 +02006716 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006717 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6718 if (tofree != NULL || s != numbuf)
6719 {
6720 p->s = s;
6721 p->tofree = tofree;
6722 }
6723 else
6724 {
6725 p->s = vim_strnsave(s, len);
6726 p->tofree = p->s;
6727 }
6728
6729 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006730 if (did_echo_string_emsg) /* recursion error, bail out */
6731 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732 }
6733
6734 /* Allocate result buffer with its total size, avoid re-allocation and
6735 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6736 if (join_gap->ga_len >= 2)
6737 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6738 if (ga_grow(gap, sumlen + 2) == FAIL)
6739 return FAIL;
6740
6741 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6742 {
6743 if (first)
6744 first = FALSE;
6745 else
6746 ga_concat(gap, sep);
6747 p = ((join_T *)join_gap->ga_data) + i;
6748
6749 if (p->s != NULL)
6750 ga_concat(gap, p->s);
6751 line_breakcheck();
6752 }
6753
6754 return OK;
6755}
6756
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006757/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006759 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006760 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006761 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006763list_join(
6764 garray_T *gap,
6765 list_T *l,
6766 char_u *sep,
6767 int echo_style,
6768 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006769{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006770 garray_T join_ga;
6771 int retval;
6772 join_T *p;
6773 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774
Bram Moolenaard39a7512015-04-16 22:51:22 +02006775 if (l->lv_len < 1)
6776 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006777 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6778 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6779
6780 /* Dispose each item in join_ga. */
6781 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006783 p = (join_T *)join_ga.ga_data;
6784 for (i = 0; i < join_ga.ga_len; ++i)
6785 {
6786 vim_free(p->tofree);
6787 ++p;
6788 }
6789 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791
6792 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006793}
6794
6795/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006796 * Return the next (unique) copy ID.
6797 * Used for serializing nested structures.
6798 */
6799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006800get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006801{
6802 current_copyID += COPYID_INC;
6803 return current_copyID;
6804}
6805
6806/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807 * Garbage collection for lists and dictionaries.
6808 *
6809 * We use reference counts to be able to free most items right away when they
6810 * are no longer used. But for composite items it's possible that it becomes
6811 * unused while the reference count is > 0: When there is a recursive
6812 * reference. Example:
6813 * :let l = [1, 2, 3]
6814 * :let d = {9: l}
6815 * :let l[1] = d
6816 *
6817 * Since this is quite unusual we handle this with garbage collection: every
6818 * once in a while find out which lists and dicts are not referenced from any
6819 * variable.
6820 *
6821 * Here is a good reference text about garbage collection (refers to Python
6822 * but it applies to all reference-counting mechanisms):
6823 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006824 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006825
6826/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006827 * Do garbage collection for lists and dicts.
6828 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006831garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006833 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006834 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835 buf_T *buf;
6836 win_T *wp;
6837 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006838 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006839 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006840 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006841#ifdef FEAT_WINDOWS
6842 tabpage_T *tp;
6843#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006844
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006845 /* Only do this once. */
6846 want_garbage_collect = FALSE;
6847 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006848 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006849
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006850 /* We advance by two because we add one for items referenced through
6851 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006852 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 /*
6855 * 1. Go through all accessible variables and mark all lists and dicts
6856 * with copyID.
6857 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858
6859 /* Don't free variables in the previous_funccal list unless they are only
6860 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006861 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6863 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006864 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6865 NULL);
6866 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6867 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 }
6869
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006870 /* script-local variables */
6871 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873
6874 /* buffer-local variables */
6875 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006876 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6877 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878
6879 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006880 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006881 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6882 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006883#ifdef FEAT_AUTOCMD
6884 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6886 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006887#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006888
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006889#ifdef FEAT_WINDOWS
6890 /* tabpage-local variables */
6891 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6893 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006894#endif
6895
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898
6899 /* function-local variables */
6900 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6901 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6903 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904 }
6905
Bram Moolenaard812df62008-11-09 12:46:09 +00006906 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006908
Bram Moolenaar1dced572012-04-05 16:54:08 +02006909#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006911#endif
6912
Bram Moolenaardb913952012-06-29 12:54:53 +02006913#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006915#endif
6916
6917#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006919#endif
6920
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006921#ifdef FEAT_CHANNEL
6922 abort = abort || set_ref_in_channel(copyID);
6923#endif
6924
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006926 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 /*
6928 * 2. Free lists and dictionaries that are not referenced.
6929 */
6930 did_free = free_unref_items(copyID);
6931
6932 /*
6933 * 3. Check if any funccal can be freed now.
6934 */
6935 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006936 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 if (can_free_funccal(*pfc, copyID))
6938 {
6939 fc = *pfc;
6940 *pfc = fc->caller;
6941 free_funccal(fc, TRUE);
6942 did_free = TRUE;
6943 did_free_funccal = TRUE;
6944 }
6945 else
6946 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006947 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 if (did_free_funccal)
6949 /* When a funccal was freed some more items might be garbage
6950 * collected, so run again. */
6951 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 else if (p_verbose > 0)
6954 {
6955 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6956 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957
6958 return did_free;
6959}
6960
6961/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006962 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 */
6964 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006965free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006967 dict_T *dd, *dd_next;
6968 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006969 int did_free = FALSE;
6970
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 */
6974 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006975 {
6976 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006979 /* Free the Dictionary and ordinary items it contains, but don't
6980 * recurse into Lists and Dictionaries, they will be in the list
6981 * of dicts or list of lists. */
6982 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006985 dd = dd_next;
6986 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987
6988 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006989 * Go through the list of lists and free items without the copyID.
6990 * But don't free a list that has a watcher (used in a for loop), these
6991 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 */
6993 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006994 {
6995 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6997 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006999 /* Free the List and ordinary items it contains, but don't recurse
7000 * into Lists and Dictionaries, they will be in the list of dicts
7001 * or list of lists. */
7002 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007005 ll = ll_next;
7006 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007007
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 return did_free;
7009}
7010
7011/*
7012 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007013 * "list_stack" is used to add lists to be marked. Can be NULL.
7014 *
7015 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007017 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007018set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019{
7020 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 hashtab_T *cur_ht;
7024 ht_stack_T *ht_stack = NULL;
7025 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 cur_ht = ht;
7028 for (;;)
7029 {
7030 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 /* Mark each item in the hashtab. If the item contains a hashtab
7033 * it is added to ht_stack, if it contains a list it is added to
7034 * list_stack. */
7035 todo = (int)cur_ht->ht_used;
7036 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7037 if (!HASHITEM_EMPTY(hi))
7038 {
7039 --todo;
7040 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7041 &ht_stack, list_stack);
7042 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044
7045 if (ht_stack == NULL)
7046 break;
7047
7048 /* take an item from the stack */
7049 cur_ht = ht_stack->ht;
7050 tempitem = ht_stack;
7051 ht_stack = ht_stack->prev;
7052 free(tempitem);
7053 }
7054
7055 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056}
7057
7058/*
7059 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007060 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7061 *
7062 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007065set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 listitem_T *li;
7068 int abort = FALSE;
7069 list_T *cur_l;
7070 list_stack_T *list_stack = NULL;
7071 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007072
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 cur_l = l;
7074 for (;;)
7075 {
7076 if (!abort)
7077 /* Mark each item in the list. If the item contains a hashtab
7078 * it is added to ht_stack, if it contains a list it is added to
7079 * list_stack. */
7080 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7081 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7082 ht_stack, &list_stack);
7083 if (list_stack == NULL)
7084 break;
7085
7086 /* take an item from the stack */
7087 cur_l = list_stack->list;
7088 tempitem = list_stack;
7089 list_stack = list_stack->prev;
7090 free(tempitem);
7091 }
7092
7093 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094}
7095
7096/*
7097 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007098 * "list_stack" is used to add lists to be marked. Can be NULL.
7099 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7100 *
7101 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007104set_ref_in_item(
7105 typval_T *tv,
7106 int copyID,
7107 ht_stack_T **ht_stack,
7108 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109{
7110 dict_T *dd;
7111 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007112 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007113
Bram Moolenaara03f2332016-02-06 18:09:59 +01007114 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007115 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007116 dd = tv->vval.v_dict;
7117 if (dd != NULL && dd->dv_copyID != copyID)
7118 {
7119 /* Didn't see this dict yet. */
7120 dd->dv_copyID = copyID;
7121 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007122 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007123 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7124 }
7125 else
7126 {
7127 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7128 if (newitem == NULL)
7129 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 else
7131 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007132 newitem->ht = &dd->dv_hashtab;
7133 newitem->prev = *ht_stack;
7134 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007136 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007137 }
7138 }
7139 else if (tv->v_type == VAR_LIST)
7140 {
7141 ll = tv->vval.v_list;
7142 if (ll != NULL && ll->lv_copyID != copyID)
7143 {
7144 /* Didn't see this list yet. */
7145 ll->lv_copyID = copyID;
7146 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007148 abort = set_ref_in_list(ll, copyID, ht_stack);
7149 }
7150 else
7151 {
7152 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007153 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007154 if (newitem == NULL)
7155 abort = TRUE;
7156 else
7157 {
7158 newitem->list = ll;
7159 newitem->prev = *list_stack;
7160 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007162 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007163 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007164 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007165 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166}
7167
7168/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 * Allocate an empty header for a dictionary.
7170 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007171 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007172dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007173{
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007175
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007178 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007179 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007180 if (first_dict != NULL)
7181 first_dict->dv_used_prev = d;
7182 d->dv_used_next = first_dict;
7183 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007184 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007185
Bram Moolenaar33570922005-01-25 22:26:29 +00007186 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007187 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007188 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007189 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007190 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007192 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193}
7194
7195/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007196 * Allocate an empty dict for a return value.
7197 * Returns OK or FAIL.
7198 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007200rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007201{
7202 dict_T *d = dict_alloc();
7203
7204 if (d == NULL)
7205 return FAIL;
7206
7207 rettv->vval.v_dict = d;
7208 rettv->v_type = VAR_DICT;
7209 ++d->dv_refcount;
7210 return OK;
7211}
7212
7213
7214/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 * Unreference a Dictionary: decrement the reference count and free it when it
7216 * becomes zero.
7217 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007219dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007221 if (d != NULL && --d->dv_refcount <= 0)
7222 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223}
7224
7225/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007226 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227 * Ignores the reference count.
7228 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007229 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007230dict_free(
7231 dict_T *d,
7232 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007234 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007236 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007238 /* Remove the dict from the list of dicts for garbage collection. */
7239 if (d->dv_used_prev == NULL)
7240 first_dict = d->dv_used_next;
7241 else
7242 d->dv_used_prev->dv_used_next = d->dv_used_next;
7243 if (d->dv_used_next != NULL)
7244 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7245
7246 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007247 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007248 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251 if (!HASHITEM_EMPTY(hi))
7252 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007253 /* Remove the item before deleting it, just in case there is
7254 * something recursive causing trouble. */
7255 di = HI2DI(hi);
7256 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007257 if (recurse || (di->di_tv.v_type != VAR_LIST
7258 && di->di_tv.v_type != VAR_DICT))
7259 clear_tv(&di->di_tv);
7260 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007261 --todo;
7262 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 vim_free(d);
7266}
7267
7268/*
7269 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 * The "key" is copied to the new item.
7271 * Note that the value of the item "di_tv" still needs to be initialized!
7272 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007274 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007275dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276{
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007279 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007281 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007283 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007284 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286}
7287
7288/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007289 * Make a copy of a Dictionary item.
7290 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007291 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007292dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293{
Bram Moolenaar33570922005-01-25 22:26:29 +00007294 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007296 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7297 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 if (di != NULL)
7299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007301 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 copy_tv(&org->di_tv, &di->di_tv);
7303 }
7304 return di;
7305}
7306
7307/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308 * Remove item "item" from Dictionary "dict" and free it.
7309 */
7310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007311dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312{
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 if (HASHITEM_EMPTY(hi))
7317 EMSG2(_(e_intern2), "dictitem_remove()");
7318 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 dictitem_free(item);
7321}
7322
7323/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007324 * Free a dict item. Also clears the value.
7325 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007327dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007330 if (item->di_flags & DI_FLAGS_ALLOC)
7331 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332}
7333
7334/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7336 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007337 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007338 * Returns NULL when out of memory.
7339 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007341dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342{
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 dict_T *copy;
7344 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007347
7348 if (orig == NULL)
7349 return NULL;
7350
7351 copy = dict_alloc();
7352 if (copy != NULL)
7353 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007354 if (copyID != 0)
7355 {
7356 orig->dv_copyID = copyID;
7357 orig->dv_copydict = copy;
7358 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007359 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007360 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 --todo;
7365
7366 di = dictitem_alloc(hi->hi_key);
7367 if (di == NULL)
7368 break;
7369 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007370 {
7371 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7372 copyID) == FAIL)
7373 {
7374 vim_free(di);
7375 break;
7376 }
7377 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007378 else
7379 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7380 if (dict_add(copy, di) == FAIL)
7381 {
7382 dictitem_free(di);
7383 break;
7384 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007389 if (todo > 0)
7390 {
7391 dict_unref(copy);
7392 copy = NULL;
7393 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 }
7395
7396 return copy;
7397}
7398
7399/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007401 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007403 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007404dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405{
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407}
7408
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007410 * Add a number or string entry to dictionary "d".
7411 * When "str" is NULL use number "nr", otherwise use "str".
7412 * Returns FAIL when out of memory and when key already exists.
7413 */
7414 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007415dict_add_nr_str(
7416 dict_T *d,
7417 char *key,
7418 long nr,
7419 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007420{
7421 dictitem_T *item;
7422
7423 item = dictitem_alloc((char_u *)key);
7424 if (item == NULL)
7425 return FAIL;
7426 item->di_tv.v_lock = 0;
7427 if (str == NULL)
7428 {
7429 item->di_tv.v_type = VAR_NUMBER;
7430 item->di_tv.vval.v_number = nr;
7431 }
7432 else
7433 {
7434 item->di_tv.v_type = VAR_STRING;
7435 item->di_tv.vval.v_string = vim_strsave(str);
7436 }
7437 if (dict_add(d, item) == FAIL)
7438 {
7439 dictitem_free(item);
7440 return FAIL;
7441 }
7442 return OK;
7443}
7444
7445/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007446 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007447 * Returns FAIL when out of memory and when key already exists.
7448 */
7449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007450dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007451{
7452 dictitem_T *item;
7453
7454 item = dictitem_alloc((char_u *)key);
7455 if (item == NULL)
7456 return FAIL;
7457 item->di_tv.v_lock = 0;
7458 item->di_tv.v_type = VAR_LIST;
7459 item->di_tv.vval.v_list = list;
7460 if (dict_add(d, item) == FAIL)
7461 {
7462 dictitem_free(item);
7463 return FAIL;
7464 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007465 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007466 return OK;
7467}
7468
7469/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007470 * Get the number of items in a Dictionary.
7471 */
7472 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007473dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 if (d == NULL)
7476 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007477 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478}
7479
7480/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481 * Find item "key[len]" in Dictionary "d".
7482 * If "len" is negative use strlen(key).
7483 * Returns NULL when not found.
7484 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007485 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007486dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488#define AKEYLEN 200
7489 char_u buf[AKEYLEN];
7490 char_u *akey;
7491 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 if (len < 0)
7495 akey = key;
7496 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007497 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007498 tofree = akey = vim_strnsave(key, len);
7499 if (akey == NULL)
7500 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007501 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 else
7503 {
7504 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007505 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 akey = buf;
7507 }
7508
Bram Moolenaar33570922005-01-25 22:26:29 +00007509 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 vim_free(tofree);
7511 if (HASHITEM_EMPTY(hi))
7512 return NULL;
7513 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514}
7515
7516/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007517 * Get a string item from a dictionary.
7518 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007519 * Returns NULL if the entry doesn't exist or out of memory.
7520 */
7521 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007522get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007523{
7524 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007525 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007526
7527 di = dict_find(d, key, -1);
7528 if (di == NULL)
7529 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007530 s = get_tv_string(&di->di_tv);
7531 if (save && s != NULL)
7532 s = vim_strsave(s);
7533 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007534}
7535
7536/*
7537 * Get a number item from a dictionary.
7538 * Returns 0 if the entry doesn't exist or out of memory.
7539 */
7540 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007541get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007542{
7543 dictitem_T *di;
7544
7545 di = dict_find(d, key, -1);
7546 if (di == NULL)
7547 return 0;
7548 return get_tv_number(&di->di_tv);
7549}
7550
7551/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552 * Return an allocated string with the string representation of a Dictionary.
7553 * May return NULL.
7554 */
7555 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557{
7558 garray_T ga;
7559 int first = TRUE;
7560 char_u *tofree;
7561 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007567 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 return NULL;
7569 ga_init2(&ga, (int)sizeof(char), 80);
7570 ga_append(&ga, '{');
7571
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007572 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007573 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 --todo;
7578
7579 if (first)
7580 first = FALSE;
7581 else
7582 ga_concat(&ga, (char_u *)", ");
7583
7584 tofree = string_quote(hi->hi_key, FALSE);
7585 if (tofree != NULL)
7586 {
7587 ga_concat(&ga, tofree);
7588 vim_free(tofree);
7589 }
7590 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007591 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 if (s != NULL)
7593 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007595 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007596 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007597 line_breakcheck();
7598
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007601 if (todo > 0)
7602 {
7603 vim_free(ga.ga_data);
7604 return NULL;
7605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606
7607 ga_append(&ga, '}');
7608 ga_append(&ga, NUL);
7609 return (char_u *)ga.ga_data;
7610}
7611
7612/*
7613 * Allocate a variable for a Dictionary and fill it from "*arg".
7614 * Return OK or FAIL. Returns NOTDONE for {expr}.
7615 */
7616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007617get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618{
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 dict_T *d = NULL;
7620 typval_T tvkey;
7621 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007622 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007625 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626
7627 /*
7628 * First check if it's not a curly-braces thing: {expr}.
7629 * Must do this without evaluating, otherwise a function may be called
7630 * twice. Unfortunately this means we need to call eval1() twice for the
7631 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007632 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007634 if (*start != '}')
7635 {
7636 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7637 return FAIL;
7638 if (*start == '}')
7639 return NOTDONE;
7640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641
7642 if (evaluate)
7643 {
7644 d = dict_alloc();
7645 if (d == NULL)
7646 return FAIL;
7647 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007648 tvkey.v_type = VAR_UNKNOWN;
7649 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650
7651 *arg = skipwhite(*arg + 1);
7652 while (**arg != '}' && **arg != NUL)
7653 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007654 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 goto failret;
7656 if (**arg != ':')
7657 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007658 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 goto failret;
7661 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007662 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007664 key = get_tv_string_buf_chk(&tvkey, buf);
7665 if (key == NULL || *key == NUL)
7666 {
7667 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7668 if (key != NULL)
7669 EMSG(_(e_emptykey));
7670 clear_tv(&tvkey);
7671 goto failret;
7672 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674
7675 *arg = skipwhite(*arg + 1);
7676 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7677 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007678 if (evaluate)
7679 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 goto failret;
7681 }
7682 if (evaluate)
7683 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007684 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685 if (item != NULL)
7686 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007687 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007688 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 clear_tv(&tv);
7690 goto failret;
7691 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692 item = dictitem_alloc(key);
7693 clear_tv(&tvkey);
7694 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007697 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007698 if (dict_add(d, item) == FAIL)
7699 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 }
7701 }
7702
7703 if (**arg == '}')
7704 break;
7705 if (**arg != ',')
7706 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007707 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 goto failret;
7709 }
7710 *arg = skipwhite(*arg + 1);
7711 }
7712
7713 if (**arg != '}')
7714 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007715 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716failret:
7717 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007718 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719 return FAIL;
7720 }
7721
7722 *arg = skipwhite(*arg + 1);
7723 if (evaluate)
7724 {
7725 rettv->v_type = VAR_DICT;
7726 rettv->vval.v_dict = d;
7727 ++d->dv_refcount;
7728 }
7729
7730 return OK;
7731}
7732
Bram Moolenaar77073442016-02-13 23:23:53 +01007733#ifdef FEAT_CHANNEL
7734 static void
7735channel_unref(channel_T *channel)
7736{
7737 if (channel != NULL && --channel->ch_refcount <= 0)
7738 channel_free(channel);
7739}
7740#endif
7741
Bram Moolenaar835dc632016-02-07 14:27:38 +01007742#ifdef FEAT_JOB
7743 static void
7744job_free(job_T *job)
7745{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007746# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007747 if (job->jv_channel != NULL)
7748 {
7749 /* The channel doesn't count as a references for the job, we need to
7750 * NULL the reference when the job is freed. */
7751 job->jv_channel->ch_job = NULL;
7752 channel_unref(job->jv_channel);
7753 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007754# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007755 mch_clear_job(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007756 vim_free(job);
7757}
7758
7759 static void
7760job_unref(job_T *job)
7761{
7762 if (job != NULL && --job->jv_refcount <= 0)
7763 job_free(job);
7764}
7765
7766/*
7767 * Allocate a job. Sets the refcount to one.
7768 */
7769 static job_T *
7770job_alloc(void)
7771{
7772 job_T *job;
7773
7774 job = (job_T *)alloc_clear(sizeof(job_T));
7775 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007776 job->jv_refcount = 1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007777 return job;
7778}
7779
7780#endif
7781
Bram Moolenaar17a13432016-01-24 14:22:10 +01007782 static char *
7783get_var_special_name(int nr)
7784{
7785 switch (nr)
7786 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007787 case VVAL_FALSE: return "v:false";
7788 case VVAL_TRUE: return "v:true";
7789 case VVAL_NONE: return "v:none";
7790 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007791 }
7792 EMSG2(_(e_intern2), "get_var_special_name()");
7793 return "42";
7794}
7795
Bram Moolenaar8c711452005-01-14 21:53:12 +00007796/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007797 * Return a string with the string representation of a variable.
7798 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007799 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007800 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007801 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007802 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803 */
7804 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007805echo_string(
7806 typval_T *tv,
7807 char_u **tofree,
7808 char_u *numbuf,
7809 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007810{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 static int recurse = 0;
7812 char_u *r = NULL;
7813
Bram Moolenaar33570922005-01-25 22:26:29 +00007814 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007815 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007816 if (!did_echo_string_emsg)
7817 {
7818 /* Only give this message once for a recursive call to avoid
7819 * flooding the user with errors. And stop iterating over lists
7820 * and dicts. */
7821 did_echo_string_emsg = TRUE;
7822 EMSG(_("E724: variable nested too deep for displaying"));
7823 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007825 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826 }
7827 ++recurse;
7828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829 switch (tv->v_type)
7830 {
7831 case VAR_FUNC:
7832 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007833 r = tv->vval.v_string;
7834 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007835
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007836 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007837 if (tv->vval.v_list == NULL)
7838 {
7839 *tofree = NULL;
7840 r = NULL;
7841 }
7842 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7843 {
7844 *tofree = NULL;
7845 r = (char_u *)"[...]";
7846 }
7847 else
7848 {
7849 tv->vval.v_list->lv_copyID = copyID;
7850 *tofree = list2string(tv, copyID);
7851 r = *tofree;
7852 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007853 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007854
Bram Moolenaar8c711452005-01-14 21:53:12 +00007855 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007856 if (tv->vval.v_dict == NULL)
7857 {
7858 *tofree = NULL;
7859 r = NULL;
7860 }
7861 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7862 {
7863 *tofree = NULL;
7864 r = (char_u *)"{...}";
7865 }
7866 else
7867 {
7868 tv->vval.v_dict->dv_copyID = copyID;
7869 *tofree = dict2string(tv, copyID);
7870 r = *tofree;
7871 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007872 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007873
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007874 case VAR_STRING:
7875 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007876 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007877 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007878 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 *tofree = NULL;
7880 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007881 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007882
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007884#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007885 *tofree = NULL;
7886 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7887 r = numbuf;
7888 break;
7889#endif
7890
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007891 case VAR_SPECIAL:
7892 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007893 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007894 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896
Bram Moolenaar8502c702014-06-17 12:51:16 +02007897 if (--recurse == 0)
7898 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007899 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007900}
7901
7902/*
7903 * Return a string with the string representation of a variable.
7904 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7905 * "numbuf" is used for a number.
7906 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007907 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007908 */
7909 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007910tv2string(
7911 typval_T *tv,
7912 char_u **tofree,
7913 char_u *numbuf,
7914 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915{
7916 switch (tv->v_type)
7917 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007918 case VAR_FUNC:
7919 *tofree = string_quote(tv->vval.v_string, TRUE);
7920 return *tofree;
7921 case VAR_STRING:
7922 *tofree = string_quote(tv->vval.v_string, FALSE);
7923 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007924#ifdef FEAT_FLOAT
7925 case VAR_FLOAT:
7926 *tofree = NULL;
7927 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7928 return numbuf;
7929#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007930 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007931 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007933 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007934 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007935 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007936 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007937 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007939 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007940}
7941
7942/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 * Return string "str" in ' quotes, doubling ' characters.
7944 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007946 */
7947 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007948string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007949{
Bram Moolenaar33570922005-01-25 22:26:29 +00007950 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007951 char_u *p, *r, *s;
7952
Bram Moolenaar33570922005-01-25 22:26:29 +00007953 len = (function ? 13 : 3);
7954 if (str != NULL)
7955 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007956 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007957 for (p = str; *p != NUL; mb_ptr_adv(p))
7958 if (*p == '\'')
7959 ++len;
7960 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007961 s = r = alloc(len);
7962 if (r != NULL)
7963 {
7964 if (function)
7965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007966 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007967 r += 10;
7968 }
7969 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007971 if (str != NULL)
7972 for (p = str; *p != NUL; )
7973 {
7974 if (*p == '\'')
7975 *r++ = '\'';
7976 MB_COPY_CHAR(p, r);
7977 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007979 if (function)
7980 *r++ = ')';
7981 *r++ = NUL;
7982 }
7983 return s;
7984}
7985
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007986#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007987/*
7988 * Convert the string "text" to a floating point number.
7989 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7990 * this always uses a decimal point.
7991 * Returns the length of the text that was consumed.
7992 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007993 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007994string2float(
7995 char_u *text,
7996 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007997{
7998 char *s = (char *)text;
7999 float_T f;
8000
8001 f = strtod(s, &s);
8002 *value = f;
8003 return (int)((char_u *)s - text);
8004}
8005#endif
8006
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 * Get the value of an environment variable.
8009 * "arg" is pointing to the '$'. It is advanced to after the name.
8010 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008011 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 */
8013 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008014get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015{
8016 char_u *string = NULL;
8017 int len;
8018 int cc;
8019 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008020 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021
8022 ++*arg;
8023 name = *arg;
8024 len = get_env_len(arg);
8025 if (evaluate)
8026 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008027 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008028 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008029
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008030 cc = name[len];
8031 name[len] = NUL;
8032 /* first try vim_getenv(), fast for normal environment vars */
8033 string = vim_getenv(name, &mustfree);
8034 if (string != NULL && *string != NUL)
8035 {
8036 if (!mustfree)
8037 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008039 else
8040 {
8041 if (mustfree)
8042 vim_free(string);
8043
8044 /* next try expanding things like $VIM and ${HOME} */
8045 string = expand_env_save(name - 1);
8046 if (string != NULL && *string == '$')
8047 {
8048 vim_free(string);
8049 string = NULL;
8050 }
8051 }
8052 name[len] = cc;
8053
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008054 rettv->v_type = VAR_STRING;
8055 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 }
8057
8058 return OK;
8059}
8060
8061/*
8062 * Array with names and number of arguments of all internal functions
8063 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8064 */
8065static struct fst
8066{
8067 char *f_name; /* function name */
8068 char f_min_argc; /* minimal number of arguments */
8069 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008070 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008071 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072} functions[] =
8073{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008074#ifdef FEAT_FLOAT
8075 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008076 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008078 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008079 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008080 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {"append", 2, 2, f_append},
8082 {"argc", 0, 0, f_argc},
8083 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008084 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008085 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008086#ifdef FEAT_FLOAT
8087 {"asin", 1, 1, f_asin}, /* WJMc */
8088#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008089 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008090 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008091 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008092 {"assert_false", 1, 2, f_assert_false},
8093 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008094#ifdef FEAT_FLOAT
8095 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008096 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008099 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"bufexists", 1, 1, f_bufexists},
8101 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8102 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8103 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8104 {"buflisted", 1, 1, f_buflisted},
8105 {"bufloaded", 1, 1, f_bufloaded},
8106 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008107 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"bufwinnr", 1, 1, f_bufwinnr},
8109 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008110 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008111 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008112 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008113#ifdef FEAT_FLOAT
8114 {"ceil", 1, 1, f_ceil},
8115#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008116#ifdef FEAT_CHANNEL
8117 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008118 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008119 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008120 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008121 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8122 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar77073442016-02-13 23:23:53 +01008123 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008124#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008125 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008126 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008128 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008130#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008131 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008132 {"complete_add", 1, 1, f_complete_add},
8133 {"complete_check", 0, 0, f_complete_check},
8134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008136 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008137#ifdef FEAT_FLOAT
8138 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008139 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008140#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008141 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008143 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008144 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008145 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008147 {"diff_filler", 1, 1, f_diff_filler},
8148 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008149 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008150 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008152 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"eventhandler", 0, 0, f_eventhandler},
8154 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008155 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008157#ifdef FEAT_FLOAT
8158 {"exp", 1, 1, f_exp},
8159#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008160 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008161 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008162 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8164 {"filereadable", 1, 1, f_filereadable},
8165 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008166 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008167 {"finddir", 1, 3, f_finddir},
8168 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008169#ifdef FEAT_FLOAT
8170 {"float2nr", 1, 1, f_float2nr},
8171 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008172 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008173#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008174 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"fnamemodify", 2, 2, f_fnamemodify},
8176 {"foldclosed", 1, 1, f_foldclosed},
8177 {"foldclosedend", 1, 1, f_foldclosedend},
8178 {"foldlevel", 1, 1, f_foldlevel},
8179 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008180 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008182 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008183 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008184 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008185 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008186 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 {"getchar", 0, 1, f_getchar},
8188 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008189 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"getcmdline", 0, 0, f_getcmdline},
8191 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008192 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008193 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008194 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008195 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008196 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008197 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"getfsize", 1, 1, f_getfsize},
8199 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008200 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008201 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008202 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008203 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008204 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008205 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008206 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008207 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008209 {"gettabvar", 2, 3, f_gettabvar},
8210 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {"getwinposx", 0, 0, f_getwinposx},
8212 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008213 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008214 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008215 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008216 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008219 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008220 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8222 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8223 {"histadd", 2, 2, f_histadd},
8224 {"histdel", 1, 2, f_histdel},
8225 {"histget", 1, 2, f_histget},
8226 {"histnr", 1, 1, f_histnr},
8227 {"hlID", 1, 1, f_hlID},
8228 {"hlexists", 1, 1, f_hlexists},
8229 {"hostname", 0, 0, f_hostname},
8230 {"iconv", 3, 3, f_iconv},
8231 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008232 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008233 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008235 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"inputrestore", 0, 0, f_inputrestore},
8237 {"inputsave", 0, 0, f_inputsave},
8238 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008239 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008240 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008242 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008243 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008244#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008245# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008246 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008247# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008248 {"job_start", 1, 2, f_job_start},
8249 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008250 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008251#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008252 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008253 {"js_decode", 1, 1, f_js_decode},
8254 {"js_encode", 1, 1, f_js_encode},
8255 {"json_decode", 1, 1, f_json_decode},
8256 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008257 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008259 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {"libcall", 3, 3, f_libcall},
8261 {"libcallnr", 3, 3, f_libcallnr},
8262 {"line", 1, 1, f_line},
8263 {"line2byte", 1, 1, f_line2byte},
8264 {"lispindent", 1, 1, f_lispindent},
8265 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008266#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008267 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008268 {"log10", 1, 1, f_log10},
8269#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008270#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008271 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008272#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008273 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008274 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008275 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008276 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008277 {"matchadd", 2, 5, f_matchadd},
8278 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008279 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008280 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008281 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008282 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008283 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008284 {"max", 1, 1, f_max},
8285 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008286#ifdef vim_mkdir
8287 {"mkdir", 1, 3, f_mkdir},
8288#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008289 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008290#ifdef FEAT_MZSCHEME
8291 {"mzeval", 1, 1, f_mzeval},
8292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008294 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008295 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008296 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008297#ifdef FEAT_PERL
8298 {"perleval", 1, 1, f_perleval},
8299#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008300#ifdef FEAT_FLOAT
8301 {"pow", 2, 2, f_pow},
8302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008304 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008305 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008306#ifdef FEAT_PYTHON3
8307 {"py3eval", 1, 1, f_py3eval},
8308#endif
8309#ifdef FEAT_PYTHON
8310 {"pyeval", 1, 1, f_pyeval},
8311#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008312 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008313 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008314 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008315 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008316 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"remote_expr", 2, 3, f_remote_expr},
8318 {"remote_foreground", 1, 1, f_remote_foreground},
8319 {"remote_peek", 1, 2, f_remote_peek},
8320 {"remote_read", 1, 1, f_remote_read},
8321 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008322 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008324 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008326 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008327#ifdef FEAT_FLOAT
8328 {"round", 1, 1, f_round},
8329#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008330 {"screenattr", 2, 2, f_screenattr},
8331 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008332 {"screencol", 0, 0, f_screencol},
8333 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008334 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008335 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008336 {"searchpair", 3, 7, f_searchpair},
8337 {"searchpairpos", 3, 7, f_searchpairpos},
8338 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {"server2client", 2, 2, f_server2client},
8340 {"serverlist", 0, 0, f_serverlist},
8341 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008342 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {"setcmdpos", 1, 1, f_setcmdpos},
8344 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008345 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008346 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008347 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008348 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008350 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008351 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008353#ifdef FEAT_CRYPT
8354 {"sha256", 1, 1, f_sha256},
8355#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008356 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008357 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008359#ifdef FEAT_FLOAT
8360 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008361 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008362#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008363 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008364 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008365 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008366 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008367 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008368#ifdef FEAT_FLOAT
8369 {"sqrt", 1, 1, f_sqrt},
8370 {"str2float", 1, 1, f_str2float},
8371#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008372 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008373 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008374 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375#ifdef HAVE_STRFTIME
8376 {"strftime", 1, 2, f_strftime},
8377#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008378 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008379 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {"strlen", 1, 1, f_strlen},
8381 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008382 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008384 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008385 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"substitute", 4, 4, f_substitute},
8387 {"synID", 3, 3, f_synID},
8388 {"synIDattr", 2, 3, f_synIDattr},
8389 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008390 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008391 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008392 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008393 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008394 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008395 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008396 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008397 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008398 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008399#ifdef FEAT_FLOAT
8400 {"tan", 1, 1, f_tan},
8401 {"tanh", 1, 1, f_tanh},
8402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008404 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {"tolower", 1, 1, f_tolower},
8406 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008407 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008408#ifdef FEAT_FLOAT
8409 {"trunc", 1, 1, f_trunc},
8410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008412 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008413 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008414 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008415 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {"virtcol", 1, 1, f_virtcol},
8417 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008418 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 {"winbufnr", 1, 1, f_winbufnr},
8420 {"wincol", 0, 0, f_wincol},
8421 {"winheight", 1, 1, f_winheight},
8422 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008423 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008425 {"winrestview", 1, 1, f_winrestview},
8426 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008428 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008429 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008430 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431};
8432
8433#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8434
8435/*
8436 * Function given to ExpandGeneric() to obtain the list of internal
8437 * or user defined function names.
8438 */
8439 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008440get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441{
8442 static int intidx = -1;
8443 char_u *name;
8444
8445 if (idx == 0)
8446 intidx = -1;
8447 if (intidx < 0)
8448 {
8449 name = get_user_func_name(xp, idx);
8450 if (name != NULL)
8451 return name;
8452 }
8453 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8454 {
8455 STRCPY(IObuff, functions[intidx].f_name);
8456 STRCAT(IObuff, "(");
8457 if (functions[intidx].f_max_argc == 0)
8458 STRCAT(IObuff, ")");
8459 return IObuff;
8460 }
8461
8462 return NULL;
8463}
8464
8465/*
8466 * Function given to ExpandGeneric() to obtain the list of internal or
8467 * user defined variable or function names.
8468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008470get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
8472 static int intidx = -1;
8473 char_u *name;
8474
8475 if (idx == 0)
8476 intidx = -1;
8477 if (intidx < 0)
8478 {
8479 name = get_function_name(xp, idx);
8480 if (name != NULL)
8481 return name;
8482 }
8483 return get_user_var_name(xp, ++intidx);
8484}
8485
8486#endif /* FEAT_CMDL_COMPL */
8487
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008488#if defined(EBCDIC) || defined(PROTO)
8489/*
8490 * Compare struct fst by function name.
8491 */
8492 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008493compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008494{
8495 struct fst *p1 = (struct fst *)s1;
8496 struct fst *p2 = (struct fst *)s2;
8497
8498 return STRCMP(p1->f_name, p2->f_name);
8499}
8500
8501/*
8502 * Sort the function table by function name.
8503 * The sorting of the table above is ASCII dependant.
8504 * On machines using EBCDIC we have to sort it.
8505 */
8506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008507sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008508{
8509 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8510
8511 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8512}
8513#endif
8514
8515
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516/*
8517 * Find internal function in table above.
8518 * Return index, or -1 if not found
8519 */
8520 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008521find_internal_func(
8522 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
8524 int first = 0;
8525 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8526 int cmp;
8527 int x;
8528
8529 /*
8530 * Find the function name in the table. Binary search.
8531 */
8532 while (first <= last)
8533 {
8534 x = first + ((unsigned)(last - first) >> 1);
8535 cmp = STRCMP(name, functions[x].f_name);
8536 if (cmp < 0)
8537 last = x - 1;
8538 else if (cmp > 0)
8539 first = x + 1;
8540 else
8541 return x;
8542 }
8543 return -1;
8544}
8545
8546/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008547 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8548 * name it contains, otherwise return "name".
8549 */
8550 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008551deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008552{
Bram Moolenaar33570922005-01-25 22:26:29 +00008553 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008554 int cc;
8555
8556 cc = name[*lenp];
8557 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008558 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008559 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008560 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008561 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008562 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008563 {
8564 *lenp = 0;
8565 return (char_u *)""; /* just in case */
8566 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008567 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008568 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008569 }
8570
8571 return name;
8572}
8573
8574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 * Allocate a variable for the result of a function.
8576 * Return OK or FAIL.
8577 */
8578 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008579get_func_tv(
8580 char_u *name, /* name of the function */
8581 int len, /* length of "name" */
8582 typval_T *rettv,
8583 char_u **arg, /* argument, pointing to the '(' */
8584 linenr_T firstline, /* first line of range */
8585 linenr_T lastline, /* last line of range */
8586 int *doesrange, /* return: function handled range */
8587 int evaluate,
8588 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 char_u *argp;
8591 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008592 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 int argcount = 0; /* number of arguments found */
8594
8595 /*
8596 * Get the arguments.
8597 */
8598 argp = *arg;
8599 while (argcount < MAX_FUNC_ARGS)
8600 {
8601 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8602 if (*argp == ')' || *argp == ',' || *argp == NUL)
8603 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8605 {
8606 ret = FAIL;
8607 break;
8608 }
8609 ++argcount;
8610 if (*argp != ',')
8611 break;
8612 }
8613 if (*argp == ')')
8614 ++argp;
8615 else
8616 ret = FAIL;
8617
8618 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008619 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008620 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008622 {
8623 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008624 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008625 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008626 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628
8629 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631
8632 *arg = skipwhite(argp);
8633 return ret;
8634}
8635
8636
8637/*
8638 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008639 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008640 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008643call_func(
8644 char_u *funcname, /* name of the function */
8645 int len, /* length of "name" */
8646 typval_T *rettv, /* return value goes here */
8647 int argcount, /* number of "argvars" */
8648 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008649 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008650 linenr_T firstline, /* first line of range */
8651 linenr_T lastline, /* last line of range */
8652 int *doesrange, /* return: function handled range */
8653 int evaluate,
8654 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657#define ERROR_UNKNOWN 0
8658#define ERROR_TOOMANY 1
8659#define ERROR_TOOFEW 2
8660#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008661#define ERROR_DICT 4
8662#define ERROR_NONE 5
8663#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 int error = ERROR_NONE;
8665 int i;
8666 int llen;
8667 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668#define FLEN_FIXED 40
8669 char_u fname_buf[FLEN_FIXED + 1];
8670 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008671 char_u *name;
8672
8673 /* Make a copy of the name, if it comes from a funcref variable it could
8674 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008675 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008676 if (name == NULL)
8677 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
8679 /*
8680 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8681 * Change <SNR>123_name() to K_SNR 123_name().
8682 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 llen = eval_fname_script(name);
8685 if (llen > 0)
8686 {
8687 fname_buf[0] = K_SPECIAL;
8688 fname_buf[1] = KS_EXTRA;
8689 fname_buf[2] = (int)KE_SNR;
8690 i = 3;
8691 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8692 {
8693 if (current_SID <= 0)
8694 error = ERROR_SCRIPT;
8695 else
8696 {
8697 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8698 i = (int)STRLEN(fname_buf);
8699 }
8700 }
8701 if (i + STRLEN(name + llen) < FLEN_FIXED)
8702 {
8703 STRCPY(fname_buf + i, name + llen);
8704 fname = fname_buf;
8705 }
8706 else
8707 {
8708 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8709 if (fname == NULL)
8710 error = ERROR_OTHER;
8711 else
8712 {
8713 mch_memmove(fname, fname_buf, (size_t)i);
8714 STRCPY(fname + i, name + llen);
8715 }
8716 }
8717 }
8718 else
8719 fname = name;
8720
8721 *doesrange = FALSE;
8722
8723
8724 /* execute the function if no errors detected and executing */
8725 if (evaluate && error == ERROR_NONE)
8726 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008727 char_u *rfname = fname;
8728
8729 /* Ignore "g:" before a function name. */
8730 if (fname[0] == 'g' && fname[1] == ':')
8731 rfname = fname + 2;
8732
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008733 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8734 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 error = ERROR_UNKNOWN;
8736
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008737 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {
8739 /*
8740 * User defined function.
8741 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008742 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008743
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008745 /* Trigger FuncUndefined event, may load the function. */
8746 if (fp == NULL
8747 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008748 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008749 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008751 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008752 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 }
8754#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008755 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008756 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008757 {
8758 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008759 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008760 }
8761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 if (fp != NULL)
8763 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008764 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008766 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008768 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008770 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008771 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 else
8773 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008774 int did_save_redo = FALSE;
8775
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 /*
8777 * Call the user function.
8778 * Save and restore search patterns, script variables and
8779 * redo buffer.
8780 */
8781 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008782#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008783 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008784#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008785 {
8786 saveRedobuff();
8787 did_save_redo = TRUE;
8788 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008789 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008791 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008792 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8793 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8794 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008795 /* Function was unreferenced while being used, free it
8796 * now. */
8797 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008798 if (did_save_redo)
8799 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 restore_search_patterns();
8801 error = ERROR_NONE;
8802 }
8803 }
8804 }
8805 else
8806 {
8807 /*
8808 * Find the function name in the table, call its implementation.
8809 */
8810 i = find_internal_func(fname);
8811 if (i >= 0)
8812 {
8813 if (argcount < functions[i].f_min_argc)
8814 error = ERROR_TOOFEW;
8815 else if (argcount > functions[i].f_max_argc)
8816 error = ERROR_TOOMANY;
8817 else
8818 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008819 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008820 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 error = ERROR_NONE;
8822 }
8823 }
8824 }
8825 /*
8826 * The function call (or "FuncUndefined" autocommand sequence) might
8827 * have been aborted by an error, an interrupt, or an explicitly thrown
8828 * exception that has not been caught so far. This situation can be
8829 * tested for by calling aborting(). For an error in an internal
8830 * function or for the "E132" error in call_user_func(), however, the
8831 * throw point at which the "force_abort" flag (temporarily reset by
8832 * emsg()) is normally updated has not been reached yet. We need to
8833 * update that flag first to make aborting() reliable.
8834 */
8835 update_force_abort();
8836 }
8837 if (error == ERROR_NONE)
8838 ret = OK;
8839
8840 /*
8841 * Report an error unless the argument evaluation or function call has been
8842 * cancelled due to an aborting error, an interrupt, or an exception.
8843 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008844 if (!aborting())
8845 {
8846 switch (error)
8847 {
8848 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008849 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008850 break;
8851 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008852 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008853 break;
8854 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008855 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008856 name);
8857 break;
8858 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008859 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008860 name);
8861 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008862 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008863 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008864 name);
8865 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008866 }
8867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 if (fname != name && fname != fname_buf)
8870 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008871 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872
8873 return ret;
8874}
8875
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008876/*
8877 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008878 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008879 */
8880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008881emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008882{
8883 char_u *p;
8884
8885 if (*name == K_SPECIAL)
8886 p = concat_str((char_u *)"<SNR>", name + 3);
8887 else
8888 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008889 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008890 if (p != name)
8891 vim_free(p);
8892}
8893
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008894/*
8895 * Return TRUE for a non-zero Number and a non-empty String.
8896 */
8897 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008898non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008899{
8900 return ((argvars[0].v_type == VAR_NUMBER
8901 && argvars[0].vval.v_number != 0)
8902 || (argvars[0].v_type == VAR_STRING
8903 && argvars[0].vval.v_string != NULL
8904 && *argvars[0].vval.v_string != NUL));
8905}
8906
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907/*********************************************
8908 * Implementation of the built-in functions
8909 */
8910
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008911#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008912static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008913
8914/*
8915 * Get the float value of "argvars[0]" into "f".
8916 * Returns FAIL when the argument is not a Number or Float.
8917 */
8918 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008919get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008920{
8921 if (argvars[0].v_type == VAR_FLOAT)
8922 {
8923 *f = argvars[0].vval.v_float;
8924 return OK;
8925 }
8926 if (argvars[0].v_type == VAR_NUMBER)
8927 {
8928 *f = (float_T)argvars[0].vval.v_number;
8929 return OK;
8930 }
8931 EMSG(_("E808: Number or Float required"));
8932 return FAIL;
8933}
8934
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008935/*
8936 * "abs(expr)" function
8937 */
8938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008939f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008940{
8941 if (argvars[0].v_type == VAR_FLOAT)
8942 {
8943 rettv->v_type = VAR_FLOAT;
8944 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8945 }
8946 else
8947 {
8948 varnumber_T n;
8949 int error = FALSE;
8950
8951 n = get_tv_number_chk(&argvars[0], &error);
8952 if (error)
8953 rettv->vval.v_number = -1;
8954 else if (n > 0)
8955 rettv->vval.v_number = n;
8956 else
8957 rettv->vval.v_number = -n;
8958 }
8959}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008960
8961/*
8962 * "acos()" function
8963 */
8964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008965f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008966{
8967 float_T f;
8968
8969 rettv->v_type = VAR_FLOAT;
8970 if (get_float_arg(argvars, &f) == OK)
8971 rettv->vval.v_float = acos(f);
8972 else
8973 rettv->vval.v_float = 0.0;
8974}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008975#endif
8976
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008978 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 */
8980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008981f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
Bram Moolenaar33570922005-01-25 22:26:29 +00008983 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008985 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008986 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008988 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008989 && !tv_check_lock(l->lv_lock,
8990 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008991 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008992 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993 }
8994 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008995 EMSG(_(e_listreq));
8996}
8997
8998/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008999 * "alloc_fail(id, countdown, repeat)" function
9000 */
9001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009002f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009003{
9004 if (argvars[0].v_type != VAR_NUMBER
9005 || argvars[0].vval.v_number <= 0
9006 || argvars[1].v_type != VAR_NUMBER
9007 || argvars[1].vval.v_number < 0
9008 || argvars[2].v_type != VAR_NUMBER)
9009 EMSG(_(e_invarg));
9010 else
9011 {
9012 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009013 if (alloc_fail_id >= aid_last)
9014 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009015 alloc_fail_countdown = argvars[1].vval.v_number;
9016 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009017 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009018 }
9019}
9020
9021/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009022 * "and(expr, expr)" function
9023 */
9024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009025f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009026{
9027 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9028 & get_tv_number_chk(&argvars[1], NULL);
9029}
9030
9031/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009032 * "append(lnum, string/list)" function
9033 */
9034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009035f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009036{
9037 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009038 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 list_T *l = NULL;
9040 listitem_T *li = NULL;
9041 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009042 long added = 0;
9043
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009044 /* When coming here from Insert mode, sync undo, so that this can be
9045 * undone separately from what was previously inserted. */
9046 if (u_sync_once == 2)
9047 {
9048 u_sync_once = 1; /* notify that u_sync() was called */
9049 u_sync(TRUE);
9050 }
9051
Bram Moolenaar0d660222005-01-07 21:51:51 +00009052 lnum = get_tv_lnum(argvars);
9053 if (lnum >= 0
9054 && lnum <= curbuf->b_ml.ml_line_count
9055 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009056 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009057 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009058 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009059 l = argvars[1].vval.v_list;
9060 if (l == NULL)
9061 return;
9062 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009063 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009064 for (;;)
9065 {
9066 if (l == NULL)
9067 tv = &argvars[1]; /* append a string */
9068 else if (li == NULL)
9069 break; /* end of list */
9070 else
9071 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009072 line = get_tv_string_chk(tv);
9073 if (line == NULL) /* type error */
9074 {
9075 rettv->vval.v_number = 1; /* Failed */
9076 break;
9077 }
9078 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009079 ++added;
9080 if (l == NULL)
9081 break;
9082 li = li->li_next;
9083 }
9084
9085 appended_lines_mark(lnum, added);
9086 if (curwin->w_cursor.lnum > lnum)
9087 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009089 else
9090 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091}
9092
9093/*
9094 * "argc()" function
9095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009097f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100}
9101
9102/*
9103 * "argidx()" function
9104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009106f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109}
9110
9111/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009112 * "arglistid()" function
9113 */
9114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009115f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009116{
9117 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009118
9119 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009120 wp = find_tabwin(&argvars[0], &argvars[1]);
9121 if (wp != NULL)
9122 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009123}
9124
9125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 * "argv(nr)" function
9127 */
9128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009129f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130{
9131 int idx;
9132
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009133 if (argvars[0].v_type != VAR_UNKNOWN)
9134 {
9135 idx = get_tv_number_chk(&argvars[0], NULL);
9136 if (idx >= 0 && idx < ARGCOUNT)
9137 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9138 else
9139 rettv->vval.v_string = NULL;
9140 rettv->v_type = VAR_STRING;
9141 }
9142 else if (rettv_list_alloc(rettv) == OK)
9143 for (idx = 0; idx < ARGCOUNT; ++idx)
9144 list_append_string(rettv->vval.v_list,
9145 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146}
9147
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009148static void prepare_assert_error(garray_T*gap);
9149static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9150static void assert_error(garray_T *gap);
9151static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009152
9153/*
9154 * Prepare "gap" for an assert error and add the sourcing position.
9155 */
9156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009157prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009158{
9159 char buf[NUMBUFLEN];
9160
9161 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009162 if (sourcing_name != NULL)
9163 {
9164 ga_concat(gap, sourcing_name);
9165 if (sourcing_lnum > 0)
9166 ga_concat(gap, (char_u *)" ");
9167 }
9168 if (sourcing_lnum > 0)
9169 {
9170 sprintf(buf, "line %ld", (long)sourcing_lnum);
9171 ga_concat(gap, (char_u *)buf);
9172 }
9173 if (sourcing_name != NULL || sourcing_lnum > 0)
9174 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009175}
9176
9177/*
9178 * Fill "gap" with information about an assert error.
9179 */
9180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009181fill_assert_error(
9182 garray_T *gap,
9183 typval_T *opt_msg_tv,
9184 char_u *exp_str,
9185 typval_T *exp_tv,
9186 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009187{
9188 char_u numbuf[NUMBUFLEN];
9189 char_u *tofree;
9190
9191 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9192 {
9193 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9194 vim_free(tofree);
9195 }
9196 else
9197 {
9198 ga_concat(gap, (char_u *)"Expected ");
9199 if (exp_str == NULL)
9200 {
9201 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9202 vim_free(tofree);
9203 }
9204 else
9205 ga_concat(gap, exp_str);
9206 ga_concat(gap, (char_u *)" but got ");
9207 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9208 vim_free(tofree);
9209 }
9210}
Bram Moolenaar43345542015-11-29 17:35:35 +01009211
9212/*
9213 * Add an assert error to v:errors.
9214 */
9215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009216assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009217{
9218 struct vimvar *vp = &vimvars[VV_ERRORS];
9219
9220 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9221 /* Make sure v:errors is a list. */
9222 set_vim_var_list(VV_ERRORS, list_alloc());
9223 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9224}
9225
Bram Moolenaar43345542015-11-29 17:35:35 +01009226/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009227 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009228 */
9229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009230f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009231{
9232 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009233
9234 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9235 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009236 prepare_assert_error(&ga);
9237 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9238 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009239 ga_clear(&ga);
9240 }
9241}
9242
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009243/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009244 * "assert_exception(string[, msg])" function
9245 */
9246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009247f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009248{
9249 garray_T ga;
9250 char *error;
9251
9252 error = (char *)get_tv_string_chk(&argvars[0]);
9253 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9254 {
9255 prepare_assert_error(&ga);
9256 ga_concat(&ga, (char_u *)"v:exception is not set");
9257 assert_error(&ga);
9258 ga_clear(&ga);
9259 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009260 else if (error != NULL
9261 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009262 {
9263 prepare_assert_error(&ga);
9264 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9265 &vimvars[VV_EXCEPTION].vv_tv);
9266 assert_error(&ga);
9267 ga_clear(&ga);
9268 }
9269}
9270
9271/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009272 * "assert_fails(cmd [, error])" function
9273 */
9274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009275f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009276{
9277 char_u *cmd = get_tv_string_chk(&argvars[0]);
9278 garray_T ga;
9279
9280 called_emsg = FALSE;
9281 suppress_errthrow = TRUE;
9282 emsg_silent = TRUE;
9283 do_cmdline_cmd(cmd);
9284 if (!called_emsg)
9285 {
9286 prepare_assert_error(&ga);
9287 ga_concat(&ga, (char_u *)"command did not fail: ");
9288 ga_concat(&ga, cmd);
9289 assert_error(&ga);
9290 ga_clear(&ga);
9291 }
9292 else if (argvars[1].v_type != VAR_UNKNOWN)
9293 {
9294 char_u buf[NUMBUFLEN];
9295 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9296
9297 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9298 {
9299 prepare_assert_error(&ga);
9300 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9301 &vimvars[VV_ERRMSG].vv_tv);
9302 assert_error(&ga);
9303 ga_clear(&ga);
9304 }
9305 }
9306
9307 called_emsg = FALSE;
9308 suppress_errthrow = FALSE;
9309 emsg_silent = FALSE;
9310 emsg_on_display = FALSE;
9311 set_vim_var_string(VV_ERRMSG, NULL, 0);
9312}
9313
9314/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009315 * Common for assert_true() and assert_false().
9316 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009318assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009319{
9320 int error = FALSE;
9321 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009322
Bram Moolenaar37127922016-02-06 20:29:28 +01009323 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009324 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009325 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009326 if (argvars[0].v_type != VAR_NUMBER
9327 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9328 || error)
9329 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009330 prepare_assert_error(&ga);
9331 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009332 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009333 NULL, &argvars[0]);
9334 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009335 ga_clear(&ga);
9336 }
9337}
9338
9339/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009340 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009341 */
9342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009343f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009344{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009345 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009346}
9347
9348/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009349 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009350 */
9351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009352f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009353{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009354 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009355}
9356
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009357#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009358/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009359 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009360 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009362f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009363{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009364 float_T f;
9365
9366 rettv->v_type = VAR_FLOAT;
9367 if (get_float_arg(argvars, &f) == OK)
9368 rettv->vval.v_float = asin(f);
9369 else
9370 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009371}
9372
9373/*
9374 * "atan()" function
9375 */
9376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009377f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009378{
9379 float_T f;
9380
9381 rettv->v_type = VAR_FLOAT;
9382 if (get_float_arg(argvars, &f) == OK)
9383 rettv->vval.v_float = atan(f);
9384 else
9385 rettv->vval.v_float = 0.0;
9386}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009387
9388/*
9389 * "atan2()" function
9390 */
9391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009392f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009393{
9394 float_T fx, fy;
9395
9396 rettv->v_type = VAR_FLOAT;
9397 if (get_float_arg(argvars, &fx) == OK
9398 && get_float_arg(&argvars[1], &fy) == OK)
9399 rettv->vval.v_float = atan2(fx, fy);
9400 else
9401 rettv->vval.v_float = 0.0;
9402}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009403#endif
9404
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405/*
9406 * "browse(save, title, initdir, default)" function
9407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009409f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
9411#ifdef FEAT_BROWSE
9412 int save;
9413 char_u *title;
9414 char_u *initdir;
9415 char_u *defname;
9416 char_u buf[NUMBUFLEN];
9417 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009418 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009420 save = get_tv_number_chk(&argvars[0], &error);
9421 title = get_tv_string_chk(&argvars[1]);
9422 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9423 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009425 if (error || title == NULL || initdir == NULL || defname == NULL)
9426 rettv->vval.v_string = NULL;
9427 else
9428 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009429 do_browse(save ? BROWSE_SAVE : 0,
9430 title, defname, NULL, initdir, NULL, curbuf);
9431#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009433#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009435}
9436
9437/*
9438 * "browsedir(title, initdir)" function
9439 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009441f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009442{
9443#ifdef FEAT_BROWSE
9444 char_u *title;
9445 char_u *initdir;
9446 char_u buf[NUMBUFLEN];
9447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009448 title = get_tv_string_chk(&argvars[0]);
9449 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009451 if (title == NULL || initdir == NULL)
9452 rettv->vval.v_string = NULL;
9453 else
9454 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009455 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460}
9461
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009462static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009463
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464/*
9465 * Find a buffer by number or exact name.
9466 */
9467 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009468find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009472 if (avar->v_type == VAR_NUMBER)
9473 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009474 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009476 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009477 if (buf == NULL)
9478 {
9479 /* No full path name match, try a match with a URL or a "nofile"
9480 * buffer, these don't use the full path. */
9481 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9482 if (buf->b_fname != NULL
9483 && (path_with_url(buf->b_fname)
9484#ifdef FEAT_QUICKFIX
9485 || bt_nofile(buf)
9486#endif
9487 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009488 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009489 break;
9490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 }
9492 return buf;
9493}
9494
9495/*
9496 * "bufexists(expr)" function
9497 */
9498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009499f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502}
9503
9504/*
9505 * "buflisted(expr)" function
9506 */
9507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009508f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
9510 buf_T *buf;
9511
9512 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514}
9515
9516/*
9517 * "bufloaded(expr)" function
9518 */
9519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009520f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521{
9522 buf_T *buf;
9523
9524 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526}
9527
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009528static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009529
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530/*
9531 * Get buffer by number or pattern.
9532 */
9533 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009534get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009536 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 int save_magic;
9538 char_u *save_cpo;
9539 buf_T *buf;
9540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 if (tv->v_type == VAR_NUMBER)
9542 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009543 if (tv->v_type != VAR_STRING)
9544 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 if (name == NULL || *name == NUL)
9546 return curbuf;
9547 if (name[0] == '$' && name[1] == NUL)
9548 return lastbuf;
9549
9550 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9551 save_magic = p_magic;
9552 p_magic = TRUE;
9553 save_cpo = p_cpo;
9554 p_cpo = (char_u *)"";
9555
9556 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009557 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558
9559 p_magic = save_magic;
9560 p_cpo = save_cpo;
9561
9562 /* If not found, try expanding the name, like done for bufexists(). */
9563 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565
9566 return buf;
9567}
9568
9569/*
9570 * "bufname(expr)" function
9571 */
9572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009573f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574{
9575 buf_T *buf;
9576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009577 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009579 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 --emsg_off;
9586}
9587
9588/*
9589 * "bufnr(expr)" function
9590 */
9591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009592f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
9594 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009595 int error = FALSE;
9596 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009598 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009600 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009601 --emsg_off;
9602
9603 /* If the buffer isn't found and the second argument is not zero create a
9604 * new buffer. */
9605 if (buf == NULL
9606 && argvars[1].v_type != VAR_UNKNOWN
9607 && get_tv_number_chk(&argvars[1], &error) != 0
9608 && !error
9609 && (name = get_tv_string_chk(&argvars[0])) != NULL
9610 && !error)
9611 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9612
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617}
9618
9619/*
9620 * "bufwinnr(nr)" function
9621 */
9622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009623f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
9625#ifdef FEAT_WINDOWS
9626 win_T *wp;
9627 int winnr = 0;
9628#endif
9629 buf_T *buf;
9630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009631 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009633 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634#ifdef FEAT_WINDOWS
9635 for (wp = firstwin; wp; wp = wp->w_next)
9636 {
9637 ++winnr;
9638 if (wp->w_buffer == buf)
9639 break;
9640 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009643 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644#endif
9645 --emsg_off;
9646}
9647
9648/*
9649 * "byte2line(byte)" function
9650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009652f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
9654#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656#else
9657 long boff = 0;
9658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009659 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 (linenr_T)0, &boff);
9665#endif
9666}
9667
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009669byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009670{
9671#ifdef FEAT_MBYTE
9672 char_u *t;
9673#endif
9674 char_u *str;
9675 long idx;
9676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009677 str = get_tv_string_chk(&argvars[0]);
9678 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009680 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009681 return;
9682
9683#ifdef FEAT_MBYTE
9684 t = str;
9685 for ( ; idx > 0; idx--)
9686 {
9687 if (*t == NUL) /* EOL reached */
9688 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009689 if (enc_utf8 && comp)
9690 t += utf_ptr2len(t);
9691 else
9692 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009693 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009694 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009695#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009696 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009698#endif
9699}
9700
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009701/*
9702 * "byteidx()" function
9703 */
9704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009705f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009706{
9707 byteidx(argvars, rettv, FALSE);
9708}
9709
9710/*
9711 * "byteidxcomp()" function
9712 */
9713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009714f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009715{
9716 byteidx(argvars, rettv, TRUE);
9717}
9718
Bram Moolenaardb913952012-06-29 12:54:53 +02009719 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009720func_call(
9721 char_u *name,
9722 typval_T *args,
9723 dict_T *selfdict,
9724 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009725{
9726 listitem_T *item;
9727 typval_T argv[MAX_FUNC_ARGS + 1];
9728 int argc = 0;
9729 int dummy;
9730 int r = 0;
9731
9732 for (item = args->vval.v_list->lv_first; item != NULL;
9733 item = item->li_next)
9734 {
9735 if (argc == MAX_FUNC_ARGS)
9736 {
9737 EMSG(_("E699: Too many arguments"));
9738 break;
9739 }
9740 /* Make a copy of each argument. This is needed to be able to set
9741 * v_lock to VAR_FIXED in the copy without changing the original list.
9742 */
9743 copy_tv(&item->li_tv, &argv[argc++]);
9744 }
9745
9746 if (item == NULL)
9747 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9748 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9749 &dummy, TRUE, selfdict);
9750
9751 /* Free the arguments. */
9752 while (argc > 0)
9753 clear_tv(&argv[--argc]);
9754
9755 return r;
9756}
9757
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009758/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009759 * "call(func, arglist)" function
9760 */
9761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009762f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009763{
9764 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009765 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009766
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009767 if (argvars[1].v_type != VAR_LIST)
9768 {
9769 EMSG(_(e_listreq));
9770 return;
9771 }
9772 if (argvars[1].vval.v_list == NULL)
9773 return;
9774
9775 if (argvars[0].v_type == VAR_FUNC)
9776 func = argvars[0].vval.v_string;
9777 else
9778 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009779 if (*func == NUL)
9780 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009781
Bram Moolenaare9a41262005-01-15 22:18:47 +00009782 if (argvars[2].v_type != VAR_UNKNOWN)
9783 {
9784 if (argvars[2].v_type != VAR_DICT)
9785 {
9786 EMSG(_(e_dictreq));
9787 return;
9788 }
9789 selfdict = argvars[2].vval.v_dict;
9790 }
9791
Bram Moolenaardb913952012-06-29 12:54:53 +02009792 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009793}
9794
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009795#ifdef FEAT_FLOAT
9796/*
9797 * "ceil({float})" function
9798 */
9799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009800f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009801{
9802 float_T f;
9803
9804 rettv->v_type = VAR_FLOAT;
9805 if (get_float_arg(argvars, &f) == OK)
9806 rettv->vval.v_float = ceil(f);
9807 else
9808 rettv->vval.v_float = 0.0;
9809}
9810#endif
9811
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009812#ifdef FEAT_CHANNEL
9813/*
Bram Moolenaar77073442016-02-13 23:23:53 +01009814 * Get the channel from the argument.
9815 * Returns NULL if the handle is invalid.
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009816 */
Bram Moolenaar77073442016-02-13 23:23:53 +01009817 static channel_T *
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009818get_channel_arg(typval_T *tv)
9819{
Bram Moolenaar77073442016-02-13 23:23:53 +01009820 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009821
Bram Moolenaar77073442016-02-13 23:23:53 +01009822 if (tv->v_type != VAR_CHANNEL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009823 {
9824 EMSG2(_(e_invarg2), get_tv_string(tv));
Bram Moolenaar77073442016-02-13 23:23:53 +01009825 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009826 }
Bram Moolenaar77073442016-02-13 23:23:53 +01009827 channel = tv->vval.v_channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009828
Bram Moolenaar77073442016-02-13 23:23:53 +01009829 if (channel == NULL || !channel_is_open(channel))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009830 {
Bram Moolenaar77073442016-02-13 23:23:53 +01009831 EMSG(_("E906: not an open channel"));
9832 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009833 }
Bram Moolenaar77073442016-02-13 23:23:53 +01009834 return channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009835}
9836
9837/*
9838 * "ch_close()" function
9839 */
9840 static void
9841f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9842{
Bram Moolenaar77073442016-02-13 23:23:53 +01009843 channel_T *channel = get_channel_arg(&argvars[0]);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009844
Bram Moolenaar77073442016-02-13 23:23:53 +01009845 if (channel != NULL)
9846 channel_close(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009847}
9848
9849/*
9850 * Get a callback from "arg". It can be a Funcref or a function name.
9851 * When "arg" is zero return an empty string.
9852 * Return NULL for an invalid argument.
9853 */
9854 static char_u *
9855get_callback(typval_T *arg)
9856{
9857 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9858 return arg->vval.v_string;
9859 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9860 return (char_u *)"";
9861 EMSG(_("E999: Invalid callback argument"));
9862 return NULL;
9863}
9864
9865/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009866 * "ch_logfile()" function
9867 */
9868 static void
9869f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9870{
9871 char_u *fname;
9872 char_u *opt = (char_u *)"";
9873 char_u buf[NUMBUFLEN];
9874 FILE *file = NULL;
9875
9876 fname = get_tv_string(&argvars[0]);
9877 if (argvars[1].v_type == VAR_STRING)
9878 opt = get_tv_string_buf(&argvars[1], buf);
9879 if (*fname != NUL)
9880 {
9881 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
9882 if (file == NULL)
9883 {
9884 EMSG2(_(e_notopen), fname);
9885 return;
9886 }
9887 }
9888 ch_logfile(file);
9889}
9890
9891/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009892 * "ch_open()" function
9893 */
9894 static void
9895f_ch_open(typval_T *argvars, typval_T *rettv)
9896{
9897 char_u *address;
9898 char_u *mode;
9899 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009900 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009901 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009902 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009903 int waittime = 0;
9904 int timeout = 2000;
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009905 ch_mode_T ch_mode = MODE_JSON;
Bram Moolenaar77073442016-02-13 23:23:53 +01009906 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009907
9908 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +01009909 rettv->v_type = VAR_CHANNEL;
9910 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009911
9912 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009913 if (argvars[1].v_type != VAR_UNKNOWN
9914 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009915 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009916 EMSG(_(e_invarg));
9917 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009918 }
9919
9920 /* parse address */
9921 p = vim_strchr(address, ':');
9922 if (p == NULL)
9923 {
9924 EMSG2(_(e_invarg2), address);
9925 return;
9926 }
9927 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009928 port = strtol((char *)p, &rest, 10);
9929 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009930 {
9931 p[-1] = ':';
9932 EMSG2(_(e_invarg2), address);
9933 return;
9934 }
9935
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009936 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009937 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009938 dict_T *dict = argvars[1].vval.v_dict;
9939 dictitem_T *item;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009940
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009941 /* parse argdict */
9942 if ((item = dict_find(dict, (char_u *)"mode", -1)) != NULL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009943 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009944 mode = get_tv_string(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009945 if (STRCMP(mode, "raw") == 0)
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009946 ch_mode = MODE_RAW;
9947 else if (STRCMP(mode, "js") == 0)
9948 ch_mode = MODE_JS;
9949 else if (STRCMP(mode, "json") == 0)
9950 ch_mode = MODE_JSON;
9951 else
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009952 {
9953 EMSG2(_(e_invarg2), mode);
9954 return;
9955 }
9956 }
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009957 if ((item = dict_find(dict, (char_u *)"waittime", -1)) != NULL)
9958 waittime = get_tv_number(&item->di_tv);
9959 if ((item = dict_find(dict, (char_u *)"timeout", -1)) != NULL)
9960 timeout = get_tv_number(&item->di_tv);
9961 if ((item = dict_find(dict, (char_u *)"callback", -1)) != NULL)
9962 callback = get_callback(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009963 }
9964 if (waittime < 0 || timeout < 0)
9965 {
9966 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009967 return;
9968 }
9969
Bram Moolenaar77073442016-02-13 23:23:53 +01009970 channel = channel_open((char *)address, port, waittime, NULL);
9971 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009972 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01009973 rettv->vval.v_channel = channel;
Bram Moolenaar77073442016-02-13 23:23:53 +01009974 channel_set_json_mode(channel, ch_mode);
9975 channel_set_timeout(channel, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009976 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +01009977 channel_set_callback(channel, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009978 }
9979}
9980
9981/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009982 * "ch_readraw()" function
9983 */
9984 static void
9985f_ch_readraw(typval_T *argvars, typval_T *rettv)
9986{
Bram Moolenaar77073442016-02-13 23:23:53 +01009987 channel_T *channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009988
9989 /* return an empty string by default */
9990 rettv->v_type = VAR_STRING;
9991 rettv->vval.v_string = NULL;
9992
Bram Moolenaar77073442016-02-13 23:23:53 +01009993 channel = get_channel_arg(&argvars[0]);
9994 if (channel != NULL)
9995 rettv->vval.v_string = channel_read_block(channel);
9996}
9997
9998/*
9999 * "ch_status()" function
10000 */
10001 static void
10002f_ch_status(typval_T *argvars, typval_T *rettv)
10003{
10004 /* return an empty string by default */
10005 rettv->v_type = VAR_STRING;
10006
10007 if (argvars[0].v_type != VAR_CHANNEL)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010008 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010009 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10010 rettv->vval.v_string = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010011 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010012 else
10013 rettv->vval.v_string = vim_strsave(
10014 (char_u *)channel_status(argvars[0].vval.v_channel));
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010015}
10016
10017/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010018 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010019 * Returns the channel if the caller should read the response.
10020 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010021 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010022 static channel_T *
Bram Moolenaara07fec92016-02-05 21:04:08 +010010023send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010024{
Bram Moolenaar77073442016-02-13 23:23:53 +010010025 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010026 char_u *callback = NULL;
10027
Bram Moolenaar77073442016-02-13 23:23:53 +010010028 channel = get_channel_arg(&argvars[0]);
10029 if (channel == NULL)
10030 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010031
10032 if (argvars[2].v_type != VAR_UNKNOWN)
10033 {
10034 callback = get_callback(&argvars[2]);
10035 if (callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010036 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010037 }
Bram Moolenaara07fec92016-02-05 21:04:08 +010010038 /* Set the callback. An empty callback means no callback and not reading
10039 * the response. */
10040 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010041 channel_set_req_callback(channel, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010042
Bram Moolenaar77073442016-02-13 23:23:53 +010010043 if (channel_send(channel, text, fun) == OK && callback == NULL)
10044 return channel;
10045 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010046}
10047
10048/*
10049 * "ch_sendexpr()" function
10050 */
10051 static void
10052f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10053{
10054 char_u *text;
10055 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010056 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010057 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010058 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010059
10060 /* return an empty string by default */
10061 rettv->v_type = VAR_STRING;
10062 rettv->vval.v_string = NULL;
10063
Bram Moolenaar77073442016-02-13 23:23:53 +010010064 channel = get_channel_arg(&argvars[0]);
10065 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010066 return;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010067
Bram Moolenaar77073442016-02-13 23:23:53 +010010068 ch_mode = channel_get_mode(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010069 if (ch_mode == MODE_RAW)
10070 {
10071 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
10072 return;
10073 }
10074
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010075 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010076 text = json_encode_nr_expr(id, &argvars[1],
10077 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010078 if (text == NULL)
10079 return;
10080
Bram Moolenaar77073442016-02-13 23:23:53 +010010081 channel = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010082 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010083 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010084 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010085 if (channel_read_json_block(channel, id, &listtv) == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010086 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010087 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010088
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010089 /* Move the item from the list and then change the type to
10090 * avoid the value being freed. */
10091 *rettv = list->lv_last->li_tv;
10092 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010093 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010094 }
10095 }
10096}
10097
10098/*
10099 * "ch_sendraw()" function
10100 */
10101 static void
10102f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10103{
10104 char_u buf[NUMBUFLEN];
10105 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010106 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010107
10108 /* return an empty string by default */
10109 rettv->v_type = VAR_STRING;
10110 rettv->vval.v_string = NULL;
10111
10112 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar77073442016-02-13 23:23:53 +010010113 channel = send_common(argvars, text, 0, "sendraw");
10114 if (channel != NULL)
10115 rettv->vval.v_string = channel_read_block(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010116}
10117#endif
10118
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010119/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010120 * "changenr()" function
10121 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010123f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010124{
10125 rettv->vval.v_number = curbuf->b_u_seq_cur;
10126}
10127
10128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 * "char2nr(string)" function
10130 */
10131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010132f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133{
10134#ifdef FEAT_MBYTE
10135 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010136 {
10137 int utf8 = 0;
10138
10139 if (argvars[1].v_type != VAR_UNKNOWN)
10140 utf8 = get_tv_number_chk(&argvars[1], NULL);
10141
10142 if (utf8)
10143 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10144 else
10145 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 else
10148#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150}
10151
10152/*
10153 * "cindent(lnum)" function
10154 */
10155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010156f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157{
10158#ifdef FEAT_CINDENT
10159 pos_T pos;
10160 linenr_T lnum;
10161
10162 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010163 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10165 {
10166 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010167 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 curwin->w_cursor = pos;
10169 }
10170 else
10171#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010172 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173}
10174
10175/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010176 * "clearmatches()" function
10177 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010179f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010180{
10181#ifdef FEAT_SEARCH_EXTRA
10182 clear_matches(curwin);
10183#endif
10184}
10185
10186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 * "col(string)" function
10188 */
10189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010190f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191{
10192 colnr_T col = 0;
10193 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010194 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010196 fp = var2fpos(&argvars[0], FALSE, &fnum);
10197 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 {
10199 if (fp->col == MAXCOL)
10200 {
10201 /* '> can be MAXCOL, get the length of the line then */
10202 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010203 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 else
10205 col = MAXCOL;
10206 }
10207 else
10208 {
10209 col = fp->col + 1;
10210#ifdef FEAT_VIRTUALEDIT
10211 /* col(".") when the cursor is on the NUL at the end of the line
10212 * because of "coladd" can be seen as an extra column. */
10213 if (virtual_active() && fp == &curwin->w_cursor)
10214 {
10215 char_u *p = ml_get_cursor();
10216
10217 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10218 curwin->w_virtcol - curwin->w_cursor.coladd))
10219 {
10220# ifdef FEAT_MBYTE
10221 int l;
10222
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010223 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 col += l;
10225# else
10226 if (*p != NUL && p[1] == NUL)
10227 ++col;
10228# endif
10229 }
10230 }
10231#endif
10232 }
10233 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010234 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235}
10236
Bram Moolenaar572cb562005-08-05 21:35:02 +000010237#if defined(FEAT_INS_EXPAND)
10238/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010239 * "complete()" function
10240 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010242f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010243{
10244 int startcol;
10245
10246 if ((State & INSERT) == 0)
10247 {
10248 EMSG(_("E785: complete() can only be used in Insert mode"));
10249 return;
10250 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010251
10252 /* Check for undo allowed here, because if something was already inserted
10253 * the line was already saved for undo and this check isn't done. */
10254 if (!undo_allowed())
10255 return;
10256
Bram Moolenaarade00832006-03-10 21:46:58 +000010257 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10258 {
10259 EMSG(_(e_invarg));
10260 return;
10261 }
10262
10263 startcol = get_tv_number_chk(&argvars[0], NULL);
10264 if (startcol <= 0)
10265 return;
10266
10267 set_completion(startcol - 1, argvars[1].vval.v_list);
10268}
10269
10270/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010271 * "complete_add()" function
10272 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010274f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010275{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010276 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010277}
10278
10279/*
10280 * "complete_check()" function
10281 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010283f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010284{
10285 int saved = RedrawingDisabled;
10286
10287 RedrawingDisabled = 0;
10288 ins_compl_check_keys(0);
10289 rettv->vval.v_number = compl_interrupted;
10290 RedrawingDisabled = saved;
10291}
10292#endif
10293
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294/*
10295 * "confirm(message, buttons[, default [, type]])" function
10296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010298f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299{
10300#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10301 char_u *message;
10302 char_u *buttons = NULL;
10303 char_u buf[NUMBUFLEN];
10304 char_u buf2[NUMBUFLEN];
10305 int def = 1;
10306 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010307 char_u *typestr;
10308 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 message = get_tv_string_chk(&argvars[0]);
10311 if (message == NULL)
10312 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010313 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010315 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10316 if (buttons == NULL)
10317 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010318 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010320 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010321 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010323 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10324 if (typestr == NULL)
10325 error = TRUE;
10326 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010328 switch (TOUPPER_ASC(*typestr))
10329 {
10330 case 'E': type = VIM_ERROR; break;
10331 case 'Q': type = VIM_QUESTION; break;
10332 case 'I': type = VIM_INFO; break;
10333 case 'W': type = VIM_WARNING; break;
10334 case 'G': type = VIM_GENERIC; break;
10335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 }
10337 }
10338 }
10339 }
10340
10341 if (buttons == NULL || *buttons == NUL)
10342 buttons = (char_u *)_("&Ok");
10343
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010344 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010345 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010346 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347#endif
10348}
10349
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010350/*
10351 * "copy()" function
10352 */
10353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010354f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010355{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010356 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010357}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010359#ifdef FEAT_FLOAT
10360/*
10361 * "cos()" function
10362 */
10363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010364f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010365{
10366 float_T f;
10367
10368 rettv->v_type = VAR_FLOAT;
10369 if (get_float_arg(argvars, &f) == OK)
10370 rettv->vval.v_float = cos(f);
10371 else
10372 rettv->vval.v_float = 0.0;
10373}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010374
10375/*
10376 * "cosh()" function
10377 */
10378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010379f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010380{
10381 float_T f;
10382
10383 rettv->v_type = VAR_FLOAT;
10384 if (get_float_arg(argvars, &f) == OK)
10385 rettv->vval.v_float = cosh(f);
10386 else
10387 rettv->vval.v_float = 0.0;
10388}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010389#endif
10390
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010392 * "count()" function
10393 */
10394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010395f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010396{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010397 long n = 0;
10398 int ic = FALSE;
10399
Bram Moolenaare9a41262005-01-15 22:18:47 +000010400 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010401 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010402 listitem_T *li;
10403 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010404 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010405
Bram Moolenaare9a41262005-01-15 22:18:47 +000010406 if ((l = argvars[0].vval.v_list) != NULL)
10407 {
10408 li = l->lv_first;
10409 if (argvars[2].v_type != VAR_UNKNOWN)
10410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010411 int error = FALSE;
10412
10413 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010414 if (argvars[3].v_type != VAR_UNKNOWN)
10415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010416 idx = get_tv_number_chk(&argvars[3], &error);
10417 if (!error)
10418 {
10419 li = list_find(l, idx);
10420 if (li == NULL)
10421 EMSGN(_(e_listidx), idx);
10422 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010423 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010424 if (error)
10425 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010426 }
10427
10428 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010429 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010430 ++n;
10431 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010432 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010433 else if (argvars[0].v_type == VAR_DICT)
10434 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010435 int todo;
10436 dict_T *d;
10437 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010438
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010439 if ((d = argvars[0].vval.v_dict) != NULL)
10440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010441 int error = FALSE;
10442
Bram Moolenaare9a41262005-01-15 22:18:47 +000010443 if (argvars[2].v_type != VAR_UNKNOWN)
10444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010446 if (argvars[3].v_type != VAR_UNKNOWN)
10447 EMSG(_(e_invarg));
10448 }
10449
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010450 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010451 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010452 {
10453 if (!HASHITEM_EMPTY(hi))
10454 {
10455 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010456 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010457 ++n;
10458 }
10459 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010460 }
10461 }
10462 else
10463 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010464 rettv->vval.v_number = n;
10465}
10466
10467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10469 *
10470 * Checks the existence of a cscope connection.
10471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010473f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474{
10475#ifdef FEAT_CSCOPE
10476 int num = 0;
10477 char_u *dbpath = NULL;
10478 char_u *prepend = NULL;
10479 char_u buf[NUMBUFLEN];
10480
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010481 if (argvars[0].v_type != VAR_UNKNOWN
10482 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484 num = (int)get_tv_number(&argvars[0]);
10485 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010486 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010487 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 }
10489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010490 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491#endif
10492}
10493
10494/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010495 * "cursor(lnum, col)" function, or
10496 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010498 * Moves the cursor to the specified line and column.
10499 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010502f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010505#ifdef FEAT_VIRTUALEDIT
10506 long coladd = 0;
10507#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010508 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010510 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010511 if (argvars[1].v_type == VAR_UNKNOWN)
10512 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010513 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010514 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010515
Bram Moolenaar493c1782014-05-28 14:34:46 +020010516 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010517 {
10518 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010519 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010520 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010521 line = pos.lnum;
10522 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010523#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010524 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010525#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010526 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010527 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010528 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010529 set_curswant = FALSE;
10530 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010531 }
10532 else
10533 {
10534 line = get_tv_lnum(argvars);
10535 col = get_tv_number_chk(&argvars[1], NULL);
10536#ifdef FEAT_VIRTUALEDIT
10537 if (argvars[2].v_type != VAR_UNKNOWN)
10538 coladd = get_tv_number_chk(&argvars[2], NULL);
10539#endif
10540 }
10541 if (line < 0 || col < 0
10542#ifdef FEAT_VIRTUALEDIT
10543 || coladd < 0
10544#endif
10545 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010546 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 if (line > 0)
10548 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 if (col > 0)
10550 curwin->w_cursor.col = col - 1;
10551#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010552 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553#endif
10554
10555 /* Make sure the cursor is in a valid position. */
10556 check_cursor();
10557#ifdef FEAT_MBYTE
10558 /* Correct cursor for multi-byte character. */
10559 if (has_mbyte)
10560 mb_adjust_cursor();
10561#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010562
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010563 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010564 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565}
10566
10567/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010568 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 */
10570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010571f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010573 int noref = 0;
10574
10575 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010577 if (noref < 0 || noref > 1)
10578 EMSG(_(e_invarg));
10579 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010580 {
10581 current_copyID += COPYID_INC;
10582 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584}
10585
10586/*
10587 * "delete()" function
10588 */
10589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010590f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010592 char_u nbuf[NUMBUFLEN];
10593 char_u *name;
10594 char_u *flags;
10595
10596 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010598 return;
10599
10600 name = get_tv_string(&argvars[0]);
10601 if (name == NULL || *name == NUL)
10602 {
10603 EMSG(_(e_invarg));
10604 return;
10605 }
10606
10607 if (argvars[1].v_type != VAR_UNKNOWN)
10608 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010610 flags = (char_u *)"";
10611
10612 if (*flags == NUL)
10613 /* delete a file */
10614 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10615 else if (STRCMP(flags, "d") == 0)
10616 /* delete an empty directory */
10617 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10618 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010619 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010620 rettv->vval.v_number = delete_recursive(name);
10621 else
10622 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623}
10624
10625/*
10626 * "did_filetype()" function
10627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010629f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630{
10631#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010632 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633#endif
10634}
10635
10636/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010637 * "diff_filler()" function
10638 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010640f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010641{
10642#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010644#endif
10645}
10646
10647/*
10648 * "diff_hlID()" function
10649 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010651f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010652{
10653#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010655 static linenr_T prev_lnum = 0;
10656 static int changedtick = 0;
10657 static int fnum = 0;
10658 static int change_start = 0;
10659 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010660 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010661 int filler_lines;
10662 int col;
10663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010664 if (lnum < 0) /* ignore type error in {lnum} arg */
10665 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010666 if (lnum != prev_lnum
10667 || changedtick != curbuf->b_changedtick
10668 || fnum != curbuf->b_fnum)
10669 {
10670 /* New line, buffer, change: need to get the values. */
10671 filler_lines = diff_check(curwin, lnum);
10672 if (filler_lines < 0)
10673 {
10674 if (filler_lines == -1)
10675 {
10676 change_start = MAXCOL;
10677 change_end = -1;
10678 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10679 hlID = HLF_ADD; /* added line */
10680 else
10681 hlID = HLF_CHD; /* changed line */
10682 }
10683 else
10684 hlID = HLF_ADD; /* added line */
10685 }
10686 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010687 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010688 prev_lnum = lnum;
10689 changedtick = curbuf->b_changedtick;
10690 fnum = curbuf->b_fnum;
10691 }
10692
10693 if (hlID == HLF_CHD || hlID == HLF_TXD)
10694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010695 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010696 if (col >= change_start && col <= change_end)
10697 hlID = HLF_TXD; /* changed text */
10698 else
10699 hlID = HLF_CHD; /* changed line */
10700 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010701 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010702#endif
10703}
10704
10705/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010706 * "disable_char_avail_for_testing({expr})" function
10707 */
10708 static void
10709f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10710{
10711 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10712}
10713
10714/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010715 * "empty({expr})" function
10716 */
10717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010718f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010719{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010720 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010721
10722 switch (argvars[0].v_type)
10723 {
10724 case VAR_STRING:
10725 case VAR_FUNC:
10726 n = argvars[0].vval.v_string == NULL
10727 || *argvars[0].vval.v_string == NUL;
10728 break;
10729 case VAR_NUMBER:
10730 n = argvars[0].vval.v_number == 0;
10731 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010732 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010733#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010734 n = argvars[0].vval.v_float == 0.0;
10735 break;
10736#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010737 case VAR_LIST:
10738 n = argvars[0].vval.v_list == NULL
10739 || argvars[0].vval.v_list->lv_first == NULL;
10740 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010741 case VAR_DICT:
10742 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010743 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010744 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010745 case VAR_SPECIAL:
10746 n = argvars[0].vval.v_number != VVAL_TRUE;
10747 break;
10748
Bram Moolenaar835dc632016-02-07 14:27:38 +010010749 case VAR_JOB:
10750#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010010751 n = argvars[0].vval.v_job == NULL
10752 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10753 break;
10754#endif
10755 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010010756#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010757 n = argvars[0].vval.v_channel == NULL
10758 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010759 break;
10760#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010761 case VAR_UNKNOWN:
10762 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10763 n = TRUE;
10764 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010765 }
10766
10767 rettv->vval.v_number = n;
10768}
10769
10770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 * "escape({string}, {chars})" function
10772 */
10773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010774f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775{
10776 char_u buf[NUMBUFLEN];
10777
Bram Moolenaar758711c2005-02-02 23:11:38 +000010778 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10779 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010780 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781}
10782
10783/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010784 * "eval()" function
10785 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010787f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010788{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010789 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 s = get_tv_string_chk(&argvars[0]);
10792 if (s != NULL)
10793 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010794
Bram Moolenaar615b9972015-01-14 17:15:05 +010010795 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010796 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10797 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010798 if (p != NULL && !aborting())
10799 EMSG2(_(e_invexpr2), p);
10800 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010801 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010802 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010803 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010804 else if (*s != NUL)
10805 EMSG(_(e_trailing));
10806}
10807
10808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 * "eventhandler()" function
10810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010812f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010814 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815}
10816
10817/*
10818 * "executable()" function
10819 */
10820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010821f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010823 char_u *name = get_tv_string(&argvars[0]);
10824
10825 /* Check in $PATH and also check directly if there is a directory name. */
10826 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10827 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010828}
10829
10830/*
10831 * "exepath()" function
10832 */
10833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010834f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010835{
10836 char_u *p = NULL;
10837
Bram Moolenaarb5971142015-03-21 17:32:19 +010010838 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010839 rettv->v_type = VAR_STRING;
10840 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841}
10842
10843/*
10844 * "exists()" function
10845 */
10846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010847f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848{
10849 char_u *p;
10850 char_u *name;
10851 int n = FALSE;
10852 int len = 0;
10853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 if (*p == '$') /* environment variable */
10856 {
10857 /* first try "normal" environment variables (fast) */
10858 if (mch_getenv(p + 1) != NULL)
10859 n = TRUE;
10860 else
10861 {
10862 /* try expanding things like $VIM and ${HOME} */
10863 p = expand_env_save(p);
10864 if (p != NULL && *p != '$')
10865 n = TRUE;
10866 vim_free(p);
10867 }
10868 }
10869 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010872 if (*skipwhite(p) != NUL)
10873 n = FALSE; /* trailing garbage */
10874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 else if (*p == '*') /* internal or user defined function */
10876 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010877 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 }
10879 else if (*p == ':')
10880 {
10881 n = cmd_exists(p + 1);
10882 }
10883 else if (*p == '#')
10884 {
10885#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010886 if (p[1] == '#')
10887 n = autocmd_supported(p + 2);
10888 else
10889 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890#endif
10891 }
10892 else /* internal variable */
10893 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010894 char_u *tofree;
10895 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010897 /* get_name_len() takes care of expanding curly braces */
10898 name = p;
10899 len = get_name_len(&p, &tofree, TRUE, FALSE);
10900 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010902 if (tofree != NULL)
10903 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010904 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010905 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010907 /* handle d.key, l[idx], f(expr) */
10908 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10909 if (n)
10910 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 }
10912 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010913 if (*p != NUL)
10914 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010916 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 }
10918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010919 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920}
10921
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010922#ifdef FEAT_FLOAT
10923/*
10924 * "exp()" function
10925 */
10926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010927f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010928{
10929 float_T f;
10930
10931 rettv->v_type = VAR_FLOAT;
10932 if (get_float_arg(argvars, &f) == OK)
10933 rettv->vval.v_float = exp(f);
10934 else
10935 rettv->vval.v_float = 0.0;
10936}
10937#endif
10938
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939/*
10940 * "expand()" function
10941 */
10942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010943f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944{
10945 char_u *s;
10946 int len;
10947 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010948 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010951 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010954 if (argvars[1].v_type != VAR_UNKNOWN
10955 && argvars[2].v_type != VAR_UNKNOWN
10956 && get_tv_number_chk(&argvars[2], &error)
10957 && !error)
10958 {
10959 rettv->v_type = VAR_LIST;
10960 rettv->vval.v_list = NULL;
10961 }
10962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 if (*s == '%' || *s == '#' || *s == '<')
10965 {
10966 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010967 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010969 if (rettv->v_type == VAR_LIST)
10970 {
10971 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10972 list_append_string(rettv->vval.v_list, result, -1);
10973 else
10974 vim_free(result);
10975 }
10976 else
10977 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 }
10979 else
10980 {
10981 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010982 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010983 if (argvars[1].v_type != VAR_UNKNOWN
10984 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010985 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010986 if (!error)
10987 {
10988 ExpandInit(&xpc);
10989 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010990 if (p_wic)
10991 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010992 if (rettv->v_type == VAR_STRING)
10993 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10994 options, WILD_ALL);
10995 else if (rettv_list_alloc(rettv) != FAIL)
10996 {
10997 int i;
10998
10999 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11000 for (i = 0; i < xpc.xp_numfiles; i++)
11001 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11002 ExpandCleanup(&xpc);
11003 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011004 }
11005 else
11006 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 }
11008}
11009
11010/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011011 * Go over all entries in "d2" and add them to "d1".
11012 * When "action" is "error" then a duplicate key is an error.
11013 * When "action" is "force" then a duplicate key is overwritten.
11014 * Otherwise duplicate keys are ignored ("action" is "keep").
11015 */
11016 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011017dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011018{
11019 dictitem_T *di1;
11020 hashitem_T *hi2;
11021 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011022 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011023
11024 todo = (int)d2->dv_hashtab.ht_used;
11025 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11026 {
11027 if (!HASHITEM_EMPTY(hi2))
11028 {
11029 --todo;
11030 di1 = dict_find(d1, hi2->hi_key, -1);
11031 if (d1->dv_scope != 0)
11032 {
11033 /* Disallow replacing a builtin function in l: and g:.
11034 * Check the key to be valid when adding to any
11035 * scope. */
11036 if (d1->dv_scope == VAR_DEF_SCOPE
11037 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11038 && var_check_func_name(hi2->hi_key,
11039 di1 == NULL))
11040 break;
11041 if (!valid_varname(hi2->hi_key))
11042 break;
11043 }
11044 if (di1 == NULL)
11045 {
11046 di1 = dictitem_copy(HI2DI(hi2));
11047 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11048 dictitem_free(di1);
11049 }
11050 else if (*action == 'e')
11051 {
11052 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11053 break;
11054 }
11055 else if (*action == 'f' && HI2DI(hi2) != di1)
11056 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011057 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11058 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011059 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011060 clear_tv(&di1->di_tv);
11061 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11062 }
11063 }
11064 }
11065}
11066
11067/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011068 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011069 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011070 */
11071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011072f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011073{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011074 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011075
Bram Moolenaare9a41262005-01-15 22:18:47 +000011076 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011077 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011078 list_T *l1, *l2;
11079 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011080 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011082
Bram Moolenaare9a41262005-01-15 22:18:47 +000011083 l1 = argvars[0].vval.v_list;
11084 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011085 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011086 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011087 {
11088 if (argvars[2].v_type != VAR_UNKNOWN)
11089 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011090 before = get_tv_number_chk(&argvars[2], &error);
11091 if (error)
11092 return; /* type error; errmsg already given */
11093
Bram Moolenaar758711c2005-02-02 23:11:38 +000011094 if (before == l1->lv_len)
11095 item = NULL;
11096 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011097 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011098 item = list_find(l1, before);
11099 if (item == NULL)
11100 {
11101 EMSGN(_(e_listidx), before);
11102 return;
11103 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011104 }
11105 }
11106 else
11107 item = NULL;
11108 list_extend(l1, l2, item);
11109
Bram Moolenaare9a41262005-01-15 22:18:47 +000011110 copy_tv(&argvars[0], rettv);
11111 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011112 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011113 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11114 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011115 dict_T *d1, *d2;
11116 char_u *action;
11117 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011118
11119 d1 = argvars[0].vval.v_dict;
11120 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011121 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011122 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011123 {
11124 /* Check the third argument. */
11125 if (argvars[2].v_type != VAR_UNKNOWN)
11126 {
11127 static char *(av[]) = {"keep", "force", "error"};
11128
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011129 action = get_tv_string_chk(&argvars[2]);
11130 if (action == NULL)
11131 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011132 for (i = 0; i < 3; ++i)
11133 if (STRCMP(action, av[i]) == 0)
11134 break;
11135 if (i == 3)
11136 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011137 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 return;
11139 }
11140 }
11141 else
11142 action = (char_u *)"force";
11143
Bram Moolenaara9922d62013-05-30 13:01:18 +020011144 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011145
Bram Moolenaare9a41262005-01-15 22:18:47 +000011146 copy_tv(&argvars[0], rettv);
11147 }
11148 }
11149 else
11150 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011151}
11152
11153/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011154 * "feedkeys()" function
11155 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011157f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011158{
11159 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011160 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011161 char_u *keys, *flags;
11162 char_u nbuf[NUMBUFLEN];
11163 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011164 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011165 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011166
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011167 /* This is not allowed in the sandbox. If the commands would still be
11168 * executed in the sandbox it would be OK, but it probably happens later,
11169 * when "sandbox" is no longer set. */
11170 if (check_secure())
11171 return;
11172
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011173 keys = get_tv_string(&argvars[0]);
11174 if (*keys != NUL)
11175 {
11176 if (argvars[1].v_type != VAR_UNKNOWN)
11177 {
11178 flags = get_tv_string_buf(&argvars[1], nbuf);
11179 for ( ; *flags != NUL; ++flags)
11180 {
11181 switch (*flags)
11182 {
11183 case 'n': remap = FALSE; break;
11184 case 'm': remap = TRUE; break;
11185 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011186 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011187 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011188 }
11189 }
11190 }
11191
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011192 /* Need to escape K_SPECIAL and CSI before putting the string in the
11193 * typeahead buffer. */
11194 keys_esc = vim_strsave_escape_csi(keys);
11195 if (keys_esc != NULL)
11196 {
11197 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011198 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011199 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011200 if (vgetc_busy)
11201 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011202 if (execute)
11203 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011204 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011205 }
11206}
11207
11208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 * "filereadable()" function
11210 */
11211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011212f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011214 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 char_u *p;
11216 int n;
11217
Bram Moolenaarc236c162008-07-13 17:41:49 +000011218#ifndef O_NONBLOCK
11219# define O_NONBLOCK 0
11220#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011222 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11223 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 {
11225 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011226 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 }
11228 else
11229 n = FALSE;
11230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232}
11233
11234/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011235 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 * rights to write into.
11237 */
11238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011239f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011241 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242}
11243
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011244 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011245findfilendir(
11246 typval_T *argvars UNUSED,
11247 typval_T *rettv,
11248 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011249{
11250#ifdef FEAT_SEARCHPATH
11251 char_u *fname;
11252 char_u *fresult = NULL;
11253 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11254 char_u *p;
11255 char_u pathbuf[NUMBUFLEN];
11256 int count = 1;
11257 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011258 int error = FALSE;
11259#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011260
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011261 rettv->vval.v_string = NULL;
11262 rettv->v_type = VAR_STRING;
11263
11264#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011266
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011267 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11270 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011271 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011272 else
11273 {
11274 if (*p != NUL)
11275 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011276
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011277 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011278 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011279 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011280 }
11281
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011282 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11283 error = TRUE;
11284
11285 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011287 do
11288 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011289 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011290 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011291 fresult = find_file_in_path_option(first ? fname : NULL,
11292 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011293 0, first, path,
11294 find_what,
11295 curbuf->b_ffname,
11296 find_what == FINDFILE_DIR
11297 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011298 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011299
11300 if (fresult != NULL && rettv->v_type == VAR_LIST)
11301 list_append_string(rettv->vval.v_list, fresult, -1);
11302
11303 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011305
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011306 if (rettv->v_type == VAR_STRING)
11307 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011308#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011309}
11310
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011311static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11312static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011313
11314/*
11315 * Implementation of map() and filter().
11316 */
11317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011318filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011319{
11320 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011321 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 listitem_T *li, *nli;
11323 list_T *l = NULL;
11324 dictitem_T *di;
11325 hashtab_T *ht;
11326 hashitem_T *hi;
11327 dict_T *d = NULL;
11328 typval_T save_val;
11329 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011330 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011331 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011332 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011333 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011334 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011335 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011336 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011337
Bram Moolenaare9a41262005-01-15 22:18:47 +000011338 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011339 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011340 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011341 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011342 return;
11343 }
11344 else if (argvars[0].v_type == VAR_DICT)
11345 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011346 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011347 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011348 return;
11349 }
11350 else
11351 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011352 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011353 return;
11354 }
11355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011356 expr = get_tv_string_buf_chk(&argvars[1], buf);
11357 /* On type errors, the preceding call has already displayed an error
11358 * message. Avoid a misleading error message for an empty string that
11359 * was not passed as argument. */
11360 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362 prepare_vimvar(VV_VAL, &save_val);
11363 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011364
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011365 /* We reset "did_emsg" to be able to detect whether an error
11366 * occurred during evaluation of the expression. */
11367 save_did_emsg = did_emsg;
11368 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011369
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011370 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 vimvars[VV_KEY].vv_type = VAR_STRING;
11374
11375 ht = &d->dv_hashtab;
11376 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011377 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011380 if (!HASHITEM_EMPTY(hi))
11381 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011382 int r;
11383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011384 --todo;
11385 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011386 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011387 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11388 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 break;
11390 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011391 r = filter_map_one(&di->di_tv, expr, map, &rem);
11392 clear_tv(&vimvars[VV_KEY].vv_tv);
11393 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011394 break;
11395 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011396 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011397 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11398 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011399 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 }
11403 }
11404 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 }
11406 else
11407 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011408 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011410 for (li = l->lv_first; li != NULL; li = nli)
11411 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011412 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011413 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011415 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011416 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011417 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011418 break;
11419 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011420 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011421 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011422 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011423 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011424
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011425 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011426 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011427
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011428 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011429 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011430
11431 copy_tv(&argvars[0], rettv);
11432}
11433
11434 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011435filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011436{
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011438 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011439 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011440
Bram Moolenaar33570922005-01-25 22:26:29 +000011441 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011442 s = expr;
11443 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011444 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011445 if (*s != NUL) /* check for trailing chars after expr */
11446 {
11447 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011448 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011449 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011450 }
11451 if (map)
11452 {
11453 /* map(): replace the list item value */
11454 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011455 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011456 *tv = rettv;
11457 }
11458 else
11459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011460 int error = FALSE;
11461
Bram Moolenaare9a41262005-01-15 22:18:47 +000011462 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011464 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465 /* On type error, nothing has been removed; return FAIL to stop the
11466 * loop. The error message was given by get_tv_number_chk(). */
11467 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011468 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011469 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011470 retval = OK;
11471theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011473 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011474}
11475
11476/*
11477 * "filter()" function
11478 */
11479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011480f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011481{
11482 filter_map(argvars, rettv, FALSE);
11483}
11484
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011485/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011486 * "finddir({fname}[, {path}[, {count}]])" function
11487 */
11488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011489f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011490{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011491 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011492}
11493
11494/*
11495 * "findfile({fname}[, {path}[, {count}]])" function
11496 */
11497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011498f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011499{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011500 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011501}
11502
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011503#ifdef FEAT_FLOAT
11504/*
11505 * "float2nr({float})" function
11506 */
11507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011508f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011509{
11510 float_T f;
11511
11512 if (get_float_arg(argvars, &f) == OK)
11513 {
11514 if (f < -0x7fffffff)
11515 rettv->vval.v_number = -0x7fffffff;
11516 else if (f > 0x7fffffff)
11517 rettv->vval.v_number = 0x7fffffff;
11518 else
11519 rettv->vval.v_number = (varnumber_T)f;
11520 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011521}
11522
11523/*
11524 * "floor({float})" function
11525 */
11526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011527f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011528{
11529 float_T f;
11530
11531 rettv->v_type = VAR_FLOAT;
11532 if (get_float_arg(argvars, &f) == OK)
11533 rettv->vval.v_float = floor(f);
11534 else
11535 rettv->vval.v_float = 0.0;
11536}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011537
11538/*
11539 * "fmod()" function
11540 */
11541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011542f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011543{
11544 float_T fx, fy;
11545
11546 rettv->v_type = VAR_FLOAT;
11547 if (get_float_arg(argvars, &fx) == OK
11548 && get_float_arg(&argvars[1], &fy) == OK)
11549 rettv->vval.v_float = fmod(fx, fy);
11550 else
11551 rettv->vval.v_float = 0.0;
11552}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011553#endif
11554
Bram Moolenaar0d660222005-01-07 21:51:51 +000011555/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011556 * "fnameescape({string})" function
11557 */
11558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011559f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011560{
11561 rettv->vval.v_string = vim_strsave_fnameescape(
11562 get_tv_string(&argvars[0]), FALSE);
11563 rettv->v_type = VAR_STRING;
11564}
11565
11566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 * "fnamemodify({fname}, {mods})" function
11568 */
11569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011570f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571{
11572 char_u *fname;
11573 char_u *mods;
11574 int usedlen = 0;
11575 int len;
11576 char_u *fbuf = NULL;
11577 char_u buf[NUMBUFLEN];
11578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011579 fname = get_tv_string_chk(&argvars[0]);
11580 mods = get_tv_string_buf_chk(&argvars[1], buf);
11581 if (fname == NULL || mods == NULL)
11582 fname = NULL;
11583 else
11584 {
11585 len = (int)STRLEN(fname);
11586 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011591 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 vim_free(fbuf);
11595}
11596
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011597static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598
11599/*
11600 * "foldclosed()" function
11601 */
11602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011603foldclosed_both(
11604 typval_T *argvars UNUSED,
11605 typval_T *rettv,
11606 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607{
11608#ifdef FEAT_FOLDING
11609 linenr_T lnum;
11610 linenr_T first, last;
11611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011612 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11614 {
11615 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11616 {
11617 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011618 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011620 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 return;
11622 }
11623 }
11624#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011625 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626}
11627
11628/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011629 * "foldclosed()" function
11630 */
11631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011632f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011633{
11634 foldclosed_both(argvars, rettv, FALSE);
11635}
11636
11637/*
11638 * "foldclosedend()" function
11639 */
11640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011641f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011642{
11643 foldclosed_both(argvars, rettv, TRUE);
11644}
11645
11646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647 * "foldlevel()" function
11648 */
11649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011650f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651{
11652#ifdef FEAT_FOLDING
11653 linenr_T lnum;
11654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011655 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659}
11660
11661/*
11662 * "foldtext()" function
11663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011665f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666{
11667#ifdef FEAT_FOLDING
11668 linenr_T lnum;
11669 char_u *s;
11670 char_u *r;
11671 int len;
11672 char *txt;
11673#endif
11674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011675 rettv->v_type = VAR_STRING;
11676 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011678 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11679 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11680 <= curbuf->b_ml.ml_line_count
11681 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 {
11683 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011684 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11685 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 {
11687 if (!linewhite(lnum))
11688 break;
11689 ++lnum;
11690 }
11691
11692 /* Find interesting text in this line. */
11693 s = skipwhite(ml_get(lnum));
11694 /* skip C comment-start */
11695 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011698 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011699 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011700 {
11701 s = skipwhite(ml_get(lnum + 1));
11702 if (*s == '*')
11703 s = skipwhite(s + 1);
11704 }
11705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 txt = _("+-%s%3ld lines: ");
11707 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011708 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 + 20 /* for %3ld */
11710 + STRLEN(s))); /* concatenated */
11711 if (r != NULL)
11712 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011713 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11714 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11715 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 len = (int)STRLEN(r);
11717 STRCAT(r, s);
11718 /* remove 'foldmarker' and 'commentstring' */
11719 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 }
11722 }
11723#endif
11724}
11725
11726/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011727 * "foldtextresult(lnum)" function
11728 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011730f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011731{
11732#ifdef FEAT_FOLDING
11733 linenr_T lnum;
11734 char_u *text;
11735 char_u buf[51];
11736 foldinfo_T foldinfo;
11737 int fold_count;
11738#endif
11739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011740 rettv->v_type = VAR_STRING;
11741 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011742#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011743 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 /* treat illegal types and illegal string values for {lnum} the same */
11745 if (lnum < 0)
11746 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011747 fold_count = foldedCount(curwin, lnum, &foldinfo);
11748 if (fold_count > 0)
11749 {
11750 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11751 &foldinfo, buf);
11752 if (text == buf)
11753 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011755 }
11756#endif
11757}
11758
11759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 * "foreground()" function
11761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011763f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765#ifdef FEAT_GUI
11766 if (gui.in_use)
11767 gui_mch_set_foreground();
11768#else
11769# ifdef WIN32
11770 win32_set_foreground();
11771# endif
11772#endif
11773}
11774
11775/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011776 * "function()" function
11777 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011779f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011780{
11781 char_u *s;
11782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011784 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011785 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011786 /* Don't check an autoload name for existence here. */
11787 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011788 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011789 else
11790 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011791 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011792 {
11793 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011794 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011795
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011796 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11797 * also be called from another script. Using trans_function_name()
11798 * would also work, but some plugins depend on the name being
11799 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011800 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011801 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011802 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011803 if (rettv->vval.v_string != NULL)
11804 {
11805 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011806 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011807 }
11808 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011809 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011810 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011811 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011812 }
11813}
11814
11815/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011816 * "garbagecollect()" function
11817 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011819f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011820{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011821 /* This is postponed until we are back at the toplevel, because we may be
11822 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11823 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011824
11825 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11826 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011827}
11828
11829/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011830 * "get()" function
11831 */
11832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011833f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011834{
Bram Moolenaar33570922005-01-25 22:26:29 +000011835 listitem_T *li;
11836 list_T *l;
11837 dictitem_T *di;
11838 dict_T *d;
11839 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011840
Bram Moolenaare9a41262005-01-15 22:18:47 +000011841 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011842 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011843 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011845 int error = FALSE;
11846
11847 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11848 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011849 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011850 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011851 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011852 else if (argvars[0].v_type == VAR_DICT)
11853 {
11854 if ((d = argvars[0].vval.v_dict) != NULL)
11855 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011856 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011857 if (di != NULL)
11858 tv = &di->di_tv;
11859 }
11860 }
11861 else
11862 EMSG2(_(e_listdictarg), "get()");
11863
11864 if (tv == NULL)
11865 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011866 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011867 copy_tv(&argvars[2], rettv);
11868 }
11869 else
11870 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011871}
11872
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011873static 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 +000011874
11875/*
11876 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011877 * Return a range (from start to end) of lines in rettv from the specified
11878 * buffer.
11879 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011880 */
11881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011882get_buffer_lines(
11883 buf_T *buf,
11884 linenr_T start,
11885 linenr_T end,
11886 int retlist,
11887 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011888{
11889 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011890
Bram Moolenaar959a1432013-12-14 12:17:38 +010011891 rettv->v_type = VAR_STRING;
11892 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011893 if (retlist && rettv_list_alloc(rettv) == FAIL)
11894 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011895
11896 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11897 return;
11898
11899 if (!retlist)
11900 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011901 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11902 p = ml_get_buf(buf, start, FALSE);
11903 else
11904 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011905 rettv->vval.v_string = vim_strsave(p);
11906 }
11907 else
11908 {
11909 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011910 return;
11911
11912 if (start < 1)
11913 start = 1;
11914 if (end > buf->b_ml.ml_line_count)
11915 end = buf->b_ml.ml_line_count;
11916 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011917 if (list_append_string(rettv->vval.v_list,
11918 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011919 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011920 }
11921}
11922
11923/*
11924 * "getbufline()" function
11925 */
11926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011927f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011928{
11929 linenr_T lnum;
11930 linenr_T end;
11931 buf_T *buf;
11932
11933 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11934 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011935 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011936 --emsg_off;
11937
Bram Moolenaar661b1822005-07-28 22:36:45 +000011938 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011939 if (argvars[2].v_type == VAR_UNKNOWN)
11940 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011941 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011942 end = get_tv_lnum_buf(&argvars[2], buf);
11943
Bram Moolenaar342337a2005-07-21 21:11:17 +000011944 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011945}
11946
Bram Moolenaar0d660222005-01-07 21:51:51 +000011947/*
11948 * "getbufvar()" function
11949 */
11950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011951f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011952{
11953 buf_T *buf;
11954 buf_T *save_curbuf;
11955 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011956 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011957 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11960 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011961 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011962 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011963
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011964 rettv->v_type = VAR_STRING;
11965 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011966
11967 if (buf != NULL && varname != NULL)
11968 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011969 /* set curbuf to be our buf, temporarily */
11970 save_curbuf = curbuf;
11971 curbuf = buf;
11972
Bram Moolenaar0d660222005-01-07 21:51:51 +000011973 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011974 {
11975 if (get_option_tv(&varname, rettv, TRUE) == OK)
11976 done = TRUE;
11977 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011978 else if (STRCMP(varname, "changedtick") == 0)
11979 {
11980 rettv->v_type = VAR_NUMBER;
11981 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011982 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011983 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011984 else
11985 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011986 /* Look up the variable. */
11987 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11988 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11989 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011990 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011991 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011992 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011993 done = TRUE;
11994 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011995 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011996
11997 /* restore previous notion of curbuf */
11998 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011999 }
12000
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012001 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12002 /* use the default value */
12003 copy_tv(&argvars[2], rettv);
12004
Bram Moolenaar0d660222005-01-07 21:51:51 +000012005 --emsg_off;
12006}
12007
12008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009 * "getchar()" function
12010 */
12011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012012f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013{
12014 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012015 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012017 /* Position the cursor. Needed after a message that ends in a space. */
12018 windgoto(msg_row, msg_col);
12019
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 ++no_mapping;
12021 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012022 for (;;)
12023 {
12024 if (argvars[0].v_type == VAR_UNKNOWN)
12025 /* getchar(): blocking wait. */
12026 n = safe_vgetc();
12027 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12028 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012029 n = vpeekc_any();
12030 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012031 /* illegal argument or getchar(0) and no char avail: return zero */
12032 n = 0;
12033 else
12034 /* getchar(0) and char avail: return char */
12035 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012036
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012037 if (n == K_IGNORE)
12038 continue;
12039 break;
12040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 --no_mapping;
12042 --allow_keys;
12043
Bram Moolenaar219b8702006-11-01 14:32:36 +000012044 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12045 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12046 vimvars[VV_MOUSE_COL].vv_nr = 0;
12047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 if (IS_SPECIAL(n) || mod_mask != 0)
12050 {
12051 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12052 int i = 0;
12053
12054 /* Turn a special key into three bytes, plus modifier. */
12055 if (mod_mask != 0)
12056 {
12057 temp[i++] = K_SPECIAL;
12058 temp[i++] = KS_MODIFIER;
12059 temp[i++] = mod_mask;
12060 }
12061 if (IS_SPECIAL(n))
12062 {
12063 temp[i++] = K_SPECIAL;
12064 temp[i++] = K_SECOND(n);
12065 temp[i++] = K_THIRD(n);
12066 }
12067#ifdef FEAT_MBYTE
12068 else if (has_mbyte)
12069 i += (*mb_char2bytes)(n, temp + i);
12070#endif
12071 else
12072 temp[i++] = n;
12073 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012074 rettv->v_type = VAR_STRING;
12075 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012076
12077#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012078 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012079 {
12080 int row = mouse_row;
12081 int col = mouse_col;
12082 win_T *win;
12083 linenr_T lnum;
12084# ifdef FEAT_WINDOWS
12085 win_T *wp;
12086# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012087 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012088
12089 if (row >= 0 && col >= 0)
12090 {
12091 /* Find the window at the mouse coordinates and compute the
12092 * text position. */
12093 win = mouse_find_win(&row, &col);
12094 (void)mouse_comp_pos(win, &row, &col, &lnum);
12095# ifdef FEAT_WINDOWS
12096 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012097 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012098# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012099 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012100 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12101 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12102 }
12103 }
12104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 }
12106}
12107
12108/*
12109 * "getcharmod()" function
12110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012112f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012114 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115}
12116
12117/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012118 * "getcharsearch()" function
12119 */
12120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012121f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012122{
12123 if (rettv_dict_alloc(rettv) != FAIL)
12124 {
12125 dict_T *dict = rettv->vval.v_dict;
12126
12127 dict_add_nr_str(dict, "char", 0L, last_csearch());
12128 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12129 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12130 }
12131}
12132
12133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 * "getcmdline()" function
12135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012137f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012139 rettv->v_type = VAR_STRING;
12140 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141}
12142
12143/*
12144 * "getcmdpos()" function
12145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012147f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150}
12151
12152/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012153 * "getcmdtype()" function
12154 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012156f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012157{
12158 rettv->v_type = VAR_STRING;
12159 rettv->vval.v_string = alloc(2);
12160 if (rettv->vval.v_string != NULL)
12161 {
12162 rettv->vval.v_string[0] = get_cmdline_type();
12163 rettv->vval.v_string[1] = NUL;
12164 }
12165}
12166
12167/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012168 * "getcmdwintype()" function
12169 */
12170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012171f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012172{
12173 rettv->v_type = VAR_STRING;
12174 rettv->vval.v_string = NULL;
12175#ifdef FEAT_CMDWIN
12176 rettv->vval.v_string = alloc(2);
12177 if (rettv->vval.v_string != NULL)
12178 {
12179 rettv->vval.v_string[0] = cmdwin_type;
12180 rettv->vval.v_string[1] = NUL;
12181 }
12182#endif
12183}
12184
12185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 * "getcwd()" function
12187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012189f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012191 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012192 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012195 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012196
12197 wp = find_tabwin(&argvars[0], &argvars[1]);
12198 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012200 if (wp->w_localdir != NULL)
12201 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12202 else if(globaldir != NULL)
12203 rettv->vval.v_string = vim_strsave(globaldir);
12204 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012205 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012206 cwd = alloc(MAXPATHL);
12207 if (cwd != NULL)
12208 {
12209 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12210 rettv->vval.v_string = vim_strsave(cwd);
12211 vim_free(cwd);
12212 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012213 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012214#ifdef BACKSLASH_IN_FILENAME
12215 if (rettv->vval.v_string != NULL)
12216 slash_adjust(rettv->vval.v_string);
12217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 }
12219}
12220
12221/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012222 * "getfontname()" function
12223 */
12224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012225f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012226{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012227 rettv->v_type = VAR_STRING;
12228 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012229#ifdef FEAT_GUI
12230 if (gui.in_use)
12231 {
12232 GuiFont font;
12233 char_u *name = NULL;
12234
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012235 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012236 {
12237 /* Get the "Normal" font. Either the name saved by
12238 * hl_set_font_name() or from the font ID. */
12239 font = gui.norm_font;
12240 name = hl_get_font_name();
12241 }
12242 else
12243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012245 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12246 return;
12247 font = gui_mch_get_font(name, FALSE);
12248 if (font == NOFONT)
12249 return; /* Invalid font name, return empty string. */
12250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012251 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012252 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012253 gui_mch_free_font(font);
12254 }
12255#endif
12256}
12257
12258/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012259 * "getfperm({fname})" function
12260 */
12261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012262f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012263{
12264 char_u *fname;
12265 struct stat st;
12266 char_u *perm = NULL;
12267 char_u flags[] = "rwx";
12268 int i;
12269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012273 if (mch_stat((char *)fname, &st) >= 0)
12274 {
12275 perm = vim_strsave((char_u *)"---------");
12276 if (perm != NULL)
12277 {
12278 for (i = 0; i < 9; i++)
12279 {
12280 if (st.st_mode & (1 << (8 - i)))
12281 perm[i] = flags[i % 3];
12282 }
12283 }
12284 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012285 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012286}
12287
12288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 * "getfsize({fname})" function
12290 */
12291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012292f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293{
12294 char_u *fname;
12295 struct stat st;
12296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300
12301 if (mch_stat((char *)fname, &st) >= 0)
12302 {
12303 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012304 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012307 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012308
12309 /* non-perfect check for overflow */
12310 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12311 rettv->vval.v_number = -2;
12312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313 }
12314 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012315 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316}
12317
12318/*
12319 * "getftime({fname})" function
12320 */
12321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012322f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323{
12324 char_u *fname;
12325 struct stat st;
12326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328
12329 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012332 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333}
12334
12335/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012336 * "getftype({fname})" function
12337 */
12338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012339f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012340{
12341 char_u *fname;
12342 struct stat st;
12343 char_u *type = NULL;
12344 char *t;
12345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012346 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012349 if (mch_lstat((char *)fname, &st) >= 0)
12350 {
12351#ifdef S_ISREG
12352 if (S_ISREG(st.st_mode))
12353 t = "file";
12354 else if (S_ISDIR(st.st_mode))
12355 t = "dir";
12356# ifdef S_ISLNK
12357 else if (S_ISLNK(st.st_mode))
12358 t = "link";
12359# endif
12360# ifdef S_ISBLK
12361 else if (S_ISBLK(st.st_mode))
12362 t = "bdev";
12363# endif
12364# ifdef S_ISCHR
12365 else if (S_ISCHR(st.st_mode))
12366 t = "cdev";
12367# endif
12368# ifdef S_ISFIFO
12369 else if (S_ISFIFO(st.st_mode))
12370 t = "fifo";
12371# endif
12372# ifdef S_ISSOCK
12373 else if (S_ISSOCK(st.st_mode))
12374 t = "fifo";
12375# endif
12376 else
12377 t = "other";
12378#else
12379# ifdef S_IFMT
12380 switch (st.st_mode & S_IFMT)
12381 {
12382 case S_IFREG: t = "file"; break;
12383 case S_IFDIR: t = "dir"; break;
12384# ifdef S_IFLNK
12385 case S_IFLNK: t = "link"; break;
12386# endif
12387# ifdef S_IFBLK
12388 case S_IFBLK: t = "bdev"; break;
12389# endif
12390# ifdef S_IFCHR
12391 case S_IFCHR: t = "cdev"; break;
12392# endif
12393# ifdef S_IFIFO
12394 case S_IFIFO: t = "fifo"; break;
12395# endif
12396# ifdef S_IFSOCK
12397 case S_IFSOCK: t = "socket"; break;
12398# endif
12399 default: t = "other";
12400 }
12401# else
12402 if (mch_isdir(fname))
12403 t = "dir";
12404 else
12405 t = "file";
12406# endif
12407#endif
12408 type = vim_strsave((char_u *)t);
12409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012410 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012411}
12412
12413/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012414 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012415 */
12416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012417f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012418{
12419 linenr_T lnum;
12420 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012421 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012422
12423 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012424 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012425 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012426 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012427 retlist = FALSE;
12428 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012429 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012430 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012431 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012432 retlist = TRUE;
12433 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012434
Bram Moolenaar342337a2005-07-21 21:11:17 +000012435 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012436}
12437
12438/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012439 * "getmatches()" function
12440 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012442f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012443{
12444#ifdef FEAT_SEARCH_EXTRA
12445 dict_T *dict;
12446 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012447 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012448
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012449 if (rettv_list_alloc(rettv) == OK)
12450 {
12451 while (cur != NULL)
12452 {
12453 dict = dict_alloc();
12454 if (dict == NULL)
12455 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012456 if (cur->match.regprog == NULL)
12457 {
12458 /* match added with matchaddpos() */
12459 for (i = 0; i < MAXPOSMATCH; ++i)
12460 {
12461 llpos_T *llpos;
12462 char buf[6];
12463 list_T *l;
12464
12465 llpos = &cur->pos.pos[i];
12466 if (llpos->lnum == 0)
12467 break;
12468 l = list_alloc();
12469 if (l == NULL)
12470 break;
12471 list_append_number(l, (varnumber_T)llpos->lnum);
12472 if (llpos->col > 0)
12473 {
12474 list_append_number(l, (varnumber_T)llpos->col);
12475 list_append_number(l, (varnumber_T)llpos->len);
12476 }
12477 sprintf(buf, "pos%d", i + 1);
12478 dict_add_list(dict, buf, l);
12479 }
12480 }
12481 else
12482 {
12483 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12484 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012485 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012486 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12487 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012488# ifdef FEAT_CONCEAL
12489 if (cur->conceal_char)
12490 {
12491 char_u buf[MB_MAXBYTES + 1];
12492
12493 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12494 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12495 }
12496# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012497 list_append_dict(rettv->vval.v_list, dict);
12498 cur = cur->next;
12499 }
12500 }
12501#endif
12502}
12503
12504/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012505 * "getpid()" function
12506 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012508f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012509{
12510 rettv->vval.v_number = mch_get_pid();
12511}
12512
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012513static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012514
12515/*
12516 * "getcurpos()" function
12517 */
12518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012519f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012520{
12521 getpos_both(argvars, rettv, TRUE);
12522}
12523
Bram Moolenaar18081e32008-02-20 19:11:07 +000012524/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012525 * "getpos(string)" function
12526 */
12527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012528f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012529{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012530 getpos_both(argvars, rettv, FALSE);
12531}
12532
12533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012534getpos_both(
12535 typval_T *argvars,
12536 typval_T *rettv,
12537 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012538{
Bram Moolenaara5525202006-03-02 22:52:09 +000012539 pos_T *fp;
12540 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012541 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012542
12543 if (rettv_list_alloc(rettv) == OK)
12544 {
12545 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012546 if (getcurpos)
12547 fp = &curwin->w_cursor;
12548 else
12549 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012550 if (fnum != -1)
12551 list_append_number(l, (varnumber_T)fnum);
12552 else
12553 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012554 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12555 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012556 list_append_number(l, (fp != NULL)
12557 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012558 : (varnumber_T)0);
12559 list_append_number(l,
12560#ifdef FEAT_VIRTUALEDIT
12561 (fp != NULL) ? (varnumber_T)fp->coladd :
12562#endif
12563 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012564 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012565 {
12566 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012567 list_append_number(l, curwin->w_curswant == MAXCOL ?
12568 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012569 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012570 }
12571 else
12572 rettv->vval.v_number = FALSE;
12573}
12574
12575/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012576 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012577 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012579f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012580{
12581#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012582 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012583#endif
12584
Bram Moolenaar2641f772005-03-25 21:58:17 +000012585#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012586 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012587 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012588 wp = NULL;
12589 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12590 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012591 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012592 if (wp == NULL)
12593 return;
12594 }
12595
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012596 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012597 }
12598#endif
12599}
12600
12601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602 * "getreg()" function
12603 */
12604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012605f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606{
12607 char_u *strregname;
12608 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012609 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012610 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012611 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012613 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012615 strregname = get_tv_string_chk(&argvars[0]);
12616 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012617 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012619 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012620 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12621 return_list = get_tv_number_chk(&argvars[2], &error);
12622 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012625 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012626
12627 if (error)
12628 return;
12629
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630 regname = (strregname == NULL ? '"' : *strregname);
12631 if (regname == 0)
12632 regname = '"';
12633
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012634 if (return_list)
12635 {
12636 rettv->v_type = VAR_LIST;
12637 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12638 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012639 if (rettv->vval.v_list != NULL)
12640 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012641 }
12642 else
12643 {
12644 rettv->v_type = VAR_STRING;
12645 rettv->vval.v_string = get_reg_contents(regname,
12646 arg2 ? GREG_EXPR_SRC : 0);
12647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648}
12649
12650/*
12651 * "getregtype()" function
12652 */
12653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012654f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655{
12656 char_u *strregname;
12657 int regname;
12658 char_u buf[NUMBUFLEN + 2];
12659 long reglen = 0;
12660
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012661 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012662 {
12663 strregname = get_tv_string_chk(&argvars[0]);
12664 if (strregname == NULL) /* type error; errmsg already given */
12665 {
12666 rettv->v_type = VAR_STRING;
12667 rettv->vval.v_string = NULL;
12668 return;
12669 }
12670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671 else
12672 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012673 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674
12675 regname = (strregname == NULL ? '"' : *strregname);
12676 if (regname == 0)
12677 regname = '"';
12678
12679 buf[0] = NUL;
12680 buf[1] = NUL;
12681 switch (get_reg_type(regname, &reglen))
12682 {
12683 case MLINE: buf[0] = 'V'; break;
12684 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 case MBLOCK:
12686 buf[0] = Ctrl_V;
12687 sprintf((char *)buf + 1, "%ld", reglen + 1);
12688 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012690 rettv->v_type = VAR_STRING;
12691 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692}
12693
12694/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012695 * "gettabvar()" function
12696 */
12697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012698f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012699{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012700 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012701 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012702 dictitem_T *v;
12703 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012704 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012705
12706 rettv->v_type = VAR_STRING;
12707 rettv->vval.v_string = NULL;
12708
12709 varname = get_tv_string_chk(&argvars[1]);
12710 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12711 if (tp != NULL && varname != NULL)
12712 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012713 /* Set tp to be our tabpage, temporarily. Also set the window to the
12714 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012715 if (switch_win(&oldcurwin, &oldtabpage,
12716 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012717 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012718 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012719 /* look up the variable */
12720 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12721 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12722 if (v != NULL)
12723 {
12724 copy_tv(&v->di_tv, rettv);
12725 done = TRUE;
12726 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012727 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012728
12729 /* restore previous notion of curwin */
12730 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012731 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012732
12733 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012734 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012735}
12736
12737/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012738 * "gettabwinvar()" function
12739 */
12740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012741f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012742{
12743 getwinvar(argvars, rettv, 1);
12744}
12745
12746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 * "getwinposx()" function
12748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012750f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012752 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753#ifdef FEAT_GUI
12754 if (gui.in_use)
12755 {
12756 int x, y;
12757
12758 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 }
12761#endif
12762}
12763
12764/*
12765 * "getwinposy()" function
12766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012768f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771#ifdef FEAT_GUI
12772 if (gui.in_use)
12773 {
12774 int x, y;
12775
12776 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 }
12779#endif
12780}
12781
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012782/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012783 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012784 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012785 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012786find_win_by_nr(
12787 typval_T *vp,
12788 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012789{
12790#ifdef FEAT_WINDOWS
12791 win_T *wp;
12792#endif
12793 int nr;
12794
12795 nr = get_tv_number_chk(vp, NULL);
12796
12797#ifdef FEAT_WINDOWS
12798 if (nr < 0)
12799 return NULL;
12800 if (nr == 0)
12801 return curwin;
12802
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012803 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12804 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012805 if (--nr <= 0)
12806 break;
12807 return wp;
12808#else
12809 if (nr == 0 || nr == 1)
12810 return curwin;
12811 return NULL;
12812#endif
12813}
12814
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012816 * Find window specified by "wvp" in tabpage "tvp".
12817 */
12818 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012819find_tabwin(
12820 typval_T *wvp, /* VAR_UNKNOWN for current window */
12821 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012822{
12823 win_T *wp = NULL;
12824 tabpage_T *tp = NULL;
12825 long n;
12826
12827 if (wvp->v_type != VAR_UNKNOWN)
12828 {
12829 if (tvp->v_type != VAR_UNKNOWN)
12830 {
12831 n = get_tv_number(tvp);
12832 if (n >= 0)
12833 tp = find_tabpage(n);
12834 }
12835 else
12836 tp = curtab;
12837
12838 if (tp != NULL)
12839 wp = find_win_by_nr(wvp, tp);
12840 }
12841 else
12842 wp = curwin;
12843
12844 return wp;
12845}
12846
12847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 * "getwinvar()" function
12849 */
12850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012851f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012853 getwinvar(argvars, rettv, 0);
12854}
12855
12856/*
12857 * getwinvar() and gettabwinvar()
12858 */
12859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012860getwinvar(
12861 typval_T *argvars,
12862 typval_T *rettv,
12863 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012864{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012865 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012867 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012868 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012869 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012870#ifdef FEAT_WINDOWS
12871 win_T *oldcurwin;
12872 tabpage_T *oldtabpage;
12873 int need_switch_win;
12874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012876#ifdef FEAT_WINDOWS
12877 if (off == 1)
12878 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12879 else
12880 tp = curtab;
12881#endif
12882 win = find_win_by_nr(&argvars[off], tp);
12883 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012884 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012886 rettv->v_type = VAR_STRING;
12887 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888
12889 if (win != NULL && varname != NULL)
12890 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012891#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012892 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012893 * otherwise the window is not valid. Only do this when needed,
12894 * autocommands get blocked. */
12895 need_switch_win = !(tp == curtab && win == curwin);
12896 if (!need_switch_win
12897 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12898#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012899 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012900 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012901 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012902 if (get_option_tv(&varname, rettv, 1) == OK)
12903 done = TRUE;
12904 }
12905 else
12906 {
12907 /* Look up the variable. */
12908 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12909 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12910 varname, FALSE);
12911 if (v != NULL)
12912 {
12913 copy_tv(&v->di_tv, rettv);
12914 done = TRUE;
12915 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012918
Bram Moolenaarba117c22015-09-29 16:53:22 +020012919#ifdef FEAT_WINDOWS
12920 if (need_switch_win)
12921 /* restore previous notion of curwin */
12922 restore_win(oldcurwin, oldtabpage, TRUE);
12923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924 }
12925
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012926 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12927 /* use the default return value */
12928 copy_tv(&argvars[off + 2], rettv);
12929
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930 --emsg_off;
12931}
12932
12933/*
12934 * "glob()" function
12935 */
12936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012937f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012939 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012941 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012943 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012944 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012946 if (argvars[1].v_type != VAR_UNKNOWN)
12947 {
12948 if (get_tv_number_chk(&argvars[1], &error))
12949 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012950 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012951 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012952 if (get_tv_number_chk(&argvars[2], &error))
12953 {
12954 rettv->v_type = VAR_LIST;
12955 rettv->vval.v_list = NULL;
12956 }
12957 if (argvars[3].v_type != VAR_UNKNOWN
12958 && get_tv_number_chk(&argvars[3], &error))
12959 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012960 }
12961 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012962 if (!error)
12963 {
12964 ExpandInit(&xpc);
12965 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012966 if (p_wic)
12967 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012968 if (rettv->v_type == VAR_STRING)
12969 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012970 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012971 else if (rettv_list_alloc(rettv) != FAIL)
12972 {
12973 int i;
12974
12975 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12976 NULL, options, WILD_ALL_KEEP);
12977 for (i = 0; i < xpc.xp_numfiles; i++)
12978 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12979
12980 ExpandCleanup(&xpc);
12981 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012982 }
12983 else
12984 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985}
12986
12987/*
12988 * "globpath()" function
12989 */
12990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012991f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012993 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012995 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012996 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012997 garray_T ga;
12998 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013000 /* When the optional second argument is non-zero, don't remove matches
13001 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013002 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013003 if (argvars[2].v_type != VAR_UNKNOWN)
13004 {
13005 if (get_tv_number_chk(&argvars[2], &error))
13006 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013007 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013008 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013009 if (get_tv_number_chk(&argvars[3], &error))
13010 {
13011 rettv->v_type = VAR_LIST;
13012 rettv->vval.v_list = NULL;
13013 }
13014 if (argvars[4].v_type != VAR_UNKNOWN
13015 && get_tv_number_chk(&argvars[4], &error))
13016 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013017 }
13018 }
13019 if (file != NULL && !error)
13020 {
13021 ga_init2(&ga, (int)sizeof(char_u *), 10);
13022 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13023 if (rettv->v_type == VAR_STRING)
13024 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13025 else if (rettv_list_alloc(rettv) != FAIL)
13026 for (i = 0; i < ga.ga_len; ++i)
13027 list_append_string(rettv->vval.v_list,
13028 ((char_u **)(ga.ga_data))[i], -1);
13029 ga_clear_strings(&ga);
13030 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013031 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013032 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033}
13034
13035/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013036 * "glob2regpat()" function
13037 */
13038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013039f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013040{
13041 char_u *pat = get_tv_string_chk(&argvars[0]);
13042
13043 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013044 rettv->vval.v_string = (pat == NULL)
13045 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013046}
13047
13048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 * "has()" function
13050 */
13051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013052f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053{
13054 int i;
13055 char_u *name;
13056 int n = FALSE;
13057 static char *(has_list[]) =
13058 {
13059#ifdef AMIGA
13060 "amiga",
13061# ifdef FEAT_ARP
13062 "arp",
13063# endif
13064#endif
13065#ifdef __BEOS__
13066 "beos",
13067#endif
13068#ifdef MSDOS
13069# ifdef DJGPP
13070 "dos32",
13071# else
13072 "dos16",
13073# endif
13074#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013075#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076 "mac",
13077#endif
13078#if defined(MACOS_X_UNIX)
13079 "macunix",
13080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081#ifdef __QNX__
13082 "qnx",
13083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084#ifdef UNIX
13085 "unix",
13086#endif
13087#ifdef VMS
13088 "vms",
13089#endif
13090#ifdef WIN16
13091 "win16",
13092#endif
13093#ifdef WIN32
13094 "win32",
13095#endif
13096#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13097 "win32unix",
13098#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013099#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100 "win64",
13101#endif
13102#ifdef EBCDIC
13103 "ebcdic",
13104#endif
13105#ifndef CASE_INSENSITIVE_FILENAME
13106 "fname_case",
13107#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013108#ifdef HAVE_ACL
13109 "acl",
13110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111#ifdef FEAT_ARABIC
13112 "arabic",
13113#endif
13114#ifdef FEAT_AUTOCMD
13115 "autocmd",
13116#endif
13117#ifdef FEAT_BEVAL
13118 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013119# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13120 "balloon_multiline",
13121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122#endif
13123#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13124 "builtin_terms",
13125# ifdef ALL_BUILTIN_TCAPS
13126 "all_builtin_terms",
13127# endif
13128#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013129#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13130 || defined(FEAT_GUI_W32) \
13131 || defined(FEAT_GUI_MOTIF))
13132 "browsefilter",
13133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134#ifdef FEAT_BYTEOFF
13135 "byte_offset",
13136#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013137#ifdef FEAT_CHANNEL
13138 "channel",
13139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140#ifdef FEAT_CINDENT
13141 "cindent",
13142#endif
13143#ifdef FEAT_CLIENTSERVER
13144 "clientserver",
13145#endif
13146#ifdef FEAT_CLIPBOARD
13147 "clipboard",
13148#endif
13149#ifdef FEAT_CMDL_COMPL
13150 "cmdline_compl",
13151#endif
13152#ifdef FEAT_CMDHIST
13153 "cmdline_hist",
13154#endif
13155#ifdef FEAT_COMMENTS
13156 "comments",
13157#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013158#ifdef FEAT_CONCEAL
13159 "conceal",
13160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161#ifdef FEAT_CRYPT
13162 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013163 "crypt-blowfish",
13164 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165#endif
13166#ifdef FEAT_CSCOPE
13167 "cscope",
13168#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013169#ifdef FEAT_CURSORBIND
13170 "cursorbind",
13171#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013172#ifdef CURSOR_SHAPE
13173 "cursorshape",
13174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175#ifdef DEBUG
13176 "debug",
13177#endif
13178#ifdef FEAT_CON_DIALOG
13179 "dialog_con",
13180#endif
13181#ifdef FEAT_GUI_DIALOG
13182 "dialog_gui",
13183#endif
13184#ifdef FEAT_DIFF
13185 "diff",
13186#endif
13187#ifdef FEAT_DIGRAPHS
13188 "digraphs",
13189#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013190#ifdef FEAT_DIRECTX
13191 "directx",
13192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193#ifdef FEAT_DND
13194 "dnd",
13195#endif
13196#ifdef FEAT_EMACS_TAGS
13197 "emacs_tags",
13198#endif
13199 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013200 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201#ifdef FEAT_SEARCH_EXTRA
13202 "extra_search",
13203#endif
13204#ifdef FEAT_FKMAP
13205 "farsi",
13206#endif
13207#ifdef FEAT_SEARCHPATH
13208 "file_in_path",
13209#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013210#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013211 "filterpipe",
13212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213#ifdef FEAT_FIND_ID
13214 "find_in_path",
13215#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013216#ifdef FEAT_FLOAT
13217 "float",
13218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219#ifdef FEAT_FOLDING
13220 "folding",
13221#endif
13222#ifdef FEAT_FOOTER
13223 "footer",
13224#endif
13225#if !defined(USE_SYSTEM) && defined(UNIX)
13226 "fork",
13227#endif
13228#ifdef FEAT_GETTEXT
13229 "gettext",
13230#endif
13231#ifdef FEAT_GUI
13232 "gui",
13233#endif
13234#ifdef FEAT_GUI_ATHENA
13235# ifdef FEAT_GUI_NEXTAW
13236 "gui_neXtaw",
13237# else
13238 "gui_athena",
13239# endif
13240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241#ifdef FEAT_GUI_GTK
13242 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013245#ifdef FEAT_GUI_GNOME
13246 "gui_gnome",
13247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248#ifdef FEAT_GUI_MAC
13249 "gui_mac",
13250#endif
13251#ifdef FEAT_GUI_MOTIF
13252 "gui_motif",
13253#endif
13254#ifdef FEAT_GUI_PHOTON
13255 "gui_photon",
13256#endif
13257#ifdef FEAT_GUI_W16
13258 "gui_win16",
13259#endif
13260#ifdef FEAT_GUI_W32
13261 "gui_win32",
13262#endif
13263#ifdef FEAT_HANGULIN
13264 "hangul_input",
13265#endif
13266#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13267 "iconv",
13268#endif
13269#ifdef FEAT_INS_EXPAND
13270 "insert_expand",
13271#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013272#ifdef FEAT_JOB
13273 "job",
13274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275#ifdef FEAT_JUMPLIST
13276 "jumplist",
13277#endif
13278#ifdef FEAT_KEYMAP
13279 "keymap",
13280#endif
13281#ifdef FEAT_LANGMAP
13282 "langmap",
13283#endif
13284#ifdef FEAT_LIBCALL
13285 "libcall",
13286#endif
13287#ifdef FEAT_LINEBREAK
13288 "linebreak",
13289#endif
13290#ifdef FEAT_LISP
13291 "lispindent",
13292#endif
13293#ifdef FEAT_LISTCMDS
13294 "listcmds",
13295#endif
13296#ifdef FEAT_LOCALMAP
13297 "localmap",
13298#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013299#ifdef FEAT_LUA
13300# ifndef DYNAMIC_LUA
13301 "lua",
13302# endif
13303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304#ifdef FEAT_MENU
13305 "menu",
13306#endif
13307#ifdef FEAT_SESSION
13308 "mksession",
13309#endif
13310#ifdef FEAT_MODIFY_FNAME
13311 "modify_fname",
13312#endif
13313#ifdef FEAT_MOUSE
13314 "mouse",
13315#endif
13316#ifdef FEAT_MOUSESHAPE
13317 "mouseshape",
13318#endif
13319#if defined(UNIX) || defined(VMS)
13320# ifdef FEAT_MOUSE_DEC
13321 "mouse_dec",
13322# endif
13323# ifdef FEAT_MOUSE_GPM
13324 "mouse_gpm",
13325# endif
13326# ifdef FEAT_MOUSE_JSB
13327 "mouse_jsbterm",
13328# endif
13329# ifdef FEAT_MOUSE_NET
13330 "mouse_netterm",
13331# endif
13332# ifdef FEAT_MOUSE_PTERM
13333 "mouse_pterm",
13334# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013335# ifdef FEAT_MOUSE_SGR
13336 "mouse_sgr",
13337# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013338# ifdef FEAT_SYSMOUSE
13339 "mouse_sysmouse",
13340# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013341# ifdef FEAT_MOUSE_URXVT
13342 "mouse_urxvt",
13343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344# ifdef FEAT_MOUSE_XTERM
13345 "mouse_xterm",
13346# endif
13347#endif
13348#ifdef FEAT_MBYTE
13349 "multi_byte",
13350#endif
13351#ifdef FEAT_MBYTE_IME
13352 "multi_byte_ime",
13353#endif
13354#ifdef FEAT_MULTI_LANG
13355 "multi_lang",
13356#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013357#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013358#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013359 "mzscheme",
13360#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362#ifdef FEAT_OLE
13363 "ole",
13364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365#ifdef FEAT_PATH_EXTRA
13366 "path_extra",
13367#endif
13368#ifdef FEAT_PERL
13369#ifndef DYNAMIC_PERL
13370 "perl",
13371#endif
13372#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013373#ifdef FEAT_PERSISTENT_UNDO
13374 "persistent_undo",
13375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376#ifdef FEAT_PYTHON
13377#ifndef DYNAMIC_PYTHON
13378 "python",
13379#endif
13380#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013381#ifdef FEAT_PYTHON3
13382#ifndef DYNAMIC_PYTHON3
13383 "python3",
13384#endif
13385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386#ifdef FEAT_POSTSCRIPT
13387 "postscript",
13388#endif
13389#ifdef FEAT_PRINTER
13390 "printer",
13391#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013392#ifdef FEAT_PROFILE
13393 "profile",
13394#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013395#ifdef FEAT_RELTIME
13396 "reltime",
13397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398#ifdef FEAT_QUICKFIX
13399 "quickfix",
13400#endif
13401#ifdef FEAT_RIGHTLEFT
13402 "rightleft",
13403#endif
13404#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13405 "ruby",
13406#endif
13407#ifdef FEAT_SCROLLBIND
13408 "scrollbind",
13409#endif
13410#ifdef FEAT_CMDL_INFO
13411 "showcmd",
13412 "cmdline_info",
13413#endif
13414#ifdef FEAT_SIGNS
13415 "signs",
13416#endif
13417#ifdef FEAT_SMARTINDENT
13418 "smartindent",
13419#endif
13420#ifdef FEAT_SNIFF
13421 "sniff",
13422#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013423#ifdef STARTUPTIME
13424 "startuptime",
13425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426#ifdef FEAT_STL_OPT
13427 "statusline",
13428#endif
13429#ifdef FEAT_SUN_WORKSHOP
13430 "sun_workshop",
13431#endif
13432#ifdef FEAT_NETBEANS_INTG
13433 "netbeans_intg",
13434#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013435#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013436 "spell",
13437#endif
13438#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 "syntax",
13440#endif
13441#if defined(USE_SYSTEM) || !defined(UNIX)
13442 "system",
13443#endif
13444#ifdef FEAT_TAG_BINS
13445 "tag_binary",
13446#endif
13447#ifdef FEAT_TAG_OLDSTATIC
13448 "tag_old_static",
13449#endif
13450#ifdef FEAT_TAG_ANYWHITE
13451 "tag_any_white",
13452#endif
13453#ifdef FEAT_TCL
13454# ifndef DYNAMIC_TCL
13455 "tcl",
13456# endif
13457#endif
13458#ifdef TERMINFO
13459 "terminfo",
13460#endif
13461#ifdef FEAT_TERMRESPONSE
13462 "termresponse",
13463#endif
13464#ifdef FEAT_TEXTOBJ
13465 "textobjects",
13466#endif
13467#ifdef HAVE_TGETENT
13468 "tgetent",
13469#endif
13470#ifdef FEAT_TITLE
13471 "title",
13472#endif
13473#ifdef FEAT_TOOLBAR
13474 "toolbar",
13475#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013476#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13477 "unnamedplus",
13478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479#ifdef FEAT_USR_CMDS
13480 "user-commands", /* was accidentally included in 5.4 */
13481 "user_commands",
13482#endif
13483#ifdef FEAT_VIMINFO
13484 "viminfo",
13485#endif
13486#ifdef FEAT_VERTSPLIT
13487 "vertsplit",
13488#endif
13489#ifdef FEAT_VIRTUALEDIT
13490 "virtualedit",
13491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493#ifdef FEAT_VISUALEXTRA
13494 "visualextra",
13495#endif
13496#ifdef FEAT_VREPLACE
13497 "vreplace",
13498#endif
13499#ifdef FEAT_WILDIGN
13500 "wildignore",
13501#endif
13502#ifdef FEAT_WILDMENU
13503 "wildmenu",
13504#endif
13505#ifdef FEAT_WINDOWS
13506 "windows",
13507#endif
13508#ifdef FEAT_WAK
13509 "winaltkeys",
13510#endif
13511#ifdef FEAT_WRITEBACKUP
13512 "writebackup",
13513#endif
13514#ifdef FEAT_XIM
13515 "xim",
13516#endif
13517#ifdef FEAT_XFONTSET
13518 "xfontset",
13519#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013520#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013521 "xpm",
13522 "xpm_w32", /* for backward compatibility */
13523#else
13524# if defined(HAVE_XPM)
13525 "xpm",
13526# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528#ifdef USE_XSMP
13529 "xsmp",
13530#endif
13531#ifdef USE_XSMP_INTERACT
13532 "xsmp_interact",
13533#endif
13534#ifdef FEAT_XCLIPBOARD
13535 "xterm_clipboard",
13536#endif
13537#ifdef FEAT_XTERM_SAVE
13538 "xterm_save",
13539#endif
13540#if defined(UNIX) && defined(FEAT_X11)
13541 "X11",
13542#endif
13543 NULL
13544 };
13545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 for (i = 0; has_list[i] != NULL; ++i)
13548 if (STRICMP(name, has_list[i]) == 0)
13549 {
13550 n = TRUE;
13551 break;
13552 }
13553
13554 if (n == FALSE)
13555 {
13556 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013557 {
13558 if (name[5] == '-'
13559 && STRLEN(name) > 11
13560 && vim_isdigit(name[6])
13561 && vim_isdigit(name[8])
13562 && vim_isdigit(name[10]))
13563 {
13564 int major = atoi((char *)name + 6);
13565 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013566
13567 /* Expect "patch-9.9.01234". */
13568 n = (major < VIM_VERSION_MAJOR
13569 || (major == VIM_VERSION_MAJOR
13570 && (minor < VIM_VERSION_MINOR
13571 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013572 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013573 }
13574 else
13575 n = has_patch(atoi((char *)name + 5));
13576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 else if (STRICMP(name, "vim_starting") == 0)
13578 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013579#ifdef FEAT_MBYTE
13580 else if (STRICMP(name, "multi_byte_encoding") == 0)
13581 n = has_mbyte;
13582#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013583#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13584 else if (STRICMP(name, "balloon_multiline") == 0)
13585 n = multiline_balloon_available();
13586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587#ifdef DYNAMIC_TCL
13588 else if (STRICMP(name, "tcl") == 0)
13589 n = tcl_enabled(FALSE);
13590#endif
13591#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13592 else if (STRICMP(name, "iconv") == 0)
13593 n = iconv_enabled(FALSE);
13594#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013595#ifdef DYNAMIC_LUA
13596 else if (STRICMP(name, "lua") == 0)
13597 n = lua_enabled(FALSE);
13598#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013599#ifdef DYNAMIC_MZSCHEME
13600 else if (STRICMP(name, "mzscheme") == 0)
13601 n = mzscheme_enabled(FALSE);
13602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603#ifdef DYNAMIC_RUBY
13604 else if (STRICMP(name, "ruby") == 0)
13605 n = ruby_enabled(FALSE);
13606#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013607#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608#ifdef DYNAMIC_PYTHON
13609 else if (STRICMP(name, "python") == 0)
13610 n = python_enabled(FALSE);
13611#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013612#endif
13613#ifdef FEAT_PYTHON3
13614#ifdef DYNAMIC_PYTHON3
13615 else if (STRICMP(name, "python3") == 0)
13616 n = python3_enabled(FALSE);
13617#endif
13618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619#ifdef DYNAMIC_PERL
13620 else if (STRICMP(name, "perl") == 0)
13621 n = perl_enabled(FALSE);
13622#endif
13623#ifdef FEAT_GUI
13624 else if (STRICMP(name, "gui_running") == 0)
13625 n = (gui.in_use || gui.starting);
13626# ifdef FEAT_GUI_W32
13627 else if (STRICMP(name, "gui_win32s") == 0)
13628 n = gui_is_win32s();
13629# endif
13630# ifdef FEAT_BROWSE
13631 else if (STRICMP(name, "browse") == 0)
13632 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13633# endif
13634#endif
13635#ifdef FEAT_SYN_HL
13636 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013637 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638#endif
13639#if defined(WIN3264)
13640 else if (STRICMP(name, "win95") == 0)
13641 n = mch_windows95();
13642#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013643#ifdef FEAT_NETBEANS_INTG
13644 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013645 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 }
13648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650}
13651
13652/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013653 * "has_key()" function
13654 */
13655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013656f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013657{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013658 if (argvars[0].v_type != VAR_DICT)
13659 {
13660 EMSG(_(e_dictreq));
13661 return;
13662 }
13663 if (argvars[0].vval.v_dict == NULL)
13664 return;
13665
13666 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013667 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013668}
13669
13670/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013671 * "haslocaldir()" function
13672 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013674f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013675{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013676 win_T *wp = NULL;
13677
13678 wp = find_tabwin(&argvars[0], &argvars[1]);
13679 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013680}
13681
13682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 * "hasmapto()" function
13684 */
13685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013686f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687{
13688 char_u *name;
13689 char_u *mode;
13690 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013691 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013694 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 mode = (char_u *)"nvo";
13696 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013698 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013699 if (argvars[2].v_type != VAR_UNKNOWN)
13700 abbr = get_tv_number(&argvars[2]);
13701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702
Bram Moolenaar2c932302006-03-18 21:42:09 +000013703 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013704 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013706 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707}
13708
13709/*
13710 * "histadd()" function
13711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013713f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714{
13715#ifdef FEAT_CMDHIST
13716 int histype;
13717 char_u *str;
13718 char_u buf[NUMBUFLEN];
13719#endif
13720
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013721 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722 if (check_restricted() || check_secure())
13723 return;
13724#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013725 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13726 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 if (histype >= 0)
13728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 if (*str != NUL)
13731 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013732 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013734 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 return;
13736 }
13737 }
13738#endif
13739}
13740
13741/*
13742 * "histdel()" function
13743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013745f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746{
13747#ifdef FEAT_CMDHIST
13748 int n;
13749 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013750 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013752 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13753 if (str == NULL)
13754 n = 0;
13755 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013757 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013758 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013760 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013761 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 else
13763 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013764 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765 get_tv_string_buf(&argvars[1], buf));
13766 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767#endif
13768}
13769
13770/*
13771 * "histget()" function
13772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013774f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775{
13776#ifdef FEAT_CMDHIST
13777 int type;
13778 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013779 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013781 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13782 if (str == NULL)
13783 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013785 {
13786 type = get_histtype(str);
13787 if (argvars[1].v_type == VAR_UNKNOWN)
13788 idx = get_history_idx(type);
13789 else
13790 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13791 /* -1 on type error */
13792 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013795 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798}
13799
13800/*
13801 * "histnr()" function
13802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013804f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805{
13806 int i;
13807
13808#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013809 char_u *history = get_tv_string_chk(&argvars[0]);
13810
13811 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 if (i >= HIST_CMD && i < HIST_COUNT)
13813 i = get_history_idx(i);
13814 else
13815#endif
13816 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013817 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818}
13819
13820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 * "highlightID(name)" function
13822 */
13823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013824f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827}
13828
13829/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013830 * "highlight_exists()" function
13831 */
13832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013833f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013834{
13835 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13836}
13837
13838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 * "hostname()" function
13840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013842f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843{
13844 char_u hostname[256];
13845
13846 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013847 rettv->v_type = VAR_STRING;
13848 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849}
13850
13851/*
13852 * iconv() function
13853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013855f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856{
13857#ifdef FEAT_MBYTE
13858 char_u buf1[NUMBUFLEN];
13859 char_u buf2[NUMBUFLEN];
13860 char_u *from, *to, *str;
13861 vimconv_T vimconv;
13862#endif
13863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013864 rettv->v_type = VAR_STRING;
13865 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866
13867#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013868 str = get_tv_string(&argvars[0]);
13869 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13870 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 vimconv.vc_type = CONV_NONE;
13872 convert_setup(&vimconv, from, to);
13873
13874 /* If the encodings are equal, no conversion needed. */
13875 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013876 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013878 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879
13880 convert_setup(&vimconv, NULL, NULL);
13881 vim_free(from);
13882 vim_free(to);
13883#endif
13884}
13885
13886/*
13887 * "indent()" function
13888 */
13889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013890f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891{
13892 linenr_T lnum;
13893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013894 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013896 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013898 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899}
13900
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013901/*
13902 * "index()" function
13903 */
13904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013905f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013906{
Bram Moolenaar33570922005-01-25 22:26:29 +000013907 list_T *l;
13908 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013909 long idx = 0;
13910 int ic = FALSE;
13911
13912 rettv->vval.v_number = -1;
13913 if (argvars[0].v_type != VAR_LIST)
13914 {
13915 EMSG(_(e_listreq));
13916 return;
13917 }
13918 l = argvars[0].vval.v_list;
13919 if (l != NULL)
13920 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013921 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013922 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013924 int error = FALSE;
13925
Bram Moolenaar758711c2005-02-02 23:11:38 +000013926 /* Start at specified item. Use the cached index that list_find()
13927 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013928 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013929 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013930 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013931 ic = get_tv_number_chk(&argvars[3], &error);
13932 if (error)
13933 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013934 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013935
Bram Moolenaar758711c2005-02-02 23:11:38 +000013936 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013937 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013938 {
13939 rettv->vval.v_number = idx;
13940 break;
13941 }
13942 }
13943}
13944
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945static int inputsecret_flag = 0;
13946
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013947static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013948
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013950 * This function is used by f_input() and f_inputdialog() functions. The third
13951 * argument to f_input() specifies the type of completion to use at the
13952 * prompt. The third argument to f_inputdialog() specifies the value to return
13953 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 */
13955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013956get_user_input(
13957 typval_T *argvars,
13958 typval_T *rettv,
13959 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013961 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 char_u *p = NULL;
13963 int c;
13964 char_u buf[NUMBUFLEN];
13965 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013966 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013967 int xp_type = EXPAND_NOTHING;
13968 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013970 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013971 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972
13973#ifdef NO_CONSOLE_INPUT
13974 /* While starting up, there is no place to enter text. */
13975 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977#endif
13978
13979 cmd_silent = FALSE; /* Want to see the prompt. */
13980 if (prompt != NULL)
13981 {
13982 /* Only the part of the message after the last NL is considered as
13983 * prompt for the command line */
13984 p = vim_strrchr(prompt, '\n');
13985 if (p == NULL)
13986 p = prompt;
13987 else
13988 {
13989 ++p;
13990 c = *p;
13991 *p = NUL;
13992 msg_start();
13993 msg_clr_eos();
13994 msg_puts_attr(prompt, echo_attr);
13995 msg_didout = FALSE;
13996 msg_starthere();
13997 *p = c;
13998 }
13999 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014001 if (argvars[1].v_type != VAR_UNKNOWN)
14002 {
14003 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14004 if (defstr != NULL)
14005 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014007 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014008 {
14009 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014010 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014011 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014012
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014013 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014014 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014015
Bram Moolenaar4463f292005-09-25 22:20:24 +000014016 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14017 if (xp_name == NULL)
14018 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014019
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014020 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014021
Bram Moolenaar4463f292005-09-25 22:20:24 +000014022 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14023 &xp_arg) == FAIL)
14024 return;
14025 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014026 }
14027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014028 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014029 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014030 int save_ex_normal_busy = ex_normal_busy;
14031 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014032 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014033 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14034 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014035 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014036 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014037 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014038 && argvars[1].v_type != VAR_UNKNOWN
14039 && argvars[2].v_type != VAR_UNKNOWN)
14040 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14041 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014042
14043 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 /* since the user typed this, no need to wait for return */
14046 need_wait_return = FALSE;
14047 msg_didout = FALSE;
14048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049 cmd_silent = cmd_silent_save;
14050}
14051
14052/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014053 * "input()" function
14054 * Also handles inputsecret() when inputsecret is set.
14055 */
14056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014057f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014058{
14059 get_user_input(argvars, rettv, FALSE);
14060}
14061
14062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063 * "inputdialog()" function
14064 */
14065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014066f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067{
14068#if defined(FEAT_GUI_TEXTDIALOG)
14069 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14070 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14071 {
14072 char_u *message;
14073 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014074 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014076 message = get_tv_string_chk(&argvars[0]);
14077 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014078 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014079 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 else
14081 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014082 if (message != NULL && defstr != NULL
14083 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014084 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 else
14087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014088 if (message != NULL && defstr != NULL
14089 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014090 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014091 rettv->vval.v_string = vim_strsave(
14092 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014094 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014096 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 }
14098 else
14099#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014100 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101}
14102
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014103/*
14104 * "inputlist()" function
14105 */
14106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014107f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014108{
14109 listitem_T *li;
14110 int selected;
14111 int mouse_used;
14112
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014113#ifdef NO_CONSOLE_INPUT
14114 /* While starting up, there is no place to enter text. */
14115 if (no_console_input())
14116 return;
14117#endif
14118 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14119 {
14120 EMSG2(_(e_listarg), "inputlist()");
14121 return;
14122 }
14123
14124 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014125 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014126 lines_left = Rows; /* avoid more prompt */
14127 msg_scroll = TRUE;
14128 msg_clr_eos();
14129
14130 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14131 {
14132 msg_puts(get_tv_string(&li->li_tv));
14133 msg_putchar('\n');
14134 }
14135
14136 /* Ask for choice. */
14137 selected = prompt_for_number(&mouse_used);
14138 if (mouse_used)
14139 selected -= lines_left;
14140
14141 rettv->vval.v_number = selected;
14142}
14143
14144
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14146
14147/*
14148 * "inputrestore()" function
14149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014151f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152{
14153 if (ga_userinput.ga_len > 0)
14154 {
14155 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14157 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014158 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159 }
14160 else if (p_verbose > 1)
14161 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014162 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014163 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164 }
14165}
14166
14167/*
14168 * "inputsave()" function
14169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014171f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014173 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 if (ga_grow(&ga_userinput, 1) == OK)
14175 {
14176 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14177 + ga_userinput.ga_len);
14178 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014179 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 }
14181 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183}
14184
14185/*
14186 * "inputsecret()" function
14187 */
14188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014189f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190{
14191 ++cmdline_star;
14192 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014193 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 --cmdline_star;
14195 --inputsecret_flag;
14196}
14197
14198/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014199 * "insert()" function
14200 */
14201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014202f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014203{
14204 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014205 listitem_T *item;
14206 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014208
14209 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014210 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014211 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014212 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014213 {
14214 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014215 before = get_tv_number_chk(&argvars[2], &error);
14216 if (error)
14217 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014218
Bram Moolenaar758711c2005-02-02 23:11:38 +000014219 if (before == l->lv_len)
14220 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014221 else
14222 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014223 item = list_find(l, before);
14224 if (item == NULL)
14225 {
14226 EMSGN(_(e_listidx), before);
14227 l = NULL;
14228 }
14229 }
14230 if (l != NULL)
14231 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014232 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014233 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014234 }
14235 }
14236}
14237
14238/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014239 * "invert(expr)" function
14240 */
14241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014242f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014243{
14244 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14245}
14246
14247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 * "isdirectory()" function
14249 */
14250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014251f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014253 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254}
14255
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014256/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014257 * "islocked()" function
14258 */
14259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014260f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014261{
14262 lval_T lv;
14263 char_u *end;
14264 dictitem_T *di;
14265
14266 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014267 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14268 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014269 if (end != NULL && lv.ll_name != NULL)
14270 {
14271 if (*end != NUL)
14272 EMSG(_(e_trailing));
14273 else
14274 {
14275 if (lv.ll_tv == NULL)
14276 {
14277 if (check_changedtick(lv.ll_name))
14278 rettv->vval.v_number = 1; /* always locked */
14279 else
14280 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014281 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014282 if (di != NULL)
14283 {
14284 /* Consider a variable locked when:
14285 * 1. the variable itself is locked
14286 * 2. the value of the variable is locked.
14287 * 3. the List or Dict value is locked.
14288 */
14289 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14290 || tv_islocked(&di->di_tv));
14291 }
14292 }
14293 }
14294 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014295 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014296 else if (lv.ll_newkey != NULL)
14297 EMSG2(_(e_dictkey), lv.ll_newkey);
14298 else if (lv.ll_list != NULL)
14299 /* List item. */
14300 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14301 else
14302 /* Dictionary item. */
14303 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14304 }
14305 }
14306
14307 clear_lval(&lv);
14308}
14309
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014310static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014311
14312/*
14313 * Turn a dict into a list:
14314 * "what" == 0: list of keys
14315 * "what" == 1: list of values
14316 * "what" == 2: list of items
14317 */
14318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014319dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014320{
Bram Moolenaar33570922005-01-25 22:26:29 +000014321 list_T *l2;
14322 dictitem_T *di;
14323 hashitem_T *hi;
14324 listitem_T *li;
14325 listitem_T *li2;
14326 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014327 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014328
Bram Moolenaar8c711452005-01-14 21:53:12 +000014329 if (argvars[0].v_type != VAR_DICT)
14330 {
14331 EMSG(_(e_dictreq));
14332 return;
14333 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014334 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014335 return;
14336
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014337 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014338 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014339
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014340 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014341 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014342 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014343 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014345 --todo;
14346 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014347
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014348 li = listitem_alloc();
14349 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014350 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014351 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014352
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014353 if (what == 0)
14354 {
14355 /* keys() */
14356 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014357 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014358 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14359 }
14360 else if (what == 1)
14361 {
14362 /* values() */
14363 copy_tv(&di->di_tv, &li->li_tv);
14364 }
14365 else
14366 {
14367 /* items() */
14368 l2 = list_alloc();
14369 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014370 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014371 li->li_tv.vval.v_list = l2;
14372 if (l2 == NULL)
14373 break;
14374 ++l2->lv_refcount;
14375
14376 li2 = listitem_alloc();
14377 if (li2 == NULL)
14378 break;
14379 list_append(l2, li2);
14380 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014381 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014382 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14383
14384 li2 = listitem_alloc();
14385 if (li2 == NULL)
14386 break;
14387 list_append(l2, li2);
14388 copy_tv(&di->di_tv, &li2->li_tv);
14389 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014390 }
14391 }
14392}
14393
14394/*
14395 * "items(dict)" function
14396 */
14397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014398f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014399{
14400 dict_list(argvars, rettv, 2);
14401}
14402
Bram Moolenaar835dc632016-02-07 14:27:38 +010014403#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014404
14405# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014406/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014407 * "job_getchannel()" function
14408 */
14409 static void
14410f_job_getchannel(typval_T *argvars, typval_T *rettv)
14411{
14412 if (argvars[0].v_type != VAR_JOB)
14413 EMSG(_(e_invarg));
14414 else
14415 {
14416 job_T *job = argvars[0].vval.v_job;
14417
Bram Moolenaar77073442016-02-13 23:23:53 +010014418 rettv->v_type = VAR_CHANNEL;
14419 rettv->vval.v_channel = job->jv_channel;
14420 if (job->jv_channel != NULL)
14421 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014422 }
14423}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014424# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014425
14426/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014427 * "job_start()" function
14428 */
14429 static void
14430f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14431{
14432 job_T *job;
14433 char_u *cmd = NULL;
14434#if defined(UNIX)
14435# define USE_ARGV
14436 char **argv = NULL;
14437 int argc = 0;
14438#else
14439 garray_T ga;
14440#endif
14441
14442 rettv->v_type = VAR_JOB;
14443 job = job_alloc();
14444 rettv->vval.v_job = job;
14445 if (job == NULL)
14446 return;
14447
14448 rettv->vval.v_job->jv_status = JOB_FAILED;
14449#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014450 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014451#endif
14452
14453 if (argvars[0].v_type == VAR_STRING)
14454 {
14455 /* Command is a string. */
14456 cmd = argvars[0].vval.v_string;
14457#ifdef USE_ARGV
14458 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14459 return;
14460 argv[argc] = NULL;
14461#endif
14462 }
14463 else if (argvars[0].v_type != VAR_LIST
14464 || argvars[0].vval.v_list == NULL
14465 || argvars[0].vval.v_list->lv_len < 1)
14466 {
14467 EMSG(_(e_invarg));
14468 return;
14469 }
14470 else
14471 {
14472 list_T *l = argvars[0].vval.v_list;
14473 listitem_T *li;
14474 char_u *s;
14475
14476#ifdef USE_ARGV
14477 /* Pass argv[] to mch_call_shell(). */
14478 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14479 if (argv == NULL)
14480 return;
14481#endif
14482 for (li = l->lv_first; li != NULL; li = li->li_next)
14483 {
14484 s = get_tv_string_chk(&li->li_tv);
14485 if (s == NULL)
14486 goto theend;
14487#ifdef USE_ARGV
14488 argv[argc++] = (char *)s;
14489#else
14490 if (li != l->lv_first)
14491 {
14492 s = vim_strsave_shellescape(s, FALSE, TRUE);
14493 if (s == NULL)
14494 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014495 ga_concat(&ga, s);
14496 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014497 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014498 else
14499 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014500 if (li->li_next != NULL)
14501 ga_append(&ga, ' ');
14502#endif
14503 }
14504#ifdef USE_ARGV
14505 argv[argc] = NULL;
14506#else
14507 cmd = ga.ga_data;
14508#endif
14509 }
14510#ifdef USE_ARGV
14511 mch_start_job(argv, job);
14512#else
14513 mch_start_job(cmd, job);
14514#endif
14515
14516theend:
14517#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014518 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014519#else
14520 vim_free(ga.ga_data);
14521#endif
14522}
14523
14524/*
14525 * "job_status()" function
14526 */
14527 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014528f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014529{
14530 char *result;
14531
14532 if (argvars[0].v_type != VAR_JOB)
14533 EMSG(_(e_invarg));
14534 else
14535 {
14536 job_T *job = argvars[0].vval.v_job;
14537
14538 if (job->jv_status == JOB_ENDED)
14539 /* No need to check, dead is dead. */
14540 result = "dead";
14541 else if (job->jv_status == JOB_FAILED)
14542 result = "fail";
14543 else
14544 result = mch_job_status(job);
14545 rettv->v_type = VAR_STRING;
14546 rettv->vval.v_string = vim_strsave((char_u *)result);
14547 }
14548}
14549
14550/*
14551 * "job_stop()" function
14552 */
14553 static void
14554f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14555{
14556 if (argvars[0].v_type != VAR_JOB)
14557 EMSG(_(e_invarg));
14558 else
14559 {
14560 char_u *arg;
14561
14562 if (argvars[1].v_type == VAR_UNKNOWN)
14563 arg = (char_u *)"";
14564 else
14565 {
14566 arg = get_tv_string_chk(&argvars[1]);
14567 if (arg == NULL)
14568 {
14569 EMSG(_(e_invarg));
14570 return;
14571 }
14572 }
14573 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14574 rettv->vval.v_number = 0;
14575 else
14576 rettv->vval.v_number = 1;
14577 }
14578}
14579#endif
14580
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014582 * "join()" function
14583 */
14584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014585f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014586{
14587 garray_T ga;
14588 char_u *sep;
14589
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014590 if (argvars[0].v_type != VAR_LIST)
14591 {
14592 EMSG(_(e_listreq));
14593 return;
14594 }
14595 if (argvars[0].vval.v_list == NULL)
14596 return;
14597 if (argvars[1].v_type == VAR_UNKNOWN)
14598 sep = (char_u *)" ";
14599 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014600 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014601
14602 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014603
14604 if (sep != NULL)
14605 {
14606 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014607 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014608 ga_append(&ga, NUL);
14609 rettv->vval.v_string = (char_u *)ga.ga_data;
14610 }
14611 else
14612 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014613}
14614
14615/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014616 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014617 */
14618 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014619f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014620{
14621 js_read_T reader;
14622
14623 reader.js_buf = get_tv_string(&argvars[0]);
14624 reader.js_fill = NULL;
14625 reader.js_used = 0;
14626 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14627 EMSG(_(e_invarg));
14628}
14629
14630/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014631 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014632 */
14633 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014634f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014635{
14636 rettv->v_type = VAR_STRING;
14637 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14638}
14639
14640/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014641 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014642 */
14643 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014644f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014645{
14646 js_read_T reader;
14647
14648 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014649 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014650 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014651 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014652 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014653}
14654
14655/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014656 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014657 */
14658 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014659f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014660{
14661 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014662 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014663}
14664
14665/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014666 * "keys()" function
14667 */
14668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014669f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014670{
14671 dict_list(argvars, rettv, 0);
14672}
14673
14674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 * "last_buffer_nr()" function.
14676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014678f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679{
14680 int n = 0;
14681 buf_T *buf;
14682
14683 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14684 if (n < buf->b_fnum)
14685 n = buf->b_fnum;
14686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014688}
14689
14690/*
14691 * "len()" function
14692 */
14693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014694f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014695{
14696 switch (argvars[0].v_type)
14697 {
14698 case VAR_STRING:
14699 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014700 rettv->vval.v_number = (varnumber_T)STRLEN(
14701 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014702 break;
14703 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014704 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014705 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014706 case VAR_DICT:
14707 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14708 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014709 case VAR_UNKNOWN:
14710 case VAR_SPECIAL:
14711 case VAR_FLOAT:
14712 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014713 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014714 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014715 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014716 break;
14717 }
14718}
14719
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014720static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014721
14722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014723libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014724{
14725#ifdef FEAT_LIBCALL
14726 char_u *string_in;
14727 char_u **string_result;
14728 int nr_result;
14729#endif
14730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014731 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014732 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014733 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014734
14735 if (check_restricted() || check_secure())
14736 return;
14737
14738#ifdef FEAT_LIBCALL
14739 /* The first two args must be strings, otherwise its meaningless */
14740 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14741 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014742 string_in = NULL;
14743 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014744 string_in = argvars[2].vval.v_string;
14745 if (type == VAR_NUMBER)
14746 string_result = NULL;
14747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014749 if (mch_libcall(argvars[0].vval.v_string,
14750 argvars[1].vval.v_string,
14751 string_in,
14752 argvars[2].vval.v_number,
14753 string_result,
14754 &nr_result) == OK
14755 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014756 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014757 }
14758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759}
14760
14761/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014762 * "libcall()" function
14763 */
14764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014765f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014766{
14767 libcall_common(argvars, rettv, VAR_STRING);
14768}
14769
14770/*
14771 * "libcallnr()" function
14772 */
14773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014774f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014775{
14776 libcall_common(argvars, rettv, VAR_NUMBER);
14777}
14778
14779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780 * "line(string)" function
14781 */
14782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014783f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784{
14785 linenr_T lnum = 0;
14786 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014787 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014789 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 if (fp != NULL)
14791 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014792 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793}
14794
14795/*
14796 * "line2byte(lnum)" function
14797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014799f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800{
14801#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014802 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803#else
14804 linenr_T lnum;
14805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014806 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014807 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014810 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14811 if (rettv->vval.v_number >= 0)
14812 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813#endif
14814}
14815
14816/*
14817 * "lispindent(lnum)" function
14818 */
14819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014820f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821{
14822#ifdef FEAT_LISP
14823 pos_T pos;
14824 linenr_T lnum;
14825
14826 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14829 {
14830 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014831 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832 curwin->w_cursor = pos;
14833 }
14834 else
14835#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014836 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837}
14838
14839/*
14840 * "localtime()" function
14841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014843f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014845 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846}
14847
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014848static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849
14850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014851get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852{
14853 char_u *keys;
14854 char_u *which;
14855 char_u buf[NUMBUFLEN];
14856 char_u *keys_buf = NULL;
14857 char_u *rhs;
14858 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014859 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014860 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014861 mapblock_T *mp;
14862 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863
14864 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014865 rettv->v_type = VAR_STRING;
14866 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014868 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869 if (*keys == NUL)
14870 return;
14871
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014872 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014874 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014875 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014876 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014877 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014878 if (argvars[3].v_type != VAR_UNKNOWN)
14879 get_dict = get_tv_number(&argvars[3]);
14880 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 else
14883 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014884 if (which == NULL)
14885 return;
14886
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887 mode = get_map_mode(&which, 0);
14888
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014889 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014890 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014892
14893 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014895 /* Return a string. */
14896 if (rhs != NULL)
14897 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898
Bram Moolenaarbd743252010-10-20 21:23:33 +020014899 }
14900 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14901 {
14902 /* Return a dictionary. */
14903 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14904 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14905 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906
Bram Moolenaarbd743252010-10-20 21:23:33 +020014907 dict_add_nr_str(dict, "lhs", 0L, lhs);
14908 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14909 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14910 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14911 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14912 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14913 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014914 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014915 dict_add_nr_str(dict, "mode", 0L, mapmode);
14916
14917 vim_free(lhs);
14918 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 }
14920}
14921
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014922#ifdef FEAT_FLOAT
14923/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014924 * "log()" function
14925 */
14926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014927f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014928{
14929 float_T f;
14930
14931 rettv->v_type = VAR_FLOAT;
14932 if (get_float_arg(argvars, &f) == OK)
14933 rettv->vval.v_float = log(f);
14934 else
14935 rettv->vval.v_float = 0.0;
14936}
14937
14938/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014939 * "log10()" function
14940 */
14941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014942f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014943{
14944 float_T f;
14945
14946 rettv->v_type = VAR_FLOAT;
14947 if (get_float_arg(argvars, &f) == OK)
14948 rettv->vval.v_float = log10(f);
14949 else
14950 rettv->vval.v_float = 0.0;
14951}
14952#endif
14953
Bram Moolenaar1dced572012-04-05 16:54:08 +020014954#ifdef FEAT_LUA
14955/*
14956 * "luaeval()" function
14957 */
14958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014959f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014960{
14961 char_u *str;
14962 char_u buf[NUMBUFLEN];
14963
14964 str = get_tv_string_buf(&argvars[0], buf);
14965 do_luaeval(str, argvars + 1, rettv);
14966}
14967#endif
14968
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014970 * "map()" function
14971 */
14972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014973f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014974{
14975 filter_map(argvars, rettv, TRUE);
14976}
14977
14978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014979 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 */
14981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014982f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985}
14986
14987/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 */
14990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014991f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014993 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994}
14995
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014996static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997
14998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014999find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015001 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015002 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015003 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 char_u *pat;
15005 regmatch_T regmatch;
15006 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015007 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 char_u *save_cpo;
15009 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015010 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015011 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015012 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015013 list_T *l = NULL;
15014 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015015 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015016 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017
15018 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15019 save_cpo = p_cpo;
15020 p_cpo = (char_u *)"";
15021
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015022 rettv->vval.v_number = -1;
15023 if (type == 3)
15024 {
15025 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015026 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015027 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015028 }
15029 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015031 rettv->v_type = VAR_STRING;
15032 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015035 if (argvars[0].v_type == VAR_LIST)
15036 {
15037 if ((l = argvars[0].vval.v_list) == NULL)
15038 goto theend;
15039 li = l->lv_first;
15040 }
15041 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015042 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015043 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015044 len = (long)STRLEN(str);
15045 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015047 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15048 if (pat == NULL)
15049 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015050
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015051 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015053 int error = FALSE;
15054
15055 start = get_tv_number_chk(&argvars[2], &error);
15056 if (error)
15057 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015058 if (l != NULL)
15059 {
15060 li = list_find(l, start);
15061 if (li == NULL)
15062 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015063 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015064 }
15065 else
15066 {
15067 if (start < 0)
15068 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015069 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015070 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015071 /* When "count" argument is there ignore matches before "start",
15072 * otherwise skip part of the string. Differs when pattern is "^"
15073 * or "\<". */
15074 if (argvars[3].v_type != VAR_UNKNOWN)
15075 startcol = start;
15076 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015077 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015078 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015079 len -= start;
15080 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015081 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015082
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015083 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015084 nth = get_tv_number_chk(&argvars[3], &error);
15085 if (error)
15086 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087 }
15088
15089 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15090 if (regmatch.regprog != NULL)
15091 {
15092 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015093
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015094 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015095 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015096 if (l != NULL)
15097 {
15098 if (li == NULL)
15099 {
15100 match = FALSE;
15101 break;
15102 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015103 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015104 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015105 if (str == NULL)
15106 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015107 }
15108
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015109 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015110
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015111 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015112 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015113 if (l == NULL && !match)
15114 break;
15115
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015116 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015117 if (l != NULL)
15118 {
15119 li = li->li_next;
15120 ++idx;
15121 }
15122 else
15123 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015124#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015125 startcol = (colnr_T)(regmatch.startp[0]
15126 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015127#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015128 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015129#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015130 if (startcol > (colnr_T)len
15131 || str + startcol <= regmatch.startp[0])
15132 {
15133 match = FALSE;
15134 break;
15135 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015136 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015137 }
15138
15139 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015141 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015142 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015143 int i;
15144
15145 /* return list with matched string and submatches */
15146 for (i = 0; i < NSUBEXP; ++i)
15147 {
15148 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015149 {
15150 if (list_append_string(rettv->vval.v_list,
15151 (char_u *)"", 0) == FAIL)
15152 break;
15153 }
15154 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015155 regmatch.startp[i],
15156 (int)(regmatch.endp[i] - regmatch.startp[i]))
15157 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015158 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015159 }
15160 }
15161 else if (type == 2)
15162 {
15163 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015164 if (l != NULL)
15165 copy_tv(&li->li_tv, rettv);
15166 else
15167 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015169 }
15170 else if (l != NULL)
15171 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172 else
15173 {
15174 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015175 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176 (varnumber_T)(regmatch.startp[0] - str);
15177 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015178 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015180 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181 }
15182 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015183 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184 }
15185
15186theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015187 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015188 p_cpo = save_cpo;
15189}
15190
15191/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015192 * "match()" function
15193 */
15194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015195f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015196{
15197 find_some_match(argvars, rettv, 1);
15198}
15199
15200/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015201 * "matchadd()" function
15202 */
15203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015204f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015205{
15206#ifdef FEAT_SEARCH_EXTRA
15207 char_u buf[NUMBUFLEN];
15208 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15209 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15210 int prio = 10; /* default priority */
15211 int id = -1;
15212 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015213 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015214
15215 rettv->vval.v_number = -1;
15216
15217 if (grp == NULL || pat == NULL)
15218 return;
15219 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015220 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015221 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015222 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015223 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015224 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015225 if (argvars[4].v_type != VAR_UNKNOWN)
15226 {
15227 if (argvars[4].v_type != VAR_DICT)
15228 {
15229 EMSG(_(e_dictreq));
15230 return;
15231 }
15232 if (dict_find(argvars[4].vval.v_dict,
15233 (char_u *)"conceal", -1) != NULL)
15234 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15235 (char_u *)"conceal", FALSE);
15236 }
15237 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015238 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015239 if (error == TRUE)
15240 return;
15241 if (id >= 1 && id <= 3)
15242 {
15243 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15244 return;
15245 }
15246
Bram Moolenaar6561d522015-07-21 15:48:27 +020015247 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15248 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015249#endif
15250}
15251
15252/*
15253 * "matchaddpos()" function
15254 */
15255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015256f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015257{
15258#ifdef FEAT_SEARCH_EXTRA
15259 char_u buf[NUMBUFLEN];
15260 char_u *group;
15261 int prio = 10;
15262 int id = -1;
15263 int error = FALSE;
15264 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015265 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015266
15267 rettv->vval.v_number = -1;
15268
15269 group = get_tv_string_buf_chk(&argvars[0], buf);
15270 if (group == NULL)
15271 return;
15272
15273 if (argvars[1].v_type != VAR_LIST)
15274 {
15275 EMSG2(_(e_listarg), "matchaddpos()");
15276 return;
15277 }
15278 l = argvars[1].vval.v_list;
15279 if (l == NULL)
15280 return;
15281
15282 if (argvars[2].v_type != VAR_UNKNOWN)
15283 {
15284 prio = get_tv_number_chk(&argvars[2], &error);
15285 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015286 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015287 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015288 if (argvars[4].v_type != VAR_UNKNOWN)
15289 {
15290 if (argvars[4].v_type != VAR_DICT)
15291 {
15292 EMSG(_(e_dictreq));
15293 return;
15294 }
15295 if (dict_find(argvars[4].vval.v_dict,
15296 (char_u *)"conceal", -1) != NULL)
15297 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15298 (char_u *)"conceal", FALSE);
15299 }
15300 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015301 }
15302 if (error == TRUE)
15303 return;
15304
15305 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15306 if (id == 1 || id == 2)
15307 {
15308 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15309 return;
15310 }
15311
Bram Moolenaar6561d522015-07-21 15:48:27 +020015312 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15313 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015314#endif
15315}
15316
15317/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015318 * "matcharg()" function
15319 */
15320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015321f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015322{
15323 if (rettv_list_alloc(rettv) == OK)
15324 {
15325#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015326 int id = get_tv_number(&argvars[0]);
15327 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015328
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015329 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015330 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015331 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15332 {
15333 list_append_string(rettv->vval.v_list,
15334 syn_id2name(m->hlg_id), -1);
15335 list_append_string(rettv->vval.v_list, m->pattern, -1);
15336 }
15337 else
15338 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015339 list_append_string(rettv->vval.v_list, NULL, -1);
15340 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015341 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015342 }
15343#endif
15344 }
15345}
15346
15347/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015348 * "matchdelete()" function
15349 */
15350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015351f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015352{
15353#ifdef FEAT_SEARCH_EXTRA
15354 rettv->vval.v_number = match_delete(curwin,
15355 (int)get_tv_number(&argvars[0]), TRUE);
15356#endif
15357}
15358
15359/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015360 * "matchend()" function
15361 */
15362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015363f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015364{
15365 find_some_match(argvars, rettv, 0);
15366}
15367
15368/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015369 * "matchlist()" function
15370 */
15371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015372f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015373{
15374 find_some_match(argvars, rettv, 3);
15375}
15376
15377/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015378 * "matchstr()" function
15379 */
15380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015381f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015382{
15383 find_some_match(argvars, rettv, 2);
15384}
15385
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015386static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015387
15388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015389max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015390{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015391 long n = 0;
15392 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015393 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015394
15395 if (argvars[0].v_type == VAR_LIST)
15396 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015397 list_T *l;
15398 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015399
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015400 l = argvars[0].vval.v_list;
15401 if (l != NULL)
15402 {
15403 li = l->lv_first;
15404 if (li != NULL)
15405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015406 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015407 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015408 {
15409 li = li->li_next;
15410 if (li == NULL)
15411 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015412 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015413 if (domax ? i > n : i < n)
15414 n = i;
15415 }
15416 }
15417 }
15418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015419 else if (argvars[0].v_type == VAR_DICT)
15420 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015421 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015422 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015423 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015424 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015425
15426 d = argvars[0].vval.v_dict;
15427 if (d != NULL)
15428 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015429 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015430 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015431 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015432 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015433 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015434 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015435 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015436 if (first)
15437 {
15438 n = i;
15439 first = FALSE;
15440 }
15441 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015442 n = i;
15443 }
15444 }
15445 }
15446 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015447 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015448 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015449 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015450}
15451
15452/*
15453 * "max()" function
15454 */
15455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015456f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015457{
15458 max_min(argvars, rettv, TRUE);
15459}
15460
15461/*
15462 * "min()" function
15463 */
15464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015465f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015466{
15467 max_min(argvars, rettv, FALSE);
15468}
15469
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015470static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015471
15472/*
15473 * Create the directory in which "dir" is located, and higher levels when
15474 * needed.
15475 */
15476 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015477mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015478{
15479 char_u *p;
15480 char_u *updir;
15481 int r = FAIL;
15482
15483 /* Get end of directory name in "dir".
15484 * We're done when it's "/" or "c:/". */
15485 p = gettail_sep(dir);
15486 if (p <= get_past_head(dir))
15487 return OK;
15488
15489 /* If the directory exists we're done. Otherwise: create it.*/
15490 updir = vim_strnsave(dir, (int)(p - dir));
15491 if (updir == NULL)
15492 return FAIL;
15493 if (mch_isdir(updir))
15494 r = OK;
15495 else if (mkdir_recurse(updir, prot) == OK)
15496 r = vim_mkdir_emsg(updir, prot);
15497 vim_free(updir);
15498 return r;
15499}
15500
15501#ifdef vim_mkdir
15502/*
15503 * "mkdir()" function
15504 */
15505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015506f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015507{
15508 char_u *dir;
15509 char_u buf[NUMBUFLEN];
15510 int prot = 0755;
15511
15512 rettv->vval.v_number = FAIL;
15513 if (check_restricted() || check_secure())
15514 return;
15515
15516 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015517 if (*dir == NUL)
15518 rettv->vval.v_number = FAIL;
15519 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015520 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015521 if (*gettail(dir) == NUL)
15522 /* remove trailing slashes */
15523 *gettail_sep(dir) = NUL;
15524
15525 if (argvars[1].v_type != VAR_UNKNOWN)
15526 {
15527 if (argvars[2].v_type != VAR_UNKNOWN)
15528 prot = get_tv_number_chk(&argvars[2], NULL);
15529 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15530 mkdir_recurse(dir, prot);
15531 }
15532 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015533 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015534}
15535#endif
15536
Bram Moolenaar0d660222005-01-07 21:51:51 +000015537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 * "mode()" function
15539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015541f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015543 char_u buf[3];
15544
15545 buf[1] = NUL;
15546 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 if (VIsual_active)
15549 {
15550 if (VIsual_select)
15551 buf[0] = VIsual_mode + 's' - 'v';
15552 else
15553 buf[0] = VIsual_mode;
15554 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015555 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015556 || State == CONFIRM)
15557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015559 if (State == ASKMORE)
15560 buf[1] = 'm';
15561 else if (State == CONFIRM)
15562 buf[1] = '?';
15563 }
15564 else if (State == EXTERNCMD)
15565 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 else if (State & INSERT)
15567 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015568#ifdef FEAT_VREPLACE
15569 if (State & VREPLACE_FLAG)
15570 {
15571 buf[0] = 'R';
15572 buf[1] = 'v';
15573 }
15574 else
15575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576 if (State & REPLACE_FLAG)
15577 buf[0] = 'R';
15578 else
15579 buf[0] = 'i';
15580 }
15581 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015584 if (exmode_active)
15585 buf[1] = 'v';
15586 }
15587 else if (exmode_active)
15588 {
15589 buf[0] = 'c';
15590 buf[1] = 'e';
15591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015595 if (finish_op)
15596 buf[1] = 'o';
15597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015599 /* Clear out the minor mode when the argument is not a non-zero number or
15600 * non-empty string. */
15601 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015602 buf[1] = NUL;
15603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015604 rettv->vval.v_string = vim_strsave(buf);
15605 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606}
15607
Bram Moolenaar429fa852013-04-15 12:27:36 +020015608#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015609/*
15610 * "mzeval()" function
15611 */
15612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015613f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015614{
15615 char_u *str;
15616 char_u buf[NUMBUFLEN];
15617
15618 str = get_tv_string_buf(&argvars[0], buf);
15619 do_mzeval(str, rettv);
15620}
Bram Moolenaar75676462013-01-30 14:55:42 +010015621
15622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015623mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015624{
15625 typval_T argvars[3];
15626
15627 argvars[0].v_type = VAR_STRING;
15628 argvars[0].vval.v_string = name;
15629 copy_tv(args, &argvars[1]);
15630 argvars[2].v_type = VAR_UNKNOWN;
15631 f_call(argvars, rettv);
15632 clear_tv(&argvars[1]);
15633}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015634#endif
15635
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015637 * "nextnonblank()" function
15638 */
15639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015640f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015641{
15642 linenr_T lnum;
15643
15644 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015646 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015647 {
15648 lnum = 0;
15649 break;
15650 }
15651 if (*skipwhite(ml_get(lnum)) != NUL)
15652 break;
15653 }
15654 rettv->vval.v_number = lnum;
15655}
15656
15657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 * "nr2char()" function
15659 */
15660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015661f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662{
15663 char_u buf[NUMBUFLEN];
15664
15665#ifdef FEAT_MBYTE
15666 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015667 {
15668 int utf8 = 0;
15669
15670 if (argvars[1].v_type != VAR_UNKNOWN)
15671 utf8 = get_tv_number_chk(&argvars[1], NULL);
15672 if (utf8)
15673 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15674 else
15675 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 else
15678#endif
15679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015680 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 buf[1] = NUL;
15682 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015683 rettv->v_type = VAR_STRING;
15684 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015685}
15686
15687/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015688 * "or(expr, expr)" function
15689 */
15690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015691f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015692{
15693 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15694 | get_tv_number_chk(&argvars[1], NULL);
15695}
15696
15697/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015698 * "pathshorten()" function
15699 */
15700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015701f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015702{
15703 char_u *p;
15704
15705 rettv->v_type = VAR_STRING;
15706 p = get_tv_string_chk(&argvars[0]);
15707 if (p == NULL)
15708 rettv->vval.v_string = NULL;
15709 else
15710 {
15711 p = vim_strsave(p);
15712 rettv->vval.v_string = p;
15713 if (p != NULL)
15714 shorten_dir(p);
15715 }
15716}
15717
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015718#ifdef FEAT_PERL
15719/*
15720 * "perleval()" function
15721 */
15722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015723f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015724{
15725 char_u *str;
15726 char_u buf[NUMBUFLEN];
15727
15728 str = get_tv_string_buf(&argvars[0], buf);
15729 do_perleval(str, rettv);
15730}
15731#endif
15732
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015733#ifdef FEAT_FLOAT
15734/*
15735 * "pow()" function
15736 */
15737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015738f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015739{
15740 float_T fx, fy;
15741
15742 rettv->v_type = VAR_FLOAT;
15743 if (get_float_arg(argvars, &fx) == OK
15744 && get_float_arg(&argvars[1], &fy) == OK)
15745 rettv->vval.v_float = pow(fx, fy);
15746 else
15747 rettv->vval.v_float = 0.0;
15748}
15749#endif
15750
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015751/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015752 * "prevnonblank()" function
15753 */
15754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015755f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015756{
15757 linenr_T lnum;
15758
15759 lnum = get_tv_lnum(argvars);
15760 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15761 lnum = 0;
15762 else
15763 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15764 --lnum;
15765 rettv->vval.v_number = lnum;
15766}
15767
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015768/* This dummy va_list is here because:
15769 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15770 * - locally in the function results in a "used before set" warning
15771 * - using va_start() to initialize it gives "function with fixed args" error */
15772static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015773
Bram Moolenaar8c711452005-01-14 21:53:12 +000015774/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015775 * "printf()" function
15776 */
15777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015778f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015779{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015780 char_u buf[NUMBUFLEN];
15781 int len;
15782 char_u *s;
15783 int saved_did_emsg = did_emsg;
15784 char *fmt;
15785
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015786 rettv->v_type = VAR_STRING;
15787 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015788
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015789 /* Get the required length, allocate the buffer and do it for real. */
15790 did_emsg = FALSE;
15791 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15792 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15793 if (!did_emsg)
15794 {
15795 s = alloc(len + 1);
15796 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015797 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015798 rettv->vval.v_string = s;
15799 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015800 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015801 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015802 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015803}
15804
15805/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015806 * "pumvisible()" function
15807 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015809f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015810{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015811#ifdef FEAT_INS_EXPAND
15812 if (pum_visible())
15813 rettv->vval.v_number = 1;
15814#endif
15815}
15816
Bram Moolenaardb913952012-06-29 12:54:53 +020015817#ifdef FEAT_PYTHON3
15818/*
15819 * "py3eval()" function
15820 */
15821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015822f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015823{
15824 char_u *str;
15825 char_u buf[NUMBUFLEN];
15826
15827 str = get_tv_string_buf(&argvars[0], buf);
15828 do_py3eval(str, rettv);
15829}
15830#endif
15831
15832#ifdef FEAT_PYTHON
15833/*
15834 * "pyeval()" function
15835 */
15836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015837f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015838{
15839 char_u *str;
15840 char_u buf[NUMBUFLEN];
15841
15842 str = get_tv_string_buf(&argvars[0], buf);
15843 do_pyeval(str, rettv);
15844}
15845#endif
15846
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015847/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015848 * "range()" function
15849 */
15850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015851f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015852{
15853 long start;
15854 long end;
15855 long stride = 1;
15856 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015857 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015859 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015860 if (argvars[1].v_type == VAR_UNKNOWN)
15861 {
15862 end = start - 1;
15863 start = 0;
15864 }
15865 else
15866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015867 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015868 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015869 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015870 }
15871
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015872 if (error)
15873 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015874 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015875 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015876 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015877 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015878 else
15879 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015880 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015881 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015882 if (list_append_number(rettv->vval.v_list,
15883 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015884 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015885 }
15886}
15887
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015888/*
15889 * "readfile()" function
15890 */
15891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015892f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015893{
15894 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015895 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015896 char_u *fname;
15897 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015898 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15899 int io_size = sizeof(buf);
15900 int readlen; /* size of last fread() */
15901 char_u *prev = NULL; /* previously read bytes, if any */
15902 long prevlen = 0; /* length of data in prev */
15903 long prevsize = 0; /* size of prev buffer */
15904 long maxline = MAXLNUM;
15905 long cnt = 0;
15906 char_u *p; /* position in buf */
15907 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015908
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015909 if (argvars[1].v_type != VAR_UNKNOWN)
15910 {
15911 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15912 binary = TRUE;
15913 if (argvars[2].v_type != VAR_UNKNOWN)
15914 maxline = get_tv_number(&argvars[2]);
15915 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015916
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015917 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015918 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015919
15920 /* Always open the file in binary mode, library functions have a mind of
15921 * their own about CR-LF conversion. */
15922 fname = get_tv_string(&argvars[0]);
15923 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15924 {
15925 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15926 return;
15927 }
15928
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015929 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015930 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015931 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015932
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015933 /* This for loop processes what was read, but is also entered at end
15934 * of file so that either:
15935 * - an incomplete line gets written
15936 * - a "binary" file gets an empty line at the end if it ends in a
15937 * newline. */
15938 for (p = buf, start = buf;
15939 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15940 ++p)
15941 {
15942 if (*p == '\n' || readlen <= 0)
15943 {
15944 listitem_T *li;
15945 char_u *s = NULL;
15946 long_u len = p - start;
15947
15948 /* Finished a line. Remove CRs before NL. */
15949 if (readlen > 0 && !binary)
15950 {
15951 while (len > 0 && start[len - 1] == '\r')
15952 --len;
15953 /* removal may cross back to the "prev" string */
15954 if (len == 0)
15955 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15956 --prevlen;
15957 }
15958 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015959 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015960 else
15961 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015962 /* Change "prev" buffer to be the right size. This way
15963 * the bytes are only copied once, and very long lines are
15964 * allocated only once. */
15965 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015966 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015967 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015968 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015969 prev = NULL; /* the list will own the string */
15970 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015971 }
15972 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015973 if (s == NULL)
15974 {
15975 do_outofmem_msg((long_u) prevlen + len + 1);
15976 failed = TRUE;
15977 break;
15978 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015979
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015980 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015981 {
15982 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015983 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015984 break;
15985 }
15986 li->li_tv.v_type = VAR_STRING;
15987 li->li_tv.v_lock = 0;
15988 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015989 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015990
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015991 start = p + 1; /* step over newline */
15992 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015993 break;
15994 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015995 else if (*p == NUL)
15996 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015997#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015998 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15999 * when finding the BF and check the previous two bytes. */
16000 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016001 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016002 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16003 * + 1, these may be in the "prev" string. */
16004 char_u back1 = p >= buf + 1 ? p[-1]
16005 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16006 char_u back2 = p >= buf + 2 ? p[-2]
16007 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16008 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016009
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016010 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016011 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016012 char_u *dest = p - 2;
16013
16014 /* Usually a BOM is at the beginning of a file, and so at
16015 * the beginning of a line; then we can just step over it.
16016 */
16017 if (start == dest)
16018 start = p + 1;
16019 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016020 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016021 /* have to shuffle buf to close gap */
16022 int adjust_prevlen = 0;
16023
16024 if (dest < buf)
16025 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016026 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016027 dest = buf;
16028 }
16029 if (readlen > p - buf + 1)
16030 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16031 readlen -= 3 - adjust_prevlen;
16032 prevlen -= adjust_prevlen;
16033 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016034 }
16035 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016036 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016037#endif
16038 } /* for */
16039
16040 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16041 break;
16042 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016043 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016044 /* There's part of a line in buf, store it in "prev". */
16045 if (p - start + prevlen >= prevsize)
16046 {
16047 /* need bigger "prev" buffer */
16048 char_u *newprev;
16049
16050 /* A common use case is ordinary text files and "prev" gets a
16051 * fragment of a line, so the first allocation is made
16052 * small, to avoid repeatedly 'allocing' large and
16053 * 'reallocing' small. */
16054 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016055 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016056 else
16057 {
16058 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016059 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016060 prevsize = grow50pc > growmin ? grow50pc : growmin;
16061 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016062 newprev = prev == NULL ? alloc(prevsize)
16063 : vim_realloc(prev, prevsize);
16064 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016065 {
16066 do_outofmem_msg((long_u)prevsize);
16067 failed = TRUE;
16068 break;
16069 }
16070 prev = newprev;
16071 }
16072 /* Add the line part to end of "prev". */
16073 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016074 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016075 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016076 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016077
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016078 /*
16079 * For a negative line count use only the lines at the end of the file,
16080 * free the rest.
16081 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016082 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016083 while (cnt > -maxline)
16084 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016085 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016086 --cnt;
16087 }
16088
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016089 if (failed)
16090 {
16091 list_free(rettv->vval.v_list, TRUE);
16092 /* readfile doc says an empty list is returned on error */
16093 rettv->vval.v_list = list_alloc();
16094 }
16095
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016096 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016097 fclose(fd);
16098}
16099
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016100#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016101static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016102
16103/*
16104 * Convert a List to proftime_T.
16105 * Return FAIL when there is something wrong.
16106 */
16107 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016108list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016109{
16110 long n1, n2;
16111 int error = FALSE;
16112
16113 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16114 || arg->vval.v_list->lv_len != 2)
16115 return FAIL;
16116 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16117 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16118# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016119 tm->HighPart = n1;
16120 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016121# else
16122 tm->tv_sec = n1;
16123 tm->tv_usec = n2;
16124# endif
16125 return error ? FAIL : OK;
16126}
16127#endif /* FEAT_RELTIME */
16128
16129/*
16130 * "reltime()" function
16131 */
16132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016133f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016134{
16135#ifdef FEAT_RELTIME
16136 proftime_T res;
16137 proftime_T start;
16138
16139 if (argvars[0].v_type == VAR_UNKNOWN)
16140 {
16141 /* No arguments: get current time. */
16142 profile_start(&res);
16143 }
16144 else if (argvars[1].v_type == VAR_UNKNOWN)
16145 {
16146 if (list2proftime(&argvars[0], &res) == FAIL)
16147 return;
16148 profile_end(&res);
16149 }
16150 else
16151 {
16152 /* Two arguments: compute the difference. */
16153 if (list2proftime(&argvars[0], &start) == FAIL
16154 || list2proftime(&argvars[1], &res) == FAIL)
16155 return;
16156 profile_sub(&res, &start);
16157 }
16158
16159 if (rettv_list_alloc(rettv) == OK)
16160 {
16161 long n1, n2;
16162
16163# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016164 n1 = res.HighPart;
16165 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016166# else
16167 n1 = res.tv_sec;
16168 n2 = res.tv_usec;
16169# endif
16170 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16171 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16172 }
16173#endif
16174}
16175
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016176#ifdef FEAT_FLOAT
16177/*
16178 * "reltimefloat()" function
16179 */
16180 static void
16181f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16182{
16183# ifdef FEAT_RELTIME
16184 proftime_T tm;
16185# endif
16186
16187 rettv->v_type = VAR_FLOAT;
16188 rettv->vval.v_float = 0;
16189# ifdef FEAT_RELTIME
16190 if (list2proftime(&argvars[0], &tm) == OK)
16191 rettv->vval.v_float = profile_float(&tm);
16192# endif
16193}
16194#endif
16195
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016196/*
16197 * "reltimestr()" function
16198 */
16199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016200f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016201{
16202#ifdef FEAT_RELTIME
16203 proftime_T tm;
16204#endif
16205
16206 rettv->v_type = VAR_STRING;
16207 rettv->vval.v_string = NULL;
16208#ifdef FEAT_RELTIME
16209 if (list2proftime(&argvars[0], &tm) == OK)
16210 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16211#endif
16212}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016213
Bram Moolenaar0d660222005-01-07 21:51:51 +000016214#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016215static void make_connection(void);
16216static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016217
16218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016219make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016220{
16221 if (X_DISPLAY == NULL
16222# ifdef FEAT_GUI
16223 && !gui.in_use
16224# endif
16225 )
16226 {
16227 x_force_connect = TRUE;
16228 setup_term_clip();
16229 x_force_connect = FALSE;
16230 }
16231}
16232
16233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016234check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016235{
16236 make_connection();
16237 if (X_DISPLAY == NULL)
16238 {
16239 EMSG(_("E240: No connection to Vim server"));
16240 return FAIL;
16241 }
16242 return OK;
16243}
16244#endif
16245
16246#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016247static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016248
16249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016250remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016251{
16252 char_u *server_name;
16253 char_u *keys;
16254 char_u *r = NULL;
16255 char_u buf[NUMBUFLEN];
16256# ifdef WIN32
16257 HWND w;
16258# else
16259 Window w;
16260# endif
16261
16262 if (check_restricted() || check_secure())
16263 return;
16264
16265# ifdef FEAT_X11
16266 if (check_connection() == FAIL)
16267 return;
16268# endif
16269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016270 server_name = get_tv_string_chk(&argvars[0]);
16271 if (server_name == NULL)
16272 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016273 keys = get_tv_string_buf(&argvars[1], buf);
16274# ifdef WIN32
16275 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16276# else
16277 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16278 < 0)
16279# endif
16280 {
16281 if (r != NULL)
16282 EMSG(r); /* sending worked but evaluation failed */
16283 else
16284 EMSG2(_("E241: Unable to send to %s"), server_name);
16285 return;
16286 }
16287
16288 rettv->vval.v_string = r;
16289
16290 if (argvars[2].v_type != VAR_UNKNOWN)
16291 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016292 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016293 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016294 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016296 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016297 v.di_tv.v_type = VAR_STRING;
16298 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016299 idvar = get_tv_string_chk(&argvars[2]);
16300 if (idvar != NULL)
16301 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016302 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016303 }
16304}
16305#endif
16306
16307/*
16308 * "remote_expr()" function
16309 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016311f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312{
16313 rettv->v_type = VAR_STRING;
16314 rettv->vval.v_string = NULL;
16315#ifdef FEAT_CLIENTSERVER
16316 remote_common(argvars, rettv, TRUE);
16317#endif
16318}
16319
16320/*
16321 * "remote_foreground()" function
16322 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016324f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016325{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016326#ifdef FEAT_CLIENTSERVER
16327# ifdef WIN32
16328 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016329 {
16330 char_u *server_name = get_tv_string_chk(&argvars[0]);
16331
16332 if (server_name != NULL)
16333 serverForeground(server_name);
16334 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016335# else
16336 /* Send a foreground() expression to the server. */
16337 argvars[1].v_type = VAR_STRING;
16338 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16339 argvars[2].v_type = VAR_UNKNOWN;
16340 remote_common(argvars, rettv, TRUE);
16341 vim_free(argvars[1].vval.v_string);
16342# endif
16343#endif
16344}
16345
Bram Moolenaar0d660222005-01-07 21:51:51 +000016346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016347f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016348{
16349#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016350 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351 char_u *s = NULL;
16352# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016353 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016354# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016355 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016356
16357 if (check_restricted() || check_secure())
16358 {
16359 rettv->vval.v_number = -1;
16360 return;
16361 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016362 serverid = get_tv_string_chk(&argvars[0]);
16363 if (serverid == NULL)
16364 {
16365 rettv->vval.v_number = -1;
16366 return; /* type error; errmsg already given */
16367 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016368# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016369 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016370 if (n == 0)
16371 rettv->vval.v_number = -1;
16372 else
16373 {
16374 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16375 rettv->vval.v_number = (s != NULL);
16376 }
16377# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016378 if (check_connection() == FAIL)
16379 return;
16380
16381 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016382 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383# endif
16384
16385 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016387 char_u *retvar;
16388
Bram Moolenaar33570922005-01-25 22:26:29 +000016389 v.di_tv.v_type = VAR_STRING;
16390 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016391 retvar = get_tv_string_chk(&argvars[1]);
16392 if (retvar != NULL)
16393 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016394 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016395 }
16396#else
16397 rettv->vval.v_number = -1;
16398#endif
16399}
16400
Bram Moolenaar0d660222005-01-07 21:51:51 +000016401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016402f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016403{
16404 char_u *r = NULL;
16405
16406#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016407 char_u *serverid = get_tv_string_chk(&argvars[0]);
16408
16409 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410 {
16411# ifdef WIN32
16412 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016413 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016414
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016415 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016416 if (n != 0)
16417 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16418 if (r == NULL)
16419# else
16420 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016421 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016422# endif
16423 EMSG(_("E277: Unable to read a server reply"));
16424 }
16425#endif
16426 rettv->v_type = VAR_STRING;
16427 rettv->vval.v_string = r;
16428}
16429
16430/*
16431 * "remote_send()" function
16432 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016434f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016435{
16436 rettv->v_type = VAR_STRING;
16437 rettv->vval.v_string = NULL;
16438#ifdef FEAT_CLIENTSERVER
16439 remote_common(argvars, rettv, FALSE);
16440#endif
16441}
16442
16443/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016444 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016445 */
16446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016447f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016448{
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 list_T *l;
16450 listitem_T *item, *item2;
16451 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016452 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016453 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016454 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016455 dict_T *d;
16456 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016457 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016458
Bram Moolenaar8c711452005-01-14 21:53:12 +000016459 if (argvars[0].v_type == VAR_DICT)
16460 {
16461 if (argvars[2].v_type != VAR_UNKNOWN)
16462 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016463 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016464 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016466 key = get_tv_string_chk(&argvars[1]);
16467 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016468 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016469 di = dict_find(d, key, -1);
16470 if (di == NULL)
16471 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016472 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16473 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016474 {
16475 *rettv = di->di_tv;
16476 init_tv(&di->di_tv);
16477 dictitem_remove(d, di);
16478 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016479 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016480 }
16481 }
16482 else if (argvars[0].v_type != VAR_LIST)
16483 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016484 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016485 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016487 int error = FALSE;
16488
16489 idx = get_tv_number_chk(&argvars[1], &error);
16490 if (error)
16491 ; /* type error: do nothing, errmsg already given */
16492 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016493 EMSGN(_(e_listidx), idx);
16494 else
16495 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016496 if (argvars[2].v_type == VAR_UNKNOWN)
16497 {
16498 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016499 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016500 *rettv = item->li_tv;
16501 vim_free(item);
16502 }
16503 else
16504 {
16505 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016506 end = get_tv_number_chk(&argvars[2], &error);
16507 if (error)
16508 ; /* type error: do nothing */
16509 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016510 EMSGN(_(e_listidx), end);
16511 else
16512 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016513 int cnt = 0;
16514
16515 for (li = item; li != NULL; li = li->li_next)
16516 {
16517 ++cnt;
16518 if (li == item2)
16519 break;
16520 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016521 if (li == NULL) /* didn't find "item2" after "item" */
16522 EMSG(_(e_invrange));
16523 else
16524 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016525 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016526 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016527 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016528 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016529 l->lv_first = item;
16530 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016531 item->li_prev = NULL;
16532 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016533 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016534 }
16535 }
16536 }
16537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016538 }
16539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540}
16541
16542/*
16543 * "rename({from}, {to})" function
16544 */
16545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016546f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547{
16548 char_u buf[NUMBUFLEN];
16549
16550 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016551 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016553 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16554 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555}
16556
16557/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016558 * "repeat()" function
16559 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016561f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016562{
16563 char_u *p;
16564 int n;
16565 int slen;
16566 int len;
16567 char_u *r;
16568 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016569
16570 n = get_tv_number(&argvars[1]);
16571 if (argvars[0].v_type == VAR_LIST)
16572 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016573 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016574 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016575 if (list_extend(rettv->vval.v_list,
16576 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016577 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016578 }
16579 else
16580 {
16581 p = get_tv_string(&argvars[0]);
16582 rettv->v_type = VAR_STRING;
16583 rettv->vval.v_string = NULL;
16584
16585 slen = (int)STRLEN(p);
16586 len = slen * n;
16587 if (len <= 0)
16588 return;
16589
16590 r = alloc(len + 1);
16591 if (r != NULL)
16592 {
16593 for (i = 0; i < n; i++)
16594 mch_memmove(r + i * slen, p, (size_t)slen);
16595 r[len] = NUL;
16596 }
16597
16598 rettv->vval.v_string = r;
16599 }
16600}
16601
16602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603 * "resolve()" function
16604 */
16605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016606f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607{
16608 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016609#ifdef HAVE_READLINK
16610 char_u *buf = NULL;
16611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016613 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614#ifdef FEAT_SHORTCUT
16615 {
16616 char_u *v = NULL;
16617
16618 v = mch_resolve_shortcut(p);
16619 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016620 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016622 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 }
16624#else
16625# ifdef HAVE_READLINK
16626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 char_u *cpy;
16628 int len;
16629 char_u *remain = NULL;
16630 char_u *q;
16631 int is_relative_to_current = FALSE;
16632 int has_trailing_pathsep = FALSE;
16633 int limit = 100;
16634
16635 p = vim_strsave(p);
16636
16637 if (p[0] == '.' && (vim_ispathsep(p[1])
16638 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16639 is_relative_to_current = TRUE;
16640
16641 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016642 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016645 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647
16648 q = getnextcomp(p);
16649 if (*q != NUL)
16650 {
16651 /* Separate the first path component in "p", and keep the
16652 * remainder (beginning with the path separator). */
16653 remain = vim_strsave(q - 1);
16654 q[-1] = NUL;
16655 }
16656
Bram Moolenaard9462e32011-04-11 21:35:11 +020016657 buf = alloc(MAXPATHL + 1);
16658 if (buf == NULL)
16659 goto fail;
16660
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661 for (;;)
16662 {
16663 for (;;)
16664 {
16665 len = readlink((char *)p, (char *)buf, MAXPATHL);
16666 if (len <= 0)
16667 break;
16668 buf[len] = NUL;
16669
16670 if (limit-- == 0)
16671 {
16672 vim_free(p);
16673 vim_free(remain);
16674 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016675 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 goto fail;
16677 }
16678
16679 /* Ensure that the result will have a trailing path separator
16680 * if the argument has one. */
16681 if (remain == NULL && has_trailing_pathsep)
16682 add_pathsep(buf);
16683
16684 /* Separate the first path component in the link value and
16685 * concatenate the remainders. */
16686 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16687 if (*q != NUL)
16688 {
16689 if (remain == NULL)
16690 remain = vim_strsave(q - 1);
16691 else
16692 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016693 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694 if (cpy != NULL)
16695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 vim_free(remain);
16697 remain = cpy;
16698 }
16699 }
16700 q[-1] = NUL;
16701 }
16702
16703 q = gettail(p);
16704 if (q > p && *q == NUL)
16705 {
16706 /* Ignore trailing path separator. */
16707 q[-1] = NUL;
16708 q = gettail(p);
16709 }
16710 if (q > p && !mch_isFullName(buf))
16711 {
16712 /* symlink is relative to directory of argument */
16713 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16714 if (cpy != NULL)
16715 {
16716 STRCPY(cpy, p);
16717 STRCPY(gettail(cpy), buf);
16718 vim_free(p);
16719 p = cpy;
16720 }
16721 }
16722 else
16723 {
16724 vim_free(p);
16725 p = vim_strsave(buf);
16726 }
16727 }
16728
16729 if (remain == NULL)
16730 break;
16731
16732 /* Append the first path component of "remain" to "p". */
16733 q = getnextcomp(remain + 1);
16734 len = q - remain - (*q != NUL);
16735 cpy = vim_strnsave(p, STRLEN(p) + len);
16736 if (cpy != NULL)
16737 {
16738 STRNCAT(cpy, remain, len);
16739 vim_free(p);
16740 p = cpy;
16741 }
16742 /* Shorten "remain". */
16743 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016744 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 else
16746 {
16747 vim_free(remain);
16748 remain = NULL;
16749 }
16750 }
16751
16752 /* If the result is a relative path name, make it explicitly relative to
16753 * the current directory if and only if the argument had this form. */
16754 if (!vim_ispathsep(*p))
16755 {
16756 if (is_relative_to_current
16757 && *p != NUL
16758 && !(p[0] == '.'
16759 && (p[1] == NUL
16760 || vim_ispathsep(p[1])
16761 || (p[1] == '.'
16762 && (p[2] == NUL
16763 || vim_ispathsep(p[2]))))))
16764 {
16765 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016766 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 if (cpy != NULL)
16768 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 vim_free(p);
16770 p = cpy;
16771 }
16772 }
16773 else if (!is_relative_to_current)
16774 {
16775 /* Strip leading "./". */
16776 q = p;
16777 while (q[0] == '.' && vim_ispathsep(q[1]))
16778 q += 2;
16779 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016780 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781 }
16782 }
16783
16784 /* Ensure that the result will have no trailing path separator
16785 * if the argument had none. But keep "/" or "//". */
16786 if (!has_trailing_pathsep)
16787 {
16788 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016789 if (after_pathsep(p, q))
16790 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 }
16792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016793 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 }
16795# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016796 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797# endif
16798#endif
16799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016800 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801
16802#ifdef HAVE_READLINK
16803fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016804 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016806 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807}
16808
16809/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016810 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811 */
16812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016813f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814{
Bram Moolenaar33570922005-01-25 22:26:29 +000016815 list_T *l;
16816 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817
Bram Moolenaar0d660222005-01-07 21:51:51 +000016818 if (argvars[0].v_type != VAR_LIST)
16819 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016820 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016821 && !tv_check_lock(l->lv_lock,
16822 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016823 {
16824 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016825 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016826 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016827 while (li != NULL)
16828 {
16829 ni = li->li_prev;
16830 list_append(l, li);
16831 li = ni;
16832 }
16833 rettv->vval.v_list = l;
16834 rettv->v_type = VAR_LIST;
16835 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016836 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838}
16839
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016840#define SP_NOMOVE 0x01 /* don't move cursor */
16841#define SP_REPEAT 0x02 /* repeat to find outer pair */
16842#define SP_RETCOUNT 0x04 /* return matchcount */
16843#define SP_SETPCMARK 0x08 /* set previous context mark */
16844#define SP_START 0x10 /* accept match at start position */
16845#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16846#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016847#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016848
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016849static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016850
16851/*
16852 * Get flags for a search function.
16853 * Possibly sets "p_ws".
16854 * Returns BACKWARD, FORWARD or zero (for an error).
16855 */
16856 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016857get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016858{
16859 int dir = FORWARD;
16860 char_u *flags;
16861 char_u nbuf[NUMBUFLEN];
16862 int mask;
16863
16864 if (varp->v_type != VAR_UNKNOWN)
16865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016866 flags = get_tv_string_buf_chk(varp, nbuf);
16867 if (flags == NULL)
16868 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016869 while (*flags != NUL)
16870 {
16871 switch (*flags)
16872 {
16873 case 'b': dir = BACKWARD; break;
16874 case 'w': p_ws = TRUE; break;
16875 case 'W': p_ws = FALSE; break;
16876 default: mask = 0;
16877 if (flagsp != NULL)
16878 switch (*flags)
16879 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016880 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016881 case 'e': mask = SP_END; break;
16882 case 'm': mask = SP_RETCOUNT; break;
16883 case 'n': mask = SP_NOMOVE; break;
16884 case 'p': mask = SP_SUBPAT; break;
16885 case 'r': mask = SP_REPEAT; break;
16886 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016887 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016888 }
16889 if (mask == 0)
16890 {
16891 EMSG2(_(e_invarg2), flags);
16892 dir = 0;
16893 }
16894 else
16895 *flagsp |= mask;
16896 }
16897 if (dir == 0)
16898 break;
16899 ++flags;
16900 }
16901 }
16902 return dir;
16903}
16904
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016906 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016908 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016909search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016911 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912 char_u *pat;
16913 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016914 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 int save_p_ws = p_ws;
16916 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016917 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016918 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016919 proftime_T tm;
16920#ifdef FEAT_RELTIME
16921 long time_limit = 0;
16922#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016923 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016924 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016926 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016927 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016928 if (dir == 0)
16929 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016930 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016931 if (flags & SP_START)
16932 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016933 if (flags & SP_END)
16934 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016935 if (flags & SP_COLUMN)
16936 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016937
Bram Moolenaar76929292008-01-06 19:07:36 +000016938 /* Optional arguments: line number to stop searching and timeout. */
16939 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016940 {
16941 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16942 if (lnum_stop < 0)
16943 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016944#ifdef FEAT_RELTIME
16945 if (argvars[3].v_type != VAR_UNKNOWN)
16946 {
16947 time_limit = get_tv_number_chk(&argvars[3], NULL);
16948 if (time_limit < 0)
16949 goto theend;
16950 }
16951#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016952 }
16953
Bram Moolenaar76929292008-01-06 19:07:36 +000016954#ifdef FEAT_RELTIME
16955 /* Set the time limit, if there is one. */
16956 profile_setlimit(time_limit, &tm);
16957#endif
16958
Bram Moolenaar231334e2005-07-25 20:46:57 +000016959 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016960 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016961 * Check to make sure only those flags are set.
16962 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16963 * flags cannot be set. Check for that condition also.
16964 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016965 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016966 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016968 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016969 goto theend;
16970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016972 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016973 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016974 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016975 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016977 if (flags & SP_SUBPAT)
16978 retval = subpatnum;
16979 else
16980 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016981 if (flags & SP_SETPCMARK)
16982 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016984 if (match_pos != NULL)
16985 {
16986 /* Store the match cursor position */
16987 match_pos->lnum = pos.lnum;
16988 match_pos->col = pos.col + 1;
16989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 /* "/$" will put the cursor after the end of the line, may need to
16991 * correct that here */
16992 check_cursor();
16993 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016994
16995 /* If 'n' flag is used: restore cursor position. */
16996 if (flags & SP_NOMOVE)
16997 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016998 else
16999 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017000theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017002
17003 return retval;
17004}
17005
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017006#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017007
17008/*
17009 * round() is not in C90, use ceil() or floor() instead.
17010 */
17011 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017012vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017013{
17014 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17015}
17016
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017017/*
17018 * "round({float})" function
17019 */
17020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017021f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017022{
17023 float_T f;
17024
17025 rettv->v_type = VAR_FLOAT;
17026 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017027 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017028 else
17029 rettv->vval.v_float = 0.0;
17030}
17031#endif
17032
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017033/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017034 * "screenattr()" function
17035 */
17036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017037f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017038{
17039 int row;
17040 int col;
17041 int c;
17042
17043 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17044 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17045 if (row < 0 || row >= screen_Rows
17046 || col < 0 || col >= screen_Columns)
17047 c = -1;
17048 else
17049 c = ScreenAttrs[LineOffset[row] + col];
17050 rettv->vval.v_number = c;
17051}
17052
17053/*
17054 * "screenchar()" function
17055 */
17056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017057f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017058{
17059 int row;
17060 int col;
17061 int off;
17062 int c;
17063
17064 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17065 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17066 if (row < 0 || row >= screen_Rows
17067 || col < 0 || col >= screen_Columns)
17068 c = -1;
17069 else
17070 {
17071 off = LineOffset[row] + col;
17072#ifdef FEAT_MBYTE
17073 if (enc_utf8 && ScreenLinesUC[off] != 0)
17074 c = ScreenLinesUC[off];
17075 else
17076#endif
17077 c = ScreenLines[off];
17078 }
17079 rettv->vval.v_number = c;
17080}
17081
17082/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017083 * "screencol()" function
17084 *
17085 * First column is 1 to be consistent with virtcol().
17086 */
17087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017088f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017089{
17090 rettv->vval.v_number = screen_screencol() + 1;
17091}
17092
17093/*
17094 * "screenrow()" function
17095 */
17096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017097f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017098{
17099 rettv->vval.v_number = screen_screenrow() + 1;
17100}
17101
17102/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017103 * "search()" function
17104 */
17105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017106f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017107{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017108 int flags = 0;
17109
17110 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111}
17112
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017114 * "searchdecl()" function
17115 */
17116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017117f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017118{
17119 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017120 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017121 int error = FALSE;
17122 char_u *name;
17123
17124 rettv->vval.v_number = 1; /* default: FAIL */
17125
17126 name = get_tv_string_chk(&argvars[0]);
17127 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017128 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017129 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017130 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17131 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17132 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017133 if (!error && name != NULL)
17134 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017135 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017136}
17137
17138/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017139 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017141 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017142searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143{
17144 char_u *spat, *mpat, *epat;
17145 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 int dir;
17148 int flags = 0;
17149 char_u nbuf1[NUMBUFLEN];
17150 char_u nbuf2[NUMBUFLEN];
17151 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017152 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017153 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017154 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017157 spat = get_tv_string_chk(&argvars[0]);
17158 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17159 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17160 if (spat == NULL || mpat == NULL || epat == NULL)
17161 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 /* Handle the optional fourth argument: flags */
17164 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017165 if (dir == 0)
17166 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017167
17168 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017169 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17170 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017171 if ((flags & (SP_END | SP_SUBPAT)) != 0
17172 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017173 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017174 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017175 goto theend;
17176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017178 /* Using 'r' implies 'W', otherwise it doesn't work. */
17179 if (flags & SP_REPEAT)
17180 p_ws = FALSE;
17181
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017182 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017183 if (argvars[3].v_type == VAR_UNKNOWN
17184 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 skip = (char_u *)"";
17186 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017188 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017189 if (argvars[5].v_type != VAR_UNKNOWN)
17190 {
17191 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17192 if (lnum_stop < 0)
17193 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017194#ifdef FEAT_RELTIME
17195 if (argvars[6].v_type != VAR_UNKNOWN)
17196 {
17197 time_limit = get_tv_number_chk(&argvars[6], NULL);
17198 if (time_limit < 0)
17199 goto theend;
17200 }
17201#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017202 }
17203 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017204 if (skip == NULL)
17205 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017207 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017208 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017209
17210theend:
17211 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017212
17213 return retval;
17214}
17215
17216/*
17217 * "searchpair()" function
17218 */
17219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017220f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017221{
17222 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17223}
17224
17225/*
17226 * "searchpairpos()" function
17227 */
17228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017229f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017230{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017231 pos_T match_pos;
17232 int lnum = 0;
17233 int col = 0;
17234
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017235 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017236 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017237
17238 if (searchpair_cmn(argvars, &match_pos) > 0)
17239 {
17240 lnum = match_pos.lnum;
17241 col = match_pos.col;
17242 }
17243
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017244 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17245 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017246}
17247
17248/*
17249 * Search for a start/middle/end thing.
17250 * Used by searchpair(), see its documentation for the details.
17251 * Returns 0 or -1 for no match,
17252 */
17253 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017254do_searchpair(
17255 char_u *spat, /* start pattern */
17256 char_u *mpat, /* middle pattern */
17257 char_u *epat, /* end pattern */
17258 int dir, /* BACKWARD or FORWARD */
17259 char_u *skip, /* skip expression */
17260 int flags, /* SP_SETPCMARK and other SP_ values */
17261 pos_T *match_pos,
17262 linenr_T lnum_stop, /* stop at this line if not zero */
17263 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017264{
17265 char_u *save_cpo;
17266 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17267 long retval = 0;
17268 pos_T pos;
17269 pos_T firstpos;
17270 pos_T foundpos;
17271 pos_T save_cursor;
17272 pos_T save_pos;
17273 int n;
17274 int r;
17275 int nest = 1;
17276 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017277 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017278 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017279
17280 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17281 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017282 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017283
Bram Moolenaar76929292008-01-06 19:07:36 +000017284#ifdef FEAT_RELTIME
17285 /* Set the time limit, if there is one. */
17286 profile_setlimit(time_limit, &tm);
17287#endif
17288
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017289 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17290 * start/middle/end (pat3, for the top pair). */
17291 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17292 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17293 if (pat2 == NULL || pat3 == NULL)
17294 goto theend;
17295 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17296 if (*mpat == NUL)
17297 STRCPY(pat3, pat2);
17298 else
17299 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17300 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017301 if (flags & SP_START)
17302 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017303
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 save_cursor = curwin->w_cursor;
17305 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017306 clearpos(&firstpos);
17307 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 pat = pat3;
17309 for (;;)
17310 {
17311 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017312 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17314 /* didn't find it or found the first match again: FAIL */
17315 break;
17316
17317 if (firstpos.lnum == 0)
17318 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017319 if (equalpos(pos, foundpos))
17320 {
17321 /* Found the same position again. Can happen with a pattern that
17322 * has "\zs" at the end and searching backwards. Advance one
17323 * character and try again. */
17324 if (dir == BACKWARD)
17325 decl(&pos);
17326 else
17327 incl(&pos);
17328 }
17329 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017331 /* clear the start flag to avoid getting stuck here */
17332 options &= ~SEARCH_START;
17333
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334 /* If the skip pattern matches, ignore this match. */
17335 if (*skip != NUL)
17336 {
17337 save_pos = curwin->w_cursor;
17338 curwin->w_cursor = pos;
17339 r = eval_to_bool(skip, &err, NULL, FALSE);
17340 curwin->w_cursor = save_pos;
17341 if (err)
17342 {
17343 /* Evaluating {skip} caused an error, break here. */
17344 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017345 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 break;
17347 }
17348 if (r)
17349 continue;
17350 }
17351
17352 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17353 {
17354 /* Found end when searching backwards or start when searching
17355 * forward: nested pair. */
17356 ++nest;
17357 pat = pat2; /* nested, don't search for middle */
17358 }
17359 else
17360 {
17361 /* Found end when searching forward or start when searching
17362 * backward: end of (nested) pair; or found middle in outer pair. */
17363 if (--nest == 1)
17364 pat = pat3; /* outer level, search for middle */
17365 }
17366
17367 if (nest == 0)
17368 {
17369 /* Found the match: return matchcount or line number. */
17370 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017371 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017373 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017374 if (flags & SP_SETPCMARK)
17375 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 curwin->w_cursor = pos;
17377 if (!(flags & SP_REPEAT))
17378 break;
17379 nest = 1; /* search for next unmatched */
17380 }
17381 }
17382
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017383 if (match_pos != NULL)
17384 {
17385 /* Store the match cursor position */
17386 match_pos->lnum = curwin->w_cursor.lnum;
17387 match_pos->col = curwin->w_cursor.col + 1;
17388 }
17389
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017391 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 curwin->w_cursor = save_cursor;
17393
17394theend:
17395 vim_free(pat2);
17396 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017397 if (p_cpo == empty_option)
17398 p_cpo = save_cpo;
17399 else
17400 /* Darn, evaluating the {skip} expression changed the value. */
17401 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017402
17403 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404}
17405
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017406/*
17407 * "searchpos()" function
17408 */
17409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017410f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017411{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017412 pos_T match_pos;
17413 int lnum = 0;
17414 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017415 int n;
17416 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017417
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017418 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017419 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017420
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017421 n = search_cmn(argvars, &match_pos, &flags);
17422 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017423 {
17424 lnum = match_pos.lnum;
17425 col = match_pos.col;
17426 }
17427
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017428 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17429 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017430 if (flags & SP_SUBPAT)
17431 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017432}
17433
Bram Moolenaar0d660222005-01-07 21:51:51 +000017434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017435f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017437#ifdef FEAT_CLIENTSERVER
17438 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017439 char_u *server = get_tv_string_chk(&argvars[0]);
17440 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441
Bram Moolenaar0d660222005-01-07 21:51:51 +000017442 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017443 if (server == NULL || reply == NULL)
17444 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017445 if (check_restricted() || check_secure())
17446 return;
17447# ifdef FEAT_X11
17448 if (check_connection() == FAIL)
17449 return;
17450# endif
17451
17452 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017454 EMSG(_("E258: Unable to send to client"));
17455 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017457 rettv->vval.v_number = 0;
17458#else
17459 rettv->vval.v_number = -1;
17460#endif
17461}
17462
Bram Moolenaar0d660222005-01-07 21:51:51 +000017463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017464f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017465{
17466 char_u *r = NULL;
17467
17468#ifdef FEAT_CLIENTSERVER
17469# ifdef WIN32
17470 r = serverGetVimNames();
17471# else
17472 make_connection();
17473 if (X_DISPLAY != NULL)
17474 r = serverGetVimNames(X_DISPLAY);
17475# endif
17476#endif
17477 rettv->v_type = VAR_STRING;
17478 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479}
17480
17481/*
17482 * "setbufvar()" function
17483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017485f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486{
17487 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017490 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491 char_u nbuf[NUMBUFLEN];
17492
17493 if (check_restricted() || check_secure())
17494 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017495 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17496 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017497 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 varp = &argvars[2];
17499
17500 if (buf != NULL && varname != NULL && varp != NULL)
17501 {
17502 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504
17505 if (*varname == '&')
17506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017507 long numval;
17508 char_u *strval;
17509 int error = FALSE;
17510
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017512 numval = get_tv_number_chk(varp, &error);
17513 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017514 if (!error && strval != NULL)
17515 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 }
17517 else
17518 {
17519 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17520 if (bufvarname != NULL)
17521 {
17522 STRCPY(bufvarname, "b:");
17523 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017524 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 vim_free(bufvarname);
17526 }
17527 }
17528
17529 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532}
17533
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017535f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017536{
17537 dict_T *d;
17538 dictitem_T *di;
17539 char_u *csearch;
17540
17541 if (argvars[0].v_type != VAR_DICT)
17542 {
17543 EMSG(_(e_dictreq));
17544 return;
17545 }
17546
17547 if ((d = argvars[0].vval.v_dict) != NULL)
17548 {
17549 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17550 if (csearch != NULL)
17551 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017552#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017553 if (enc_utf8)
17554 {
17555 int pcc[MAX_MCO];
17556 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017557
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017558 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17559 }
17560 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017561#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017562 set_last_csearch(PTR2CHAR(csearch),
17563 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017564 }
17565
17566 di = dict_find(d, (char_u *)"forward", -1);
17567 if (di != NULL)
17568 set_csearch_direction(get_tv_number(&di->di_tv)
17569 ? FORWARD : BACKWARD);
17570
17571 di = dict_find(d, (char_u *)"until", -1);
17572 if (di != NULL)
17573 set_csearch_until(!!get_tv_number(&di->di_tv));
17574 }
17575}
17576
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577/*
17578 * "setcmdpos()" function
17579 */
17580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017581f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017583 int pos = (int)get_tv_number(&argvars[0]) - 1;
17584
17585 if (pos >= 0)
17586 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587}
17588
17589/*
17590 * "setline()" function
17591 */
17592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017593f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594{
17595 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017596 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017597 list_T *l = NULL;
17598 listitem_T *li = NULL;
17599 long added = 0;
17600 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017602 lnum = get_tv_lnum(&argvars[0]);
17603 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017605 l = argvars[1].vval.v_list;
17606 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017608 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017609 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017610
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017611 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017612 for (;;)
17613 {
17614 if (l != NULL)
17615 {
17616 /* list argument, get next string */
17617 if (li == NULL)
17618 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017619 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017620 li = li->li_next;
17621 }
17622
17623 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017624 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017625 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017626
17627 /* When coming here from Insert mode, sync undo, so that this can be
17628 * undone separately from what was previously inserted. */
17629 if (u_sync_once == 2)
17630 {
17631 u_sync_once = 1; /* notify that u_sync() was called */
17632 u_sync(TRUE);
17633 }
17634
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017635 if (lnum <= curbuf->b_ml.ml_line_count)
17636 {
17637 /* existing line, replace it */
17638 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17639 {
17640 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017641 if (lnum == curwin->w_cursor.lnum)
17642 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017643 rettv->vval.v_number = 0; /* OK */
17644 }
17645 }
17646 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17647 {
17648 /* lnum is one past the last line, append the line */
17649 ++added;
17650 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17651 rettv->vval.v_number = 0; /* OK */
17652 }
17653
17654 if (l == NULL) /* only one string argument */
17655 break;
17656 ++lnum;
17657 }
17658
17659 if (added > 0)
17660 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661}
17662
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017663static 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 +000017664
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017666 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017667 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017669set_qf_ll_list(
17670 win_T *wp UNUSED,
17671 typval_T *list_arg UNUSED,
17672 typval_T *action_arg UNUSED,
17673 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017674{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017675#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017676 char_u *act;
17677 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017678#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017679
Bram Moolenaar2641f772005-03-25 21:58:17 +000017680 rettv->vval.v_number = -1;
17681
17682#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017683 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017684 EMSG(_(e_listreq));
17685 else
17686 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017687 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017688
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017689 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017690 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017691 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017692 if (act == NULL)
17693 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017694 if (*act == 'a' || *act == 'r')
17695 action = *act;
17696 }
17697
Bram Moolenaar81484f42012-12-05 15:16:47 +010017698 if (l != NULL && set_errorlist(wp, l, action,
17699 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017700 rettv->vval.v_number = 0;
17701 }
17702#endif
17703}
17704
17705/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017706 * "setloclist()" function
17707 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017709f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017710{
17711 win_T *win;
17712
17713 rettv->vval.v_number = -1;
17714
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017715 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017716 if (win != NULL)
17717 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17718}
17719
17720/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017721 * "setmatches()" function
17722 */
17723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017724f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017725{
17726#ifdef FEAT_SEARCH_EXTRA
17727 list_T *l;
17728 listitem_T *li;
17729 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017730 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017731
17732 rettv->vval.v_number = -1;
17733 if (argvars[0].v_type != VAR_LIST)
17734 {
17735 EMSG(_(e_listreq));
17736 return;
17737 }
17738 if ((l = argvars[0].vval.v_list) != NULL)
17739 {
17740
17741 /* To some extent make sure that we are dealing with a list from
17742 * "getmatches()". */
17743 li = l->lv_first;
17744 while (li != NULL)
17745 {
17746 if (li->li_tv.v_type != VAR_DICT
17747 || (d = li->li_tv.vval.v_dict) == NULL)
17748 {
17749 EMSG(_(e_invarg));
17750 return;
17751 }
17752 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017753 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17754 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017755 && dict_find(d, (char_u *)"priority", -1) != NULL
17756 && dict_find(d, (char_u *)"id", -1) != NULL))
17757 {
17758 EMSG(_(e_invarg));
17759 return;
17760 }
17761 li = li->li_next;
17762 }
17763
17764 clear_matches(curwin);
17765 li = l->lv_first;
17766 while (li != NULL)
17767 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017768 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017769 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017770 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017771 char_u *group;
17772 int priority;
17773 int id;
17774 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017775
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017776 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017777 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17778 {
17779 if (s == NULL)
17780 {
17781 s = list_alloc();
17782 if (s == NULL)
17783 return;
17784 }
17785
17786 /* match from matchaddpos() */
17787 for (i = 1; i < 9; i++)
17788 {
17789 sprintf((char *)buf, (char *)"pos%d", i);
17790 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17791 {
17792 if (di->di_tv.v_type != VAR_LIST)
17793 return;
17794
17795 list_append_tv(s, &di->di_tv);
17796 s->lv_refcount++;
17797 }
17798 else
17799 break;
17800 }
17801 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017802
17803 group = get_dict_string(d, (char_u *)"group", FALSE);
17804 priority = (int)get_dict_number(d, (char_u *)"priority");
17805 id = (int)get_dict_number(d, (char_u *)"id");
17806 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17807 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17808 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017809 if (i == 0)
17810 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017811 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017812 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017813 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017814 }
17815 else
17816 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017817 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017818 list_unref(s);
17819 s = NULL;
17820 }
17821
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017822 li = li->li_next;
17823 }
17824 rettv->vval.v_number = 0;
17825 }
17826#endif
17827}
17828
17829/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017830 * "setpos()" function
17831 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017833f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017834{
17835 pos_T pos;
17836 int fnum;
17837 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017838 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017839
Bram Moolenaar08250432008-02-13 11:42:46 +000017840 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017841 name = get_tv_string_chk(argvars);
17842 if (name != NULL)
17843 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017844 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017845 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017846 if (--pos.col < 0)
17847 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017848 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017849 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017850 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017851 if (fnum == curbuf->b_fnum)
17852 {
17853 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017854 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017855 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017856 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017857 curwin->w_set_curswant = FALSE;
17858 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017859 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017860 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017861 }
17862 else
17863 EMSG(_(e_invarg));
17864 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017865 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17866 {
17867 /* set mark */
17868 if (setmark_pos(name[1], &pos, fnum) == OK)
17869 rettv->vval.v_number = 0;
17870 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017871 else
17872 EMSG(_(e_invarg));
17873 }
17874 }
17875}
17876
17877/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017878 * "setqflist()" function
17879 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017881f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017882{
17883 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17884}
17885
17886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887 * "setreg()" function
17888 */
17889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017890f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891{
17892 int regname;
17893 char_u *strregname;
17894 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017895 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 int append;
17897 char_u yank_type;
17898 long block_len;
17899
17900 block_len = -1;
17901 yank_type = MAUTO;
17902 append = FALSE;
17903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017904 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017905 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017907 if (strregname == NULL)
17908 return; /* type error; errmsg already given */
17909 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 if (regname == 0 || regname == '@')
17911 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017913 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017915 stropt = get_tv_string_chk(&argvars[2]);
17916 if (stropt == NULL)
17917 return; /* type error */
17918 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 switch (*stropt)
17920 {
17921 case 'a': case 'A': /* append */
17922 append = TRUE;
17923 break;
17924 case 'v': case 'c': /* character-wise selection */
17925 yank_type = MCHAR;
17926 break;
17927 case 'V': case 'l': /* line-wise selection */
17928 yank_type = MLINE;
17929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 case 'b': case Ctrl_V: /* block-wise selection */
17931 yank_type = MBLOCK;
17932 if (VIM_ISDIGIT(stropt[1]))
17933 {
17934 ++stropt;
17935 block_len = getdigits(&stropt) - 1;
17936 --stropt;
17937 }
17938 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 }
17940 }
17941
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017942 if (argvars[1].v_type == VAR_LIST)
17943 {
17944 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017945 char_u **allocval;
17946 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017947 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017948 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017949 int len = argvars[1].vval.v_list->lv_len;
17950 listitem_T *li;
17951
Bram Moolenaar7d647822014-04-05 21:28:56 +020017952 /* First half: use for pointers to result lines; second half: use for
17953 * pointers to allocated copies. */
17954 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017955 if (lstval == NULL)
17956 return;
17957 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017958 allocval = lstval + len + 2;
17959 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017960
17961 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17962 li = li->li_next)
17963 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017964 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017965 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017966 goto free_lstval;
17967 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017968 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017969 /* Need to make a copy, next get_tv_string_buf_chk() will
17970 * overwrite the string. */
17971 strval = vim_strsave(buf);
17972 if (strval == NULL)
17973 goto free_lstval;
17974 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017975 }
17976 *curval++ = strval;
17977 }
17978 *curval++ = NULL;
17979
17980 write_reg_contents_lst(regname, lstval, -1,
17981 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017982free_lstval:
17983 while (curallocval > allocval)
17984 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017985 vim_free(lstval);
17986 }
17987 else
17988 {
17989 strval = get_tv_string_chk(&argvars[1]);
17990 if (strval == NULL)
17991 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017992 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017995 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996}
17997
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017998/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017999 * "settabvar()" function
18000 */
18001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018002f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018003{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018004#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018005 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018006 tabpage_T *tp;
18007#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018008 char_u *varname, *tabvarname;
18009 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018010
18011 rettv->vval.v_number = 0;
18012
18013 if (check_restricted() || check_secure())
18014 return;
18015
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018016#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018017 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018018#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018019 varname = get_tv_string_chk(&argvars[1]);
18020 varp = &argvars[2];
18021
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018022 if (varname != NULL && varp != NULL
18023#ifdef FEAT_WINDOWS
18024 && tp != NULL
18025#endif
18026 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018027 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018028#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018029 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018030 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018031#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018032
18033 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18034 if (tabvarname != NULL)
18035 {
18036 STRCPY(tabvarname, "t:");
18037 STRCPY(tabvarname + 2, varname);
18038 set_var(tabvarname, varp, TRUE);
18039 vim_free(tabvarname);
18040 }
18041
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018042#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018043 /* Restore current tabpage */
18044 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018045 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018046#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018047 }
18048}
18049
18050/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018051 * "settabwinvar()" function
18052 */
18053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018054f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018055{
18056 setwinvar(argvars, rettv, 1);
18057}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058
18059/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018060 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018063f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018065 setwinvar(argvars, rettv, 0);
18066}
18067
18068/*
18069 * "setwinvar()" and "settabwinvar()" functions
18070 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018071
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018073setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018074{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 win_T *win;
18076#ifdef FEAT_WINDOWS
18077 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018078 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018079 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080#endif
18081 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018082 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018084 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085
18086 if (check_restricted() || check_secure())
18087 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018088
18089#ifdef FEAT_WINDOWS
18090 if (off == 1)
18091 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18092 else
18093 tp = curtab;
18094#endif
18095 win = find_win_by_nr(&argvars[off], tp);
18096 varname = get_tv_string_chk(&argvars[off + 1]);
18097 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098
18099 if (win != NULL && varname != NULL && varp != NULL)
18100 {
18101#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018102 need_switch_win = !(tp == curtab && win == curwin);
18103 if (!need_switch_win
18104 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018107 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018109 long numval;
18110 char_u *strval;
18111 int error = FALSE;
18112
18113 ++varname;
18114 numval = get_tv_number_chk(varp, &error);
18115 strval = get_tv_string_buf_chk(varp, nbuf);
18116 if (!error && strval != NULL)
18117 set_option_value(varname, numval, strval, OPT_LOCAL);
18118 }
18119 else
18120 {
18121 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18122 if (winvarname != NULL)
18123 {
18124 STRCPY(winvarname, "w:");
18125 STRCPY(winvarname + 2, varname);
18126 set_var(winvarname, varp, TRUE);
18127 vim_free(winvarname);
18128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 }
18130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018132 if (need_switch_win)
18133 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134#endif
18135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136}
18137
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018138#ifdef FEAT_CRYPT
18139/*
18140 * "sha256({string})" function
18141 */
18142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018143f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018144{
18145 char_u *p;
18146
18147 p = get_tv_string(&argvars[0]);
18148 rettv->vval.v_string = vim_strsave(
18149 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18150 rettv->v_type = VAR_STRING;
18151}
18152#endif /* FEAT_CRYPT */
18153
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018155 * "shellescape({string})" function
18156 */
18157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018158f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018159{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018160 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018161 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018162 rettv->v_type = VAR_STRING;
18163}
18164
18165/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018166 * shiftwidth() function
18167 */
18168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018169f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018170{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018171 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018172}
18173
18174/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018175 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 */
18177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018178f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018180 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181
Bram Moolenaar0d660222005-01-07 21:51:51 +000018182 p = get_tv_string(&argvars[0]);
18183 rettv->vval.v_string = vim_strsave(p);
18184 simplify_filename(rettv->vval.v_string); /* simplify in place */
18185 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186}
18187
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018188#ifdef FEAT_FLOAT
18189/*
18190 * "sin()" function
18191 */
18192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018193f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018194{
18195 float_T f;
18196
18197 rettv->v_type = VAR_FLOAT;
18198 if (get_float_arg(argvars, &f) == OK)
18199 rettv->vval.v_float = sin(f);
18200 else
18201 rettv->vval.v_float = 0.0;
18202}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018203
18204/*
18205 * "sinh()" function
18206 */
18207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018208f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018209{
18210 float_T f;
18211
18212 rettv->v_type = VAR_FLOAT;
18213 if (get_float_arg(argvars, &f) == OK)
18214 rettv->vval.v_float = sinh(f);
18215 else
18216 rettv->vval.v_float = 0.0;
18217}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018218#endif
18219
Bram Moolenaar0d660222005-01-07 21:51:51 +000018220static int
18221#ifdef __BORLANDC__
18222 _RTLENTRYF
18223#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018224 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018225static int
18226#ifdef __BORLANDC__
18227 _RTLENTRYF
18228#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018229 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018230
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018231/* struct used in the array that's given to qsort() */
18232typedef struct
18233{
18234 listitem_T *item;
18235 int idx;
18236} sortItem_T;
18237
Bram Moolenaar0d660222005-01-07 21:51:51 +000018238static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018239static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018240static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018241#ifdef FEAT_FLOAT
18242static int item_compare_float;
18243#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018244static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018245static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018246static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018247static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018248static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018249#define ITEM_COMPARE_FAIL 999
18250
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018252 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018254 static int
18255#ifdef __BORLANDC__
18256_RTLENTRYF
18257#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018258item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018260 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018261 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018262 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018263 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264 int res;
18265 char_u numbuf1[NUMBUFLEN];
18266 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018268 si1 = (sortItem_T *)s1;
18269 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018270 tv1 = &si1->item->li_tv;
18271 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018272
18273 if (item_compare_numbers)
18274 {
18275 long v1 = get_tv_number(tv1);
18276 long v2 = get_tv_number(tv2);
18277
18278 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18279 }
18280
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018281#ifdef FEAT_FLOAT
18282 if (item_compare_float)
18283 {
18284 float_T v1 = get_tv_float(tv1);
18285 float_T v2 = get_tv_float(tv2);
18286
18287 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18288 }
18289#endif
18290
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018291 /* tv2string() puts quotes around a string and allocates memory. Don't do
18292 * that for string variables. Use a single quote when comparing with a
18293 * non-string to do what the docs promise. */
18294 if (tv1->v_type == VAR_STRING)
18295 {
18296 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18297 p1 = (char_u *)"'";
18298 else
18299 p1 = tv1->vval.v_string;
18300 }
18301 else
18302 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18303 if (tv2->v_type == VAR_STRING)
18304 {
18305 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18306 p2 = (char_u *)"'";
18307 else
18308 p2 = tv2->vval.v_string;
18309 }
18310 else
18311 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018312 if (p1 == NULL)
18313 p1 = (char_u *)"";
18314 if (p2 == NULL)
18315 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018316 if (!item_compare_numeric)
18317 {
18318 if (item_compare_ic)
18319 res = STRICMP(p1, p2);
18320 else
18321 res = STRCMP(p1, p2);
18322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018324 {
18325 double n1, n2;
18326 n1 = strtod((char *)p1, (char **)&p1);
18327 n2 = strtod((char *)p2, (char **)&p2);
18328 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18329 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018330
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018331 /* When the result would be zero, compare the item indexes. Makes the
18332 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018333 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018334 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018335
Bram Moolenaar0d660222005-01-07 21:51:51 +000018336 vim_free(tofree1);
18337 vim_free(tofree2);
18338 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339}
18340
18341 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018342#ifdef __BORLANDC__
18343_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018345item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018346{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018347 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018348 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018349 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018350 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018351 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018353 /* shortcut after failure in previous call; compare all items equal */
18354 if (item_compare_func_err)
18355 return 0;
18356
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018357 si1 = (sortItem_T *)s1;
18358 si2 = (sortItem_T *)s2;
18359
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018360 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018361 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018362 copy_tv(&si1->item->li_tv, &argv[0]);
18363 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018364
18365 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018366 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018367 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18368 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018369 clear_tv(&argv[0]);
18370 clear_tv(&argv[1]);
18371
18372 if (res == FAIL)
18373 res = ITEM_COMPARE_FAIL;
18374 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018375 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18376 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018377 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018378 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018379
18380 /* When the result would be zero, compare the pointers themselves. Makes
18381 * the sort stable. */
18382 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018383 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018384
Bram Moolenaar0d660222005-01-07 21:51:51 +000018385 return res;
18386}
18387
18388/*
18389 * "sort({list})" function
18390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018392do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393{
Bram Moolenaar33570922005-01-25 22:26:29 +000018394 list_T *l;
18395 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018396 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018397 long len;
18398 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399
Bram Moolenaar0d660222005-01-07 21:51:51 +000018400 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018401 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402 else
18403 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018404 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018405 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018406 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18407 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018408 return;
18409 rettv->vval.v_list = l;
18410 rettv->v_type = VAR_LIST;
18411 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413 len = list_len(l);
18414 if (len <= 1)
18415 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416
Bram Moolenaar0d660222005-01-07 21:51:51 +000018417 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018418 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018419 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018420#ifdef FEAT_FLOAT
18421 item_compare_float = FALSE;
18422#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018423 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018424 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018425 if (argvars[1].v_type != VAR_UNKNOWN)
18426 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018427 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018428 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018429 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018430 else
18431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018432 int error = FALSE;
18433
18434 i = get_tv_number_chk(&argvars[1], &error);
18435 if (error)
18436 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018437 if (i == 1)
18438 item_compare_ic = TRUE;
18439 else
18440 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018441 if (item_compare_func != NULL)
18442 {
18443 if (STRCMP(item_compare_func, "n") == 0)
18444 {
18445 item_compare_func = NULL;
18446 item_compare_numeric = TRUE;
18447 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018448 else if (STRCMP(item_compare_func, "N") == 0)
18449 {
18450 item_compare_func = NULL;
18451 item_compare_numbers = TRUE;
18452 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018453#ifdef FEAT_FLOAT
18454 else if (STRCMP(item_compare_func, "f") == 0)
18455 {
18456 item_compare_func = NULL;
18457 item_compare_float = TRUE;
18458 }
18459#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018460 else if (STRCMP(item_compare_func, "i") == 0)
18461 {
18462 item_compare_func = NULL;
18463 item_compare_ic = TRUE;
18464 }
18465 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018466 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018467
18468 if (argvars[2].v_type != VAR_UNKNOWN)
18469 {
18470 /* optional third argument: {dict} */
18471 if (argvars[2].v_type != VAR_DICT)
18472 {
18473 EMSG(_(e_dictreq));
18474 return;
18475 }
18476 item_compare_selfdict = argvars[2].vval.v_dict;
18477 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479
Bram Moolenaar0d660222005-01-07 21:51:51 +000018480 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018481 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018482 if (ptrs == NULL)
18483 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484
Bram Moolenaar327aa022014-03-25 18:24:23 +010018485 i = 0;
18486 if (sort)
18487 {
18488 /* sort(): ptrs will be the list to sort */
18489 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018490 {
18491 ptrs[i].item = li;
18492 ptrs[i].idx = i;
18493 ++i;
18494 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018495
18496 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018497 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018498 /* test the compare function */
18499 if (item_compare_func != NULL
18500 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018501 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018502 EMSG(_("E702: Sort compare function failed"));
18503 else
18504 {
18505 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018506 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018507 item_compare_func == NULL ? item_compare : item_compare2);
18508
18509 if (!item_compare_func_err)
18510 {
18511 /* Clear the List and append the items in sorted order. */
18512 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18513 l->lv_len = 0;
18514 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018515 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018516 }
18517 }
18518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018520 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018521 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018522
18523 /* f_uniq(): ptrs will be a stack of items to remove */
18524 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018525 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018526 item_compare_func_ptr = item_compare_func
18527 ? item_compare2 : item_compare;
18528
18529 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18530 li = li->li_next)
18531 {
18532 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18533 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018534 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018535 if (item_compare_func_err)
18536 {
18537 EMSG(_("E882: Uniq compare function failed"));
18538 break;
18539 }
18540 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018541
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018542 if (!item_compare_func_err)
18543 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018544 while (--i >= 0)
18545 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018546 li = ptrs[i].item->li_next;
18547 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018548 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018549 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018550 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018551 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018552 list_fix_watch(l, li);
18553 listitem_free(li);
18554 l->lv_len--;
18555 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018556 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018557 }
18558
18559 vim_free(ptrs);
18560 }
18561}
18562
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018563/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018564 * "sort({list})" function
18565 */
18566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018567f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018568{
18569 do_sort_uniq(argvars, rettv, TRUE);
18570}
18571
18572/*
18573 * "uniq({list})" function
18574 */
18575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018576f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018577{
18578 do_sort_uniq(argvars, rettv, FALSE);
18579}
18580
18581/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018582 * "soundfold({word})" function
18583 */
18584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018585f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018586{
18587 char_u *s;
18588
18589 rettv->v_type = VAR_STRING;
18590 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018591#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018592 rettv->vval.v_string = eval_soundfold(s);
18593#else
18594 rettv->vval.v_string = vim_strsave(s);
18595#endif
18596}
18597
18598/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018599 * "spellbadword()" function
18600 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018602f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018603{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018604 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018605 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018606 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018607
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018608 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018609 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018610
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018611#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018612 if (argvars[0].v_type == VAR_UNKNOWN)
18613 {
18614 /* Find the start and length of the badly spelled word. */
18615 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18616 if (len != 0)
18617 word = ml_get_cursor();
18618 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018619 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018620 {
18621 char_u *str = get_tv_string_chk(&argvars[0]);
18622 int capcol = -1;
18623
18624 if (str != NULL)
18625 {
18626 /* Check the argument for spelling. */
18627 while (*str != NUL)
18628 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018629 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018630 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018631 {
18632 word = str;
18633 break;
18634 }
18635 str += len;
18636 }
18637 }
18638 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018639#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018640
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018641 list_append_string(rettv->vval.v_list, word, len);
18642 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018643 attr == HLF_SPB ? "bad" :
18644 attr == HLF_SPR ? "rare" :
18645 attr == HLF_SPL ? "local" :
18646 attr == HLF_SPC ? "caps" :
18647 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018648}
18649
18650/*
18651 * "spellsuggest()" function
18652 */
18653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018654f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018655{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018656#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018657 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018658 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018659 int maxcount;
18660 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018661 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018662 listitem_T *li;
18663 int need_capital = FALSE;
18664#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018665
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018666 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018667 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018668
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018669#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018670 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018671 {
18672 str = get_tv_string(&argvars[0]);
18673 if (argvars[1].v_type != VAR_UNKNOWN)
18674 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018675 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018676 if (maxcount <= 0)
18677 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018678 if (argvars[2].v_type != VAR_UNKNOWN)
18679 {
18680 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18681 if (typeerr)
18682 return;
18683 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018684 }
18685 else
18686 maxcount = 25;
18687
Bram Moolenaar4770d092006-01-12 23:22:24 +000018688 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018689
18690 for (i = 0; i < ga.ga_len; ++i)
18691 {
18692 str = ((char_u **)ga.ga_data)[i];
18693
18694 li = listitem_alloc();
18695 if (li == NULL)
18696 vim_free(str);
18697 else
18698 {
18699 li->li_tv.v_type = VAR_STRING;
18700 li->li_tv.v_lock = 0;
18701 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018702 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018703 }
18704 }
18705 ga_clear(&ga);
18706 }
18707#endif
18708}
18709
Bram Moolenaar0d660222005-01-07 21:51:51 +000018710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018711f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018712{
18713 char_u *str;
18714 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018715 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018716 regmatch_T regmatch;
18717 char_u patbuf[NUMBUFLEN];
18718 char_u *save_cpo;
18719 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018720 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018721 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018722 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018723
18724 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18725 save_cpo = p_cpo;
18726 p_cpo = (char_u *)"";
18727
18728 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018729 if (argvars[1].v_type != VAR_UNKNOWN)
18730 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018731 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18732 if (pat == NULL)
18733 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018734 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018735 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018736 }
18737 if (pat == NULL || *pat == NUL)
18738 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018739
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018740 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018742 if (typeerr)
18743 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744
Bram Moolenaar0d660222005-01-07 21:51:51 +000018745 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18746 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018748 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018749 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018750 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018751 if (*str == NUL)
18752 match = FALSE; /* empty item at the end */
18753 else
18754 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018755 if (match)
18756 end = regmatch.startp[0];
18757 else
18758 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018759 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18760 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018761 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018762 if (list_append_string(rettv->vval.v_list, str,
18763 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018764 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018765 }
18766 if (!match)
18767 break;
18768 /* Advance to just after the match. */
18769 if (regmatch.endp[0] > str)
18770 col = 0;
18771 else
18772 {
18773 /* Don't get stuck at the same match. */
18774#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018775 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018776#else
18777 col = 1;
18778#endif
18779 }
18780 str = regmatch.endp[0];
18781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782
Bram Moolenaar473de612013-06-08 18:19:48 +020018783 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785
Bram Moolenaar0d660222005-01-07 21:51:51 +000018786 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787}
18788
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018789#ifdef FEAT_FLOAT
18790/*
18791 * "sqrt()" function
18792 */
18793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018794f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018795{
18796 float_T f;
18797
18798 rettv->v_type = VAR_FLOAT;
18799 if (get_float_arg(argvars, &f) == OK)
18800 rettv->vval.v_float = sqrt(f);
18801 else
18802 rettv->vval.v_float = 0.0;
18803}
18804
18805/*
18806 * "str2float()" function
18807 */
18808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018809f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018810{
18811 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18812
18813 if (*p == '+')
18814 p = skipwhite(p + 1);
18815 (void)string2float(p, &rettv->vval.v_float);
18816 rettv->v_type = VAR_FLOAT;
18817}
18818#endif
18819
Bram Moolenaar2c932302006-03-18 21:42:09 +000018820/*
18821 * "str2nr()" function
18822 */
18823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018824f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018825{
18826 int base = 10;
18827 char_u *p;
18828 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018829 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018830
18831 if (argvars[1].v_type != VAR_UNKNOWN)
18832 {
18833 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018834 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018835 {
18836 EMSG(_(e_invarg));
18837 return;
18838 }
18839 }
18840
18841 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018842 if (*p == '+')
18843 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018844 switch (base)
18845 {
18846 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18847 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18848 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18849 default: what = 0;
18850 }
18851 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018852 rettv->vval.v_number = n;
18853}
18854
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855#ifdef HAVE_STRFTIME
18856/*
18857 * "strftime({format}[, {time}])" function
18858 */
18859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018860f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861{
18862 char_u result_buf[256];
18863 struct tm *curtime;
18864 time_t seconds;
18865 char_u *p;
18866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018867 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018869 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018870 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871 seconds = time(NULL);
18872 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018873 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874 curtime = localtime(&seconds);
18875 /* MSVC returns NULL for an invalid value of seconds. */
18876 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018877 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 else
18879 {
18880# ifdef FEAT_MBYTE
18881 vimconv_T conv;
18882 char_u *enc;
18883
18884 conv.vc_type = CONV_NONE;
18885 enc = enc_locale();
18886 convert_setup(&conv, p_enc, enc);
18887 if (conv.vc_type != CONV_NONE)
18888 p = string_convert(&conv, p, NULL);
18889# endif
18890 if (p != NULL)
18891 (void)strftime((char *)result_buf, sizeof(result_buf),
18892 (char *)p, curtime);
18893 else
18894 result_buf[0] = NUL;
18895
18896# ifdef FEAT_MBYTE
18897 if (conv.vc_type != CONV_NONE)
18898 vim_free(p);
18899 convert_setup(&conv, enc, p_enc);
18900 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018901 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 else
18903# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018904 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905
18906# ifdef FEAT_MBYTE
18907 /* Release conversion descriptors */
18908 convert_setup(&conv, NULL, NULL);
18909 vim_free(enc);
18910# endif
18911 }
18912}
18913#endif
18914
18915/*
18916 * "stridx()" function
18917 */
18918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018919f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920{
18921 char_u buf[NUMBUFLEN];
18922 char_u *needle;
18923 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018924 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018926 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018928 needle = get_tv_string_chk(&argvars[1]);
18929 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018930 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018931 if (needle == NULL || haystack == NULL)
18932 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933
Bram Moolenaar33570922005-01-25 22:26:29 +000018934 if (argvars[2].v_type != VAR_UNKNOWN)
18935 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018936 int error = FALSE;
18937
18938 start_idx = get_tv_number_chk(&argvars[2], &error);
18939 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018940 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018941 if (start_idx >= 0)
18942 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018943 }
18944
18945 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18946 if (pos != NULL)
18947 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948}
18949
18950/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018951 * "string()" function
18952 */
18953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018954f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018955{
18956 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018957 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018959 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018960 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018961 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018962 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018963 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964}
18965
18966/*
18967 * "strlen()" function
18968 */
18969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018970f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018972 rettv->vval.v_number = (varnumber_T)(STRLEN(
18973 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974}
18975
18976/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018977 * "strchars()" function
18978 */
18979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018980f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018981{
18982 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018983 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018984#ifdef FEAT_MBYTE
18985 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018986 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018987#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018988
18989 if (argvars[1].v_type != VAR_UNKNOWN)
18990 skipcc = get_tv_number_chk(&argvars[1], NULL);
18991 if (skipcc < 0 || skipcc > 1)
18992 EMSG(_(e_invarg));
18993 else
18994 {
18995#ifdef FEAT_MBYTE
18996 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18997 while (*s != NUL)
18998 {
18999 func_mb_ptr2char_adv(&s);
19000 ++len;
19001 }
19002 rettv->vval.v_number = len;
19003#else
19004 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19005#endif
19006 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019007}
19008
19009/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019010 * "strdisplaywidth()" function
19011 */
19012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019013f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019014{
19015 char_u *s = get_tv_string(&argvars[0]);
19016 int col = 0;
19017
19018 if (argvars[1].v_type != VAR_UNKNOWN)
19019 col = get_tv_number(&argvars[1]);
19020
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019021 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019022}
19023
19024/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019025 * "strwidth()" function
19026 */
19027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019028f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019029{
19030 char_u *s = get_tv_string(&argvars[0]);
19031
19032 rettv->vval.v_number = (varnumber_T)(
19033#ifdef FEAT_MBYTE
19034 mb_string2cells(s, -1)
19035#else
19036 STRLEN(s)
19037#endif
19038 );
19039}
19040
19041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 * "strpart()" function
19043 */
19044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019045f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046{
19047 char_u *p;
19048 int n;
19049 int len;
19050 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019051 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019053 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 slen = (int)STRLEN(p);
19055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019056 n = get_tv_number_chk(&argvars[1], &error);
19057 if (error)
19058 len = 0;
19059 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019060 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 else
19062 len = slen - n; /* default len: all bytes that are available. */
19063
19064 /*
19065 * Only return the overlap between the specified part and the actual
19066 * string.
19067 */
19068 if (n < 0)
19069 {
19070 len += n;
19071 n = 0;
19072 }
19073 else if (n > slen)
19074 n = slen;
19075 if (len < 0)
19076 len = 0;
19077 else if (n + len > slen)
19078 len = slen - n;
19079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019080 rettv->v_type = VAR_STRING;
19081 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082}
19083
19084/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019085 * "strridx()" function
19086 */
19087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019088f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019089{
19090 char_u buf[NUMBUFLEN];
19091 char_u *needle;
19092 char_u *haystack;
19093 char_u *rest;
19094 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019095 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019097 needle = get_tv_string_chk(&argvars[1]);
19098 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019099
19100 rettv->vval.v_number = -1;
19101 if (needle == NULL || haystack == NULL)
19102 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019103
19104 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019105 if (argvars[2].v_type != VAR_UNKNOWN)
19106 {
19107 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019108 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019109 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019110 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019111 }
19112 else
19113 end_idx = haystack_len;
19114
Bram Moolenaar0d660222005-01-07 21:51:51 +000019115 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019116 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019117 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019118 lastmatch = haystack + end_idx;
19119 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019120 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019121 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019122 for (rest = haystack; *rest != '\0'; ++rest)
19123 {
19124 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019125 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019126 break;
19127 lastmatch = rest;
19128 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019129 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019130
19131 if (lastmatch == NULL)
19132 rettv->vval.v_number = -1;
19133 else
19134 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19135}
19136
19137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 * "strtrans()" function
19139 */
19140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019141f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019143 rettv->v_type = VAR_STRING;
19144 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145}
19146
19147/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019148 * "submatch()" function
19149 */
19150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019151f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019152{
Bram Moolenaar41571762014-04-02 19:00:58 +020019153 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019154 int no;
19155 int retList = 0;
19156
19157 no = (int)get_tv_number_chk(&argvars[0], &error);
19158 if (error)
19159 return;
19160 error = FALSE;
19161 if (argvars[1].v_type != VAR_UNKNOWN)
19162 retList = get_tv_number_chk(&argvars[1], &error);
19163 if (error)
19164 return;
19165
19166 if (retList == 0)
19167 {
19168 rettv->v_type = VAR_STRING;
19169 rettv->vval.v_string = reg_submatch(no);
19170 }
19171 else
19172 {
19173 rettv->v_type = VAR_LIST;
19174 rettv->vval.v_list = reg_submatch_list(no);
19175 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019176}
19177
19178/*
19179 * "substitute()" function
19180 */
19181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019182f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019183{
19184 char_u patbuf[NUMBUFLEN];
19185 char_u subbuf[NUMBUFLEN];
19186 char_u flagsbuf[NUMBUFLEN];
19187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019188 char_u *str = get_tv_string_chk(&argvars[0]);
19189 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19190 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19191 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19192
Bram Moolenaar0d660222005-01-07 21:51:51 +000019193 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019194 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19195 rettv->vval.v_string = NULL;
19196 else
19197 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019198}
19199
19200/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019201 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019204f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205{
19206 int id = 0;
19207#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019208 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 long col;
19210 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019211 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019213 lnum = get_tv_lnum(argvars); /* -1 on type error */
19214 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19215 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019217 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019218 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019219 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220#endif
19221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019222 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223}
19224
19225/*
19226 * "synIDattr(id, what [, mode])" function
19227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019229f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230{
19231 char_u *p = NULL;
19232#ifdef FEAT_SYN_HL
19233 int id;
19234 char_u *what;
19235 char_u *mode;
19236 char_u modebuf[NUMBUFLEN];
19237 int modec;
19238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019239 id = get_tv_number(&argvars[0]);
19240 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019241 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019243 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019245 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 modec = 0; /* replace invalid with current */
19247 }
19248 else
19249 {
19250#ifdef FEAT_GUI
19251 if (gui.in_use)
19252 modec = 'g';
19253 else
19254#endif
19255 if (t_colors > 1)
19256 modec = 'c';
19257 else
19258 modec = 't';
19259 }
19260
19261
19262 switch (TOLOWER_ASC(what[0]))
19263 {
19264 case 'b':
19265 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19266 p = highlight_color(id, what, modec);
19267 else /* bold */
19268 p = highlight_has_attr(id, HL_BOLD, modec);
19269 break;
19270
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019271 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 p = highlight_color(id, what, modec);
19273 break;
19274
19275 case 'i':
19276 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19277 p = highlight_has_attr(id, HL_INVERSE, modec);
19278 else /* italic */
19279 p = highlight_has_attr(id, HL_ITALIC, modec);
19280 break;
19281
19282 case 'n': /* name */
19283 p = get_highlight_name(NULL, id - 1);
19284 break;
19285
19286 case 'r': /* reverse */
19287 p = highlight_has_attr(id, HL_INVERSE, modec);
19288 break;
19289
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019290 case 's':
19291 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19292 p = highlight_color(id, what, modec);
19293 else /* standout */
19294 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 break;
19296
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019297 case 'u':
19298 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19299 /* underline */
19300 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19301 else
19302 /* undercurl */
19303 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 break;
19305 }
19306
19307 if (p != NULL)
19308 p = vim_strsave(p);
19309#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019310 rettv->v_type = VAR_STRING;
19311 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312}
19313
19314/*
19315 * "synIDtrans(id)" function
19316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019318f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319{
19320 int id;
19321
19322#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019323 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324
19325 if (id > 0)
19326 id = syn_get_final_id(id);
19327 else
19328#endif
19329 id = 0;
19330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019331 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332}
19333
19334/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019335 * "synconcealed(lnum, col)" function
19336 */
19337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019338f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019339{
19340#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19341 long lnum;
19342 long col;
19343 int syntax_flags = 0;
19344 int cchar;
19345 int matchid = 0;
19346 char_u str[NUMBUFLEN];
19347#endif
19348
19349 rettv->v_type = VAR_LIST;
19350 rettv->vval.v_list = NULL;
19351
19352#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19353 lnum = get_tv_lnum(argvars); /* -1 on type error */
19354 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19355
19356 vim_memset(str, NUL, sizeof(str));
19357
19358 if (rettv_list_alloc(rettv) != FAIL)
19359 {
19360 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19361 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19362 && curwin->w_p_cole > 0)
19363 {
19364 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19365 syntax_flags = get_syntax_info(&matchid);
19366
19367 /* get the conceal character */
19368 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19369 {
19370 cchar = syn_get_sub_char();
19371 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19372 cchar = lcs_conceal;
19373 if (cchar != NUL)
19374 {
19375# ifdef FEAT_MBYTE
19376 if (has_mbyte)
19377 (*mb_char2bytes)(cchar, str);
19378 else
19379# endif
19380 str[0] = cchar;
19381 }
19382 }
19383 }
19384
19385 list_append_number(rettv->vval.v_list,
19386 (syntax_flags & HL_CONCEAL) != 0);
19387 /* -1 to auto-determine strlen */
19388 list_append_string(rettv->vval.v_list, str, -1);
19389 list_append_number(rettv->vval.v_list, matchid);
19390 }
19391#endif
19392}
19393
19394/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019395 * "synstack(lnum, col)" function
19396 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019398f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019399{
19400#ifdef FEAT_SYN_HL
19401 long lnum;
19402 long col;
19403 int i;
19404 int id;
19405#endif
19406
19407 rettv->v_type = VAR_LIST;
19408 rettv->vval.v_list = NULL;
19409
19410#ifdef FEAT_SYN_HL
19411 lnum = get_tv_lnum(argvars); /* -1 on type error */
19412 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19413
19414 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019415 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019416 && rettv_list_alloc(rettv) != FAIL)
19417 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019418 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019419 for (i = 0; ; ++i)
19420 {
19421 id = syn_get_stack_item(i);
19422 if (id < 0)
19423 break;
19424 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19425 break;
19426 }
19427 }
19428#endif
19429}
19430
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019432get_cmd_output_as_rettv(
19433 typval_T *argvars,
19434 typval_T *rettv,
19435 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019437 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019439 char_u *infile = NULL;
19440 char_u buf[NUMBUFLEN];
19441 int err = FALSE;
19442 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019443 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019444 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019446 rettv->v_type = VAR_STRING;
19447 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019448 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019449 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019450
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019451 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019452 {
19453 /*
19454 * Write the string to a temp file, to be used for input of the shell
19455 * command.
19456 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019457 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019458 {
19459 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019460 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019461 }
19462
19463 fd = mch_fopen((char *)infile, WRITEBIN);
19464 if (fd == NULL)
19465 {
19466 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019467 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019468 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019469 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019470 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019471 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19472 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019473 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019474 else
19475 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019476 size_t len;
19477
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019478 p = get_tv_string_buf_chk(&argvars[1], buf);
19479 if (p == NULL)
19480 {
19481 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019482 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019483 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019484 len = STRLEN(p);
19485 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019486 err = TRUE;
19487 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019488 if (fclose(fd) != 0)
19489 err = TRUE;
19490 if (err)
19491 {
19492 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019493 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019494 }
19495 }
19496
Bram Moolenaar52a72462014-08-29 15:53:52 +020019497 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19498 * echoes typeahead, that messes up the display. */
19499 if (!msg_silent)
19500 flags += SHELL_COOKED;
19501
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019502 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019504 int len;
19505 listitem_T *li;
19506 char_u *s = NULL;
19507 char_u *start;
19508 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019509 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510
Bram Moolenaar52a72462014-08-29 15:53:52 +020019511 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019512 if (res == NULL)
19513 goto errret;
19514
19515 list = list_alloc();
19516 if (list == NULL)
19517 goto errret;
19518
19519 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019521 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019522 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019523 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019524 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019525
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019526 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019527 if (s == NULL)
19528 goto errret;
19529
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019530 for (p = s; start < end; ++p, ++start)
19531 *p = *start == NUL ? NL : *start;
19532 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019533
19534 li = listitem_alloc();
19535 if (li == NULL)
19536 {
19537 vim_free(s);
19538 goto errret;
19539 }
19540 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019541 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019542 li->li_tv.vval.v_string = s;
19543 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019545
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019546 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019547 rettv->v_type = VAR_LIST;
19548 rettv->vval.v_list = list;
19549 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019551 else
19552 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019553 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019554#ifdef USE_CR
19555 /* translate <CR> into <NL> */
19556 if (res != NULL)
19557 {
19558 char_u *s;
19559
19560 for (s = res; *s; ++s)
19561 {
19562 if (*s == CAR)
19563 *s = NL;
19564 }
19565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566#else
19567# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019568 /* translate <CR><NL> into <NL> */
19569 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019571 char_u *s, *d;
19572
19573 d = res;
19574 for (s = res; *s; ++s)
19575 {
19576 if (s[0] == CAR && s[1] == NL)
19577 ++s;
19578 *d++ = *s;
19579 }
19580 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582# endif
19583#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019584 rettv->vval.v_string = res;
19585 res = NULL;
19586 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019587
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019588errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019589 if (infile != NULL)
19590 {
19591 mch_remove(infile);
19592 vim_free(infile);
19593 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019594 if (res != NULL)
19595 vim_free(res);
19596 if (list != NULL)
19597 list_free(list, TRUE);
19598}
19599
19600/*
19601 * "system()" function
19602 */
19603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019604f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019605{
19606 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19607}
19608
19609/*
19610 * "systemlist()" function
19611 */
19612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019613f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019614{
19615 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616}
19617
19618/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019619 * "tabpagebuflist()" function
19620 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019622f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019623{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019624#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019625 tabpage_T *tp;
19626 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019627
19628 if (argvars[0].v_type == VAR_UNKNOWN)
19629 wp = firstwin;
19630 else
19631 {
19632 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19633 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019634 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019635 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019636 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019637 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019638 for (; wp != NULL; wp = wp->w_next)
19639 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019640 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019641 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019642 }
19643#endif
19644}
19645
19646
19647/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019648 * "tabpagenr()" function
19649 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019651f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019652{
19653 int nr = 1;
19654#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019655 char_u *arg;
19656
19657 if (argvars[0].v_type != VAR_UNKNOWN)
19658 {
19659 arg = get_tv_string_chk(&argvars[0]);
19660 nr = 0;
19661 if (arg != NULL)
19662 {
19663 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019664 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019665 else
19666 EMSG2(_(e_invexpr2), arg);
19667 }
19668 }
19669 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019670 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019671#endif
19672 rettv->vval.v_number = nr;
19673}
19674
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019675
19676#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019677static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019678
19679/*
19680 * Common code for tabpagewinnr() and winnr().
19681 */
19682 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019683get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019684{
19685 win_T *twin;
19686 int nr = 1;
19687 win_T *wp;
19688 char_u *arg;
19689
19690 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19691 if (argvar->v_type != VAR_UNKNOWN)
19692 {
19693 arg = get_tv_string_chk(argvar);
19694 if (arg == NULL)
19695 nr = 0; /* type error; errmsg already given */
19696 else if (STRCMP(arg, "$") == 0)
19697 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19698 else if (STRCMP(arg, "#") == 0)
19699 {
19700 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19701 if (twin == NULL)
19702 nr = 0;
19703 }
19704 else
19705 {
19706 EMSG2(_(e_invexpr2), arg);
19707 nr = 0;
19708 }
19709 }
19710
19711 if (nr > 0)
19712 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19713 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019714 {
19715 if (wp == NULL)
19716 {
19717 /* didn't find it in this tabpage */
19718 nr = 0;
19719 break;
19720 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019721 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019722 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019723 return nr;
19724}
19725#endif
19726
19727/*
19728 * "tabpagewinnr()" function
19729 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019731f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019732{
19733 int nr = 1;
19734#ifdef FEAT_WINDOWS
19735 tabpage_T *tp;
19736
19737 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19738 if (tp == NULL)
19739 nr = 0;
19740 else
19741 nr = get_winnr(tp, &argvars[1]);
19742#endif
19743 rettv->vval.v_number = nr;
19744}
19745
19746
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019747/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019748 * "tagfiles()" function
19749 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019751f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019752{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019753 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019754 tagname_T tn;
19755 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019756
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019757 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019758 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019759 fname = alloc(MAXPATHL);
19760 if (fname == NULL)
19761 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019762
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019763 for (first = TRUE; ; first = FALSE)
19764 if (get_tagfname(&tn, first, fname) == FAIL
19765 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019766 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019767 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019768 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019769}
19770
19771/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019772 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019773 */
19774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019775f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019776{
19777 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019778
19779 tag_pattern = get_tv_string(&argvars[0]);
19780
19781 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019782 if (*tag_pattern == NUL)
19783 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019784
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019785 if (rettv_list_alloc(rettv) == OK)
19786 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019787}
19788
19789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 * "tempname()" function
19791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019793f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794{
19795 static int x = 'A';
19796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019797 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019798 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799
19800 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19801 * names. Skip 'I' and 'O', they are used for shell redirection. */
19802 do
19803 {
19804 if (x == 'Z')
19805 x = '0';
19806 else if (x == '9')
19807 x = 'A';
19808 else
19809 {
19810#ifdef EBCDIC
19811 if (x == 'I')
19812 x = 'J';
19813 else if (x == 'R')
19814 x = 'S';
19815 else
19816#endif
19817 ++x;
19818 }
19819 } while (x == 'I' || x == 'O');
19820}
19821
19822/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019823 * "test(list)" function: Just checking the walls...
19824 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019826f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019827{
19828 /* Used for unit testing. Change the code below to your liking. */
19829#if 0
19830 listitem_T *li;
19831 list_T *l;
19832 char_u *bad, *good;
19833
19834 if (argvars[0].v_type != VAR_LIST)
19835 return;
19836 l = argvars[0].vval.v_list;
19837 if (l == NULL)
19838 return;
19839 li = l->lv_first;
19840 if (li == NULL)
19841 return;
19842 bad = get_tv_string(&li->li_tv);
19843 li = li->li_next;
19844 if (li == NULL)
19845 return;
19846 good = get_tv_string(&li->li_tv);
19847 rettv->vval.v_number = test_edit_score(bad, good);
19848#endif
19849}
19850
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019851#ifdef FEAT_FLOAT
19852/*
19853 * "tan()" function
19854 */
19855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019856f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019857{
19858 float_T f;
19859
19860 rettv->v_type = VAR_FLOAT;
19861 if (get_float_arg(argvars, &f) == OK)
19862 rettv->vval.v_float = tan(f);
19863 else
19864 rettv->vval.v_float = 0.0;
19865}
19866
19867/*
19868 * "tanh()" function
19869 */
19870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019871f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019872{
19873 float_T f;
19874
19875 rettv->v_type = VAR_FLOAT;
19876 if (get_float_arg(argvars, &f) == OK)
19877 rettv->vval.v_float = tanh(f);
19878 else
19879 rettv->vval.v_float = 0.0;
19880}
19881#endif
19882
Bram Moolenaard52d9742005-08-21 22:20:28 +000019883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884 * "tolower(string)" function
19885 */
19886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019887f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888{
19889 char_u *p;
19890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019891 p = vim_strsave(get_tv_string(&argvars[0]));
19892 rettv->v_type = VAR_STRING;
19893 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894
19895 if (p != NULL)
19896 while (*p != NUL)
19897 {
19898#ifdef FEAT_MBYTE
19899 int l;
19900
19901 if (enc_utf8)
19902 {
19903 int c, lc;
19904
19905 c = utf_ptr2char(p);
19906 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019907 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 /* TODO: reallocate string when byte count changes. */
19909 if (utf_char2len(lc) == l)
19910 utf_char2bytes(lc, p);
19911 p += l;
19912 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019913 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 p += l; /* skip multi-byte character */
19915 else
19916#endif
19917 {
19918 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19919 ++p;
19920 }
19921 }
19922}
19923
19924/*
19925 * "toupper(string)" function
19926 */
19927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019928f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019930 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019931 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932}
19933
19934/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019935 * "tr(string, fromstr, tostr)" function
19936 */
19937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019938f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019939{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019940 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019941 char_u *fromstr;
19942 char_u *tostr;
19943 char_u *p;
19944#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019945 int inlen;
19946 int fromlen;
19947 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019948 int idx;
19949 char_u *cpstr;
19950 int cplen;
19951 int first = TRUE;
19952#endif
19953 char_u buf[NUMBUFLEN];
19954 char_u buf2[NUMBUFLEN];
19955 garray_T ga;
19956
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019957 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019958 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19959 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019960
19961 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019962 rettv->v_type = VAR_STRING;
19963 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019964 if (fromstr == NULL || tostr == NULL)
19965 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019966 ga_init2(&ga, (int)sizeof(char), 80);
19967
19968#ifdef FEAT_MBYTE
19969 if (!has_mbyte)
19970#endif
19971 /* not multi-byte: fromstr and tostr must be the same length */
19972 if (STRLEN(fromstr) != STRLEN(tostr))
19973 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019974#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019975error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019976#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019977 EMSG2(_(e_invarg2), fromstr);
19978 ga_clear(&ga);
19979 return;
19980 }
19981
19982 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019983 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019984 {
19985#ifdef FEAT_MBYTE
19986 if (has_mbyte)
19987 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019988 inlen = (*mb_ptr2len)(in_str);
19989 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019990 cplen = inlen;
19991 idx = 0;
19992 for (p = fromstr; *p != NUL; p += fromlen)
19993 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019994 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019995 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019996 {
19997 for (p = tostr; *p != NUL; p += tolen)
19998 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019999 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020000 if (idx-- == 0)
20001 {
20002 cplen = tolen;
20003 cpstr = p;
20004 break;
20005 }
20006 }
20007 if (*p == NUL) /* tostr is shorter than fromstr */
20008 goto error;
20009 break;
20010 }
20011 ++idx;
20012 }
20013
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020014 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020015 {
20016 /* Check that fromstr and tostr have the same number of
20017 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020018 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020019 first = FALSE;
20020 for (p = tostr; *p != NUL; p += tolen)
20021 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020022 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020023 --idx;
20024 }
20025 if (idx != 0)
20026 goto error;
20027 }
20028
Bram Moolenaarcde88542015-08-11 19:14:00 +020020029 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020030 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020031 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020032
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020033 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020034 }
20035 else
20036#endif
20037 {
20038 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020039 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020040 if (p != NULL)
20041 ga_append(&ga, tostr[p - fromstr]);
20042 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020043 ga_append(&ga, *in_str);
20044 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020045 }
20046 }
20047
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020048 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020049 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020050 ga_append(&ga, NUL);
20051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020052 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020053}
20054
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020055#ifdef FEAT_FLOAT
20056/*
20057 * "trunc({float})" function
20058 */
20059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020060f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020061{
20062 float_T f;
20063
20064 rettv->v_type = VAR_FLOAT;
20065 if (get_float_arg(argvars, &f) == OK)
20066 /* trunc() is not in C90, use floor() or ceil() instead. */
20067 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20068 else
20069 rettv->vval.v_float = 0.0;
20070}
20071#endif
20072
Bram Moolenaar8299df92004-07-10 09:47:34 +000020073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 * "type(expr)" function
20075 */
20076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020077f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020079 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020080
20081 switch (argvars[0].v_type)
20082 {
20083 case VAR_NUMBER: n = 0; break;
20084 case VAR_STRING: n = 1; break;
20085 case VAR_FUNC: n = 2; break;
20086 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020087 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020088 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020089 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020090 if (argvars[0].vval.v_number == VVAL_FALSE
20091 || argvars[0].vval.v_number == VVAL_TRUE)
20092 n = 6;
20093 else
20094 n = 7;
20095 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020096 case VAR_JOB: n = 8; break;
20097 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020098 case VAR_UNKNOWN:
20099 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20100 n = -1;
20101 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020102 }
20103 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104}
20105
20106/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020107 * "undofile(name)" function
20108 */
20109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020110f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020111{
20112 rettv->v_type = VAR_STRING;
20113#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020114 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020115 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020116
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020117 if (*fname == NUL)
20118 {
20119 /* If there is no file name there will be no undo file. */
20120 rettv->vval.v_string = NULL;
20121 }
20122 else
20123 {
20124 char_u *ffname = FullName_save(fname, FALSE);
20125
20126 if (ffname != NULL)
20127 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20128 vim_free(ffname);
20129 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020130 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020131#else
20132 rettv->vval.v_string = NULL;
20133#endif
20134}
20135
20136/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020137 * "undotree()" function
20138 */
20139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020140f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020141{
20142 if (rettv_dict_alloc(rettv) == OK)
20143 {
20144 dict_T *dict = rettv->vval.v_dict;
20145 list_T *list;
20146
Bram Moolenaar730cde92010-06-27 05:18:54 +020020147 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020148 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020149 dict_add_nr_str(dict, "save_last",
20150 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020151 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20152 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020153 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020154
20155 list = list_alloc();
20156 if (list != NULL)
20157 {
20158 u_eval_tree(curbuf->b_u_oldhead, list);
20159 dict_add_list(dict, "entries", list);
20160 }
20161 }
20162}
20163
20164/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020165 * "values(dict)" function
20166 */
20167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020168f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020169{
20170 dict_list(argvars, rettv, 1);
20171}
20172
20173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 * "virtcol(string)" function
20175 */
20176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020177f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178{
20179 colnr_T vcol = 0;
20180 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020181 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020183 fp = var2fpos(&argvars[0], FALSE, &fnum);
20184 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20185 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186 {
20187 getvvcol(curwin, fp, NULL, NULL, &vcol);
20188 ++vcol;
20189 }
20190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020191 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192}
20193
20194/*
20195 * "visualmode()" function
20196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020198f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 char_u str[2];
20201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020202 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 str[0] = curbuf->b_visual_mode_eval;
20204 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020205 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206
20207 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020208 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210}
20211
20212/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020213 * "wildmenumode()" function
20214 */
20215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020216f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020217{
20218#ifdef FEAT_WILDMENU
20219 if (wild_menu_showing)
20220 rettv->vval.v_number = 1;
20221#endif
20222}
20223
20224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 * "winbufnr(nr)" function
20226 */
20227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020228f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229{
20230 win_T *wp;
20231
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020232 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020236 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237}
20238
20239/*
20240 * "wincol()" function
20241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020243f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244{
20245 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020246 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247}
20248
20249/*
20250 * "winheight(nr)" function
20251 */
20252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020253f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254{
20255 win_T *wp;
20256
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020257 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020259 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020261 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262}
20263
20264/*
20265 * "winline()" function
20266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020268f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269{
20270 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020271 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272}
20273
20274/*
20275 * "winnr()" function
20276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020278f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279{
20280 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020281
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020283 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020285 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286}
20287
20288/*
20289 * "winrestcmd()" function
20290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020292f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293{
20294#ifdef FEAT_WINDOWS
20295 win_T *wp;
20296 int winnr = 1;
20297 garray_T ga;
20298 char_u buf[50];
20299
20300 ga_init2(&ga, (int)sizeof(char), 70);
20301 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20302 {
20303 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20304 ga_concat(&ga, buf);
20305# ifdef FEAT_VERTSPLIT
20306 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20307 ga_concat(&ga, buf);
20308# endif
20309 ++winnr;
20310 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020311 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020313 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020315 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020317 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318}
20319
20320/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020321 * "winrestview()" function
20322 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020324f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020325{
20326 dict_T *dict;
20327
20328 if (argvars[0].v_type != VAR_DICT
20329 || (dict = argvars[0].vval.v_dict) == NULL)
20330 EMSG(_(e_invarg));
20331 else
20332 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020333 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20334 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20335 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20336 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020337#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020338 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20339 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020340#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020341 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20342 {
20343 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20344 curwin->w_set_curswant = FALSE;
20345 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020346
Bram Moolenaar82c25852014-05-28 16:47:16 +020020347 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20348 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020349#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020350 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20351 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020352#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020353 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20354 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20355 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20356 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020357
20358 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020359 win_new_height(curwin, curwin->w_height);
20360# ifdef FEAT_VERTSPLIT
20361 win_new_width(curwin, W_WIDTH(curwin));
20362# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020363 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020364
Bram Moolenaarb851a962014-10-31 15:45:52 +010020365 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020366 curwin->w_topline = 1;
20367 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20368 curwin->w_topline = curbuf->b_ml.ml_line_count;
20369#ifdef FEAT_DIFF
20370 check_topfill(curwin, TRUE);
20371#endif
20372 }
20373}
20374
20375/*
20376 * "winsaveview()" function
20377 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020379f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020380{
20381 dict_T *dict;
20382
Bram Moolenaara800b422010-06-27 01:15:55 +020020383 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020384 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020385 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020386
20387 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20388 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20389#ifdef FEAT_VIRTUALEDIT
20390 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20391#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020392 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020393 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20394
20395 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20396#ifdef FEAT_DIFF
20397 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20398#endif
20399 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20400 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20401}
20402
20403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 * "winwidth(nr)" function
20405 */
20406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020407f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408{
20409 win_T *wp;
20410
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020411 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020413 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414 else
20415#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020416 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020418 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419#endif
20420}
20421
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020423 * "wordcount()" function
20424 */
20425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020426f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020427{
20428 if (rettv_dict_alloc(rettv) == FAIL)
20429 return;
20430 cursor_pos_info(rettv->vval.v_dict);
20431}
20432
20433/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020434 * Write list of strings to file
20435 */
20436 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020437write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020438{
20439 listitem_T *li;
20440 int c;
20441 int ret = OK;
20442 char_u *s;
20443
20444 for (li = list->lv_first; li != NULL; li = li->li_next)
20445 {
20446 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20447 {
20448 if (*s == '\n')
20449 c = putc(NUL, fd);
20450 else
20451 c = putc(*s, fd);
20452 if (c == EOF)
20453 {
20454 ret = FAIL;
20455 break;
20456 }
20457 }
20458 if (!binary || li->li_next != NULL)
20459 if (putc('\n', fd) == EOF)
20460 {
20461 ret = FAIL;
20462 break;
20463 }
20464 if (ret == FAIL)
20465 {
20466 EMSG(_(e_write));
20467 break;
20468 }
20469 }
20470 return ret;
20471}
20472
20473/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020474 * "writefile()" function
20475 */
20476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020477f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020478{
20479 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020480 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020481 char_u *fname;
20482 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020483 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020484
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020485 if (check_restricted() || check_secure())
20486 return;
20487
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020488 if (argvars[0].v_type != VAR_LIST)
20489 {
20490 EMSG2(_(e_listarg), "writefile()");
20491 return;
20492 }
20493 if (argvars[0].vval.v_list == NULL)
20494 return;
20495
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020496 if (argvars[2].v_type != VAR_UNKNOWN)
20497 {
20498 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20499 binary = TRUE;
20500 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20501 append = TRUE;
20502 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020503
20504 /* Always open the file in binary mode, library functions have a mind of
20505 * their own about CR-LF conversion. */
20506 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020507 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20508 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020509 {
20510 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20511 ret = -1;
20512 }
20513 else
20514 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020515 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20516 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020517 fclose(fd);
20518 }
20519
20520 rettv->vval.v_number = ret;
20521}
20522
20523/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020524 * "xor(expr, expr)" function
20525 */
20526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020527f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020528{
20529 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20530 ^ get_tv_number_chk(&argvars[1], NULL);
20531}
20532
20533
20534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020536 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 */
20538 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020539var2fpos(
20540 typval_T *varp,
20541 int dollar_lnum, /* TRUE when $ is last line */
20542 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020544 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020546 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547
Bram Moolenaara5525202006-03-02 22:52:09 +000020548 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020549 if (varp->v_type == VAR_LIST)
20550 {
20551 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020552 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020553 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020554 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020555
20556 l = varp->vval.v_list;
20557 if (l == NULL)
20558 return NULL;
20559
20560 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020561 pos.lnum = list_find_nr(l, 0L, &error);
20562 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020563 return NULL; /* invalid line number */
20564
20565 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020566 pos.col = list_find_nr(l, 1L, &error);
20567 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020568 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020569 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020570
20571 /* We accept "$" for the column number: last column. */
20572 li = list_find(l, 1L);
20573 if (li != NULL && li->li_tv.v_type == VAR_STRING
20574 && li->li_tv.vval.v_string != NULL
20575 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20576 pos.col = len + 1;
20577
Bram Moolenaara5525202006-03-02 22:52:09 +000020578 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020579 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020580 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020581 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020582
Bram Moolenaara5525202006-03-02 22:52:09 +000020583#ifdef FEAT_VIRTUALEDIT
20584 /* Get the virtual offset. Defaults to zero. */
20585 pos.coladd = list_find_nr(l, 2L, &error);
20586 if (error)
20587 pos.coladd = 0;
20588#endif
20589
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020590 return &pos;
20591 }
20592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020593 name = get_tv_string_chk(varp);
20594 if (name == NULL)
20595 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020596 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020598 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20599 {
20600 if (VIsual_active)
20601 return &VIsual;
20602 return &curwin->w_cursor;
20603 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020604 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020606 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20608 return NULL;
20609 return pp;
20610 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020611
20612#ifdef FEAT_VIRTUALEDIT
20613 pos.coladd = 0;
20614#endif
20615
Bram Moolenaar477933c2007-07-17 14:32:23 +000020616 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020617 {
20618 pos.col = 0;
20619 if (name[1] == '0') /* "w0": first visible line */
20620 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020621 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020622 pos.lnum = curwin->w_topline;
20623 return &pos;
20624 }
20625 else if (name[1] == '$') /* "w$": last visible line */
20626 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020627 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020628 pos.lnum = curwin->w_botline - 1;
20629 return &pos;
20630 }
20631 }
20632 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020634 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 {
20636 pos.lnum = curbuf->b_ml.ml_line_count;
20637 pos.col = 0;
20638 }
20639 else
20640 {
20641 pos.lnum = curwin->w_cursor.lnum;
20642 pos.col = (colnr_T)STRLEN(ml_get_curline());
20643 }
20644 return &pos;
20645 }
20646 return NULL;
20647}
20648
20649/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020650 * Convert list in "arg" into a position and optional file number.
20651 * When "fnump" is NULL there is no file number, only 3 items.
20652 * Note that the column is passed on as-is, the caller may want to decrement
20653 * it to use 1 for the first column.
20654 * Return FAIL when conversion is not possible, doesn't check the position for
20655 * validity.
20656 */
20657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020658list2fpos(
20659 typval_T *arg,
20660 pos_T *posp,
20661 int *fnump,
20662 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020663{
20664 list_T *l = arg->vval.v_list;
20665 long i = 0;
20666 long n;
20667
Bram Moolenaar493c1782014-05-28 14:34:46 +020020668 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20669 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020670 if (arg->v_type != VAR_LIST
20671 || l == NULL
20672 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020673 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020674 return FAIL;
20675
20676 if (fnump != NULL)
20677 {
20678 n = list_find_nr(l, i++, NULL); /* fnum */
20679 if (n < 0)
20680 return FAIL;
20681 if (n == 0)
20682 n = curbuf->b_fnum; /* current buffer */
20683 *fnump = n;
20684 }
20685
20686 n = list_find_nr(l, i++, NULL); /* lnum */
20687 if (n < 0)
20688 return FAIL;
20689 posp->lnum = n;
20690
20691 n = list_find_nr(l, i++, NULL); /* col */
20692 if (n < 0)
20693 return FAIL;
20694 posp->col = n;
20695
20696#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020697 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020698 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020699 posp->coladd = 0;
20700 else
20701 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020702#endif
20703
Bram Moolenaar493c1782014-05-28 14:34:46 +020020704 if (curswantp != NULL)
20705 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20706
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020707 return OK;
20708}
20709
20710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 * Get the length of an environment variable name.
20712 * Advance "arg" to the first character after the name.
20713 * Return 0 for error.
20714 */
20715 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020716get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717{
20718 char_u *p;
20719 int len;
20720
20721 for (p = *arg; vim_isIDc(*p); ++p)
20722 ;
20723 if (p == *arg) /* no name found */
20724 return 0;
20725
20726 len = (int)(p - *arg);
20727 *arg = p;
20728 return len;
20729}
20730
20731/*
20732 * Get the length of the name of a function or internal variable.
20733 * "arg" is advanced to the first non-white character after the name.
20734 * Return 0 if something is wrong.
20735 */
20736 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020737get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738{
20739 char_u *p;
20740 int len;
20741
20742 /* Find the end of the name. */
20743 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020744 {
20745 if (*p == ':')
20746 {
20747 /* "s:" is start of "s:var", but "n:" is not and can be used in
20748 * slice "[n:]". Also "xx:" is not a namespace. */
20749 len = (int)(p - *arg);
20750 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20751 || len > 1)
20752 break;
20753 }
20754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755 if (p == *arg) /* no name found */
20756 return 0;
20757
20758 len = (int)(p - *arg);
20759 *arg = skipwhite(p);
20760
20761 return len;
20762}
20763
20764/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020765 * Get the length of the name of a variable or function.
20766 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020768 * Return -1 if curly braces expansion failed.
20769 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770 * If the name contains 'magic' {}'s, expand them and return the
20771 * expanded name in an allocated string via 'alias' - caller must free.
20772 */
20773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020774get_name_len(
20775 char_u **arg,
20776 char_u **alias,
20777 int evaluate,
20778 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779{
20780 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 char_u *p;
20782 char_u *expr_start;
20783 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784
20785 *alias = NULL; /* default to no alias */
20786
20787 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20788 && (*arg)[2] == (int)KE_SNR)
20789 {
20790 /* hard coded <SNR>, already translated */
20791 *arg += 3;
20792 return get_id_len(arg) + 3;
20793 }
20794 len = eval_fname_script(*arg);
20795 if (len > 0)
20796 {
20797 /* literal "<SID>", "s:" or "<SNR>" */
20798 *arg += len;
20799 }
20800
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020802 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020804 p = find_name_end(*arg, &expr_start, &expr_end,
20805 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 if (expr_start != NULL)
20807 {
20808 char_u *temp_string;
20809
20810 if (!evaluate)
20811 {
20812 len += (int)(p - *arg);
20813 *arg = skipwhite(p);
20814 return len;
20815 }
20816
20817 /*
20818 * Include any <SID> etc in the expanded string:
20819 * Thus the -len here.
20820 */
20821 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20822 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020823 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 *alias = temp_string;
20825 *arg = skipwhite(p);
20826 return (int)STRLEN(temp_string);
20827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828
20829 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020830 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 EMSG2(_(e_invexpr2), *arg);
20832
20833 return len;
20834}
20835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020836/*
20837 * Find the end of a variable or function name, taking care of magic braces.
20838 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20839 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020840 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020841 * Return a pointer to just after the name. Equal to "arg" if there is no
20842 * valid name.
20843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020845find_name_end(
20846 char_u *arg,
20847 char_u **expr_start,
20848 char_u **expr_end,
20849 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020851 int mb_nest = 0;
20852 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020854 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020856 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020858 *expr_start = NULL;
20859 *expr_end = NULL;
20860 }
20861
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020862 /* Quick check for valid starting character. */
20863 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20864 return arg;
20865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020866 for (p = arg; *p != NUL
20867 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020868 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020869 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020870 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020871 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020872 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020873 if (*p == '\'')
20874 {
20875 /* skip over 'string' to avoid counting [ and ] inside it. */
20876 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20877 ;
20878 if (*p == NUL)
20879 break;
20880 }
20881 else if (*p == '"')
20882 {
20883 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20884 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20885 if (*p == '\\' && p[1] != NUL)
20886 ++p;
20887 if (*p == NUL)
20888 break;
20889 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020890 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20891 {
20892 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020893 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020894 len = (int)(p - arg);
20895 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020896 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020897 break;
20898 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020900 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020902 if (*p == '[')
20903 ++br_nest;
20904 else if (*p == ']')
20905 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020908 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020910 if (*p == '{')
20911 {
20912 mb_nest++;
20913 if (expr_start != NULL && *expr_start == NULL)
20914 *expr_start = p;
20915 }
20916 else if (*p == '}')
20917 {
20918 mb_nest--;
20919 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20920 *expr_end = p;
20921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 }
20924
20925 return p;
20926}
20927
20928/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020929 * Expands out the 'magic' {}'s in a variable/function name.
20930 * Note that this can call itself recursively, to deal with
20931 * constructs like foo{bar}{baz}{bam}
20932 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20933 * "in_start" ^
20934 * "expr_start" ^
20935 * "expr_end" ^
20936 * "in_end" ^
20937 *
20938 * Returns a new allocated string, which the caller must free.
20939 * Returns NULL for failure.
20940 */
20941 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020942make_expanded_name(
20943 char_u *in_start,
20944 char_u *expr_start,
20945 char_u *expr_end,
20946 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020947{
20948 char_u c1;
20949 char_u *retval = NULL;
20950 char_u *temp_result;
20951 char_u *nextcmd = NULL;
20952
20953 if (expr_end == NULL || in_end == NULL)
20954 return NULL;
20955 *expr_start = NUL;
20956 *expr_end = NUL;
20957 c1 = *in_end;
20958 *in_end = NUL;
20959
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020960 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020961 if (temp_result != NULL && nextcmd == NULL)
20962 {
20963 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20964 + (in_end - expr_end) + 1));
20965 if (retval != NULL)
20966 {
20967 STRCPY(retval, in_start);
20968 STRCAT(retval, temp_result);
20969 STRCAT(retval, expr_end + 1);
20970 }
20971 }
20972 vim_free(temp_result);
20973
20974 *in_end = c1; /* put char back for error messages */
20975 *expr_start = '{';
20976 *expr_end = '}';
20977
20978 if (retval != NULL)
20979 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020980 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020981 if (expr_start != NULL)
20982 {
20983 /* Further expansion! */
20984 temp_result = make_expanded_name(retval, expr_start,
20985 expr_end, temp_result);
20986 vim_free(retval);
20987 retval = temp_result;
20988 }
20989 }
20990
20991 return retval;
20992}
20993
20994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020996 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 */
20998 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020999eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021001 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21002}
21003
21004/*
21005 * Return TRUE if character "c" can be used as the first character in a
21006 * variable or function name (excluding '{' and '}').
21007 */
21008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021009eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021010{
21011 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012}
21013
21014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 * Set number v: variable to "val".
21016 */
21017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021018set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021020 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021}
21022
21023/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021024 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 */
21026 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021027get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021029 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030}
21031
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021032/*
21033 * Get string v: variable value. Uses a static buffer, can only be used once.
21034 */
21035 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021036get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021037{
21038 return get_tv_string(&vimvars[idx].vv_tv);
21039}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021040
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021042 * Get List v: variable value. Caller must take care of reference count when
21043 * needed.
21044 */
21045 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021046get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021047{
21048 return vimvars[idx].vv_list;
21049}
21050
21051/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021052 * Set v:char to character "c".
21053 */
21054 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021055set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021056{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021057 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021058
21059#ifdef FEAT_MBYTE
21060 if (has_mbyte)
21061 buf[(*mb_char2bytes)(c, buf)] = NUL;
21062 else
21063#endif
21064 {
21065 buf[0] = c;
21066 buf[1] = NUL;
21067 }
21068 set_vim_var_string(VV_CHAR, buf, -1);
21069}
21070
21071/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021072 * Set v:count to "count" and v:count1 to "count1".
21073 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 */
21075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021076set_vcount(
21077 long count,
21078 long count1,
21079 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021081 if (set_prevcount)
21082 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021083 vimvars[VV_COUNT].vv_nr = count;
21084 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085}
21086
21087/*
21088 * Set string v: variable to a copy of "val".
21089 */
21090 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021091set_vim_var_string(
21092 int idx,
21093 char_u *val,
21094 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095{
Bram Moolenaara542c682016-01-31 16:28:04 +010021096 clear_tv(&vimvars[idx].vv_di.di_tv);
21097 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021099 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021101 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021103 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104}
21105
21106/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021107 * Set List v: variable to "val".
21108 */
21109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021110set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021111{
Bram Moolenaara542c682016-01-31 16:28:04 +010021112 clear_tv(&vimvars[idx].vv_di.di_tv);
21113 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021114 vimvars[idx].vv_list = val;
21115 if (val != NULL)
21116 ++val->lv_refcount;
21117}
21118
21119/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021120 * Set Dictionary v: variable to "val".
21121 */
21122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021123set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021124{
21125 int todo;
21126 hashitem_T *hi;
21127
Bram Moolenaara542c682016-01-31 16:28:04 +010021128 clear_tv(&vimvars[idx].vv_di.di_tv);
21129 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021130 vimvars[idx].vv_dict = val;
21131 if (val != NULL)
21132 {
21133 ++val->dv_refcount;
21134
21135 /* Set readonly */
21136 todo = (int)val->dv_hashtab.ht_used;
21137 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21138 {
21139 if (HASHITEM_EMPTY(hi))
21140 continue;
21141 --todo;
21142 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21143 }
21144 }
21145}
21146
21147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 * Set v:register if needed.
21149 */
21150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021151set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152{
21153 char_u regname;
21154
21155 if (c == 0 || c == ' ')
21156 regname = '"';
21157 else
21158 regname = c;
21159 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021160 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 set_vim_var_string(VV_REG, &regname, 1);
21162}
21163
21164/*
21165 * Get or set v:exception. If "oldval" == NULL, return the current value.
21166 * Otherwise, restore the value to "oldval" and return NULL.
21167 * Must always be called in pairs to save and restore v:exception! Does not
21168 * take care of memory allocations.
21169 */
21170 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021171v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172{
21173 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021174 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175
Bram Moolenaare9a41262005-01-15 22:18:47 +000021176 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177 return NULL;
21178}
21179
21180/*
21181 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21182 * Otherwise, restore the value to "oldval" and return NULL.
21183 * Must always be called in pairs to save and restore v:throwpoint! Does not
21184 * take care of memory allocations.
21185 */
21186 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021187v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188{
21189 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021190 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191
Bram Moolenaare9a41262005-01-15 22:18:47 +000021192 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 return NULL;
21194}
21195
21196#if defined(FEAT_AUTOCMD) || defined(PROTO)
21197/*
21198 * Set v:cmdarg.
21199 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21200 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21201 * Must always be called in pairs!
21202 */
21203 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021204set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205{
21206 char_u *oldval;
21207 char_u *newval;
21208 unsigned len;
21209
Bram Moolenaare9a41262005-01-15 22:18:47 +000021210 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021211 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021213 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021214 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021215 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 }
21217
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021218 if (eap->force_bin == FORCE_BIN)
21219 len = 6;
21220 else if (eap->force_bin == FORCE_NOBIN)
21221 len = 8;
21222 else
21223 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021224
21225 if (eap->read_edit)
21226 len += 7;
21227
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021228 if (eap->force_ff != 0)
21229 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21230# ifdef FEAT_MBYTE
21231 if (eap->force_enc != 0)
21232 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021233 if (eap->bad_char != 0)
21234 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021235# endif
21236
21237 newval = alloc(len + 1);
21238 if (newval == NULL)
21239 return NULL;
21240
21241 if (eap->force_bin == FORCE_BIN)
21242 sprintf((char *)newval, " ++bin");
21243 else if (eap->force_bin == FORCE_NOBIN)
21244 sprintf((char *)newval, " ++nobin");
21245 else
21246 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021247
21248 if (eap->read_edit)
21249 STRCAT(newval, " ++edit");
21250
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021251 if (eap->force_ff != 0)
21252 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21253 eap->cmd + eap->force_ff);
21254# ifdef FEAT_MBYTE
21255 if (eap->force_enc != 0)
21256 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21257 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021258 if (eap->bad_char == BAD_KEEP)
21259 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21260 else if (eap->bad_char == BAD_DROP)
21261 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21262 else if (eap->bad_char != 0)
21263 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021264# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021265 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021266 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267}
21268#endif
21269
21270/*
21271 * Get the value of internal variable "name".
21272 * Return OK or FAIL.
21273 */
21274 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021275get_var_tv(
21276 char_u *name,
21277 int len, /* length of "name" */
21278 typval_T *rettv, /* NULL when only checking existence */
21279 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21280 int verbose, /* may give error message */
21281 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282{
21283 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021284 typval_T *tv = NULL;
21285 typval_T atv;
21286 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288
21289 /* truncate the name, so that we can use strcmp() */
21290 cc = name[len];
21291 name[len] = NUL;
21292
21293 /*
21294 * Check for "b:changedtick".
21295 */
21296 if (STRCMP(name, "b:changedtick") == 0)
21297 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021298 atv.v_type = VAR_NUMBER;
21299 atv.vval.v_number = curbuf->b_changedtick;
21300 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 }
21302
21303 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 * Check for user-defined variables.
21305 */
21306 else
21307 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021308 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021310 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021311 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021312 if (dip != NULL)
21313 *dip = v;
21314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 }
21316
Bram Moolenaare9a41262005-01-15 22:18:47 +000021317 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021319 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021320 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321 ret = FAIL;
21322 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021323 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021324 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325
21326 name[len] = cc;
21327
21328 return ret;
21329}
21330
21331/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021332 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21333 * Also handle function call with Funcref variable: func(expr)
21334 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21335 */
21336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021337handle_subscript(
21338 char_u **arg,
21339 typval_T *rettv,
21340 int evaluate, /* do more than finding the end */
21341 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021342{
21343 int ret = OK;
21344 dict_T *selfdict = NULL;
21345 char_u *s;
21346 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021347 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021348
21349 while (ret == OK
21350 && (**arg == '['
21351 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021352 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021353 && !vim_iswhite(*(*arg - 1)))
21354 {
21355 if (**arg == '(')
21356 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021357 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021358 if (evaluate)
21359 {
21360 functv = *rettv;
21361 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021362
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021363 /* Invoke the function. Recursive! */
21364 s = functv.vval.v_string;
21365 }
21366 else
21367 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021368 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021369 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21370 &len, evaluate, selfdict);
21371
21372 /* Clear the funcref afterwards, so that deleting it while
21373 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021374 if (evaluate)
21375 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021376
21377 /* Stop the expression evaluation when immediately aborting on
21378 * error, or when an interrupt occurred or an exception was thrown
21379 * but not caught. */
21380 if (aborting())
21381 {
21382 if (ret == OK)
21383 clear_tv(rettv);
21384 ret = FAIL;
21385 }
21386 dict_unref(selfdict);
21387 selfdict = NULL;
21388 }
21389 else /* **arg == '[' || **arg == '.' */
21390 {
21391 dict_unref(selfdict);
21392 if (rettv->v_type == VAR_DICT)
21393 {
21394 selfdict = rettv->vval.v_dict;
21395 if (selfdict != NULL)
21396 ++selfdict->dv_refcount;
21397 }
21398 else
21399 selfdict = NULL;
21400 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21401 {
21402 clear_tv(rettv);
21403 ret = FAIL;
21404 }
21405 }
21406 }
21407 dict_unref(selfdict);
21408 return ret;
21409}
21410
21411/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021412 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021413 * value).
21414 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021415 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021416alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021417{
Bram Moolenaar33570922005-01-25 22:26:29 +000021418 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021419}
21420
21421/*
21422 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423 * The string "s" must have been allocated, it is consumed.
21424 * Return NULL for out of memory, the variable otherwise.
21425 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021426 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021427alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428{
Bram Moolenaar33570922005-01-25 22:26:29 +000021429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021431 rettv = alloc_tv();
21432 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434 rettv->v_type = VAR_STRING;
21435 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021436 }
21437 else
21438 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021439 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440}
21441
21442/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021443 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021446free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447{
21448 if (varp != NULL)
21449 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021450 switch (varp->v_type)
21451 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021452 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021453 func_unref(varp->vval.v_string);
21454 /*FALLTHROUGH*/
21455 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021456 vim_free(varp->vval.v_string);
21457 break;
21458 case VAR_LIST:
21459 list_unref(varp->vval.v_list);
21460 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021461 case VAR_DICT:
21462 dict_unref(varp->vval.v_dict);
21463 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021464 case VAR_JOB:
21465#ifdef FEAT_JOB
21466 job_unref(varp->vval.v_job);
21467 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021468#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021469 case VAR_CHANNEL:
21470#ifdef FEAT_CHANNEL
21471 channel_unref(varp->vval.v_channel);
21472 break;
21473#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021474 case VAR_NUMBER:
21475 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021476 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021477 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021478 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 vim_free(varp);
21481 }
21482}
21483
21484/*
21485 * Free the memory for a variable value and set the value to NULL or 0.
21486 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021487 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021488clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489{
21490 if (varp != NULL)
21491 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021492 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021494 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021495 func_unref(varp->vval.v_string);
21496 /*FALLTHROUGH*/
21497 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021498 vim_free(varp->vval.v_string);
21499 varp->vval.v_string = NULL;
21500 break;
21501 case VAR_LIST:
21502 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021503 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021504 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021505 case VAR_DICT:
21506 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021507 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021508 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021509 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021510 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021511 varp->vval.v_number = 0;
21512 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021513 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021514#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021515 varp->vval.v_float = 0.0;
21516 break;
21517#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021518 case VAR_JOB:
21519#ifdef FEAT_JOB
21520 job_unref(varp->vval.v_job);
21521 varp->vval.v_job = NULL;
21522#endif
21523 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021524 case VAR_CHANNEL:
21525#ifdef FEAT_CHANNEL
21526 channel_unref(varp->vval.v_channel);
21527 varp->vval.v_channel = NULL;
21528#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021529 case VAR_UNKNOWN:
21530 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021532 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 }
21534}
21535
21536/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021537 * Set the value of a variable to NULL without freeing items.
21538 */
21539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021540init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021541{
21542 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021543 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021544}
21545
21546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 * Get the number value of a variable.
21548 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021549 * For incompatible types, return 0.
21550 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21551 * caller of incompatible types: it sets *denote to TRUE if "denote"
21552 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 */
21554 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021555get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021557 int error = FALSE;
21558
21559 return get_tv_number_chk(varp, &error); /* return 0L on error */
21560}
21561
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021562 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021563get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021564{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021565 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021567 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021569 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021570 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021571 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021572#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021573 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021574 break;
21575#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021576 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021577 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021578 break;
21579 case VAR_STRING:
21580 if (varp->vval.v_string != NULL)
21581 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021582 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021583 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021584 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021585 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021586 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021587 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021588 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021589 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021590 case VAR_SPECIAL:
21591 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21592 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021593 case VAR_JOB:
21594#ifdef FEAT_JOB
21595 EMSG(_("E910: Using a Job as a Number"));
21596 break;
21597#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021598 case VAR_CHANNEL:
21599#ifdef FEAT_CHANNEL
21600 EMSG(_("E913: Using a Channel as a Number"));
21601 break;
21602#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021603 case VAR_UNKNOWN:
21604 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021605 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021607 if (denote == NULL) /* useful for values that must be unsigned */
21608 n = -1;
21609 else
21610 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021611 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612}
21613
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021614#ifdef FEAT_FLOAT
21615 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021616get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021617{
21618 switch (varp->v_type)
21619 {
21620 case VAR_NUMBER:
21621 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021622 case VAR_FLOAT:
21623 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021624 case VAR_FUNC:
21625 EMSG(_("E891: Using a Funcref as a Float"));
21626 break;
21627 case VAR_STRING:
21628 EMSG(_("E892: Using a String as a Float"));
21629 break;
21630 case VAR_LIST:
21631 EMSG(_("E893: Using a List as a Float"));
21632 break;
21633 case VAR_DICT:
21634 EMSG(_("E894: Using a Dictionary as a Float"));
21635 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021636 case VAR_SPECIAL:
21637 EMSG(_("E907: Using a special value as a Float"));
21638 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021639 case VAR_JOB:
21640# ifdef FEAT_JOB
21641 EMSG(_("E911: Using a Job as a Float"));
21642 break;
21643# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021644 case VAR_CHANNEL:
21645# ifdef FEAT_CHANNEL
21646 EMSG(_("E914: Using a Channel as a Float"));
21647 break;
21648# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021649 case VAR_UNKNOWN:
21650 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021651 break;
21652 }
21653 return 0;
21654}
21655#endif
21656
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021658 * Get the lnum from the first argument.
21659 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021660 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 */
21662 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021663get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664{
Bram Moolenaar33570922005-01-25 22:26:29 +000021665 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 linenr_T lnum;
21667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021668 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 if (lnum == 0) /* no valid number, try using line() */
21670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021671 rettv.v_type = VAR_NUMBER;
21672 f_line(argvars, &rettv);
21673 lnum = rettv.vval.v_number;
21674 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 }
21676 return lnum;
21677}
21678
21679/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021680 * Get the lnum from the first argument.
21681 * Also accepts "$", then "buf" is used.
21682 * Returns 0 on error.
21683 */
21684 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021685get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021686{
21687 if (argvars[0].v_type == VAR_STRING
21688 && argvars[0].vval.v_string != NULL
21689 && argvars[0].vval.v_string[0] == '$'
21690 && buf != NULL)
21691 return buf->b_ml.ml_line_count;
21692 return get_tv_number_chk(&argvars[0], NULL);
21693}
21694
21695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 * Get the string value of a variable.
21697 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021698 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21699 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 * If the String variable has never been set, return an empty string.
21701 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021702 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21703 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 */
21705 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021706get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707{
21708 static char_u mybuf[NUMBUFLEN];
21709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021710 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711}
21712
21713 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021714get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021716 char_u *res = get_tv_string_buf_chk(varp, buf);
21717
21718 return res != NULL ? res : (char_u *)"";
21719}
21720
Bram Moolenaar7d647822014-04-05 21:28:56 +020021721/*
21722 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21723 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021724 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021725get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021726{
21727 static char_u mybuf[NUMBUFLEN];
21728
21729 return get_tv_string_buf_chk(varp, mybuf);
21730}
21731
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021732 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021733get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021734{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021735 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021737 case VAR_NUMBER:
21738 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21739 return buf;
21740 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021741 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021742 break;
21743 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021744 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021745 break;
21746 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021747 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021748 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021749 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021750#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021751 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021752 break;
21753#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021754 case VAR_STRING:
21755 if (varp->vval.v_string != NULL)
21756 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021757 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021758 case VAR_SPECIAL:
21759 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21760 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021761 case VAR_JOB:
21762#ifdef FEAT_JOB
21763 {
21764 job_T *job = varp->vval.v_job;
21765 char *status = job->jv_status == JOB_FAILED ? "fail"
21766 : job->jv_status == JOB_ENDED ? "dead"
21767 : "run";
21768# ifdef UNIX
21769 vim_snprintf((char *)buf, NUMBUFLEN,
21770 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021771# elif defined(WIN32)
21772 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021773 "process %ld %s",
21774 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021775 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021776# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021777 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021778 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21779# endif
21780 return buf;
21781 }
21782#endif
21783 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021784 case VAR_CHANNEL:
21785#ifdef FEAT_CHANNEL
21786 {
21787 channel_T *channel = varp->vval.v_channel;
21788 char *status = channel_status(channel);
21789
21790 vim_snprintf((char *)buf, NUMBUFLEN,
21791 "channel %d %s", channel->ch_id, status);
21792 return buf;
21793 }
21794#endif
21795 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021796 case VAR_UNKNOWN:
21797 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021798 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021800 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801}
21802
21803/*
21804 * Find variable "name" in the list of variables.
21805 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021806 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021807 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021808 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021810 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021811find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021814 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815
Bram Moolenaara7043832005-01-21 11:56:39 +000021816 ht = find_var_ht(name, &varname);
21817 if (htp != NULL)
21818 *htp = ht;
21819 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021821 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822}
21823
21824/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021825 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021826 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021828 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021829find_var_in_ht(
21830 hashtab_T *ht,
21831 int htname,
21832 char_u *varname,
21833 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021834{
Bram Moolenaar33570922005-01-25 22:26:29 +000021835 hashitem_T *hi;
21836
21837 if (*varname == NUL)
21838 {
21839 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021840 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021841 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021842 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021843 case 'g': return &globvars_var;
21844 case 'v': return &vimvars_var;
21845 case 'b': return &curbuf->b_bufvar;
21846 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021847#ifdef FEAT_WINDOWS
21848 case 't': return &curtab->tp_winvar;
21849#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021850 case 'l': return current_funccal == NULL
21851 ? NULL : &current_funccal->l_vars_var;
21852 case 'a': return current_funccal == NULL
21853 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021854 }
21855 return NULL;
21856 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021857
21858 hi = hash_find(ht, varname);
21859 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021860 {
21861 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021862 * worked find the variable again. Don't auto-load a script if it was
21863 * loaded already, otherwise it would be loaded every time when
21864 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021865 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021866 {
21867 /* Note: script_autoload() may make "hi" invalid. It must either
21868 * be obtained again or not used. */
21869 if (!script_autoload(varname, FALSE) || aborting())
21870 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021871 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021872 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021873 if (HASHITEM_EMPTY(hi))
21874 return NULL;
21875 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021876 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021877}
21878
21879/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021880 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021881 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021882 * Set "varname" to the start of name without ':'.
21883 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021884 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021885find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021887 hashitem_T *hi;
21888
Bram Moolenaar73627d02015-08-11 15:46:09 +020021889 if (name[0] == NUL)
21890 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 if (name[1] != ':')
21892 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021893 /* The name must not start with a colon or #. */
21894 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895 return NULL;
21896 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021897
21898 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021899 hi = hash_find(&compat_hashtab, name);
21900 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021901 return &compat_hashtab;
21902
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021904 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021905 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 }
21907 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021908 if (*name == 'g') /* global variable */
21909 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021910 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21911 */
21912 if (vim_strchr(name + 2, ':') != NULL
21913 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021914 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021916 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021918 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021919#ifdef FEAT_WINDOWS
21920 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021921 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021922#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021923 if (*name == 'v') /* v: variable */
21924 return &vimvarht;
21925 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021926 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021927 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021928 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 if (*name == 's' /* script variable */
21930 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21931 return &SCRIPT_VARS(current_SID);
21932 return NULL;
21933}
21934
21935/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021936 * Get function call environment based on bactrace debug level
21937 */
21938 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021939get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021940{
21941 int i;
21942 funccall_T *funccal;
21943 funccall_T *temp_funccal;
21944
21945 funccal = current_funccal;
21946 if (debug_backtrace_level > 0)
21947 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021948 for (i = 0; i < debug_backtrace_level; i++)
21949 {
21950 temp_funccal = funccal->caller;
21951 if (temp_funccal)
21952 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021953 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021954 /* backtrace level overflow. reset to max */
21955 debug_backtrace_level = i;
21956 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021957 }
21958 return funccal;
21959}
21960
21961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021963 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 * Returns NULL when it doesn't exist.
21965 */
21966 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021967get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968{
Bram Moolenaar33570922005-01-25 22:26:29 +000021969 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021971 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 if (v == NULL)
21973 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021974 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975}
21976
21977/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021978 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 * sourcing this script and when executing functions defined in the script.
21980 */
21981 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021982new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983{
Bram Moolenaara7043832005-01-21 11:56:39 +000021984 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021985 hashtab_T *ht;
21986 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021987
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21989 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021990 /* Re-allocating ga_data means that an ht_array pointing to
21991 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021992 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021993 for (i = 1; i <= ga_scripts.ga_len; ++i)
21994 {
21995 ht = &SCRIPT_VARS(i);
21996 if (ht->ht_mask == HT_INIT_SIZE - 1)
21997 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021998 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021999 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022000 }
22001
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 while (ga_scripts.ga_len < id)
22003 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022004 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022005 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022006 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 }
22009 }
22010}
22011
22012/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022013 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22014 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015 */
22016 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022017init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018{
Bram Moolenaar33570922005-01-25 22:26:29 +000022019 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022020 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022021 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022022 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022023 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022024 dict_var->di_tv.vval.v_dict = dict;
22025 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022026 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022027 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22028 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029}
22030
22031/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022032 * Unreference a dictionary initialized by init_var_dict().
22033 */
22034 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022035unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022036{
22037 /* Now the dict needs to be freed if no one else is using it, go back to
22038 * normal reference counting. */
22039 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22040 dict_unref(dict);
22041}
22042
22043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022045 * Frees all allocated variables and the value they contain.
22046 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 */
22048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022049vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022050{
22051 vars_clear_ext(ht, TRUE);
22052}
22053
22054/*
22055 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22056 */
22057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022058vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059{
Bram Moolenaara7043832005-01-21 11:56:39 +000022060 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022061 hashitem_T *hi;
22062 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063
Bram Moolenaar33570922005-01-25 22:26:29 +000022064 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022065 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022066 for (hi = ht->ht_array; todo > 0; ++hi)
22067 {
22068 if (!HASHITEM_EMPTY(hi))
22069 {
22070 --todo;
22071
Bram Moolenaar33570922005-01-25 22:26:29 +000022072 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022073 * ht_array might change then. hash_clear() takes care of it
22074 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022075 v = HI2DI(hi);
22076 if (free_val)
22077 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022078 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022079 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022080 }
22081 }
22082 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022083 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084}
22085
Bram Moolenaara7043832005-01-21 11:56:39 +000022086/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022087 * Delete a variable from hashtab "ht" at item "hi".
22088 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022091delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092{
Bram Moolenaar33570922005-01-25 22:26:29 +000022093 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022094
22095 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022096 clear_tv(&di->di_tv);
22097 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098}
22099
22100/*
22101 * List the value of one internal variable.
22102 */
22103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022104list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022106 char_u *tofree;
22107 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022108 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022109
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022110 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022111 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022112 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022113 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114}
22115
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022117list_one_var_a(
22118 char_u *prefix,
22119 char_u *name,
22120 int type,
22121 char_u *string,
22122 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123{
Bram Moolenaar31859182007-08-14 20:41:13 +000022124 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22125 msg_start();
22126 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 if (name != NULL) /* "a:" vars don't have a name stored */
22128 msg_puts(name);
22129 msg_putchar(' ');
22130 msg_advance(22);
22131 if (type == VAR_NUMBER)
22132 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022133 else if (type == VAR_FUNC)
22134 msg_putchar('*');
22135 else if (type == VAR_LIST)
22136 {
22137 msg_putchar('[');
22138 if (*string == '[')
22139 ++string;
22140 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022141 else if (type == VAR_DICT)
22142 {
22143 msg_putchar('{');
22144 if (*string == '{')
22145 ++string;
22146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147 else
22148 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022149
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022151
22152 if (type == VAR_FUNC)
22153 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022154 if (*first)
22155 {
22156 msg_clr_eos();
22157 *first = FALSE;
22158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159}
22160
22161/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022162 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 * If the variable already exists, the value is updated.
22164 * Otherwise the variable is created.
22165 */
22166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022167set_var(
22168 char_u *name,
22169 typval_T *tv,
22170 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171{
Bram Moolenaar33570922005-01-25 22:26:29 +000022172 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022174 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022176 ht = find_var_ht(name, &varname);
22177 if (ht == NULL || *varname == NUL)
22178 {
22179 EMSG2(_(e_illvar), name);
22180 return;
22181 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022182 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022183
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022184 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22185 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022186
Bram Moolenaar33570922005-01-25 22:26:29 +000022187 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022189 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022190 if (var_check_ro(v->di_flags, name, FALSE)
22191 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022192 return;
22193 if (v->di_tv.v_type != tv->v_type
22194 && !((v->di_tv.v_type == VAR_STRING
22195 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022196 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022197 || tv->v_type == VAR_NUMBER))
22198#ifdef FEAT_FLOAT
22199 && !((v->di_tv.v_type == VAR_NUMBER
22200 || v->di_tv.v_type == VAR_FLOAT)
22201 && (tv->v_type == VAR_NUMBER
22202 || tv->v_type == VAR_FLOAT))
22203#endif
22204 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022205 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022206 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022207 return;
22208 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022209
22210 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022211 * Handle setting internal v: variables separately where needed to
22212 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022213 */
22214 if (ht == &vimvarht)
22215 {
22216 if (v->di_tv.v_type == VAR_STRING)
22217 {
22218 vim_free(v->di_tv.vval.v_string);
22219 if (copy || tv->v_type != VAR_STRING)
22220 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22221 else
22222 {
22223 /* Take over the string to avoid an extra alloc/free. */
22224 v->di_tv.vval.v_string = tv->vval.v_string;
22225 tv->vval.v_string = NULL;
22226 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022227 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022228 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022229 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022230 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022231 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022232 if (STRCMP(varname, "searchforward") == 0)
22233 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022234#ifdef FEAT_SEARCH_EXTRA
22235 else if (STRCMP(varname, "hlsearch") == 0)
22236 {
22237 no_hlsearch = !v->di_tv.vval.v_number;
22238 redraw_all_later(SOME_VALID);
22239 }
22240#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022241 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022242 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022243 else if (v->di_tv.v_type != tv->v_type)
22244 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022245 }
22246
22247 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 }
22249 else /* add a new variable */
22250 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022251 /* Can't add "v:" variable. */
22252 if (ht == &vimvarht)
22253 {
22254 EMSG2(_(e_illvar), name);
22255 return;
22256 }
22257
Bram Moolenaar92124a32005-06-17 22:03:40 +000022258 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022259 if (!valid_varname(varname))
22260 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022261
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022262 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22263 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022264 if (v == NULL)
22265 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022266 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022267 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022269 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270 return;
22271 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022272 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022274
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022275 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022276 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022277 else
22278 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022279 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022280 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022281 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283}
22284
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022285/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022286 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022287 * Also give an error message.
22288 */
22289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022290var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022291{
22292 if (flags & DI_FLAGS_RO)
22293 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022294 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022295 return TRUE;
22296 }
22297 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22298 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022299 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022300 return TRUE;
22301 }
22302 return FALSE;
22303}
22304
22305/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022306 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22307 * Also give an error message.
22308 */
22309 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022310var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022311{
22312 if (flags & DI_FLAGS_FIX)
22313 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022314 EMSG2(_("E795: Cannot delete variable %s"),
22315 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022316 return TRUE;
22317 }
22318 return FALSE;
22319}
22320
22321/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022322 * Check if a funcref is assigned to a valid variable name.
22323 * Return TRUE and give an error if not.
22324 */
22325 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022326var_check_func_name(
22327 char_u *name, /* points to start of variable name */
22328 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022329{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022330 /* Allow for w: b: s: and t:. */
22331 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022332 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22333 ? name[2] : name[0]))
22334 {
22335 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22336 name);
22337 return TRUE;
22338 }
22339 /* Don't allow hiding a function. When "v" is not NULL we might be
22340 * assigning another function to the same var, the type is checked
22341 * below. */
22342 if (new_var && function_exists(name))
22343 {
22344 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22345 name);
22346 return TRUE;
22347 }
22348 return FALSE;
22349}
22350
22351/*
22352 * Check if a variable name is valid.
22353 * Return FALSE and give an error if not.
22354 */
22355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022356valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022357{
22358 char_u *p;
22359
22360 for (p = varname; *p != NUL; ++p)
22361 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22362 && *p != AUTOLOAD_CHAR)
22363 {
22364 EMSG2(_(e_illvar), varname);
22365 return FALSE;
22366 }
22367 return TRUE;
22368}
22369
22370/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022371 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022372 * Also give an error message, using "name" or _("name") when use_gettext is
22373 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022374 */
22375 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022376tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022377{
22378 if (lock & VAR_LOCKED)
22379 {
22380 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022381 name == NULL ? (char_u *)_("Unknown")
22382 : use_gettext ? (char_u *)_(name)
22383 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022384 return TRUE;
22385 }
22386 if (lock & VAR_FIXED)
22387 {
22388 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022389 name == NULL ? (char_u *)_("Unknown")
22390 : use_gettext ? (char_u *)_(name)
22391 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022392 return TRUE;
22393 }
22394 return FALSE;
22395}
22396
22397/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022398 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022399 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022400 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022401 * It is OK for "from" and "to" to point to the same item. This is used to
22402 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022403 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022404 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022405copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022407 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022408 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022409 switch (from->v_type)
22410 {
22411 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022412 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022413 to->vval.v_number = from->vval.v_number;
22414 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022415 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022416#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022417 to->vval.v_float = from->vval.v_float;
22418 break;
22419#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022420 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022421#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022422 to->vval.v_job = from->vval.v_job;
22423 ++to->vval.v_job->jv_refcount;
22424 break;
22425#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022426 case VAR_CHANNEL:
22427#ifdef FEAT_CHANNEL
22428 to->vval.v_channel = from->vval.v_channel;
22429 ++to->vval.v_channel->ch_refcount;
22430 break;
22431#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022432 case VAR_STRING:
22433 case VAR_FUNC:
22434 if (from->vval.v_string == NULL)
22435 to->vval.v_string = NULL;
22436 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022437 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022438 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022439 if (from->v_type == VAR_FUNC)
22440 func_ref(to->vval.v_string);
22441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022442 break;
22443 case VAR_LIST:
22444 if (from->vval.v_list == NULL)
22445 to->vval.v_list = NULL;
22446 else
22447 {
22448 to->vval.v_list = from->vval.v_list;
22449 ++to->vval.v_list->lv_refcount;
22450 }
22451 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022452 case VAR_DICT:
22453 if (from->vval.v_dict == NULL)
22454 to->vval.v_dict = NULL;
22455 else
22456 {
22457 to->vval.v_dict = from->vval.v_dict;
22458 ++to->vval.v_dict->dv_refcount;
22459 }
22460 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022461 case VAR_UNKNOWN:
22462 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022463 break;
22464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465}
22466
22467/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022468 * Make a copy of an item.
22469 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022470 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22471 * reference to an already copied list/dict can be used.
22472 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022473 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022475item_copy(
22476 typval_T *from,
22477 typval_T *to,
22478 int deep,
22479 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022480{
22481 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022482 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022483
Bram Moolenaar33570922005-01-25 22:26:29 +000022484 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022485 {
22486 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022487 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022488 }
22489 ++recurse;
22490
22491 switch (from->v_type)
22492 {
22493 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022494 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022495 case VAR_STRING:
22496 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022497 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022498 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022499 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022500 copy_tv(from, to);
22501 break;
22502 case VAR_LIST:
22503 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022504 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022505 if (from->vval.v_list == NULL)
22506 to->vval.v_list = NULL;
22507 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22508 {
22509 /* use the copy made earlier */
22510 to->vval.v_list = from->vval.v_list->lv_copylist;
22511 ++to->vval.v_list->lv_refcount;
22512 }
22513 else
22514 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22515 if (to->vval.v_list == NULL)
22516 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022517 break;
22518 case VAR_DICT:
22519 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022520 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022521 if (from->vval.v_dict == NULL)
22522 to->vval.v_dict = NULL;
22523 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22524 {
22525 /* use the copy made earlier */
22526 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22527 ++to->vval.v_dict->dv_refcount;
22528 }
22529 else
22530 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22531 if (to->vval.v_dict == NULL)
22532 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022533 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022534 case VAR_UNKNOWN:
22535 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022536 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022537 }
22538 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022539 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022540}
22541
22542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 * ":echo expr1 ..." print each argument separated with a space, add a
22544 * newline at the end.
22545 * ":echon expr1 ..." print each argument plain.
22546 */
22547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022548ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549{
22550 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022551 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022552 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553 char_u *p;
22554 int needclr = TRUE;
22555 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022556 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557
22558 if (eap->skip)
22559 ++emsg_skip;
22560 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22561 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022562 /* If eval1() causes an error message the text from the command may
22563 * still need to be cleared. E.g., "echo 22,44". */
22564 need_clr_eos = needclr;
22565
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022567 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 {
22569 /*
22570 * Report the invalid expression unless the expression evaluation
22571 * has been cancelled due to an aborting error, an interrupt, or an
22572 * exception.
22573 */
22574 if (!aborting())
22575 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022576 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 break;
22578 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022579 need_clr_eos = FALSE;
22580
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 if (!eap->skip)
22582 {
22583 if (atstart)
22584 {
22585 atstart = FALSE;
22586 /* Call msg_start() after eval1(), evaluating the expression
22587 * may cause a message to appear. */
22588 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022589 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022590 /* Mark the saved text as finishing the line, so that what
22591 * follows is displayed on a new line when scrolling back
22592 * at the more prompt. */
22593 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 }
22597 else if (eap->cmdidx == CMD_echo)
22598 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022599 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022600 if (p != NULL)
22601 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022603 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022605 if (*p != TAB && needclr)
22606 {
22607 /* remove any text still there from the command */
22608 msg_clr_eos();
22609 needclr = FALSE;
22610 }
22611 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 }
22613 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022614 {
22615#ifdef FEAT_MBYTE
22616 if (has_mbyte)
22617 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022618 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022619
22620 (void)msg_outtrans_len_attr(p, i, echo_attr);
22621 p += i - 1;
22622 }
22623 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022625 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022628 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022630 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 arg = skipwhite(arg);
22632 }
22633 eap->nextcmd = check_nextcmd(arg);
22634
22635 if (eap->skip)
22636 --emsg_skip;
22637 else
22638 {
22639 /* remove text that may still be there from the command */
22640 if (needclr)
22641 msg_clr_eos();
22642 if (eap->cmdidx == CMD_echo)
22643 msg_end();
22644 }
22645}
22646
22647/*
22648 * ":echohl {name}".
22649 */
22650 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022651ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652{
22653 int id;
22654
22655 id = syn_name2id(eap->arg);
22656 if (id == 0)
22657 echo_attr = 0;
22658 else
22659 echo_attr = syn_id2attr(id);
22660}
22661
22662/*
22663 * ":execute expr1 ..." execute the result of an expression.
22664 * ":echomsg expr1 ..." Print a message
22665 * ":echoerr expr1 ..." Print an error
22666 * Each gets spaces around each argument and a newline at the end for
22667 * echo commands
22668 */
22669 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022670ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671{
22672 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022673 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022674 int ret = OK;
22675 char_u *p;
22676 garray_T ga;
22677 int len;
22678 int save_did_emsg;
22679
22680 ga_init2(&ga, 1, 80);
22681
22682 if (eap->skip)
22683 ++emsg_skip;
22684 while (*arg != NUL && *arg != '|' && *arg != '\n')
22685 {
22686 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022687 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688 {
22689 /*
22690 * Report the invalid expression unless the expression evaluation
22691 * has been cancelled due to an aborting error, an interrupt, or an
22692 * exception.
22693 */
22694 if (!aborting())
22695 EMSG2(_(e_invexpr2), p);
22696 ret = FAIL;
22697 break;
22698 }
22699
22700 if (!eap->skip)
22701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022702 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 len = (int)STRLEN(p);
22704 if (ga_grow(&ga, len + 2) == FAIL)
22705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022706 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 ret = FAIL;
22708 break;
22709 }
22710 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713 ga.ga_len += len;
22714 }
22715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022716 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 arg = skipwhite(arg);
22718 }
22719
22720 if (ret != FAIL && ga.ga_data != NULL)
22721 {
22722 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022725 out_flush();
22726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 else if (eap->cmdidx == CMD_echoerr)
22728 {
22729 /* We don't want to abort following commands, restore did_emsg. */
22730 save_did_emsg = did_emsg;
22731 EMSG((char_u *)ga.ga_data);
22732 if (!force_abort)
22733 did_emsg = save_did_emsg;
22734 }
22735 else if (eap->cmdidx == CMD_execute)
22736 do_cmdline((char_u *)ga.ga_data,
22737 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22738 }
22739
22740 ga_clear(&ga);
22741
22742 if (eap->skip)
22743 --emsg_skip;
22744
22745 eap->nextcmd = check_nextcmd(arg);
22746}
22747
22748/*
22749 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22750 * "arg" points to the "&" or '+' when called, to "option" when returning.
22751 * Returns NULL when no option name found. Otherwise pointer to the char
22752 * after the option name.
22753 */
22754 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022755find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756{
22757 char_u *p = *arg;
22758
22759 ++p;
22760 if (*p == 'g' && p[1] == ':')
22761 {
22762 *opt_flags = OPT_GLOBAL;
22763 p += 2;
22764 }
22765 else if (*p == 'l' && p[1] == ':')
22766 {
22767 *opt_flags = OPT_LOCAL;
22768 p += 2;
22769 }
22770 else
22771 *opt_flags = 0;
22772
22773 if (!ASCII_ISALPHA(*p))
22774 return NULL;
22775 *arg = p;
22776
22777 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22778 p += 4; /* termcap option */
22779 else
22780 while (ASCII_ISALPHA(*p))
22781 ++p;
22782 return p;
22783}
22784
22785/*
22786 * ":function"
22787 */
22788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022789ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790{
22791 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022792 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 int j;
22794 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022796 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 char_u *name = NULL;
22798 char_u *p;
22799 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022800 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022801 garray_T newargs;
22802 garray_T newlines;
22803 int varargs = FALSE;
22804 int mustend = FALSE;
22805 int flags = 0;
22806 ufunc_T *fp;
22807 int indent;
22808 int nesting;
22809 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022810 dictitem_T *v;
22811 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022812 static int func_nr = 0; /* number for nameless function */
22813 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022814 hashtab_T *ht;
22815 int todo;
22816 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022817 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818
22819 /*
22820 * ":function" without argument: list functions.
22821 */
22822 if (ends_excmd(*eap->arg))
22823 {
22824 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022825 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022826 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022827 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022828 {
22829 if (!HASHITEM_EMPTY(hi))
22830 {
22831 --todo;
22832 fp = HI2UF(hi);
22833 if (!isdigit(*fp->uf_name))
22834 list_func_head(fp, FALSE);
22835 }
22836 }
22837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 eap->nextcmd = check_nextcmd(eap->arg);
22839 return;
22840 }
22841
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022842 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022843 * ":function /pat": list functions matching pattern.
22844 */
22845 if (*eap->arg == '/')
22846 {
22847 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22848 if (!eap->skip)
22849 {
22850 regmatch_T regmatch;
22851
22852 c = *p;
22853 *p = NUL;
22854 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22855 *p = c;
22856 if (regmatch.regprog != NULL)
22857 {
22858 regmatch.rm_ic = p_ic;
22859
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022860 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022861 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22862 {
22863 if (!HASHITEM_EMPTY(hi))
22864 {
22865 --todo;
22866 fp = HI2UF(hi);
22867 if (!isdigit(*fp->uf_name)
22868 && vim_regexec(&regmatch, fp->uf_name, 0))
22869 list_func_head(fp, FALSE);
22870 }
22871 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022872 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022873 }
22874 }
22875 if (*p == '/')
22876 ++p;
22877 eap->nextcmd = check_nextcmd(p);
22878 return;
22879 }
22880
22881 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022882 * Get the function name. There are these situations:
22883 * func normal function name
22884 * "name" == func, "fudi.fd_dict" == NULL
22885 * dict.func new dictionary entry
22886 * "name" == NULL, "fudi.fd_dict" set,
22887 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22888 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022889 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022890 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22891 * dict.func existing dict entry that's not a Funcref
22892 * "name" == NULL, "fudi.fd_dict" set,
22893 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022894 * s:func script-local function name
22895 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022898 name = trans_function_name(&p, eap->skip, 0, &fudi);
22899 paren = (vim_strchr(p, '(') != NULL);
22900 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 {
22902 /*
22903 * Return on an invalid expression in braces, unless the expression
22904 * evaluation has been cancelled due to an aborting error, an
22905 * interrupt, or an exception.
22906 */
22907 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022908 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022909 if (!eap->skip && fudi.fd_newkey != NULL)
22910 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022911 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 else
22915 eap->skip = TRUE;
22916 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022917
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 /* An error in a function call during evaluation of an expression in magic
22919 * braces should not cause the function not to be defined. */
22920 saved_did_emsg = did_emsg;
22921 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922
22923 /*
22924 * ":function func" with only function name: list function.
22925 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022926 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 {
22928 if (!ends_excmd(*skipwhite(p)))
22929 {
22930 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022931 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 }
22933 eap->nextcmd = check_nextcmd(p);
22934 if (eap->nextcmd != NULL)
22935 *p = NUL;
22936 if (!eap->skip && !got_int)
22937 {
22938 fp = find_func(name);
22939 if (fp != NULL)
22940 {
22941 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022942 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022944 if (FUNCLINE(fp, j) == NULL)
22945 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 msg_putchar('\n');
22947 msg_outnum((long)(j + 1));
22948 if (j < 9)
22949 msg_putchar(' ');
22950 if (j < 99)
22951 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022952 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 out_flush(); /* show a line at a time */
22954 ui_breakcheck();
22955 }
22956 if (!got_int)
22957 {
22958 msg_putchar('\n');
22959 msg_puts((char_u *)" endfunction");
22960 }
22961 }
22962 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022963 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022965 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 }
22967
22968 /*
22969 * ":function name(arg1, arg2)" Define function.
22970 */
22971 p = skipwhite(p);
22972 if (*p != '(')
22973 {
22974 if (!eap->skip)
22975 {
22976 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022977 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 }
22979 /* attempt to continue by skipping some text */
22980 if (vim_strchr(p, '(') != NULL)
22981 p = vim_strchr(p, '(');
22982 }
22983 p = skipwhite(p + 1);
22984
22985 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22986 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22987
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022988 if (!eap->skip)
22989 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022990 /* Check the name of the function. Unless it's a dictionary function
22991 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022992 if (name != NULL)
22993 arg = name;
22994 else
22995 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022996 if (arg != NULL && (fudi.fd_di == NULL
22997 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022998 {
22999 if (*arg == K_SPECIAL)
23000 j = 3;
23001 else
23002 j = 0;
23003 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23004 : eval_isnamec(arg[j])))
23005 ++j;
23006 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023007 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023008 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023009 /* Disallow using the g: dict. */
23010 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23011 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023012 }
23013
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 /*
23015 * Isolate the arguments: "arg1, arg2, ...)"
23016 */
23017 while (*p != ')')
23018 {
23019 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23020 {
23021 varargs = TRUE;
23022 p += 3;
23023 mustend = TRUE;
23024 }
23025 else
23026 {
23027 arg = p;
23028 while (ASCII_ISALNUM(*p) || *p == '_')
23029 ++p;
23030 if (arg == p || isdigit(*arg)
23031 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23032 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23033 {
23034 if (!eap->skip)
23035 EMSG2(_("E125: Illegal argument: %s"), arg);
23036 break;
23037 }
23038 if (ga_grow(&newargs, 1) == FAIL)
23039 goto erret;
23040 c = *p;
23041 *p = NUL;
23042 arg = vim_strsave(arg);
23043 if (arg == NULL)
23044 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023045
23046 /* Check for duplicate argument name. */
23047 for (i = 0; i < newargs.ga_len; ++i)
23048 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23049 {
23050 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023051 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023052 goto erret;
23053 }
23054
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23056 *p = c;
23057 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 if (*p == ',')
23059 ++p;
23060 else
23061 mustend = TRUE;
23062 }
23063 p = skipwhite(p);
23064 if (mustend && *p != ')')
23065 {
23066 if (!eap->skip)
23067 EMSG2(_(e_invarg2), eap->arg);
23068 break;
23069 }
23070 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023071 if (*p != ')')
23072 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 ++p; /* skip the ')' */
23074
Bram Moolenaare9a41262005-01-15 22:18:47 +000023075 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076 for (;;)
23077 {
23078 p = skipwhite(p);
23079 if (STRNCMP(p, "range", 5) == 0)
23080 {
23081 flags |= FC_RANGE;
23082 p += 5;
23083 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023084 else if (STRNCMP(p, "dict", 4) == 0)
23085 {
23086 flags |= FC_DICT;
23087 p += 4;
23088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 else if (STRNCMP(p, "abort", 5) == 0)
23090 {
23091 flags |= FC_ABORT;
23092 p += 5;
23093 }
23094 else
23095 break;
23096 }
23097
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023098 /* When there is a line break use what follows for the function body.
23099 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23100 if (*p == '\n')
23101 line_arg = p + 1;
23102 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 EMSG(_(e_trailing));
23104
23105 /*
23106 * Read the body of the function, until ":endfunction" is found.
23107 */
23108 if (KeyTyped)
23109 {
23110 /* Check if the function already exists, don't let the user type the
23111 * whole function before telling him it doesn't work! For a script we
23112 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023113 if (!eap->skip && !eap->forceit)
23114 {
23115 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23116 EMSG(_(e_funcdict));
23117 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023118 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023121 if (!eap->skip && did_emsg)
23122 goto erret;
23123
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 msg_putchar('\n'); /* don't overwrite the function name */
23125 cmdline_row = msg_row;
23126 }
23127
23128 indent = 2;
23129 nesting = 0;
23130 for (;;)
23131 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023132 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023133 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023134 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023135 saved_wait_return = FALSE;
23136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023138 sourcing_lnum_off = sourcing_lnum;
23139
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023140 if (line_arg != NULL)
23141 {
23142 /* Use eap->arg, split up in parts by line breaks. */
23143 theline = line_arg;
23144 p = vim_strchr(theline, '\n');
23145 if (p == NULL)
23146 line_arg += STRLEN(line_arg);
23147 else
23148 {
23149 *p = NUL;
23150 line_arg = p + 1;
23151 }
23152 }
23153 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 theline = getcmdline(':', 0L, indent);
23155 else
23156 theline = eap->getline(':', eap->cookie, indent);
23157 if (KeyTyped)
23158 lines_left = Rows - 1;
23159 if (theline == NULL)
23160 {
23161 EMSG(_("E126: Missing :endfunction"));
23162 goto erret;
23163 }
23164
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023165 /* Detect line continuation: sourcing_lnum increased more than one. */
23166 if (sourcing_lnum > sourcing_lnum_off + 1)
23167 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23168 else
23169 sourcing_lnum_off = 0;
23170
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 if (skip_until != NULL)
23172 {
23173 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23174 * don't check for ":endfunc". */
23175 if (STRCMP(theline, skip_until) == 0)
23176 {
23177 vim_free(skip_until);
23178 skip_until = NULL;
23179 }
23180 }
23181 else
23182 {
23183 /* skip ':' and blanks*/
23184 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23185 ;
23186
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023187 /* Check for "endfunction". */
23188 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023190 if (line_arg == NULL)
23191 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192 break;
23193 }
23194
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023195 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 * at "end". */
23197 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23198 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023199 else if (STRNCMP(p, "if", 2) == 0
23200 || STRNCMP(p, "wh", 2) == 0
23201 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 || STRNCMP(p, "try", 3) == 0)
23203 indent += 2;
23204
23205 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023206 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023208 if (*p == '!')
23209 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023211 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23212 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023214 ++nesting;
23215 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 }
23217 }
23218
23219 /* Check for ":append" or ":insert". */
23220 p = skip_range(p, NULL);
23221 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23222 || (p[0] == 'i'
23223 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23224 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23225 skip_until = vim_strsave((char_u *)".");
23226
23227 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23228 arg = skipwhite(skiptowhite(p));
23229 if (arg[0] == '<' && arg[1] =='<'
23230 && ((p[0] == 'p' && p[1] == 'y'
23231 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23232 || (p[0] == 'p' && p[1] == 'e'
23233 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23234 || (p[0] == 't' && p[1] == 'c'
23235 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023236 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23237 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23239 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023240 || (p[0] == 'm' && p[1] == 'z'
23241 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242 ))
23243 {
23244 /* ":python <<" continues until a dot, like ":append" */
23245 p = skipwhite(arg + 2);
23246 if (*p == NUL)
23247 skip_until = vim_strsave((char_u *)".");
23248 else
23249 skip_until = vim_strsave(p);
23250 }
23251 }
23252
23253 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023254 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023255 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023256 if (line_arg == NULL)
23257 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023259 }
23260
23261 /* Copy the line to newly allocated memory. get_one_sourceline()
23262 * allocates 250 bytes per line, this saves 80% on average. The cost
23263 * is an extra alloc/free. */
23264 p = vim_strsave(theline);
23265 if (p != NULL)
23266 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023267 if (line_arg == NULL)
23268 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023269 theline = p;
23270 }
23271
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023272 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23273
23274 /* Add NULL lines for continuation lines, so that the line count is
23275 * equal to the index in the growarray. */
23276 while (sourcing_lnum_off-- > 0)
23277 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023278
23279 /* Check for end of eap->arg. */
23280 if (line_arg != NULL && *line_arg == NUL)
23281 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 }
23283
23284 /* Don't define the function when skipping commands or when an error was
23285 * detected. */
23286 if (eap->skip || did_emsg)
23287 goto erret;
23288
23289 /*
23290 * If there are no errors, add the function
23291 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023292 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023293 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023294 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023295 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023296 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023297 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023298 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023299 goto erret;
23300 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023301
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023302 fp = find_func(name);
23303 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023304 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023305 if (!eap->forceit)
23306 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023307 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023308 goto erret;
23309 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023310 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023311 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023312 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023313 name);
23314 goto erret;
23315 }
23316 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023317 ga_clear_strings(&(fp->uf_args));
23318 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023319 vim_free(name);
23320 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023322 }
23323 else
23324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023325 char numbuf[20];
23326
23327 fp = NULL;
23328 if (fudi.fd_newkey == NULL && !eap->forceit)
23329 {
23330 EMSG(_(e_funcdict));
23331 goto erret;
23332 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023333 if (fudi.fd_di == NULL)
23334 {
23335 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023336 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023337 goto erret;
23338 }
23339 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023340 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023341 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023342
23343 /* Give the function a sequential number. Can only be used with a
23344 * Funcref! */
23345 vim_free(name);
23346 sprintf(numbuf, "%d", ++func_nr);
23347 name = vim_strsave((char_u *)numbuf);
23348 if (name == NULL)
23349 goto erret;
23350 }
23351
23352 if (fp == NULL)
23353 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023354 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023355 {
23356 int slen, plen;
23357 char_u *scriptname;
23358
23359 /* Check that the autoload name matches the script name. */
23360 j = FAIL;
23361 if (sourcing_name != NULL)
23362 {
23363 scriptname = autoload_name(name);
23364 if (scriptname != NULL)
23365 {
23366 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023367 plen = (int)STRLEN(p);
23368 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023369 if (slen > plen && fnamecmp(p,
23370 sourcing_name + slen - plen) == 0)
23371 j = OK;
23372 vim_free(scriptname);
23373 }
23374 }
23375 if (j == FAIL)
23376 {
23377 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23378 goto erret;
23379 }
23380 }
23381
23382 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 if (fp == NULL)
23384 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023385
23386 if (fudi.fd_dict != NULL)
23387 {
23388 if (fudi.fd_di == NULL)
23389 {
23390 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023391 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023392 if (fudi.fd_di == NULL)
23393 {
23394 vim_free(fp);
23395 goto erret;
23396 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023397 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23398 {
23399 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023400 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023401 goto erret;
23402 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023403 }
23404 else
23405 /* overwrite existing dict entry */
23406 clear_tv(&fudi.fd_di->di_tv);
23407 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023408 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023409 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023410 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023411
23412 /* behave like "dict" was used */
23413 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023414 }
23415
Bram Moolenaar071d4272004-06-13 20:20:40 +000023416 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023417 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023418 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23419 {
23420 vim_free(fp);
23421 goto erret;
23422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023424 fp->uf_args = newargs;
23425 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023426#ifdef FEAT_PROFILE
23427 fp->uf_tml_count = NULL;
23428 fp->uf_tml_total = NULL;
23429 fp->uf_tml_self = NULL;
23430 fp->uf_profiling = FALSE;
23431 if (prof_def_func())
23432 func_do_profile(fp);
23433#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023434 fp->uf_varargs = varargs;
23435 fp->uf_flags = flags;
23436 fp->uf_calls = 0;
23437 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023438 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439
23440erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 ga_clear_strings(&newargs);
23442 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023443ret_free:
23444 vim_free(skip_until);
23445 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023447 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023448 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449}
23450
23451/*
23452 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023453 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023455 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023456 * TFN_INT: internal function name OK
23457 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023458 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459 * Advances "pp" to just after the function name (if no error).
23460 */
23461 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023462trans_function_name(
23463 char_u **pp,
23464 int skip, /* only find the end, don't evaluate */
23465 int flags,
23466 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023468 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 char_u *start;
23470 char_u *end;
23471 int lead;
23472 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023474 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023475
23476 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023477 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023479
23480 /* Check for hard coded <SNR>: already translated function ID (from a user
23481 * command). */
23482 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23483 && (*pp)[2] == (int)KE_SNR)
23484 {
23485 *pp += 3;
23486 len = get_id_len(pp) + 3;
23487 return vim_strnsave(start, len);
23488 }
23489
23490 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23491 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023493 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023495
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023496 /* Note that TFN_ flags use the same values as GLV_ flags. */
23497 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023498 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023499 if (end == start)
23500 {
23501 if (!skip)
23502 EMSG(_("E129: Function name required"));
23503 goto theend;
23504 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023505 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023506 {
23507 /*
23508 * Report an invalid expression in braces, unless the expression
23509 * evaluation has been cancelled due to an aborting error, an
23510 * interrupt, or an exception.
23511 */
23512 if (!aborting())
23513 {
23514 if (end != NULL)
23515 EMSG2(_(e_invarg2), start);
23516 }
23517 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023518 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023519 goto theend;
23520 }
23521
23522 if (lv.ll_tv != NULL)
23523 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023524 if (fdp != NULL)
23525 {
23526 fdp->fd_dict = lv.ll_dict;
23527 fdp->fd_newkey = lv.ll_newkey;
23528 lv.ll_newkey = NULL;
23529 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023530 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023531 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23532 {
23533 name = vim_strsave(lv.ll_tv->vval.v_string);
23534 *pp = end;
23535 }
23536 else
23537 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023538 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23539 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023540 EMSG(_(e_funcref));
23541 else
23542 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023543 name = NULL;
23544 }
23545 goto theend;
23546 }
23547
23548 if (lv.ll_name == NULL)
23549 {
23550 /* Error found, but continue after the function name. */
23551 *pp = end;
23552 goto theend;
23553 }
23554
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023555 /* Check if the name is a Funcref. If so, use the value. */
23556 if (lv.ll_exp_name != NULL)
23557 {
23558 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023559 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023560 if (name == lv.ll_exp_name)
23561 name = NULL;
23562 }
23563 else
23564 {
23565 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023566 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023567 if (name == *pp)
23568 name = NULL;
23569 }
23570 if (name != NULL)
23571 {
23572 name = vim_strsave(name);
23573 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023574 if (STRNCMP(name, "<SNR>", 5) == 0)
23575 {
23576 /* Change "<SNR>" to the byte sequence. */
23577 name[0] = K_SPECIAL;
23578 name[1] = KS_EXTRA;
23579 name[2] = (int)KE_SNR;
23580 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23581 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023582 goto theend;
23583 }
23584
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023585 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023586 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023587 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023588 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23589 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23590 {
23591 /* When there was "s:" already or the name expanded to get a
23592 * leading "s:" then remove it. */
23593 lv.ll_name += 2;
23594 len -= 2;
23595 lead = 2;
23596 }
23597 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023598 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023599 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023600 /* skip over "s:" and "g:" */
23601 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023602 lv.ll_name += 2;
23603 len = (int)(end - lv.ll_name);
23604 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023605
23606 /*
23607 * Copy the function name to allocated memory.
23608 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23609 * Accept <SNR>123_name() outside a script.
23610 */
23611 if (skip)
23612 lead = 0; /* do nothing */
23613 else if (lead > 0)
23614 {
23615 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023616 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23617 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023618 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023619 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023620 if (current_SID <= 0)
23621 {
23622 EMSG(_(e_usingsid));
23623 goto theend;
23624 }
23625 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23626 lead += (int)STRLEN(sid_buf);
23627 }
23628 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023629 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023630 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023631 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023632 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023633 goto theend;
23634 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023635 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023636 {
23637 char_u *cp = vim_strchr(lv.ll_name, ':');
23638
23639 if (cp != NULL && cp < end)
23640 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023641 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023642 goto theend;
23643 }
23644 }
23645
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023646 name = alloc((unsigned)(len + lead + 1));
23647 if (name != NULL)
23648 {
23649 if (lead > 0)
23650 {
23651 name[0] = K_SPECIAL;
23652 name[1] = KS_EXTRA;
23653 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023654 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023655 STRCPY(name + 3, sid_buf);
23656 }
23657 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023658 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023659 }
23660 *pp = end;
23661
23662theend:
23663 clear_lval(&lv);
23664 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665}
23666
23667/*
23668 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23669 * Return 2 if "p" starts with "s:".
23670 * Return 0 otherwise.
23671 */
23672 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023673eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023674{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023675 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23676 * the standard library function. */
23677 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23678 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 return 5;
23680 if (p[0] == 's' && p[1] == ':')
23681 return 2;
23682 return 0;
23683}
23684
23685/*
23686 * Return TRUE if "p" starts with "<SID>" or "s:".
23687 * Only works if eval_fname_script() returned non-zero for "p"!
23688 */
23689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023690eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691{
23692 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23693}
23694
23695/*
23696 * List the head of the function: "name(arg1, arg2)".
23697 */
23698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023699list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023700{
23701 int j;
23702
23703 msg_start();
23704 if (indent)
23705 MSG_PUTS(" ");
23706 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023707 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708 {
23709 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023710 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023711 }
23712 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023713 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023715 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 {
23717 if (j)
23718 MSG_PUTS(", ");
23719 msg_puts(FUNCARG(fp, j));
23720 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023721 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 {
23723 if (j)
23724 MSG_PUTS(", ");
23725 MSG_PUTS("...");
23726 }
23727 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023728 if (fp->uf_flags & FC_ABORT)
23729 MSG_PUTS(" abort");
23730 if (fp->uf_flags & FC_RANGE)
23731 MSG_PUTS(" range");
23732 if (fp->uf_flags & FC_DICT)
23733 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023734 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023735 if (p_verbose > 0)
23736 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737}
23738
23739/*
23740 * Find a function by name, return pointer to it in ufuncs.
23741 * Return NULL for unknown function.
23742 */
23743 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023744find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023746 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023748 hi = hash_find(&func_hashtab, name);
23749 if (!HASHITEM_EMPTY(hi))
23750 return HI2UF(hi);
23751 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752}
23753
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023754#if defined(EXITFREE) || defined(PROTO)
23755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023756free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023757{
23758 hashitem_T *hi;
23759
23760 /* Need to start all over every time, because func_free() may change the
23761 * hash table. */
23762 while (func_hashtab.ht_used > 0)
23763 for (hi = func_hashtab.ht_array; ; ++hi)
23764 if (!HASHITEM_EMPTY(hi))
23765 {
23766 func_free(HI2UF(hi));
23767 break;
23768 }
23769}
23770#endif
23771
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023773translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023774{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023775 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023776 return find_internal_func(name) >= 0;
23777 return find_func(name) != NULL;
23778}
23779
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023780/*
23781 * Return TRUE if a function "name" exists.
23782 */
23783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023784function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023785{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023786 char_u *nm = name;
23787 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023788 int n = FALSE;
23789
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023790 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23791 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023792 nm = skipwhite(nm);
23793
23794 /* Only accept "funcname", "funcname ", "funcname (..." and
23795 * "funcname(...", not "funcname!...". */
23796 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023797 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023798 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023799 return n;
23800}
23801
Bram Moolenaara1544c02013-05-30 12:35:52 +020023802 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023803get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023804{
23805 char_u *nm = name;
23806 char_u *p;
23807
23808 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23809
23810 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023811 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023812 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023813
Bram Moolenaara1544c02013-05-30 12:35:52 +020023814 vim_free(p);
23815 return NULL;
23816}
23817
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023818/*
23819 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023820 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23821 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023822 */
23823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023824builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023825{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023826 char_u *p;
23827
23828 if (!ASCII_ISLOWER(name[0]))
23829 return FALSE;
23830 p = vim_strchr(name, AUTOLOAD_CHAR);
23831 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023832}
23833
Bram Moolenaar05159a02005-02-26 23:04:13 +000023834#if defined(FEAT_PROFILE) || defined(PROTO)
23835/*
23836 * Start profiling function "fp".
23837 */
23838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023839func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023840{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023841 int len = fp->uf_lines.ga_len;
23842
23843 if (len == 0)
23844 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023845 fp->uf_tm_count = 0;
23846 profile_zero(&fp->uf_tm_self);
23847 profile_zero(&fp->uf_tm_total);
23848 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023849 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023850 if (fp->uf_tml_total == NULL)
23851 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023852 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023853 if (fp->uf_tml_self == NULL)
23854 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023855 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023856 fp->uf_tml_idx = -1;
23857 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23858 || fp->uf_tml_self == NULL)
23859 return; /* out of memory */
23860
23861 fp->uf_profiling = TRUE;
23862}
23863
23864/*
23865 * Dump the profiling results for all functions in file "fd".
23866 */
23867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023868func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023869{
23870 hashitem_T *hi;
23871 int todo;
23872 ufunc_T *fp;
23873 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023874 ufunc_T **sorttab;
23875 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023876
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023877 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023878 if (todo == 0)
23879 return; /* nothing to dump */
23880
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023881 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023882
Bram Moolenaar05159a02005-02-26 23:04:13 +000023883 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23884 {
23885 if (!HASHITEM_EMPTY(hi))
23886 {
23887 --todo;
23888 fp = HI2UF(hi);
23889 if (fp->uf_profiling)
23890 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023891 if (sorttab != NULL)
23892 sorttab[st_len++] = fp;
23893
Bram Moolenaar05159a02005-02-26 23:04:13 +000023894 if (fp->uf_name[0] == K_SPECIAL)
23895 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23896 else
23897 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23898 if (fp->uf_tm_count == 1)
23899 fprintf(fd, "Called 1 time\n");
23900 else
23901 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23902 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23903 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23904 fprintf(fd, "\n");
23905 fprintf(fd, "count total (s) self (s)\n");
23906
23907 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23908 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023909 if (FUNCLINE(fp, i) == NULL)
23910 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023911 prof_func_line(fd, fp->uf_tml_count[i],
23912 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023913 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23914 }
23915 fprintf(fd, "\n");
23916 }
23917 }
23918 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023919
23920 if (sorttab != NULL && st_len > 0)
23921 {
23922 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23923 prof_total_cmp);
23924 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23925 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23926 prof_self_cmp);
23927 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23928 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023929
23930 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023931}
Bram Moolenaar73830342005-02-28 22:48:19 +000023932
23933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023934prof_sort_list(
23935 FILE *fd,
23936 ufunc_T **sorttab,
23937 int st_len,
23938 char *title,
23939 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023940{
23941 int i;
23942 ufunc_T *fp;
23943
23944 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23945 fprintf(fd, "count total (s) self (s) function\n");
23946 for (i = 0; i < 20 && i < st_len; ++i)
23947 {
23948 fp = sorttab[i];
23949 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23950 prefer_self);
23951 if (fp->uf_name[0] == K_SPECIAL)
23952 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23953 else
23954 fprintf(fd, " %s()\n", fp->uf_name);
23955 }
23956 fprintf(fd, "\n");
23957}
23958
23959/*
23960 * Print the count and times for one function or function line.
23961 */
23962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023963prof_func_line(
23964 FILE *fd,
23965 int count,
23966 proftime_T *total,
23967 proftime_T *self,
23968 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023969{
23970 if (count > 0)
23971 {
23972 fprintf(fd, "%5d ", count);
23973 if (prefer_self && profile_equal(total, self))
23974 fprintf(fd, " ");
23975 else
23976 fprintf(fd, "%s ", profile_msg(total));
23977 if (!prefer_self && profile_equal(total, self))
23978 fprintf(fd, " ");
23979 else
23980 fprintf(fd, "%s ", profile_msg(self));
23981 }
23982 else
23983 fprintf(fd, " ");
23984}
23985
23986/*
23987 * Compare function for total time sorting.
23988 */
23989 static int
23990#ifdef __BORLANDC__
23991_RTLENTRYF
23992#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023993prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023994{
23995 ufunc_T *p1, *p2;
23996
23997 p1 = *(ufunc_T **)s1;
23998 p2 = *(ufunc_T **)s2;
23999 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24000}
24001
24002/*
24003 * Compare function for self time sorting.
24004 */
24005 static int
24006#ifdef __BORLANDC__
24007_RTLENTRYF
24008#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024009prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024010{
24011 ufunc_T *p1, *p2;
24012
24013 p1 = *(ufunc_T **)s1;
24014 p2 = *(ufunc_T **)s2;
24015 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24016}
24017
Bram Moolenaar05159a02005-02-26 23:04:13 +000024018#endif
24019
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024020/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024021 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024022 * Return TRUE if a package was loaded.
24023 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024025script_autoload(
24026 char_u *name,
24027 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024028{
24029 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024030 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024031 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024032 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024033
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024034 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024035 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024036 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024037 return FALSE;
24038
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024039 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024040
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024041 /* Find the name in the list of previously loaded package names. Skip
24042 * "autoload/", it's always the same. */
24043 for (i = 0; i < ga_loaded.ga_len; ++i)
24044 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24045 break;
24046 if (!reload && i < ga_loaded.ga_len)
24047 ret = FALSE; /* was loaded already */
24048 else
24049 {
24050 /* Remember the name if it wasn't loaded already. */
24051 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24052 {
24053 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24054 tofree = NULL;
24055 }
24056
24057 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024058 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024059 ret = TRUE;
24060 }
24061
24062 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024063 return ret;
24064}
24065
24066/*
24067 * Return the autoload script name for a function or variable name.
24068 * Returns NULL when out of memory.
24069 */
24070 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024071autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024072{
24073 char_u *p;
24074 char_u *scriptname;
24075
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024076 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024077 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24078 if (scriptname == NULL)
24079 return FALSE;
24080 STRCPY(scriptname, "autoload/");
24081 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024082 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024083 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024084 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024085 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024086 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024087}
24088
Bram Moolenaar071d4272004-06-13 20:20:40 +000024089#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24090
24091/*
24092 * Function given to ExpandGeneric() to obtain the list of user defined
24093 * function names.
24094 */
24095 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024096get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024098 static long_u done;
24099 static hashitem_T *hi;
24100 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101
24102 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024104 done = 0;
24105 hi = func_hashtab.ht_array;
24106 }
24107 if (done < func_hashtab.ht_used)
24108 {
24109 if (done++ > 0)
24110 ++hi;
24111 while (HASHITEM_EMPTY(hi))
24112 ++hi;
24113 fp = HI2UF(hi);
24114
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024115 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024116 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024117
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024118 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24119 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120
24121 cat_func_name(IObuff, fp);
24122 if (xp->xp_context != EXPAND_USER_FUNC)
24123 {
24124 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024125 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126 STRCAT(IObuff, ")");
24127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024128 return IObuff;
24129 }
24130 return NULL;
24131}
24132
24133#endif /* FEAT_CMDL_COMPL */
24134
24135/*
24136 * Copy the function name of "fp" to buffer "buf".
24137 * "buf" must be able to hold the function name plus three bytes.
24138 * Takes care of script-local function names.
24139 */
24140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024141cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024143 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 {
24145 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024146 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147 }
24148 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024149 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150}
24151
24152/*
24153 * ":delfunction {name}"
24154 */
24155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024156ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024158 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159 char_u *p;
24160 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024161 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162
24163 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024164 name = trans_function_name(&p, eap->skip, 0, &fudi);
24165 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024167 {
24168 if (fudi.fd_dict != NULL && !eap->skip)
24169 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024172 if (!ends_excmd(*skipwhite(p)))
24173 {
24174 vim_free(name);
24175 EMSG(_(e_trailing));
24176 return;
24177 }
24178 eap->nextcmd = check_nextcmd(p);
24179 if (eap->nextcmd != NULL)
24180 *p = NUL;
24181
24182 if (!eap->skip)
24183 fp = find_func(name);
24184 vim_free(name);
24185
24186 if (!eap->skip)
24187 {
24188 if (fp == NULL)
24189 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024190 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191 return;
24192 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024193 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024194 {
24195 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24196 return;
24197 }
24198
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024199 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024201 /* Delete the dict item that refers to the function, it will
24202 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024203 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024205 else
24206 func_free(fp);
24207 }
24208}
24209
24210/*
24211 * Free a function and remove it from the list of functions.
24212 */
24213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024214func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024215{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024216 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024217
24218 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024219 ga_clear_strings(&(fp->uf_args));
24220 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024221#ifdef FEAT_PROFILE
24222 vim_free(fp->uf_tml_count);
24223 vim_free(fp->uf_tml_total);
24224 vim_free(fp->uf_tml_self);
24225#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024226
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024227 /* remove the function from the function hashtable */
24228 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24229 if (HASHITEM_EMPTY(hi))
24230 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024231 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024232 hash_remove(&func_hashtab, hi);
24233
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024234 vim_free(fp);
24235}
24236
24237/*
24238 * Unreference a Function: decrement the reference count and free it when it
24239 * becomes zero. Only for numbered functions.
24240 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024241 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024242func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024243{
24244 ufunc_T *fp;
24245
24246 if (name != NULL && isdigit(*name))
24247 {
24248 fp = find_func(name);
24249 if (fp == NULL)
24250 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024251 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024252 {
24253 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024254 * when "uf_calls" becomes zero. */
24255 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024256 func_free(fp);
24257 }
24258 }
24259}
24260
24261/*
24262 * Count a reference to a Function.
24263 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024265func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024266{
24267 ufunc_T *fp;
24268
24269 if (name != NULL && isdigit(*name))
24270 {
24271 fp = find_func(name);
24272 if (fp == NULL)
24273 EMSG2(_(e_intern2), "func_ref()");
24274 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024275 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024276 }
24277}
24278
24279/*
24280 * Call a user function.
24281 */
24282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024283call_user_func(
24284 ufunc_T *fp, /* pointer to function */
24285 int argcount, /* nr of args */
24286 typval_T *argvars, /* arguments */
24287 typval_T *rettv, /* return value */
24288 linenr_T firstline, /* first line of range */
24289 linenr_T lastline, /* last line of range */
24290 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291{
Bram Moolenaar33570922005-01-25 22:26:29 +000024292 char_u *save_sourcing_name;
24293 linenr_T save_sourcing_lnum;
24294 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024295 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024296 int save_did_emsg;
24297 static int depth = 0;
24298 dictitem_T *v;
24299 int fixvar_idx = 0; /* index in fixvar[] */
24300 int i;
24301 int ai;
24302 char_u numbuf[NUMBUFLEN];
24303 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024304 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024305#ifdef FEAT_PROFILE
24306 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024307 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309
24310 /* If depth of calling is getting too high, don't execute the function */
24311 if (depth >= p_mfd)
24312 {
24313 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024314 rettv->v_type = VAR_NUMBER;
24315 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316 return;
24317 }
24318 ++depth;
24319
24320 line_breakcheck(); /* check for CTRL-C hit */
24321
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024322 fc = (funccall_T *)alloc(sizeof(funccall_T));
24323 fc->caller = current_funccal;
24324 current_funccal = fc;
24325 fc->func = fp;
24326 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024327 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024328 fc->linenr = 0;
24329 fc->returned = FALSE;
24330 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024332 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24333 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334
Bram Moolenaar33570922005-01-25 22:26:29 +000024335 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024336 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024337 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24338 * each argument variable and saves a lot of time.
24339 */
24340 /*
24341 * Init l: variables.
24342 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024343 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024344 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024345 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024346 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24347 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024348 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024349 name = v->di_key;
24350 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024351 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024352 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024353 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024354 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024355 v->di_tv.vval.v_dict = selfdict;
24356 ++selfdict->dv_refcount;
24357 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024358
Bram Moolenaar33570922005-01-25 22:26:29 +000024359 /*
24360 * Init a: variables.
24361 * Set a:0 to "argcount".
24362 * Set a:000 to a list with room for the "..." arguments.
24363 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024364 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024365 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024366 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024367 /* Use "name" to avoid a warning from some compiler that checks the
24368 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024369 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024370 name = v->di_key;
24371 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024372 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024373 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024374 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024375 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024376 v->di_tv.vval.v_list = &fc->l_varlist;
24377 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24378 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24379 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024380
24381 /*
24382 * Set a:firstline to "firstline" and a:lastline to "lastline".
24383 * Set a:name to named arguments.
24384 * Set a:N to the "..." arguments.
24385 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024386 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024387 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024388 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024389 (varnumber_T)lastline);
24390 for (i = 0; i < argcount; ++i)
24391 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024392 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024393 if (ai < 0)
24394 /* named argument a:name */
24395 name = FUNCARG(fp, i);
24396 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024397 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024398 /* "..." argument a:1, a:2, etc. */
24399 sprintf((char *)numbuf, "%d", ai + 1);
24400 name = numbuf;
24401 }
24402 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24403 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024404 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024405 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24406 }
24407 else
24408 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024409 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24410 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024411 if (v == NULL)
24412 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024413 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024414 }
24415 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024416 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024417
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024418 /* Note: the values are copied directly to avoid alloc/free.
24419 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024420 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024421 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024422
24423 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24424 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024425 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24426 fc->l_listitems[ai].li_tv = argvars[i];
24427 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024428 }
24429 }
24430
Bram Moolenaar071d4272004-06-13 20:20:40 +000024431 /* Don't redraw while executing the function. */
24432 ++RedrawingDisabled;
24433 save_sourcing_name = sourcing_name;
24434 save_sourcing_lnum = sourcing_lnum;
24435 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024436 /* need space for function name + ("function " + 3) or "[number]" */
24437 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24438 + STRLEN(fp->uf_name) + 20;
24439 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440 if (sourcing_name != NULL)
24441 {
24442 if (save_sourcing_name != NULL
24443 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024444 sprintf((char *)sourcing_name, "%s[%d]..",
24445 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 else
24447 STRCPY(sourcing_name, "function ");
24448 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24449
24450 if (p_verbose >= 12)
24451 {
24452 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024453 verbose_enter_scroll();
24454
Bram Moolenaar555b2802005-05-19 21:08:39 +000024455 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024456 if (p_verbose >= 14)
24457 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024459 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024460 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024461 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462
24463 msg_puts((char_u *)"(");
24464 for (i = 0; i < argcount; ++i)
24465 {
24466 if (i > 0)
24467 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024468 if (argvars[i].v_type == VAR_NUMBER)
24469 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470 else
24471 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024472 /* Do not want errors such as E724 here. */
24473 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024474 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024475 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024476 if (s != NULL)
24477 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024478 if (vim_strsize(s) > MSG_BUF_CLEN)
24479 {
24480 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24481 s = buf;
24482 }
24483 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024484 vim_free(tofree);
24485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024486 }
24487 }
24488 msg_puts((char_u *)")");
24489 }
24490 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024491
24492 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493 --no_wait_return;
24494 }
24495 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024496#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024497 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024498 {
24499 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24500 func_do_profile(fp);
24501 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024502 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024503 {
24504 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024505 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024506 profile_zero(&fp->uf_tm_children);
24507 }
24508 script_prof_save(&wait_start);
24509 }
24510#endif
24511
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024513 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024514 save_did_emsg = did_emsg;
24515 did_emsg = FALSE;
24516
24517 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024518 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024519 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24520
24521 --RedrawingDisabled;
24522
24523 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024524 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024526 clear_tv(rettv);
24527 rettv->v_type = VAR_NUMBER;
24528 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529 }
24530
Bram Moolenaar05159a02005-02-26 23:04:13 +000024531#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024532 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024533 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024534 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024535 profile_end(&call_start);
24536 profile_sub_wait(&wait_start, &call_start);
24537 profile_add(&fp->uf_tm_total, &call_start);
24538 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024539 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024540 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024541 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24542 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024543 }
24544 }
24545#endif
24546
Bram Moolenaar071d4272004-06-13 20:20:40 +000024547 /* when being verbose, mention the return value */
24548 if (p_verbose >= 12)
24549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024551 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024552
Bram Moolenaar071d4272004-06-13 20:20:40 +000024553 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024554 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024555 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024556 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024557 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024558 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024559 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024560 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024561 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024562 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024563 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024564
Bram Moolenaar555b2802005-05-19 21:08:39 +000024565 /* The value may be very long. Skip the middle part, so that we
24566 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024567 * truncate it at the end. Don't want errors such as E724 here. */
24568 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024569 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024570 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024571 if (s != NULL)
24572 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024573 if (vim_strsize(s) > MSG_BUF_CLEN)
24574 {
24575 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24576 s = buf;
24577 }
24578 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024579 vim_free(tofree);
24580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024581 }
24582 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024583
24584 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585 --no_wait_return;
24586 }
24587
24588 vim_free(sourcing_name);
24589 sourcing_name = save_sourcing_name;
24590 sourcing_lnum = save_sourcing_lnum;
24591 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024592#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024593 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024594 script_prof_restore(&wait_start);
24595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596
24597 if (p_verbose >= 12 && sourcing_name != NULL)
24598 {
24599 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024600 verbose_enter_scroll();
24601
Bram Moolenaar555b2802005-05-19 21:08:39 +000024602 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024604
24605 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024606 --no_wait_return;
24607 }
24608
24609 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024610 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024611 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024612
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024613 /* If the a:000 list and the l: and a: dicts are not referenced we can
24614 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024615 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24616 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24617 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24618 {
24619 free_funccal(fc, FALSE);
24620 }
24621 else
24622 {
24623 hashitem_T *hi;
24624 listitem_T *li;
24625 int todo;
24626
24627 /* "fc" is still in use. This can happen when returning "a:000" or
24628 * assigning "l:" to a global variable.
24629 * Link "fc" in the list for garbage collection later. */
24630 fc->caller = previous_funccal;
24631 previous_funccal = fc;
24632
24633 /* Make a copy of the a: variables, since we didn't do that above. */
24634 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24635 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24636 {
24637 if (!HASHITEM_EMPTY(hi))
24638 {
24639 --todo;
24640 v = HI2DI(hi);
24641 copy_tv(&v->di_tv, &v->di_tv);
24642 }
24643 }
24644
24645 /* Make a copy of the a:000 items, since we didn't do that above. */
24646 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24647 copy_tv(&li->li_tv, &li->li_tv);
24648 }
24649}
24650
24651/*
24652 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024653 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024654 */
24655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024656can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024657{
24658 return (fc->l_varlist.lv_copyID != copyID
24659 && fc->l_vars.dv_copyID != copyID
24660 && fc->l_avars.dv_copyID != copyID);
24661}
24662
24663/*
24664 * Free "fc" and what it contains.
24665 */
24666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024667free_funccal(
24668 funccall_T *fc,
24669 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024670{
24671 listitem_T *li;
24672
24673 /* The a: variables typevals may not have been allocated, only free the
24674 * allocated variables. */
24675 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24676
24677 /* free all l: variables */
24678 vars_clear(&fc->l_vars.dv_hashtab);
24679
24680 /* Free the a:000 variables if they were allocated. */
24681 if (free_val)
24682 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24683 clear_tv(&li->li_tv);
24684
24685 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686}
24687
24688/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024689 * Add a number variable "name" to dict "dp" with value "nr".
24690 */
24691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024692add_nr_var(
24693 dict_T *dp,
24694 dictitem_T *v,
24695 char *name,
24696 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024697{
24698 STRCPY(v->di_key, name);
24699 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24700 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24701 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024702 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024703 v->di_tv.vval.v_number = nr;
24704}
24705
24706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707 * ":return [expr]"
24708 */
24709 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024710ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024711{
24712 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024713 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714 int returning = FALSE;
24715
24716 if (current_funccal == NULL)
24717 {
24718 EMSG(_("E133: :return not inside a function"));
24719 return;
24720 }
24721
24722 if (eap->skip)
24723 ++emsg_skip;
24724
24725 eap->nextcmd = NULL;
24726 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024727 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024728 {
24729 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024730 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024731 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024732 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733 }
24734 /* It's safer to return also on error. */
24735 else if (!eap->skip)
24736 {
24737 /*
24738 * Return unless the expression evaluation has been cancelled due to an
24739 * aborting error, an interrupt, or an exception.
24740 */
24741 if (!aborting())
24742 returning = do_return(eap, FALSE, TRUE, NULL);
24743 }
24744
24745 /* When skipping or the return gets pending, advance to the next command
24746 * in this line (!returning). Otherwise, ignore the rest of the line.
24747 * Following lines will be ignored by get_func_line(). */
24748 if (returning)
24749 eap->nextcmd = NULL;
24750 else if (eap->nextcmd == NULL) /* no argument */
24751 eap->nextcmd = check_nextcmd(arg);
24752
24753 if (eap->skip)
24754 --emsg_skip;
24755}
24756
24757/*
24758 * Return from a function. Possibly makes the return pending. Also called
24759 * for a pending return at the ":endtry" or after returning from an extra
24760 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024761 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024762 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763 * FALSE when the return gets pending.
24764 */
24765 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024766do_return(
24767 exarg_T *eap,
24768 int reanimate,
24769 int is_cmd,
24770 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771{
24772 int idx;
24773 struct condstack *cstack = eap->cstack;
24774
24775 if (reanimate)
24776 /* Undo the return. */
24777 current_funccal->returned = FALSE;
24778
24779 /*
24780 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24781 * not in its finally clause (which then is to be executed next) is found.
24782 * In this case, make the ":return" pending for execution at the ":endtry".
24783 * Otherwise, return normally.
24784 */
24785 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24786 if (idx >= 0)
24787 {
24788 cstack->cs_pending[idx] = CSTP_RETURN;
24789
24790 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024791 /* A pending return again gets pending. "rettv" points to an
24792 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024794 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024795 else
24796 {
24797 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024798 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024799 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024800 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024802 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024803 {
24804 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024805 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024806 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807 else
24808 EMSG(_(e_outofmem));
24809 }
24810 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024811 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812
24813 if (reanimate)
24814 {
24815 /* The pending return value could be overwritten by a ":return"
24816 * without argument in a finally clause; reset the default
24817 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024818 current_funccal->rettv->v_type = VAR_NUMBER;
24819 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820 }
24821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024822 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823 }
24824 else
24825 {
24826 current_funccal->returned = TRUE;
24827
24828 /* If the return is carried out now, store the return value. For
24829 * a return immediately after reanimation, the value is already
24830 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024831 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024833 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024834 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024835 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024836 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024837 }
24838 }
24839
24840 return idx < 0;
24841}
24842
24843/*
24844 * Free the variable with a pending return value.
24845 */
24846 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024847discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848{
Bram Moolenaar33570922005-01-25 22:26:29 +000024849 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850}
24851
24852/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024853 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854 * is an allocated string. Used by report_pending() for verbose messages.
24855 */
24856 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024857get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024858{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024859 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024860 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024861 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024863 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024864 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024865 if (s == NULL)
24866 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024867
24868 STRCPY(IObuff, ":return ");
24869 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24870 if (STRLEN(s) + 8 >= IOSIZE)
24871 STRCPY(IObuff + IOSIZE - 4, "...");
24872 vim_free(tofree);
24873 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024874}
24875
24876/*
24877 * Get next function line.
24878 * Called by do_cmdline() to get the next line.
24879 * Returns allocated string, or NULL for end of function.
24880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024881 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024882get_func_line(
24883 int c UNUSED,
24884 void *cookie,
24885 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886{
Bram Moolenaar33570922005-01-25 22:26:29 +000024887 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024888 ufunc_T *fp = fcp->func;
24889 char_u *retval;
24890 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024891
24892 /* If breakpoints have been added/deleted need to check for it. */
24893 if (fcp->dbg_tick != debug_tick)
24894 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024895 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896 sourcing_lnum);
24897 fcp->dbg_tick = debug_tick;
24898 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024899#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024900 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024901 func_line_end(cookie);
24902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903
Bram Moolenaar05159a02005-02-26 23:04:13 +000024904 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024905 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24906 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024907 retval = NULL;
24908 else
24909 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024910 /* Skip NULL lines (continuation lines). */
24911 while (fcp->linenr < gap->ga_len
24912 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24913 ++fcp->linenr;
24914 if (fcp->linenr >= gap->ga_len)
24915 retval = NULL;
24916 else
24917 {
24918 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24919 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024920#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024921 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024922 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024923#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 }
24926
24927 /* Did we encounter a breakpoint? */
24928 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24929 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024930 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024932 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933 sourcing_lnum);
24934 fcp->dbg_tick = debug_tick;
24935 }
24936
24937 return retval;
24938}
24939
Bram Moolenaar05159a02005-02-26 23:04:13 +000024940#if defined(FEAT_PROFILE) || defined(PROTO)
24941/*
24942 * Called when starting to read a function line.
24943 * "sourcing_lnum" must be correct!
24944 * When skipping lines it may not actually be executed, but we won't find out
24945 * until later and we need to store the time now.
24946 */
24947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024948func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024949{
24950 funccall_T *fcp = (funccall_T *)cookie;
24951 ufunc_T *fp = fcp->func;
24952
24953 if (fp->uf_profiling && sourcing_lnum >= 1
24954 && sourcing_lnum <= fp->uf_lines.ga_len)
24955 {
24956 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024957 /* Skip continuation lines. */
24958 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24959 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024960 fp->uf_tml_execed = FALSE;
24961 profile_start(&fp->uf_tml_start);
24962 profile_zero(&fp->uf_tml_children);
24963 profile_get_wait(&fp->uf_tml_wait);
24964 }
24965}
24966
24967/*
24968 * Called when actually executing a function line.
24969 */
24970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024971func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024972{
24973 funccall_T *fcp = (funccall_T *)cookie;
24974 ufunc_T *fp = fcp->func;
24975
24976 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24977 fp->uf_tml_execed = TRUE;
24978}
24979
24980/*
24981 * Called when done with a function line.
24982 */
24983 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024984func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024985{
24986 funccall_T *fcp = (funccall_T *)cookie;
24987 ufunc_T *fp = fcp->func;
24988
24989 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24990 {
24991 if (fp->uf_tml_execed)
24992 {
24993 ++fp->uf_tml_count[fp->uf_tml_idx];
24994 profile_end(&fp->uf_tml_start);
24995 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024996 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024997 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24998 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024999 }
25000 fp->uf_tml_idx = -1;
25001 }
25002}
25003#endif
25004
Bram Moolenaar071d4272004-06-13 20:20:40 +000025005/*
25006 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025007 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025008 */
25009 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025010func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025011{
Bram Moolenaar33570922005-01-25 22:26:29 +000025012 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013
25014 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25015 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025016 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017 || fcp->returned);
25018}
25019
25020/*
25021 * return TRUE if cookie indicates a function which "abort"s on errors.
25022 */
25023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025024func_has_abort(
25025 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025027 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025028}
25029
25030#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25031typedef enum
25032{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025033 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25034 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25035 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025036} var_flavour_T;
25037
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025038static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039
25040 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025041var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042{
25043 char_u *p = varname;
25044
25045 if (ASCII_ISUPPER(*p))
25046 {
25047 while (*(++p))
25048 if (ASCII_ISLOWER(*p))
25049 return VAR_FLAVOUR_SESSION;
25050 return VAR_FLAVOUR_VIMINFO;
25051 }
25052 else
25053 return VAR_FLAVOUR_DEFAULT;
25054}
25055#endif
25056
25057#if defined(FEAT_VIMINFO) || defined(PROTO)
25058/*
25059 * Restore global vars that start with a capital from the viminfo file
25060 */
25061 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025062read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025063{
25064 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025065 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025066 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025067 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068
25069 if (!writing && (find_viminfo_parameter('!') != NULL))
25070 {
25071 tab = vim_strchr(virp->vir_line + 1, '\t');
25072 if (tab != NULL)
25073 {
25074 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025075 switch (*tab)
25076 {
25077 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025078#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025079 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025080#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025081 case 'D': type = VAR_DICT; break;
25082 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025083 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085
25086 tab = vim_strchr(tab, '\t');
25087 if (tab != NULL)
25088 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025089 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025090 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025091 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025093#ifdef FEAT_FLOAT
25094 else if (type == VAR_FLOAT)
25095 (void)string2float(tab + 1, &tv.vval.v_float);
25096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025097 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025098 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025099 if (type == VAR_DICT || type == VAR_LIST)
25100 {
25101 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25102
25103 if (etv == NULL)
25104 /* Failed to parse back the dict or list, use it as a
25105 * string. */
25106 tv.v_type = VAR_STRING;
25107 else
25108 {
25109 vim_free(tv.vval.v_string);
25110 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025111 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025112 }
25113 }
25114
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025115 /* when in a function use global variables */
25116 save_funccal = current_funccal;
25117 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025118 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025119 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025120
25121 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025122 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025123 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25124 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025125 }
25126 }
25127 }
25128
25129 return viminfo_readline(virp);
25130}
25131
25132/*
25133 * Write global vars that start with a capital to the viminfo file
25134 */
25135 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025136write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025137{
Bram Moolenaar33570922005-01-25 22:26:29 +000025138 hashitem_T *hi;
25139 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025140 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025141 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025142 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025143 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025144 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025145
25146 if (find_viminfo_parameter('!') == NULL)
25147 return;
25148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025149 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025150
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025151 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025152 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025154 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025156 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025157 this_var = HI2DI(hi);
25158 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025159 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025160 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025161 {
25162 case VAR_STRING: s = "STR"; break;
25163 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025164 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025165 case VAR_DICT: s = "DIC"; break;
25166 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025167 case VAR_SPECIAL: s = "XPL"; break;
25168
25169 case VAR_UNKNOWN:
25170 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025171 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025172 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025173 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025174 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025175 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025176 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025177 if (p != NULL)
25178 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025179 vim_free(tofree);
25180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 }
25182 }
25183}
25184#endif
25185
25186#if defined(FEAT_SESSION) || defined(PROTO)
25187 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025188store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189{
Bram Moolenaar33570922005-01-25 22:26:29 +000025190 hashitem_T *hi;
25191 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025192 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193 char_u *p, *t;
25194
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025195 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025196 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025198 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025200 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025201 this_var = HI2DI(hi);
25202 if ((this_var->di_tv.v_type == VAR_NUMBER
25203 || this_var->di_tv.v_type == VAR_STRING)
25204 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025205 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025206 /* Escape special characters with a backslash. Turn a LF and
25207 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025208 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025209 (char_u *)"\\\"\n\r");
25210 if (p == NULL) /* out of memory */
25211 break;
25212 for (t = p; *t != NUL; ++t)
25213 if (*t == '\n')
25214 *t = 'n';
25215 else if (*t == '\r')
25216 *t = 'r';
25217 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025218 this_var->di_key,
25219 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25220 : ' ',
25221 p,
25222 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25223 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025224 || put_eol(fd) == FAIL)
25225 {
25226 vim_free(p);
25227 return FAIL;
25228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025229 vim_free(p);
25230 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025231#ifdef FEAT_FLOAT
25232 else if (this_var->di_tv.v_type == VAR_FLOAT
25233 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25234 {
25235 float_T f = this_var->di_tv.vval.v_float;
25236 int sign = ' ';
25237
25238 if (f < 0)
25239 {
25240 f = -f;
25241 sign = '-';
25242 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025243 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025244 this_var->di_key, sign, f) < 0)
25245 || put_eol(fd) == FAIL)
25246 return FAIL;
25247 }
25248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 }
25250 }
25251 return OK;
25252}
25253#endif
25254
Bram Moolenaar661b1822005-07-28 22:36:45 +000025255/*
25256 * Display script name where an item was last set.
25257 * Should only be invoked when 'verbose' is non-zero.
25258 */
25259 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025260last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025261{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025262 char_u *p;
25263
Bram Moolenaar661b1822005-07-28 22:36:45 +000025264 if (scriptID != 0)
25265 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025266 p = home_replace_save(NULL, get_scriptname(scriptID));
25267 if (p != NULL)
25268 {
25269 verbose_enter();
25270 MSG_PUTS(_("\n\tLast set from "));
25271 MSG_PUTS(p);
25272 vim_free(p);
25273 verbose_leave();
25274 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025275 }
25276}
25277
Bram Moolenaard812df62008-11-09 12:46:09 +000025278/*
25279 * List v:oldfiles in a nice way.
25280 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025282ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025283{
25284 list_T *l = vimvars[VV_OLDFILES].vv_list;
25285 listitem_T *li;
25286 int nr = 0;
25287
25288 if (l == NULL)
25289 msg((char_u *)_("No old files"));
25290 else
25291 {
25292 msg_start();
25293 msg_scroll = TRUE;
25294 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25295 {
25296 msg_outnum((long)++nr);
25297 MSG_PUTS(": ");
25298 msg_outtrans(get_tv_string(&li->li_tv));
25299 msg_putchar('\n');
25300 out_flush(); /* output one line at a time */
25301 ui_breakcheck();
25302 }
25303 /* Assume "got_int" was set to truncate the listing. */
25304 got_int = FALSE;
25305
25306#ifdef FEAT_BROWSE_CMD
25307 if (cmdmod.browse)
25308 {
25309 quit_more = FALSE;
25310 nr = prompt_for_number(FALSE);
25311 msg_starthere();
25312 if (nr > 0)
25313 {
25314 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25315 (long)nr);
25316
25317 if (p != NULL)
25318 {
25319 p = expand_env_save(p);
25320 eap->arg = p;
25321 eap->cmdidx = CMD_edit;
25322 cmdmod.browse = FALSE;
25323 do_exedit(eap, NULL);
25324 vim_free(p);
25325 }
25326 }
25327 }
25328#endif
25329 }
25330}
25331
Bram Moolenaar53744302015-07-17 17:38:22 +020025332/* reset v:option_new, v:option_old and v:option_type */
25333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025334reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025335{
25336 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25337 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25338 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25339}
25340
25341
Bram Moolenaar071d4272004-06-13 20:20:40 +000025342#endif /* FEAT_EVAL */
25343
Bram Moolenaar071d4272004-06-13 20:20:40 +000025344
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025345#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346
25347#ifdef WIN3264
25348/*
25349 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25350 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025351static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25352static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25353static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354
25355/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025356 * Get the short path (8.3) for the filename in "fnamep".
25357 * Only works for a valid file name.
25358 * When the path gets longer "fnamep" is changed and the allocated buffer
25359 * is put in "bufp".
25360 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25361 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025362 */
25363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025364get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025365{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025366 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367 char_u *newbuf;
25368
25369 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370 l = GetShortPathName(*fnamep, *fnamep, len);
25371 if (l > len - 1)
25372 {
25373 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025374 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025375 newbuf = vim_strnsave(*fnamep, l);
25376 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025377 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025378
25379 vim_free(*bufp);
25380 *fnamep = *bufp = newbuf;
25381
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025382 /* Really should always succeed, as the buffer is big enough. */
25383 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025384 }
25385
25386 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025387 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388}
25389
25390/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025391 * Get the short path (8.3) for the filename in "fname". The converted
25392 * path is returned in "bufp".
25393 *
25394 * Some of the directories specified in "fname" may not exist. This function
25395 * will shorten the existing directories at the beginning of the path and then
25396 * append the remaining non-existing path.
25397 *
25398 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025399 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025400 * bufp - Pointer to an allocated buffer for the filename.
25401 * fnamelen - Length of the filename pointed to by fname
25402 *
25403 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404 */
25405 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025406shortpath_for_invalid_fname(
25407 char_u **fname,
25408 char_u **bufp,
25409 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025411 char_u *short_fname, *save_fname, *pbuf_unused;
25412 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025413 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025414 int old_len, len;
25415 int new_len, sfx_len;
25416 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417
25418 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025419 old_len = *fnamelen;
25420 save_fname = vim_strnsave(*fname, old_len);
25421 pbuf_unused = NULL;
25422 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025423
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025424 endp = save_fname + old_len - 1; /* Find the end of the copy */
25425 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025427 /*
25428 * Try shortening the supplied path till it succeeds by removing one
25429 * directory at a time from the tail of the path.
25430 */
25431 len = 0;
25432 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025434 /* go back one path-separator */
25435 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25436 --endp;
25437 if (endp <= save_fname)
25438 break; /* processed the complete path */
25439
25440 /*
25441 * Replace the path separator with a NUL and try to shorten the
25442 * resulting path.
25443 */
25444 ch = *endp;
25445 *endp = 0;
25446 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025447 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025448 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25449 {
25450 retval = FAIL;
25451 goto theend;
25452 }
25453 *endp = ch; /* preserve the string */
25454
25455 if (len > 0)
25456 break; /* successfully shortened the path */
25457
25458 /* failed to shorten the path. Skip the path separator */
25459 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025460 }
25461
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025462 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025464 /*
25465 * Succeeded in shortening the path. Now concatenate the shortened
25466 * path with the remaining path at the tail.
25467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025468
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025469 /* Compute the length of the new path. */
25470 sfx_len = (int)(save_endp - endp) + 1;
25471 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025473 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025475 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025477 /* There is not enough space in the currently allocated string,
25478 * copy it to a buffer big enough. */
25479 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025481 {
25482 retval = FAIL;
25483 goto theend;
25484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485 }
25486 else
25487 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025488 /* Transfer short_fname to the main buffer (it's big enough),
25489 * unless get_short_pathname() did its work in-place. */
25490 *fname = *bufp = save_fname;
25491 if (short_fname != save_fname)
25492 vim_strncpy(save_fname, short_fname, len);
25493 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025495
25496 /* concat the not-shortened part of the path */
25497 vim_strncpy(*fname + len, endp, sfx_len);
25498 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025500
25501theend:
25502 vim_free(pbuf_unused);
25503 vim_free(save_fname);
25504
25505 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506}
25507
25508/*
25509 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025510 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511 */
25512 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025513shortpath_for_partial(
25514 char_u **fnamep,
25515 char_u **bufp,
25516 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025517{
25518 int sepcount, len, tflen;
25519 char_u *p;
25520 char_u *pbuf, *tfname;
25521 int hasTilde;
25522
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025523 /* Count up the path separators from the RHS.. so we know which part
25524 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025526 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527 if (vim_ispathsep(*p))
25528 ++sepcount;
25529
25530 /* Need full path first (use expand_env() to remove a "~/") */
25531 hasTilde = (**fnamep == '~');
25532 if (hasTilde)
25533 pbuf = tfname = expand_env_save(*fnamep);
25534 else
25535 pbuf = tfname = FullName_save(*fnamep, FALSE);
25536
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025537 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025539 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25540 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025541
25542 if (len == 0)
25543 {
25544 /* Don't have a valid filename, so shorten the rest of the
25545 * path if we can. This CAN give us invalid 8.3 filenames, but
25546 * there's not a lot of point in guessing what it might be.
25547 */
25548 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025549 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551 }
25552
25553 /* Count the paths backward to find the beginning of the desired string. */
25554 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025555 {
25556#ifdef FEAT_MBYTE
25557 if (has_mbyte)
25558 p -= mb_head_off(tfname, p);
25559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025560 if (vim_ispathsep(*p))
25561 {
25562 if (sepcount == 0 || (hasTilde && sepcount == 1))
25563 break;
25564 else
25565 sepcount --;
25566 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568 if (hasTilde)
25569 {
25570 --p;
25571 if (p >= tfname)
25572 *p = '~';
25573 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025574 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025575 }
25576 else
25577 ++p;
25578
25579 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25580 vim_free(*bufp);
25581 *fnamelen = (int)STRLEN(p);
25582 *bufp = pbuf;
25583 *fnamep = p;
25584
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025585 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586}
25587#endif /* WIN3264 */
25588
25589/*
25590 * Adjust a filename, according to a string of modifiers.
25591 * *fnamep must be NUL terminated when called. When returning, the length is
25592 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025593 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594 * When there is an error, *fnamep is set to NULL.
25595 */
25596 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025597modify_fname(
25598 char_u *src, /* string with modifiers */
25599 int *usedlen, /* characters after src that are used */
25600 char_u **fnamep, /* file name so far */
25601 char_u **bufp, /* buffer for allocated file name or NULL */
25602 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025603{
25604 int valid = 0;
25605 char_u *tail;
25606 char_u *s, *p, *pbuf;
25607 char_u dirname[MAXPATHL];
25608 int c;
25609 int has_fullname = 0;
25610#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025611 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612 int has_shortname = 0;
25613#endif
25614
25615repeat:
25616 /* ":p" - full path/file_name */
25617 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25618 {
25619 has_fullname = 1;
25620
25621 valid |= VALID_PATH;
25622 *usedlen += 2;
25623
25624 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25625 if ((*fnamep)[0] == '~'
25626#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25627 && ((*fnamep)[1] == '/'
25628# ifdef BACKSLASH_IN_FILENAME
25629 || (*fnamep)[1] == '\\'
25630# endif
25631 || (*fnamep)[1] == NUL)
25632
25633#endif
25634 )
25635 {
25636 *fnamep = expand_env_save(*fnamep);
25637 vim_free(*bufp); /* free any allocated file name */
25638 *bufp = *fnamep;
25639 if (*fnamep == NULL)
25640 return -1;
25641 }
25642
25643 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025644 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645 {
25646 if (vim_ispathsep(*p)
25647 && p[1] == '.'
25648 && (p[2] == NUL
25649 || vim_ispathsep(p[2])
25650 || (p[2] == '.'
25651 && (p[3] == NUL || vim_ispathsep(p[3])))))
25652 break;
25653 }
25654
25655 /* FullName_save() is slow, don't use it when not needed. */
25656 if (*p != NUL || !vim_isAbsName(*fnamep))
25657 {
25658 *fnamep = FullName_save(*fnamep, *p != NUL);
25659 vim_free(*bufp); /* free any allocated file name */
25660 *bufp = *fnamep;
25661 if (*fnamep == NULL)
25662 return -1;
25663 }
25664
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025665#ifdef WIN3264
25666# if _WIN32_WINNT >= 0x0500
25667 if (vim_strchr(*fnamep, '~') != NULL)
25668 {
25669 /* Expand 8.3 filename to full path. Needed to make sure the same
25670 * file does not have two different names.
25671 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25672 p = alloc(_MAX_PATH + 1);
25673 if (p != NULL)
25674 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025675 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025676 {
25677 vim_free(*bufp);
25678 *bufp = *fnamep = p;
25679 }
25680 else
25681 vim_free(p);
25682 }
25683 }
25684# endif
25685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686 /* Append a path separator to a directory. */
25687 if (mch_isdir(*fnamep))
25688 {
25689 /* Make room for one or two extra characters. */
25690 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25691 vim_free(*bufp); /* free any allocated file name */
25692 *bufp = *fnamep;
25693 if (*fnamep == NULL)
25694 return -1;
25695 add_pathsep(*fnamep);
25696 }
25697 }
25698
25699 /* ":." - path relative to the current directory */
25700 /* ":~" - path relative to the home directory */
25701 /* ":8" - shortname path - postponed till after */
25702 while (src[*usedlen] == ':'
25703 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25704 {
25705 *usedlen += 2;
25706 if (c == '8')
25707 {
25708#ifdef WIN3264
25709 has_shortname = 1; /* Postpone this. */
25710#endif
25711 continue;
25712 }
25713 pbuf = NULL;
25714 /* Need full path first (use expand_env() to remove a "~/") */
25715 if (!has_fullname)
25716 {
25717 if (c == '.' && **fnamep == '~')
25718 p = pbuf = expand_env_save(*fnamep);
25719 else
25720 p = pbuf = FullName_save(*fnamep, FALSE);
25721 }
25722 else
25723 p = *fnamep;
25724
25725 has_fullname = 0;
25726
25727 if (p != NULL)
25728 {
25729 if (c == '.')
25730 {
25731 mch_dirname(dirname, MAXPATHL);
25732 s = shorten_fname(p, dirname);
25733 if (s != NULL)
25734 {
25735 *fnamep = s;
25736 if (pbuf != NULL)
25737 {
25738 vim_free(*bufp); /* free any allocated file name */
25739 *bufp = pbuf;
25740 pbuf = NULL;
25741 }
25742 }
25743 }
25744 else
25745 {
25746 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25747 /* Only replace it when it starts with '~' */
25748 if (*dirname == '~')
25749 {
25750 s = vim_strsave(dirname);
25751 if (s != NULL)
25752 {
25753 *fnamep = s;
25754 vim_free(*bufp);
25755 *bufp = s;
25756 }
25757 }
25758 }
25759 vim_free(pbuf);
25760 }
25761 }
25762
25763 tail = gettail(*fnamep);
25764 *fnamelen = (int)STRLEN(*fnamep);
25765
25766 /* ":h" - head, remove "/file_name", can be repeated */
25767 /* Don't remove the first "/" or "c:\" */
25768 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25769 {
25770 valid |= VALID_HEAD;
25771 *usedlen += 2;
25772 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025773 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025774 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025775 *fnamelen = (int)(tail - *fnamep);
25776#ifdef VMS
25777 if (*fnamelen > 0)
25778 *fnamelen += 1; /* the path separator is part of the path */
25779#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025780 if (*fnamelen == 0)
25781 {
25782 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25783 p = vim_strsave((char_u *)".");
25784 if (p == NULL)
25785 return -1;
25786 vim_free(*bufp);
25787 *bufp = *fnamep = tail = p;
25788 *fnamelen = 1;
25789 }
25790 else
25791 {
25792 while (tail > s && !after_pathsep(s, tail))
25793 mb_ptr_back(*fnamep, tail);
25794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795 }
25796
25797 /* ":8" - shortname */
25798 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25799 {
25800 *usedlen += 2;
25801#ifdef WIN3264
25802 has_shortname = 1;
25803#endif
25804 }
25805
25806#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025807 /*
25808 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809 */
25810 if (has_shortname)
25811 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025812 /* Copy the string if it is shortened by :h and when it wasn't copied
25813 * yet, because we are going to change it in place. Avoids changing
25814 * the buffer name for "%:8". */
25815 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816 {
25817 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025818 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025819 return -1;
25820 vim_free(*bufp);
25821 *bufp = *fnamep = p;
25822 }
25823
25824 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025825 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826 if (!has_fullname && !vim_isAbsName(*fnamep))
25827 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025828 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829 return -1;
25830 }
25831 else
25832 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025833 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834
Bram Moolenaardc935552011-08-17 15:23:23 +020025835 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025837 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 return -1;
25839
25840 if (l == 0)
25841 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025842 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025843 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025844 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025845 return -1;
25846 }
25847 *fnamelen = l;
25848 }
25849 }
25850#endif /* WIN3264 */
25851
25852 /* ":t" - tail, just the basename */
25853 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25854 {
25855 *usedlen += 2;
25856 *fnamelen -= (int)(tail - *fnamep);
25857 *fnamep = tail;
25858 }
25859
25860 /* ":e" - extension, can be repeated */
25861 /* ":r" - root, without extension, can be repeated */
25862 while (src[*usedlen] == ':'
25863 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25864 {
25865 /* find a '.' in the tail:
25866 * - for second :e: before the current fname
25867 * - otherwise: The last '.'
25868 */
25869 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25870 s = *fnamep - 2;
25871 else
25872 s = *fnamep + *fnamelen - 1;
25873 for ( ; s > tail; --s)
25874 if (s[0] == '.')
25875 break;
25876 if (src[*usedlen + 1] == 'e') /* :e */
25877 {
25878 if (s > tail)
25879 {
25880 *fnamelen += (int)(*fnamep - (s + 1));
25881 *fnamep = s + 1;
25882#ifdef VMS
25883 /* cut version from the extension */
25884 s = *fnamep + *fnamelen - 1;
25885 for ( ; s > *fnamep; --s)
25886 if (s[0] == ';')
25887 break;
25888 if (s > *fnamep)
25889 *fnamelen = s - *fnamep;
25890#endif
25891 }
25892 else if (*fnamep <= tail)
25893 *fnamelen = 0;
25894 }
25895 else /* :r */
25896 {
25897 if (s > tail) /* remove one extension */
25898 *fnamelen = (int)(s - *fnamep);
25899 }
25900 *usedlen += 2;
25901 }
25902
25903 /* ":s?pat?foo?" - substitute */
25904 /* ":gs?pat?foo?" - global substitute */
25905 if (src[*usedlen] == ':'
25906 && (src[*usedlen + 1] == 's'
25907 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25908 {
25909 char_u *str;
25910 char_u *pat;
25911 char_u *sub;
25912 int sep;
25913 char_u *flags;
25914 int didit = FALSE;
25915
25916 flags = (char_u *)"";
25917 s = src + *usedlen + 2;
25918 if (src[*usedlen + 1] == 'g')
25919 {
25920 flags = (char_u *)"g";
25921 ++s;
25922 }
25923
25924 sep = *s++;
25925 if (sep)
25926 {
25927 /* find end of pattern */
25928 p = vim_strchr(s, sep);
25929 if (p != NULL)
25930 {
25931 pat = vim_strnsave(s, (int)(p - s));
25932 if (pat != NULL)
25933 {
25934 s = p + 1;
25935 /* find end of substitution */
25936 p = vim_strchr(s, sep);
25937 if (p != NULL)
25938 {
25939 sub = vim_strnsave(s, (int)(p - s));
25940 str = vim_strnsave(*fnamep, *fnamelen);
25941 if (sub != NULL && str != NULL)
25942 {
25943 *usedlen = (int)(p + 1 - src);
25944 s = do_string_sub(str, pat, sub, flags);
25945 if (s != NULL)
25946 {
25947 *fnamep = s;
25948 *fnamelen = (int)STRLEN(s);
25949 vim_free(*bufp);
25950 *bufp = s;
25951 didit = TRUE;
25952 }
25953 }
25954 vim_free(sub);
25955 vim_free(str);
25956 }
25957 vim_free(pat);
25958 }
25959 }
25960 /* after using ":s", repeat all the modifiers */
25961 if (didit)
25962 goto repeat;
25963 }
25964 }
25965
Bram Moolenaar26df0922014-02-23 23:39:13 +010025966 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25967 {
25968 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25969 if (p == NULL)
25970 return -1;
25971 vim_free(*bufp);
25972 *bufp = *fnamep = p;
25973 *fnamelen = (int)STRLEN(p);
25974 *usedlen += 2;
25975 }
25976
Bram Moolenaar071d4272004-06-13 20:20:40 +000025977 return valid;
25978}
25979
25980/*
25981 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25982 * "flags" can be "g" to do a global substitute.
25983 * Returns an allocated string, NULL for error.
25984 */
25985 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025986do_string_sub(
25987 char_u *str,
25988 char_u *pat,
25989 char_u *sub,
25990 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025991{
25992 int sublen;
25993 regmatch_T regmatch;
25994 int i;
25995 int do_all;
25996 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025997 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998 garray_T ga;
25999 char_u *ret;
26000 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026001 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026002
26003 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26004 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026005 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026006
26007 ga_init2(&ga, 1, 200);
26008
26009 do_all = (flags[0] == 'g');
26010
26011 regmatch.rm_ic = p_ic;
26012 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26013 if (regmatch.regprog != NULL)
26014 {
26015 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026016 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026017 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26018 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026019 /* Skip empty match except for first match. */
26020 if (regmatch.startp[0] == regmatch.endp[0])
26021 {
26022 if (zero_width == regmatch.startp[0])
26023 {
26024 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026025 i = MB_PTR2LEN(tail);
26026 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26027 (size_t)i);
26028 ga.ga_len += i;
26029 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026030 continue;
26031 }
26032 zero_width = regmatch.startp[0];
26033 }
26034
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035 /*
26036 * Get some space for a temporary buffer to do the substitution
26037 * into. It will contain:
26038 * - The text up to where the match is.
26039 * - The substituted text.
26040 * - The text after the match.
26041 */
26042 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026043 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026044 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26045 {
26046 ga_clear(&ga);
26047 break;
26048 }
26049
26050 /* copy the text up to where the match is */
26051 i = (int)(regmatch.startp[0] - tail);
26052 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26053 /* add the substituted text */
26054 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26055 + ga.ga_len + i, TRUE, TRUE, FALSE);
26056 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026057 tail = regmatch.endp[0];
26058 if (*tail == NUL)
26059 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026060 if (!do_all)
26061 break;
26062 }
26063
26064 if (ga.ga_data != NULL)
26065 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26066
Bram Moolenaar473de612013-06-08 18:19:48 +020026067 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068 }
26069
26070 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26071 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026072 if (p_cpo == empty_option)
26073 p_cpo = save_cpo;
26074 else
26075 /* Darn, evaluating {sub} expression changed the value. */
26076 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077
26078 return ret;
26079}
26080
26081#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */