blob: b82a98fdb182763f78cd510a6752306212db24de [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 Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200101static char *e_stringreq = N_("E928: String required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200111#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200112static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000114
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100115#define NAMESPACE_CHAR (char_u *)"abglstvw"
116
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200117static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000118#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
120/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000121 * Old Vim variables such as "v:version" are also available without the "v:".
122 * Also in functions. We need a special hashtable for them.
123 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000124static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125
126/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000127 * When recursively copying lists and dicts we need to remember which ones we
128 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000129 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000130 */
131static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000132#define COPYID_INC 2
133#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134
Bram Moolenaar8502c702014-06-17 12:51:16 +0200135/* Abort conversion to string after a recursion error. */
136static int did_echo_string_emsg = FALSE;
137
Bram Moolenaard9fba312005-06-26 22:34:35 +0000138/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000139 * Array to hold the hashtab with variables local to each sourced script.
140 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000142typedef struct
143{
144 dictitem_T sv_var;
145 dict_T sv_dict;
146} scriptvar_T;
147
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200148static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
149#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
150#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152static int echo_attr = 0; /* attributes used for ":echo" */
153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000154/* Values for trans_function_name() argument: */
155#define TFN_INT 1 /* internal function name OK */
156#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100157#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
158
159/* Values for get_lval() flags argument: */
160#define GLV_QUIET TFN_QUIET /* no error messages */
161#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000162
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163/*
164 * Structure to hold info for a user function.
165 */
166typedef struct ufunc ufunc_T;
167
168struct ufunc
169{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000170 int uf_varargs; /* variable nr of arguments */
171 int uf_flags;
172 int uf_calls; /* nr of active calls */
173 garray_T uf_args; /* arguments */
174 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000175#ifdef FEAT_PROFILE
176 int uf_profiling; /* TRUE when func is being profiled */
177 /* profiling the function as a whole */
178 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000179 proftime_T uf_tm_total; /* time spent in function + children */
180 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000181 proftime_T uf_tm_children; /* time spent in children this call */
182 /* profiling the function per line */
183 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000184 proftime_T *uf_tml_total; /* time spent in a line + children */
185 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000186 proftime_T uf_tml_start; /* start time for current line */
187 proftime_T uf_tml_children; /* time spent in children for this line */
188 proftime_T uf_tml_wait; /* start wait time for current line */
189 int uf_tml_idx; /* index of line being timed; -1 if none */
190 int uf_tml_execed; /* line being timed was executed */
191#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000192 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 int uf_refcount; /* for numbered function: reference count */
195 char_u uf_name[1]; /* name of function (actually longer); can
196 start with <SNR>123_ (<SNR> is K_SPECIAL
197 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198};
199
200/* function flags */
201#define FC_ABORT 1 /* abort function on error */
202#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000203#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000206 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000208static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000210/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000211static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
212
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100213/* List heads for garbage collection. Although there can be a reference loop
214 * from partial to dict to partial, we don't need to keep track of the partial,
215 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000216static 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 Moolenaarbee6c0c2016-03-25 15:40:50 +0100296#define VV_NAME(s, t) s, {{t, 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 Moolenaarbee6c0c2016-03-25 15:40:50 +0100301 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100371 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100372 {VV_NAME("false", VAR_SPECIAL), VV_RO},
373 {VV_NAME("true", VAR_SPECIAL), VV_RO},
374 {VV_NAME("null", VAR_SPECIAL), VV_RO},
375 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100376 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200377 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000378};
379
380/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000381#define vv_type vv_di.di_tv.v_type
382#define vv_nr vv_di.di_tv.vval.v_number
383#define vv_float vv_di.di_tv.vval.v_float
384#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000385#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200386#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000387#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000388
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200389static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000390#define vimvarht vimvardict.dv_hashtab
391
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100392static void prepare_vimvar(int idx, typval_T *save_tv);
393static void restore_vimvar(int idx, typval_T *save_tv);
394static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
395static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
396static char_u *skip_var_one(char_u *arg);
397static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
398static void list_glob_vars(int *first);
399static void list_buf_vars(int *first);
400static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000401#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100402static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000403#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100404static void list_vim_vars(int *first);
405static void list_script_vars(int *first);
406static void list_func_vars(int *first);
407static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
408static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
409static int check_changedtick(char_u *arg);
410static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
411static void clear_lval(lval_T *lp);
412static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
413static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
414static void list_fix_watch(list_T *l, listitem_T *item);
415static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
416static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
417static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
418static void item_lock(typval_T *tv, int deep, int lock);
419static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000420
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100421static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
422static int eval1(char_u **arg, typval_T *rettv, int evaluate);
423static int eval2(char_u **arg, typval_T *rettv, int evaluate);
424static int eval3(char_u **arg, typval_T *rettv, int evaluate);
425static int eval4(char_u **arg, typval_T *rettv, int evaluate);
426static int eval5(char_u **arg, typval_T *rettv, int evaluate);
427static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
428static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000429
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100430static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
431static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
434static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200435static void list_free_contents(list_T *l);
436static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100437static long list_len(list_T *l);
438static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
439static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
440static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
441static long list_find_nr(list_T *l, long idx, int *errorp);
442static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100443static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
444static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
445static list_T *list_copy(list_T *orig, int deep, int copyID);
446static char_u *list2string(typval_T *tv, int copyID);
447static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
448static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
449static int free_unref_items(int copyID);
450static dictitem_T *dictitem_copy(dictitem_T *org);
451static void dictitem_remove(dict_T *dict, dictitem_T *item);
452static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
453static long dict_len(dict_T *d);
454static char_u *dict2string(typval_T *tv, int copyID);
455static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
456static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static char_u *string_quote(char_u *str, int function);
458static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
459static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100460static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
461static 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, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static void emsg_funcname(char *ermsg, char_u *name);
463static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200465static void dict_free_contents(dict_T *d);
466static void dict_free_dict(dict_T *d);
467
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100469static void f_abs(typval_T *argvars, typval_T *rettv);
470static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100472static void f_add(typval_T *argvars, typval_T *rettv);
473static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
474static void f_and(typval_T *argvars, typval_T *rettv);
475static void f_append(typval_T *argvars, typval_T *rettv);
476static void f_argc(typval_T *argvars, typval_T *rettv);
477static void f_argidx(typval_T *argvars, typval_T *rettv);
478static void f_arglistid(typval_T *argvars, typval_T *rettv);
479static void f_argv(typval_T *argvars, typval_T *rettv);
480static void f_assert_equal(typval_T *argvars, typval_T *rettv);
481static void f_assert_exception(typval_T *argvars, typval_T *rettv);
482static void f_assert_fails(typval_T *argvars, typval_T *rettv);
483static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200484static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200485static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
486static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100487static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_asin(typval_T *argvars, typval_T *rettv);
490static void f_atan(typval_T *argvars, typval_T *rettv);
491static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100493static void f_browse(typval_T *argvars, typval_T *rettv);
494static void f_browsedir(typval_T *argvars, typval_T *rettv);
495static void f_bufexists(typval_T *argvars, typval_T *rettv);
496static void f_buflisted(typval_T *argvars, typval_T *rettv);
497static void f_bufloaded(typval_T *argvars, typval_T *rettv);
498static void f_bufname(typval_T *argvars, typval_T *rettv);
499static void f_bufnr(typval_T *argvars, typval_T *rettv);
500static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
501static void f_byte2line(typval_T *argvars, typval_T *rettv);
502static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
503static void f_byteidx(typval_T *argvars, typval_T *rettv);
504static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
505static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000506#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100507static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100509#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100511static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
512static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100513static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100514static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100515static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100516static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100517static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
518static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100519static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100520static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100521static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
522static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100523static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100524static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100525#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100526static void f_changenr(typval_T *argvars, typval_T *rettv);
527static void f_char2nr(typval_T *argvars, typval_T *rettv);
528static void f_cindent(typval_T *argvars, typval_T *rettv);
529static void f_clearmatches(typval_T *argvars, typval_T *rettv);
530static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000531#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100532static void f_complete(typval_T *argvars, typval_T *rettv);
533static void f_complete_add(typval_T *argvars, typval_T *rettv);
534static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000535#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100536static void f_confirm(typval_T *argvars, typval_T *rettv);
537static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_cos(typval_T *argvars, typval_T *rettv);
540static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000541#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_count(typval_T *argvars, typval_T *rettv);
543static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
544static void f_cursor(typval_T *argsvars, typval_T *rettv);
545static void f_deepcopy(typval_T *argvars, typval_T *rettv);
546static void f_delete(typval_T *argvars, typval_T *rettv);
547static void f_did_filetype(typval_T *argvars, typval_T *rettv);
548static void f_diff_filler(typval_T *argvars, typval_T *rettv);
549static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100550static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_empty(typval_T *argvars, typval_T *rettv);
552static void f_escape(typval_T *argvars, typval_T *rettv);
553static void f_eval(typval_T *argvars, typval_T *rettv);
554static void f_eventhandler(typval_T *argvars, typval_T *rettv);
555static void f_executable(typval_T *argvars, typval_T *rettv);
556static void f_exepath(typval_T *argvars, typval_T *rettv);
557static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200558#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200560#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_expand(typval_T *argvars, typval_T *rettv);
562static void f_extend(typval_T *argvars, typval_T *rettv);
563static void f_feedkeys(typval_T *argvars, typval_T *rettv);
564static void f_filereadable(typval_T *argvars, typval_T *rettv);
565static void f_filewritable(typval_T *argvars, typval_T *rettv);
566static void f_filter(typval_T *argvars, typval_T *rettv);
567static void f_finddir(typval_T *argvars, typval_T *rettv);
568static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000569#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100570static void f_float2nr(typval_T *argvars, typval_T *rettv);
571static void f_floor(typval_T *argvars, typval_T *rettv);
572static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000573#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100574static void f_fnameescape(typval_T *argvars, typval_T *rettv);
575static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
576static void f_foldclosed(typval_T *argvars, typval_T *rettv);
577static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
578static void f_foldlevel(typval_T *argvars, typval_T *rettv);
579static void f_foldtext(typval_T *argvars, typval_T *rettv);
580static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
581static void f_foreground(typval_T *argvars, typval_T *rettv);
582static void f_function(typval_T *argvars, typval_T *rettv);
583static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200584static void f_garbagecollect_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100585static void f_get(typval_T *argvars, typval_T *rettv);
586static void f_getbufline(typval_T *argvars, typval_T *rettv);
587static void f_getbufvar(typval_T *argvars, typval_T *rettv);
588static void f_getchar(typval_T *argvars, typval_T *rettv);
589static void f_getcharmod(typval_T *argvars, typval_T *rettv);
590static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
591static void f_getcmdline(typval_T *argvars, typval_T *rettv);
592static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
593static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
594static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
595static void f_getcwd(typval_T *argvars, typval_T *rettv);
596static void f_getfontname(typval_T *argvars, typval_T *rettv);
597static void f_getfperm(typval_T *argvars, typval_T *rettv);
598static void f_getfsize(typval_T *argvars, typval_T *rettv);
599static void f_getftime(typval_T *argvars, typval_T *rettv);
600static void f_getftype(typval_T *argvars, typval_T *rettv);
601static void f_getline(typval_T *argvars, typval_T *rettv);
602static void f_getmatches(typval_T *argvars, typval_T *rettv);
603static void f_getpid(typval_T *argvars, typval_T *rettv);
604static void f_getcurpos(typval_T *argvars, typval_T *rettv);
605static void f_getpos(typval_T *argvars, typval_T *rettv);
606static void f_getqflist(typval_T *argvars, typval_T *rettv);
607static void f_getreg(typval_T *argvars, typval_T *rettv);
608static void f_getregtype(typval_T *argvars, typval_T *rettv);
609static void f_gettabvar(typval_T *argvars, typval_T *rettv);
610static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
611static void f_getwinposx(typval_T *argvars, typval_T *rettv);
612static void f_getwinposy(typval_T *argvars, typval_T *rettv);
613static void f_getwinvar(typval_T *argvars, typval_T *rettv);
614static void f_glob(typval_T *argvars, typval_T *rettv);
615static void f_globpath(typval_T *argvars, typval_T *rettv);
616static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
617static void f_has(typval_T *argvars, typval_T *rettv);
618static void f_has_key(typval_T *argvars, typval_T *rettv);
619static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
620static void f_hasmapto(typval_T *argvars, typval_T *rettv);
621static void f_histadd(typval_T *argvars, typval_T *rettv);
622static void f_histdel(typval_T *argvars, typval_T *rettv);
623static void f_histget(typval_T *argvars, typval_T *rettv);
624static void f_histnr(typval_T *argvars, typval_T *rettv);
625static void f_hlID(typval_T *argvars, typval_T *rettv);
626static void f_hlexists(typval_T *argvars, typval_T *rettv);
627static void f_hostname(typval_T *argvars, typval_T *rettv);
628static void f_iconv(typval_T *argvars, typval_T *rettv);
629static void f_indent(typval_T *argvars, typval_T *rettv);
630static void f_index(typval_T *argvars, typval_T *rettv);
631static void f_input(typval_T *argvars, typval_T *rettv);
632static void f_inputdialog(typval_T *argvars, typval_T *rettv);
633static void f_inputlist(typval_T *argvars, typval_T *rettv);
634static void f_inputrestore(typval_T *argvars, typval_T *rettv);
635static void f_inputsave(typval_T *argvars, typval_T *rettv);
636static void f_inputsecret(typval_T *argvars, typval_T *rettv);
637static void f_insert(typval_T *argvars, typval_T *rettv);
638static void f_invert(typval_T *argvars, typval_T *rettv);
639static void f_isdirectory(typval_T *argvars, typval_T *rettv);
640static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100641#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
642static void f_isnan(typval_T *argvars, typval_T *rettv);
643#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100645#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100646static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100647static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100648static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100649static void f_job_start(typval_T *argvars, typval_T *rettv);
650static void f_job_stop(typval_T *argvars, typval_T *rettv);
651static void f_job_status(typval_T *argvars, typval_T *rettv);
652#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100653static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100654static void f_js_decode(typval_T *argvars, typval_T *rettv);
655static void f_js_encode(typval_T *argvars, typval_T *rettv);
656static void f_json_decode(typval_T *argvars, typval_T *rettv);
657static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_keys(typval_T *argvars, typval_T *rettv);
659static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
660static void f_len(typval_T *argvars, typval_T *rettv);
661static void f_libcall(typval_T *argvars, typval_T *rettv);
662static void f_libcallnr(typval_T *argvars, typval_T *rettv);
663static void f_line(typval_T *argvars, typval_T *rettv);
664static void f_line2byte(typval_T *argvars, typval_T *rettv);
665static void f_lispindent(typval_T *argvars, typval_T *rettv);
666static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000667#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100668static void f_log(typval_T *argvars, typval_T *rettv);
669static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200671#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200673#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_map(typval_T *argvars, typval_T *rettv);
675static void f_maparg(typval_T *argvars, typval_T *rettv);
676static void f_mapcheck(typval_T *argvars, typval_T *rettv);
677static void f_match(typval_T *argvars, typval_T *rettv);
678static void f_matchadd(typval_T *argvars, typval_T *rettv);
679static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
680static void f_matcharg(typval_T *argvars, typval_T *rettv);
681static void f_matchdelete(typval_T *argvars, typval_T *rettv);
682static void f_matchend(typval_T *argvars, typval_T *rettv);
683static void f_matchlist(typval_T *argvars, typval_T *rettv);
684static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200685static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_max(typval_T *argvars, typval_T *rettv);
687static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000688#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100692#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
696static void f_nr2char(typval_T *argvars, typval_T *rettv);
697static void f_or(typval_T *argvars, typval_T *rettv);
698static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100699#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100701#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
706static void f_printf(typval_T *argvars, typval_T *rettv);
707static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200708#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200710#endif
711#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200713#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100714static void f_range(typval_T *argvars, typval_T *rettv);
715static void f_readfile(typval_T *argvars, typval_T *rettv);
716static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100717#ifdef FEAT_FLOAT
718static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
719#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_reltimestr(typval_T *argvars, typval_T *rettv);
721static void f_remote_expr(typval_T *argvars, typval_T *rettv);
722static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
723static void f_remote_peek(typval_T *argvars, typval_T *rettv);
724static void f_remote_read(typval_T *argvars, typval_T *rettv);
725static void f_remote_send(typval_T *argvars, typval_T *rettv);
726static void f_remove(typval_T *argvars, typval_T *rettv);
727static void f_rename(typval_T *argvars, typval_T *rettv);
728static void f_repeat(typval_T *argvars, typval_T *rettv);
729static void f_resolve(typval_T *argvars, typval_T *rettv);
730static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000731#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100732static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_screenattr(typval_T *argvars, typval_T *rettv);
735static void f_screenchar(typval_T *argvars, typval_T *rettv);
736static void f_screencol(typval_T *argvars, typval_T *rettv);
737static void f_screenrow(typval_T *argvars, typval_T *rettv);
738static void f_search(typval_T *argvars, typval_T *rettv);
739static void f_searchdecl(typval_T *argvars, typval_T *rettv);
740static void f_searchpair(typval_T *argvars, typval_T *rettv);
741static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
742static void f_searchpos(typval_T *argvars, typval_T *rettv);
743static void f_server2client(typval_T *argvars, typval_T *rettv);
744static void f_serverlist(typval_T *argvars, typval_T *rettv);
745static void f_setbufvar(typval_T *argvars, typval_T *rettv);
746static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
747static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100748static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_setline(typval_T *argvars, typval_T *rettv);
750static void f_setloclist(typval_T *argvars, typval_T *rettv);
751static void f_setmatches(typval_T *argvars, typval_T *rettv);
752static void f_setpos(typval_T *argvars, typval_T *rettv);
753static void f_setqflist(typval_T *argvars, typval_T *rettv);
754static void f_setreg(typval_T *argvars, typval_T *rettv);
755static void f_settabvar(typval_T *argvars, typval_T *rettv);
756static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
757static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100758#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100760#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_shellescape(typval_T *argvars, typval_T *rettv);
762static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
763static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_sin(typval_T *argvars, typval_T *rettv);
766static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_sort(typval_T *argvars, typval_T *rettv);
769static void f_soundfold(typval_T *argvars, typval_T *rettv);
770static void f_spellbadword(typval_T *argvars, typval_T *rettv);
771static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
772static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000773#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100774static void f_sqrt(typval_T *argvars, typval_T *rettv);
775static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000776#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100777static void f_str2nr(typval_T *argvars, typval_T *rettv);
778static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000779#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200782static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100783static void f_stridx(typval_T *argvars, typval_T *rettv);
784static void f_string(typval_T *argvars, typval_T *rettv);
785static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200786static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_strpart(typval_T *argvars, typval_T *rettv);
788static void f_strridx(typval_T *argvars, typval_T *rettv);
789static void f_strtrans(typval_T *argvars, typval_T *rettv);
790static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
791static void f_strwidth(typval_T *argvars, typval_T *rettv);
792static void f_submatch(typval_T *argvars, typval_T *rettv);
793static void f_substitute(typval_T *argvars, typval_T *rettv);
794static void f_synID(typval_T *argvars, typval_T *rettv);
795static void f_synIDattr(typval_T *argvars, typval_T *rettv);
796static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
797static void f_synstack(typval_T *argvars, typval_T *rettv);
798static void f_synconcealed(typval_T *argvars, typval_T *rettv);
799static void f_system(typval_T *argvars, typval_T *rettv);
800static void f_systemlist(typval_T *argvars, typval_T *rettv);
801static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
802static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
803static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
804static void f_taglist(typval_T *argvars, typval_T *rettv);
805static void f_tagfiles(typval_T *argvars, typval_T *rettv);
806static void f_tempname(typval_T *argvars, typval_T *rettv);
807static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200808#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100809static void f_tan(typval_T *argvars, typval_T *rettv);
810static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200811#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100812#ifdef FEAT_TIMERS
813static void f_timer_start(typval_T *argvars, typval_T *rettv);
814static void f_timer_stop(typval_T *argvars, typval_T *rettv);
815#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100816static void f_tolower(typval_T *argvars, typval_T *rettv);
817static void f_toupper(typval_T *argvars, typval_T *rettv);
818static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000819#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100820static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000821#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100822static void f_type(typval_T *argvars, typval_T *rettv);
823static void f_undofile(typval_T *argvars, typval_T *rettv);
824static void f_undotree(typval_T *argvars, typval_T *rettv);
825static void f_uniq(typval_T *argvars, typval_T *rettv);
826static void f_values(typval_T *argvars, typval_T *rettv);
827static void f_virtcol(typval_T *argvars, typval_T *rettv);
828static void f_visualmode(typval_T *argvars, typval_T *rettv);
829static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100830static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100831static void f_win_getid(typval_T *argvars, typval_T *rettv);
832static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
833static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
834static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static void f_winbufnr(typval_T *argvars, typval_T *rettv);
836static void f_wincol(typval_T *argvars, typval_T *rettv);
837static void f_winheight(typval_T *argvars, typval_T *rettv);
838static void f_winline(typval_T *argvars, typval_T *rettv);
839static void f_winnr(typval_T *argvars, typval_T *rettv);
840static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
841static void f_winrestview(typval_T *argvars, typval_T *rettv);
842static void f_winsaveview(typval_T *argvars, typval_T *rettv);
843static void f_winwidth(typval_T *argvars, typval_T *rettv);
844static void f_writefile(typval_T *argvars, typval_T *rettv);
845static void f_wordcount(typval_T *argvars, typval_T *rettv);
846static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000847
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100848static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
849static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
850static int get_env_len(char_u **arg);
851static int get_id_len(char_u **arg);
852static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
853static 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 +0000854#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
855#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
856 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100857static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
858static int eval_isnamec(int c);
859static int eval_isnamec1(int c);
860static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
861static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100862static typval_T *alloc_string_tv(char_u *string);
863static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100864#ifdef FEAT_FLOAT
865static float_T get_tv_float(typval_T *varp);
866#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100867static linenr_T get_tv_lnum(typval_T *argvars);
868static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
870static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
871static hashtab_T *find_var_ht(char_u *name, char_u **varname);
872static funccall_T *get_funccal(void);
873static void vars_clear_ext(hashtab_T *ht, int free_val);
874static void delete_var(hashtab_T *ht, hashitem_T *hi);
875static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
876static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
877static void set_var(char_u *name, typval_T *varp, int copy);
878static int var_check_ro(int flags, char_u *name, int use_gettext);
879static int var_check_fixed(int flags, char_u *name, int use_gettext);
880static int var_check_func_name(char_u *name, int new_var);
881static int valid_varname(char_u *varname);
882static int tv_check_lock(int lock, char_u *name, int use_gettext);
883static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
884static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100885static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886static int eval_fname_script(char_u *p);
887static int eval_fname_sid(char_u *p);
888static void list_func_head(ufunc_T *fp, int indent);
889static ufunc_T *find_func(char_u *name);
890static int function_exists(char_u *name);
891static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100893static void func_do_profile(ufunc_T *fp);
894static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
895static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000896static int
897# ifdef __BORLANDC__
898 _RTLENTRYF
899# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100900 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000901static int
902# ifdef __BORLANDC__
903 _RTLENTRYF
904# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100905 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100907static int script_autoload(char_u *name, int reload);
908static char_u *autoload_name(char_u *name);
909static void cat_func_name(char_u *buf, ufunc_T *fp);
910static void func_free(ufunc_T *fp);
911static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
912static int can_free_funccal(funccall_T *fc, int copyID) ;
913static void free_funccal(funccall_T *fc, int free_val);
914static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
915static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
916static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
917static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
918static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
919static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
920static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
921static int write_list(FILE *fd, list_T *list, int binary);
922static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000923
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200924
925#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100926static int compare_func_name(const void *s1, const void *s2);
927static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200928#endif
929
Bram Moolenaar33570922005-01-25 22:26:29 +0000930/*
931 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000932 */
933 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100934eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000935{
Bram Moolenaar33570922005-01-25 22:26:29 +0000936 int i;
937 struct vimvar *p;
938
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200939 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
940 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200941 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000942 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000943 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000944
945 for (i = 0; i < VV_LEN; ++i)
946 {
947 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200948 if (STRLEN(p->vv_name) > 16)
949 {
950 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
951 getout(1);
952 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000953 STRCPY(p->vv_di.di_key, p->vv_name);
954 if (p->vv_flags & VV_RO)
955 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
956 else if (p->vv_flags & VV_RO_SBX)
957 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
958 else
959 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000960
961 /* add to v: scope dict, unless the value is not always available */
962 if (p->vv_type != VAR_UNKNOWN)
963 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000964 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000965 /* add to compat scope dict */
966 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000967 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100968 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
969
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000970 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100971 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200972 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100973 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100974
975 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
976 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
977 set_vim_var_nr(VV_NONE, VVAL_NONE);
978 set_vim_var_nr(VV_NULL, VVAL_NULL);
979
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200980 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200981
982#ifdef EBCDIC
983 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100984 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200985 */
986 sortFunctions();
987#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000988}
989
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990#if defined(EXITFREE) || defined(PROTO)
991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100992eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000993{
994 int i;
995 struct vimvar *p;
996
997 for (i = 0; i < VV_LEN; ++i)
998 {
999 p = &vimvars[i];
1000 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001001 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001002 vim_free(p->vv_str);
1003 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001004 }
1005 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1006 {
1007 list_unref(p->vv_list);
1008 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001009 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001010 }
1011 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001012 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001013 hash_clear(&compat_hashtab);
1014
Bram Moolenaard9fba312005-06-26 22:34:35 +00001015 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001016# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001017 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001018# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001019
1020 /* global variables */
1021 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001022
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001023 /* autoloaded script names */
1024 ga_clear_strings(&ga_loaded);
1025
Bram Moolenaarcca74132013-09-25 21:00:28 +02001026 /* Script-local variables. First clear all the variables and in a second
1027 * loop free the scriptvar_T, because a variable in one script might hold
1028 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001029 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001030 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001031 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001032 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001033 ga_clear(&ga_scripts);
1034
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001035 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001036 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001037
1038 /* functions */
1039 free_all_functions();
1040 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001041}
1042#endif
1043
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 * Return the name of the executed function.
1046 */
1047 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001048func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
1052
1053/*
1054 * Return the address holding the next breakpoint line for a funccall cookie.
1055 */
1056 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001057func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
Bram Moolenaar33570922005-01-25 22:26:29 +00001059 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060}
1061
1062/*
1063 * Return the address holding the debug tick for a funccall cookie.
1064 */
1065 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001066func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
Bram Moolenaar33570922005-01-25 22:26:29 +00001068 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069}
1070
1071/*
1072 * Return the nesting level for a funccall cookie.
1073 */
1074 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001075func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
Bram Moolenaar33570922005-01-25 22:26:29 +00001077 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078}
1079
1080/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001081funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001083/* pointer to list of previously used funccal, still around because some
1084 * item in it is still being used. */
1085funccall_T *previous_funccal = NULL;
1086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087/*
1088 * Return TRUE when a function was ended by a ":return" command.
1089 */
1090 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001091current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
1093 return current_funccal->returned;
1094}
1095
1096
1097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 * Set an internal variable to a string value. Creates the variable if it does
1099 * not already exist.
1100 */
1101 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001102set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001104 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001105 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
1107 val = vim_strsave(value);
1108 if (val != NULL)
1109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001110 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001111 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001113 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001114 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 }
1116 }
1117}
1118
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121static char_u *redir_endp = NULL;
1122static char_u *redir_varname = NULL;
1123
1124/*
1125 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001126 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 * Returns OK if successfully completed the setup. FAIL otherwise.
1128 */
1129 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001130var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131{
1132 int save_emsg;
1133 int err;
1134 typval_T tv;
1135
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001136 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001137 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 {
1139 EMSG(_(e_invarg));
1140 return FAIL;
1141 }
1142
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 redir_varname = vim_strsave(name);
1145 if (redir_varname == NULL)
1146 return FAIL;
1147
1148 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1149 if (redir_lval == NULL)
1150 {
1151 var_redir_stop();
1152 return FAIL;
1153 }
1154
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 /* The output is stored in growarray "redir_ga" until redirection ends. */
1156 ga_init2(&redir_ga, (int)sizeof(char), 500);
1157
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001159 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001160 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1162 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001163 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 if (redir_endp != NULL && *redir_endp != NUL)
1165 /* Trailing characters are present after the variable name */
1166 EMSG(_(e_trailing));
1167 else
1168 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001169 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 var_redir_stop();
1171 return FAIL;
1172 }
1173
1174 /* check if we can write to the variable: set it to or append an empty
1175 * string */
1176 save_emsg = did_emsg;
1177 did_emsg = FALSE;
1178 tv.v_type = VAR_STRING;
1179 tv.vval.v_string = (char_u *)"";
1180 if (append)
1181 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1182 else
1183 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001184 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001186 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 if (err)
1188 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001189 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190 var_redir_stop();
1191 return FAIL;
1192 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193
1194 return OK;
1195}
1196
1197/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001198 * Append "value[value_len]" to the variable set by var_redir_start().
1199 * The actual appending is postponed until redirection ends, because the value
1200 * appended may in fact be the string we write to, changing it may cause freed
1201 * memory to be used:
1202 * :redir => foo
1203 * :let foo
1204 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205 */
1206 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001207var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001209 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210
1211 if (redir_lval == NULL)
1212 return;
1213
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001214 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001215 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001217 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001218
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001219 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001220 {
1221 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001222 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001223 }
1224 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001225 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001226}
1227
1228/*
1229 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001230 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001231 */
1232 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001233var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001234{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001235 typval_T tv;
1236
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237 if (redir_lval != NULL)
1238 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001239 /* If there was no error: assign the text to the variable. */
1240 if (redir_endp != NULL)
1241 {
1242 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1243 tv.v_type = VAR_STRING;
1244 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001245 /* Call get_lval() again, if it's inside a Dict or List it may
1246 * have changed. */
1247 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001248 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001249 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1250 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1251 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001252 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001253
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001254 /* free the collected output */
1255 vim_free(redir_ga.ga_data);
1256 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001257
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001258 vim_free(redir_lval);
1259 redir_lval = NULL;
1260 }
1261 vim_free(redir_varname);
1262 redir_varname = NULL;
1263}
1264
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265# if defined(FEAT_MBYTE) || defined(PROTO)
1266 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001267eval_charconvert(
1268 char_u *enc_from,
1269 char_u *enc_to,
1270 char_u *fname_from,
1271 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272{
1273 int err = FALSE;
1274
1275 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1276 set_vim_var_string(VV_CC_TO, enc_to, -1);
1277 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1278 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1279 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1280 err = TRUE;
1281 set_vim_var_string(VV_CC_FROM, NULL, -1);
1282 set_vim_var_string(VV_CC_TO, NULL, -1);
1283 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1284 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1285
1286 if (err)
1287 return FAIL;
1288 return OK;
1289}
1290# endif
1291
1292# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1293 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001294eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
1296 int err = FALSE;
1297
1298 set_vim_var_string(VV_FNAME_IN, fname, -1);
1299 set_vim_var_string(VV_CMDARG, args, -1);
1300 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1301 err = TRUE;
1302 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1303 set_vim_var_string(VV_CMDARG, NULL, -1);
1304
1305 if (err)
1306 {
1307 mch_remove(fname);
1308 return FAIL;
1309 }
1310 return OK;
1311}
1312# endif
1313
1314# if defined(FEAT_DIFF) || defined(PROTO)
1315 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001316eval_diff(
1317 char_u *origfile,
1318 char_u *newfile,
1319 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320{
1321 int err = FALSE;
1322
1323 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1324 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1325 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1326 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1327 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1328 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1329 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1330}
1331
1332 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001333eval_patch(
1334 char_u *origfile,
1335 char_u *difffile,
1336 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
1338 int err;
1339
1340 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1341 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1342 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1343 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1344 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1345 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1346 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1347}
1348# endif
1349
1350/*
1351 * Top level evaluation function, returning a boolean.
1352 * Sets "error" to TRUE if there was an error.
1353 * Return TRUE or FALSE.
1354 */
1355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001356eval_to_bool(
1357 char_u *arg,
1358 int *error,
1359 char_u **nextcmd,
1360 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
Bram Moolenaar33570922005-01-25 22:26:29 +00001362 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 int retval = FALSE;
1364
1365 if (skip)
1366 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 else
1370 {
1371 *error = FALSE;
1372 if (!skip)
1373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001374 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
1377 }
1378 if (skip)
1379 --emsg_skip;
1380
1381 return retval;
1382}
1383
1384/*
1385 * Top level evaluation function, returning a string. If "skip" is TRUE,
1386 * only parsing to "nextcmd" is done, without reporting errors. Return
1387 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1388 */
1389 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001390eval_to_string_skip(
1391 char_u *arg,
1392 char_u **nextcmd,
1393 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
Bram Moolenaar33570922005-01-25 22:26:29 +00001395 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 char_u *retval;
1397
1398 if (skip)
1399 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001400 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 retval = NULL;
1402 else
1403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001404 retval = vim_strsave(get_tv_string(&tv));
1405 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 }
1407 if (skip)
1408 --emsg_skip;
1409
1410 return retval;
1411}
1412
1413/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001414 * Skip over an expression at "*pp".
1415 * Return FAIL for an error, OK otherwise.
1416 */
1417 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001418skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001419{
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001421
1422 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001424}
1425
1426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001428 * When "convert" is TRUE convert a List into a sequence of lines and convert
1429 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 * Return pointer to allocated memory, or NULL for failure.
1431 */
1432 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001433eval_to_string(
1434 char_u *arg,
1435 char_u **nextcmd,
1436 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
Bram Moolenaar33570922005-01-25 22:26:29 +00001438 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001440 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001441#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001442 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001445 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 retval = NULL;
1447 else
1448 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001449 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001450 {
1451 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001452 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001453 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001454 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001455 if (tv.vval.v_list->lv_len > 0)
1456 ga_append(&ga, NL);
1457 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001458 ga_append(&ga, NUL);
1459 retval = (char_u *)ga.ga_data;
1460 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001461#ifdef FEAT_FLOAT
1462 else if (convert && tv.v_type == VAR_FLOAT)
1463 {
1464 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1465 retval = vim_strsave(numbuf);
1466 }
1467#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001468 else
1469 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001470 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 }
1472
1473 return retval;
1474}
1475
1476/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001477 * Call eval_to_string() without using current local variables and using
1478 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 */
1480 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001481eval_to_string_safe(
1482 char_u *arg,
1483 char_u **nextcmd,
1484 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
1486 char_u *retval;
1487 void *save_funccalp;
1488
1489 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001490 if (use_sandbox)
1491 ++sandbox;
1492 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001493 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001494 if (use_sandbox)
1495 --sandbox;
1496 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 restore_funccal(save_funccalp);
1498 return retval;
1499}
1500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501/*
1502 * Top level evaluation function, returning a number.
1503 * Evaluates "expr" silently.
1504 * Returns -1 for an error.
1505 */
1506 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001507eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
Bram Moolenaar33570922005-01-25 22:26:29 +00001509 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001511 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
1513 ++emsg_off;
1514
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001515 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 retval = -1;
1517 else
1518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001519 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 }
1522 --emsg_off;
1523
1524 return retval;
1525}
1526
Bram Moolenaara40058a2005-07-11 22:42:07 +00001527/*
1528 * Prepare v: variable "idx" to be used.
1529 * Save the current typeval in "save_tv".
1530 * When not used yet add the variable to the v: hashtable.
1531 */
1532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001533prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001534{
1535 *save_tv = vimvars[idx].vv_tv;
1536 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1537 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1538}
1539
1540/*
1541 * Restore v: variable "idx" to typeval "save_tv".
1542 * When no longer defined, remove the variable from the v: hashtable.
1543 */
1544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001545restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001546{
1547 hashitem_T *hi;
1548
Bram Moolenaara40058a2005-07-11 22:42:07 +00001549 vimvars[idx].vv_tv = *save_tv;
1550 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1551 {
1552 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1553 if (HASHITEM_EMPTY(hi))
1554 EMSG2(_(e_intern2), "restore_vimvar()");
1555 else
1556 hash_remove(&vimvarht, hi);
1557 }
1558}
1559
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001560#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001561/*
1562 * Evaluate an expression to a list with suggestions.
1563 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001564 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001565 */
1566 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001567eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001568{
1569 typval_T save_val;
1570 typval_T rettv;
1571 list_T *list = NULL;
1572 char_u *p = skipwhite(expr);
1573
1574 /* Set "v:val" to the bad word. */
1575 prepare_vimvar(VV_VAL, &save_val);
1576 vimvars[VV_VAL].vv_type = VAR_STRING;
1577 vimvars[VV_VAL].vv_str = badword;
1578 if (p_verbose == 0)
1579 ++emsg_off;
1580
1581 if (eval1(&p, &rettv, TRUE) == OK)
1582 {
1583 if (rettv.v_type != VAR_LIST)
1584 clear_tv(&rettv);
1585 else
1586 list = rettv.vval.v_list;
1587 }
1588
1589 if (p_verbose == 0)
1590 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001591 restore_vimvar(VV_VAL, &save_val);
1592
1593 return list;
1594}
1595
1596/*
1597 * "list" is supposed to contain two items: a word and a number. Return the
1598 * word in "pp" and the number as the return value.
1599 * Return -1 if anything isn't right.
1600 * Used to get the good word and score from the eval_spell_expr() result.
1601 */
1602 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001603get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001604{
1605 listitem_T *li;
1606
1607 li = list->lv_first;
1608 if (li == NULL)
1609 return -1;
1610 *pp = get_tv_string(&li->li_tv);
1611
1612 li = li->li_next;
1613 if (li == NULL)
1614 return -1;
1615 return get_tv_number(&li->li_tv);
1616}
1617#endif
1618
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001619/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001620 * Top level evaluation function.
1621 * Returns an allocated typval_T with the result.
1622 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001623 */
1624 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001625eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001626{
1627 typval_T *tv;
1628
1629 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001630 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001631 {
1632 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001633 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001634 }
1635
1636 return tv;
1637}
1638
1639
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001642 * Uses argv[argc] for the function arguments. Only Number and String
1643 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001646 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001647call_vim_function(
1648 char_u *func,
1649 int argc,
1650 char_u **argv,
1651 int safe, /* use the sandbox */
1652 int str_arg_only, /* all arguments are strings */
1653 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
Bram Moolenaar33570922005-01-25 22:26:29 +00001655 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 long n;
1657 int len;
1658 int i;
1659 int doesrange;
1660 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001661 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001663 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
1667 for (i = 0; i < argc; i++)
1668 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001669 /* Pass a NULL or empty argument as an empty string */
1670 if (argv[i] == NULL || *argv[i] == NUL)
1671 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001672 argvars[i].v_type = VAR_STRING;
1673 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001674 continue;
1675 }
1676
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001677 if (str_arg_only)
1678 len = 0;
1679 else
1680 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001681 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if (len != 0 && len == (int)STRLEN(argv[i]))
1683 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001684 argvars[i].v_type = VAR_NUMBER;
1685 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 }
1687 else
1688 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001689 argvars[i].v_type = VAR_STRING;
1690 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 }
1692 }
1693
1694 if (safe)
1695 {
1696 save_funccalp = save_funccal();
1697 ++sandbox;
1698 }
1699
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001700 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1701 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001703 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 if (safe)
1705 {
1706 --sandbox;
1707 restore_funccal(save_funccalp);
1708 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709 vim_free(argvars);
1710
1711 if (ret == FAIL)
1712 clear_tv(rettv);
1713
1714 return ret;
1715}
1716
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001717/*
1718 * Call vimL function "func" and return the result as a number.
1719 * Returns -1 when calling the function fails.
1720 * Uses argv[argc] for the function arguments.
1721 */
1722 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001723call_func_retnr(
1724 char_u *func,
1725 int argc,
1726 char_u **argv,
1727 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001728{
1729 typval_T rettv;
1730 long retval;
1731
1732 /* All arguments are passed as strings, no conversion to number. */
1733 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1734 return -1;
1735
1736 retval = get_tv_number_chk(&rettv, NULL);
1737 clear_tv(&rettv);
1738 return retval;
1739}
1740
1741#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1742 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1743
Bram Moolenaar4f688582007-07-24 12:34:30 +00001744# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001746 * Call vimL function "func" and return the result as a string.
1747 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748 * Uses argv[argc] for the function arguments.
1749 */
1750 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001751call_func_retstr(
1752 char_u *func,
1753 int argc,
1754 char_u **argv,
1755 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756{
1757 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001758 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001759
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001760 /* All arguments are passed as strings, no conversion to number. */
1761 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001762 return NULL;
1763
1764 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001765 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 return retval;
1767}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001768# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001769
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001770/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001771 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001772 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001773 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001774 */
1775 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776call_func_retlist(
1777 char_u *func,
1778 int argc,
1779 char_u **argv,
1780 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001781{
1782 typval_T rettv;
1783
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001784 /* All arguments are passed as strings, no conversion to number. */
1785 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001786 return NULL;
1787
1788 if (rettv.v_type != VAR_LIST)
1789 {
1790 clear_tv(&rettv);
1791 return NULL;
1792 }
1793
1794 return rettv.vval.v_list;
1795}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#endif
1797
1798/*
1799 * Save the current function call pointer, and set it to NULL.
1800 * Used when executing autocommands and for ":source".
1801 */
1802 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001805 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 current_funccal = NULL;
1808 return (void *)fc;
1809}
1810
1811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001812restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001814 funccall_T *fc = (funccall_T *)vfc;
1815
1816 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817}
1818
Bram Moolenaar05159a02005-02-26 23:04:13 +00001819#if defined(FEAT_PROFILE) || defined(PROTO)
1820/*
1821 * Prepare profiling for entering a child or something else that is not
1822 * counted for the script/function itself.
1823 * Should always be called in pair with prof_child_exit().
1824 */
1825 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001826prof_child_enter(
1827 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001828{
1829 funccall_T *fc = current_funccal;
1830
1831 if (fc != NULL && fc->func->uf_profiling)
1832 profile_start(&fc->prof_child);
1833 script_prof_save(tm);
1834}
1835
1836/*
1837 * Take care of time spent in a child.
1838 * Should always be called after prof_child_enter().
1839 */
1840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001841prof_child_exit(
1842 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001843{
1844 funccall_T *fc = current_funccal;
1845
1846 if (fc != NULL && fc->func->uf_profiling)
1847 {
1848 profile_end(&fc->prof_child);
1849 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1850 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1851 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1852 }
1853 script_prof_restore(tm);
1854}
1855#endif
1856
1857
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858#ifdef FEAT_FOLDING
1859/*
1860 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1861 * it in "*cp". Doesn't give error messages.
1862 */
1863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001864eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865{
Bram Moolenaar33570922005-01-25 22:26:29 +00001866 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 int retval;
1868 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001869 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1870 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
1872 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001873 if (use_sandbox)
1874 ++sandbox;
1875 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 retval = 0;
1879 else
1880 {
1881 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 if (tv.v_type == VAR_NUMBER)
1883 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001884 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 retval = 0;
1886 else
1887 {
1888 /* If the result is a string, check if there is a non-digit before
1889 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (!VIM_ISDIGIT(*s) && *s != '-')
1892 *cp = *s++;
1893 retval = atol((char *)s);
1894 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001898 if (use_sandbox)
1899 --sandbox;
1900 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
1902 return retval;
1903}
1904#endif
1905
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 * ":let" list all variable values
1908 * ":let var1 var2" list variable values
1909 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 * ":let var += expr" assignment command.
1911 * ":let var -= expr" assignment command.
1912 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 */
1915 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001916ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
1918 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 int var_count = 0;
1923 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001925 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001926 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
Bram Moolenaardb552d602006-03-23 22:59:57 +00001928 argend = skip_var_list(arg, &var_count, &semicolon);
1929 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001931 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1932 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001933 expr = skipwhite(argend);
1934 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1935 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001937 /*
1938 * ":let" without "=": list variables
1939 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940 if (*arg == '[')
1941 EMSG(_(e_invarg));
1942 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001944 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001948 list_glob_vars(&first);
1949 list_buf_vars(&first);
1950 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001951#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001952 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001953#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001954 list_script_vars(&first);
1955 list_func_vars(&first);
1956 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 eap->nextcmd = check_nextcmd(arg);
1959 }
1960 else
1961 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 op[0] = '=';
1963 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001964 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001966 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1967 op[0] = *expr; /* +=, -= or .= */
1968 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001970 else
1971 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001972
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 if (eap->skip)
1974 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 if (eap->skip)
1977 {
1978 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001979 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 --emsg_skip;
1981 }
1982 else if (i != FAIL)
1983 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 }
1988 }
1989}
1990
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991/*
1992 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1993 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001994 * When "nextchars" is not NULL it points to a string with characters that
1995 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1996 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 * Returns OK or FAIL;
1998 */
1999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002000ex_let_vars(
2001 char_u *arg_start,
2002 typval_T *tv,
2003 int copy, /* copy values from "tv", don't move */
2004 int semicolon, /* from skip_var_list() */
2005 int var_count, /* from skip_var_list() */
2006 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007{
2008 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002009 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002011 listitem_T *item;
2012 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013
2014 if (*arg != '[')
2015 {
2016 /*
2017 * ":let var = expr" or ":for var in list"
2018 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 return FAIL;
2021 return OK;
2022 }
2023
2024 /*
2025 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2026 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002027 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 {
2029 EMSG(_(e_listreq));
2030 return FAIL;
2031 }
2032
2033 i = list_len(l);
2034 if (semicolon == 0 && var_count < i)
2035 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002036 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037 return FAIL;
2038 }
2039 if (var_count - semicolon > i)
2040 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002041 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002042 return FAIL;
2043 }
2044
2045 item = l->lv_first;
2046 while (*arg != ']')
2047 {
2048 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002049 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050 item = item->li_next;
2051 if (arg == NULL)
2052 return FAIL;
2053
2054 arg = skipwhite(arg);
2055 if (*arg == ';')
2056 {
2057 /* Put the rest of the list (may be empty) in the var after ';'.
2058 * Create a new list for this. */
2059 l = list_alloc();
2060 if (l == NULL)
2061 return FAIL;
2062 while (item != NULL)
2063 {
2064 list_append_tv(l, &item->li_tv);
2065 item = item->li_next;
2066 }
2067
2068 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002069 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 ltv.vval.v_list = l;
2071 l->lv_refcount = 1;
2072
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002073 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2074 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002075 clear_tv(&ltv);
2076 if (arg == NULL)
2077 return FAIL;
2078 break;
2079 }
2080 else if (*arg != ',' && *arg != ']')
2081 {
2082 EMSG2(_(e_intern2), "ex_let_vars()");
2083 return FAIL;
2084 }
2085 }
2086
2087 return OK;
2088}
2089
2090/*
2091 * Skip over assignable variable "var" or list of variables "[var, var]".
2092 * Used for ":let varvar = expr" and ":for varvar in expr".
2093 * For "[var, var]" increment "*var_count" for each variable.
2094 * for "[var, var; var]" set "semicolon".
2095 * Return NULL for an error.
2096 */
2097 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002098skip_var_list(
2099 char_u *arg,
2100 int *var_count,
2101 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102{
2103 char_u *p, *s;
2104
2105 if (*arg == '[')
2106 {
2107 /* "[var, var]": find the matching ']'. */
2108 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002109 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002110 {
2111 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2112 s = skip_var_one(p);
2113 if (s == p)
2114 {
2115 EMSG2(_(e_invarg2), p);
2116 return NULL;
2117 }
2118 ++*var_count;
2119
2120 p = skipwhite(s);
2121 if (*p == ']')
2122 break;
2123 else if (*p == ';')
2124 {
2125 if (*semicolon == 1)
2126 {
2127 EMSG(_("Double ; in list of variables"));
2128 return NULL;
2129 }
2130 *semicolon = 1;
2131 }
2132 else if (*p != ',')
2133 {
2134 EMSG2(_(e_invarg2), p);
2135 return NULL;
2136 }
2137 }
2138 return p + 1;
2139 }
2140 else
2141 return skip_var_one(arg);
2142}
2143
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002144/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002145 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002146 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002147 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002148 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002149skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002150{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002151 if (*arg == '@' && arg[1] != NUL)
2152 return arg + 2;
2153 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2154 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002155}
2156
Bram Moolenaara7043832005-01-21 11:56:39 +00002157/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002158 * List variables for hashtab "ht" with prefix "prefix".
2159 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002160 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002162list_hashtable_vars(
2163 hashtab_T *ht,
2164 char_u *prefix,
2165 int empty,
2166 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002167{
Bram Moolenaar33570922005-01-25 22:26:29 +00002168 hashitem_T *hi;
2169 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002170 int todo;
2171
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002172 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002173 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2174 {
2175 if (!HASHITEM_EMPTY(hi))
2176 {
2177 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002178 di = HI2DI(hi);
2179 if (empty || di->di_tv.v_type != VAR_STRING
2180 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002182 }
2183 }
2184}
2185
2186/*
2187 * List global variables.
2188 */
2189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002191{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002193}
2194
2195/*
2196 * List buffer variables.
2197 */
2198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002200{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002201 char_u numbuf[NUMBUFLEN];
2202
Bram Moolenaar429fa852013-04-15 12:27:36 +02002203 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002205
2206 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2208 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002209}
2210
2211/*
2212 * List window variables.
2213 */
2214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002215list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002216{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002217 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002219}
2220
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002221#ifdef FEAT_WINDOWS
2222/*
2223 * List tab page variables.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002227{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002228 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002229 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002230}
2231#endif
2232
Bram Moolenaara7043832005-01-21 11:56:39 +00002233/*
2234 * List Vim variables.
2235 */
2236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002239 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240}
2241
2242/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002243 * List script-local variables, if there is a script.
2244 */
2245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002246list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002247{
2248 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002249 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2250 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002251}
2252
2253/*
2254 * List function variables, if there is a function.
2255 */
2256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002257list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002258{
2259 if (current_funccal != NULL)
2260 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002261 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002262}
2263
2264/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 * List variables in "arg".
2266 */
2267 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002268list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269{
2270 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 char_u *name_start;
2274 char_u *arg_subsc;
2275 char_u *tofree;
2276 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277
2278 while (!ends_excmd(*arg) && !got_int)
2279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002282 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2284 {
2285 emsg_severe = TRUE;
2286 EMSG(_(e_trailing));
2287 break;
2288 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 /* get_name_len() takes care of expanding curly braces */
2293 name_start = name = arg;
2294 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2295 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 /* This is mainly to keep test 49 working: when expanding
2298 * curly braces fails overrule the exception error message. */
2299 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 emsg_severe = TRUE;
2302 EMSG2(_(e_invarg2), arg);
2303 break;
2304 }
2305 error = TRUE;
2306 }
2307 else
2308 {
2309 if (tofree != NULL)
2310 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002311 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 else
2314 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 /* handle d.key, l[idx], f(expr) */
2316 arg_subsc = arg;
2317 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002318 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002322 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002324 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002325 case 'g': list_glob_vars(first); break;
2326 case 'b': list_buf_vars(first); break;
2327 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002328#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002329 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002330#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002331 case 'v': list_vim_vars(first); break;
2332 case 's': list_script_vars(first); break;
2333 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334 default:
2335 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002336 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002337 }
2338 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339 {
2340 char_u numbuf[NUMBUFLEN];
2341 char_u *tf;
2342 int c;
2343 char_u *s;
2344
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002345 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002346 c = *arg;
2347 *arg = NUL;
2348 list_one_var_a((char_u *)"",
2349 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002350 tv.v_type,
2351 s == NULL ? (char_u *)"" : s,
2352 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002353 *arg = c;
2354 vim_free(tf);
2355 }
2356 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 }
2359 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002360
2361 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002363
2364 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 }
2366
2367 return arg;
2368}
2369
2370/*
2371 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2372 * Returns a pointer to the char just after the var name.
2373 * Returns NULL if there is an error.
2374 */
2375 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002376ex_let_one(
2377 char_u *arg, /* points to variable name */
2378 typval_T *tv, /* value to assign to variable */
2379 int copy, /* copy value from "tv" */
2380 char_u *endchars, /* valid chars after variable name or NULL */
2381 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382{
2383 int c1;
2384 char_u *name;
2385 char_u *p;
2386 char_u *arg_end = NULL;
2387 int len;
2388 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390
2391 /*
2392 * ":let $VAR = expr": Set environment variable.
2393 */
2394 if (*arg == '$')
2395 {
2396 /* Find the end of the name. */
2397 ++arg;
2398 name = arg;
2399 len = get_env_len(&arg);
2400 if (len == 0)
2401 EMSG2(_(e_invarg2), name - 1);
2402 else
2403 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 if (op != NULL && (*op == '+' || *op == '-'))
2405 EMSG2(_(e_letwrong), op);
2406 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002407 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002408 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002409 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 {
2411 c1 = name[len];
2412 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413 p = get_tv_string_chk(tv);
2414 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 {
2416 int mustfree = FALSE;
2417 char_u *s = vim_getenv(name, &mustfree);
2418
2419 if (s != NULL)
2420 {
2421 p = tofree = concat_str(s, p);
2422 if (mustfree)
2423 vim_free(s);
2424 }
2425 }
2426 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002427 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 if (STRICMP(name, "HOME") == 0)
2430 init_homedir();
2431 else if (didset_vim && STRICMP(name, "VIM") == 0)
2432 didset_vim = FALSE;
2433 else if (didset_vimruntime
2434 && STRICMP(name, "VIMRUNTIME") == 0)
2435 didset_vimruntime = FALSE;
2436 arg_end = arg;
2437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 }
2441 }
2442 }
2443
2444 /*
2445 * ":let &option = expr": Set option value.
2446 * ":let &l:option = expr": Set local option value.
2447 * ":let &g:option = expr": Set global option value.
2448 */
2449 else if (*arg == '&')
2450 {
2451 /* Find the end of the name. */
2452 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002453 if (p == NULL || (endchars != NULL
2454 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 EMSG(_(e_letunexp));
2456 else
2457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 long n;
2459 int opt_type;
2460 long numval;
2461 char_u *stringval = NULL;
2462 char_u *s;
2463
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 c1 = *p;
2465 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466
2467 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 s = get_tv_string_chk(tv); /* != NULL if number or string */
2469 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
2471 opt_type = get_option_value(arg, &numval,
2472 &stringval, opt_flags);
2473 if ((opt_type == 1 && *op == '.')
2474 || (opt_type == 0 && *op != '.'))
2475 EMSG2(_(e_letwrong), op);
2476 else
2477 {
2478 if (opt_type == 1) /* number */
2479 {
2480 if (*op == '+')
2481 n = numval + n;
2482 else
2483 n = numval - n;
2484 }
2485 else if (opt_type == 0 && stringval != NULL) /* string */
2486 {
2487 s = concat_str(stringval, s);
2488 vim_free(stringval);
2489 stringval = s;
2490 }
2491 }
2492 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002493 if (s != NULL)
2494 {
2495 set_option_value(arg, n, s, opt_flags);
2496 arg_end = p;
2497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 }
2501 }
2502
2503 /*
2504 * ":let @r = expr": Set register contents.
2505 */
2506 else if (*arg == '@')
2507 {
2508 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509 if (op != NULL && (*op == '+' || *op == '-'))
2510 EMSG2(_(e_letwrong), op);
2511 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002512 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 EMSG(_(e_letunexp));
2514 else
2515 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002516 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002517 char_u *s;
2518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002519 p = get_tv_string_chk(tv);
2520 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002521 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002522 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 if (s != NULL)
2524 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002525 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002526 vim_free(s);
2527 }
2528 }
2529 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002530 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002531 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002532 arg_end = arg + 1;
2533 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002534 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 }
2536 }
2537
2538 /*
2539 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002542 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002545
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002546 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2550 EMSG(_(e_letunexp));
2551 else
2552 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002553 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 arg_end = p;
2555 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 }
2559
2560 else
2561 EMSG2(_(e_invarg2), arg);
2562
2563 return arg_end;
2564}
2565
2566/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2568 */
2569 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002570check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002571{
2572 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2573 {
2574 EMSG2(_(e_readonlyvar), arg);
2575 return TRUE;
2576 }
2577 return FALSE;
2578}
2579
2580/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 * Get an lval: variable, Dict item or List item that can be assigned a value
2582 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2583 * "name.key", "name.key[expr]" etc.
2584 * Indexing only works if "name" is an existing List or Dictionary.
2585 * "name" points to the start of the name.
2586 * If "rettv" is not NULL it points to the value to be assigned.
2587 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2588 * wrong; must end in space or cmd separator.
2589 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002590 * flags:
2591 * GLV_QUIET: do not give error messages
2592 * GLV_NO_AUTOLOAD: do not use script autoloading
2593 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002595 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002597 */
2598 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002599get_lval(
2600 char_u *name,
2601 typval_T *rettv,
2602 lval_T *lp,
2603 int unlet,
2604 int skip,
2605 int flags, /* GLV_ values */
2606 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002607{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 char_u *expr_start, *expr_end;
2610 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002611 dictitem_T *v;
2612 typval_T var1;
2613 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002615 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002618 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002619 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002622 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623
2624 if (skip)
2625 {
2626 /* When skipping just find the end of the name. */
2627 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002628 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 }
2630
2631 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002632 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (expr_start != NULL)
2634 {
2635 /* Don't expand the name when we already know there is an error. */
2636 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2637 && *p != '[' && *p != '.')
2638 {
2639 EMSG(_(e_trailing));
2640 return NULL;
2641 }
2642
2643 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2644 if (lp->ll_exp_name == NULL)
2645 {
2646 /* Report an invalid expression in braces, unless the
2647 * expression evaluation has been cancelled due to an
2648 * aborting error, an interrupt, or an exception. */
2649 if (!aborting() && !quiet)
2650 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002651 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 EMSG2(_(e_invarg2), name);
2653 return NULL;
2654 }
2655 }
2656 lp->ll_name = lp->ll_exp_name;
2657 }
2658 else
2659 lp->ll_name = name;
2660
2661 /* Without [idx] or .key we are done. */
2662 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2663 return p;
2664
2665 cc = *p;
2666 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002667 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (v == NULL && !quiet)
2669 EMSG2(_(e_undefvar), lp->ll_name);
2670 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002671 if (v == NULL)
2672 return NULL;
2673
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 /*
2675 * Loop until no more [idx] or .key is following.
2676 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002677 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2681 && !(lp->ll_tv->v_type == VAR_DICT
2682 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002683 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (!quiet)
2685 EMSG(_("E689: Can only index a List or Dictionary"));
2686 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002689 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (!quiet)
2691 EMSG(_("E708: [:] must come last"));
2692 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002693 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 len = -1;
2696 if (*p == '.')
2697 {
2698 key = p + 1;
2699 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2700 ;
2701 if (len == 0)
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (!quiet)
2704 EMSG(_(e_emptykey));
2705 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 }
2707 p = key + len;
2708 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 else
2710 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (*p == ':')
2714 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 else
2716 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 empty1 = FALSE;
2718 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002720 if (get_tv_string_chk(&var1) == NULL)
2721 {
2722 /* not a number or string */
2723 clear_tv(&var1);
2724 return NULL;
2725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727
2728 /* Optionally get the second index [ :expr]. */
2729 if (*p == ':')
2730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002734 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002735 if (!empty1)
2736 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002738 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (rettv != NULL && (rettv->v_type != VAR_LIST
2740 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (!quiet)
2743 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (!empty1)
2745 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 }
2748 p = skipwhite(p + 1);
2749 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 else
2752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (!empty1)
2757 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 if (get_tv_string_chk(&var2) == NULL)
2761 {
2762 /* not a number or string */
2763 if (!empty1)
2764 clear_tv(&var1);
2765 clear_tv(&var2);
2766 return NULL;
2767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002770 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002773
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (!quiet)
2777 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 if (!empty1)
2779 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
2784
2785 /* Skip to past ']'. */
2786 ++p;
2787 }
2788
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 {
2791 if (len == -1)
2792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002794 key = get_tv_string_chk(&var1); /* is number or string */
2795 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002801 lp->ll_list = NULL;
2802 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002803 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002804
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002805 /* When assigning to a scope dictionary check that a function and
2806 * variable name is valid (only variable name unless it is l: or
2807 * g: dictionary). Disallow overwriting a builtin function. */
2808 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002809 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002810 int prevval;
2811 int wrong;
2812
2813 if (len != -1)
2814 {
2815 prevval = key[len];
2816 key[len] = NUL;
2817 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002818 else
2819 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002820 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2821 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002822 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002823 || !valid_varname(key);
2824 if (len != -1)
2825 key[len] = prevval;
2826 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002827 return NULL;
2828 }
2829
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002832 /* Can't add "v:" variable. */
2833 if (lp->ll_dict == &vimvardict)
2834 {
2835 EMSG2(_(e_illvar), name);
2836 return NULL;
2837 }
2838
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002839 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 if (len == -1)
2845 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 }
2848 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 if (len == -1)
2853 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 p = NULL;
2856 break;
2857 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002858 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002859 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002860 return NULL;
2861
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 if (len == -1)
2863 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 }
2866 else
2867 {
2868 /*
2869 * Get the number and item for the only or first index of the List.
2870 */
2871 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 else
2874 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002875 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 clear_tv(&var1);
2877 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002878 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 lp->ll_list = lp->ll_tv->vval.v_list;
2880 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2881 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002883 if (lp->ll_n1 < 0)
2884 {
2885 lp->ll_n1 = 0;
2886 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2887 }
2888 }
2889 if (lp->ll_li == NULL)
2890 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002892 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002893 if (!quiet)
2894 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 }
2897
2898 /*
2899 * May need to find the item or absolute index for the second
2900 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 * When no index given: "lp->ll_empty2" is TRUE.
2902 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002906 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002911 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002912 {
2913 if (!quiet)
2914 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002916 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918 }
2919
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2921 if (lp->ll_n1 < 0)
2922 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2923 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002924 {
2925 if (!quiet)
2926 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002928 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002929 }
2930
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002932 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 }
2934
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 return p;
2936}
2937
2938/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 */
2941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002942clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943{
2944 vim_free(lp->ll_exp_name);
2945 vim_free(lp->ll_newkey);
2946}
2947
2948/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002949 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 */
2953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002954set_var_lval(
2955 lval_T *lp,
2956 char_u *endp,
2957 typval_T *rettv,
2958 int copy,
2959 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960{
2961 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002962 listitem_T *ri;
2963 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964
2965 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002966 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 cc = *endp;
2970 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002971 if (op != NULL && *op != '=')
2972 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002973 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002974
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002976 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002977 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002978 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002979 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002980 if ((di == NULL
2981 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2982 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2983 FALSE)))
2984 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985 set_var(lp->ll_name, &tv, FALSE);
2986 clear_tv(&tv);
2987 }
2988 }
2989 else
2990 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002992 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002994 else if (tv_check_lock(lp->ll_newkey == NULL
2995 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002996 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002997 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 else if (lp->ll_range)
2999 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003000 listitem_T *ll_li = lp->ll_li;
3001 int ll_n1 = lp->ll_n1;
3002
3003 /*
3004 * Check whether any of the list items is locked
3005 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003006 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003007 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003008 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003009 return;
3010 ri = ri->li_next;
3011 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3012 break;
3013 ll_li = ll_li->li_next;
3014 ++ll_n1;
3015 }
3016
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 /*
3018 * Assign the List values to the list items.
3019 */
3020 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003021 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 if (op != NULL && *op != '=')
3023 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3024 else
3025 {
3026 clear_tv(&lp->ll_li->li_tv);
3027 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3028 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 ri = ri->li_next;
3030 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3031 break;
3032 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003033 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003035 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003036 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 ri = NULL;
3038 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003039 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003040 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 lp->ll_li = lp->ll_li->li_next;
3042 ++lp->ll_n1;
3043 }
3044 if (ri != NULL)
3045 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 else if (lp->ll_empty2
3047 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 : lp->ll_n1 != lp->ll_n2)
3049 EMSG(_("E711: List value has not enough items"));
3050 }
3051 else
3052 {
3053 /*
3054 * Assign to a List or Dictionary item.
3055 */
3056 if (lp->ll_newkey != NULL)
3057 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058 if (op != NULL && *op != '=')
3059 {
3060 EMSG2(_(e_letwrong), op);
3061 return;
3062 }
3063
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003065 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003066 if (di == NULL)
3067 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003068 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3069 {
3070 vim_free(di);
3071 return;
3072 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003073 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 else if (op != NULL && *op != '=')
3076 {
3077 tv_op(lp->ll_tv, rettv, op);
3078 return;
3079 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003080 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003081 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003082
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003083 /*
3084 * Assign the value to the variable or list item.
3085 */
3086 if (copy)
3087 copy_tv(rettv, lp->ll_tv);
3088 else
3089 {
3090 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003091 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003092 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003093 }
3094 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003095}
3096
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003097/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3099 * Returns OK or FAIL.
3100 */
3101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003102tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003103{
3104 long n;
3105 char_u numbuf[NUMBUFLEN];
3106 char_u *s;
3107
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003108 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3109 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3110 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003111 {
3112 switch (tv1->v_type)
3113 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003114 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003115 case VAR_DICT:
3116 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003117 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003118 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003119 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003120 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003121 break;
3122
3123 case VAR_LIST:
3124 if (*op != '+' || tv2->v_type != VAR_LIST)
3125 break;
3126 /* List += List */
3127 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3128 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3129 return OK;
3130
3131 case VAR_NUMBER:
3132 case VAR_STRING:
3133 if (tv2->v_type == VAR_LIST)
3134 break;
3135 if (*op == '+' || *op == '-')
3136 {
3137 /* nr += nr or nr -= nr*/
3138 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003139#ifdef FEAT_FLOAT
3140 if (tv2->v_type == VAR_FLOAT)
3141 {
3142 float_T f = n;
3143
3144 if (*op == '+')
3145 f += tv2->vval.v_float;
3146 else
3147 f -= tv2->vval.v_float;
3148 clear_tv(tv1);
3149 tv1->v_type = VAR_FLOAT;
3150 tv1->vval.v_float = f;
3151 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003152 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003153#endif
3154 {
3155 if (*op == '+')
3156 n += get_tv_number(tv2);
3157 else
3158 n -= get_tv_number(tv2);
3159 clear_tv(tv1);
3160 tv1->v_type = VAR_NUMBER;
3161 tv1->vval.v_number = n;
3162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003163 }
3164 else
3165 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003166 if (tv2->v_type == VAR_FLOAT)
3167 break;
3168
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003169 /* str .= str */
3170 s = get_tv_string(tv1);
3171 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3172 clear_tv(tv1);
3173 tv1->v_type = VAR_STRING;
3174 tv1->vval.v_string = s;
3175 }
3176 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003179#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 {
3181 float_T f;
3182
3183 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3184 && tv2->v_type != VAR_NUMBER
3185 && tv2->v_type != VAR_STRING))
3186 break;
3187 if (tv2->v_type == VAR_FLOAT)
3188 f = tv2->vval.v_float;
3189 else
3190 f = get_tv_number(tv2);
3191 if (*op == '+')
3192 tv1->vval.v_float += f;
3193 else
3194 tv1->vval.v_float -= f;
3195 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003196#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003197 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003198 }
3199 }
3200
3201 EMSG2(_(e_letwrong), op);
3202 return FAIL;
3203}
3204
3205/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206 * Add a watcher to a list.
3207 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003209list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003210{
3211 lw->lw_next = l->lv_watch;
3212 l->lv_watch = lw;
3213}
3214
3215/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003216 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 * No warning when it isn't found...
3218 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221{
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223
3224 lwp = &l->lv_watch;
3225 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3226 {
3227 if (lw == lwrem)
3228 {
3229 *lwp = lw->lw_next;
3230 break;
3231 }
3232 lwp = &lw->lw_next;
3233 }
3234}
3235
3236/*
3237 * Just before removing an item from a list: advance watchers to the next
3238 * item.
3239 */
3240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003241list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242{
Bram Moolenaar33570922005-01-25 22:26:29 +00003243 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244
3245 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3246 if (lw->lw_item == item)
3247 lw->lw_item = item->li_next;
3248}
3249
3250/*
3251 * Evaluate the expression used in a ":for var in expr" command.
3252 * "arg" points to "var".
3253 * Set "*errp" to TRUE for an error, FALSE otherwise;
3254 * Return a pointer that holds the info. Null when there is an error.
3255 */
3256 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003257eval_for_line(
3258 char_u *arg,
3259 int *errp,
3260 char_u **nextcmdp,
3261 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262{
Bram Moolenaar33570922005-01-25 22:26:29 +00003263 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 typval_T tv;
3266 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267
3268 *errp = TRUE; /* default: there is an error */
3269
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 if (fi == NULL)
3272 return NULL;
3273
3274 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3275 if (expr == NULL)
3276 return fi;
3277
3278 expr = skipwhite(expr);
3279 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3280 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003281 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 return fi;
3283 }
3284
3285 if (skip)
3286 ++emsg_skip;
3287 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3288 {
3289 *errp = FALSE;
3290 if (!skip)
3291 {
3292 l = tv.vval.v_list;
3293 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003294 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003296 clear_tv(&tv);
3297 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 else
3299 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003300 /* No need to increment the refcount, it's already set for the
3301 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 fi->fi_list = l;
3303 list_add_watch(l, &fi->fi_lw);
3304 fi->fi_lw.lw_item = l->lv_first;
3305 }
3306 }
3307 }
3308 if (skip)
3309 --emsg_skip;
3310
3311 return fi;
3312}
3313
3314/*
3315 * Use the first item in a ":for" list. Advance to the next.
3316 * Assign the values to the variable (list). "arg" points to the first one.
3317 * Return TRUE when a valid item was found, FALSE when at end of list or
3318 * something wrong.
3319 */
3320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003321next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003323 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326
3327 item = fi->fi_lw.lw_item;
3328 if (item == NULL)
3329 result = FALSE;
3330 else
3331 {
3332 fi->fi_lw.lw_item = item->li_next;
3333 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3334 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3335 }
3336 return result;
3337}
3338
3339/*
3340 * Free the structure used to store info used by ":for".
3341 */
3342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003343free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344{
Bram Moolenaar33570922005-01-25 22:26:29 +00003345 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003347 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003348 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003350 list_unref(fi->fi_list);
3351 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 vim_free(fi);
3353}
3354
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3356
3357 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003358set_context_for_expression(
3359 expand_T *xp,
3360 char_u *arg,
3361 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 int got_eq = FALSE;
3364 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003367 if (cmdidx == CMD_let)
3368 {
3369 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003370 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003371 {
3372 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003373 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003374 {
3375 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003376 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003377 if (vim_iswhite(*p))
3378 break;
3379 }
3380 return;
3381 }
3382 }
3383 else
3384 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3385 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 while ((xp->xp_pattern = vim_strpbrk(arg,
3387 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3388 {
3389 c = *xp->xp_pattern;
3390 if (c == '&')
3391 {
3392 c = xp->xp_pattern[1];
3393 if (c == '&')
3394 {
3395 ++xp->xp_pattern;
3396 xp->xp_context = cmdidx != CMD_let || got_eq
3397 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3398 }
3399 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003402 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3403 xp->xp_pattern += 2;
3404
3405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
3407 else if (c == '$')
3408 {
3409 /* environment variable */
3410 xp->xp_context = EXPAND_ENV_VARS;
3411 }
3412 else if (c == '=')
3413 {
3414 got_eq = TRUE;
3415 xp->xp_context = EXPAND_EXPRESSION;
3416 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003417 else if (c == '#'
3418 && xp->xp_context == EXPAND_EXPRESSION)
3419 {
3420 /* Autoload function/variable contains '#'. */
3421 break;
3422 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003423 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 && xp->xp_context == EXPAND_FUNCTIONS
3425 && vim_strchr(xp->xp_pattern, '(') == NULL)
3426 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003427 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 break;
3429 }
3430 else if (cmdidx != CMD_let || got_eq)
3431 {
3432 if (c == '"') /* string */
3433 {
3434 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3435 if (c == '\\' && xp->xp_pattern[1] != NUL)
3436 ++xp->xp_pattern;
3437 xp->xp_context = EXPAND_NOTHING;
3438 }
3439 else if (c == '\'') /* literal string */
3440 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003441 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3443 /* skip */ ;
3444 xp->xp_context = EXPAND_NOTHING;
3445 }
3446 else if (c == '|')
3447 {
3448 if (xp->xp_pattern[1] == '|')
3449 {
3450 ++xp->xp_pattern;
3451 xp->xp_context = EXPAND_EXPRESSION;
3452 }
3453 else
3454 xp->xp_context = EXPAND_COMMANDS;
3455 }
3456 else
3457 xp->xp_context = EXPAND_EXPRESSION;
3458 }
3459 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003460 /* Doesn't look like something valid, expand as an expression
3461 * anyway. */
3462 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 arg = xp->xp_pattern;
3464 if (*arg != NUL)
3465 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3466 /* skip */ ;
3467 }
3468 xp->xp_pattern = arg;
3469}
3470
3471#endif /* FEAT_CMDL_COMPL */
3472
3473/*
3474 * ":1,25call func(arg1, arg2)" function call.
3475 */
3476 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003477ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
3479 char_u *arg = eap->arg;
3480 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003482 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003484 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 linenr_T lnum;
3486 int doesrange;
3487 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003488 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003489 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003491 if (eap->skip)
3492 {
3493 /* trans_function_name() doesn't work well when skipping, use eval0()
3494 * instead to skip to any following command, e.g. for:
3495 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003496 ++emsg_skip;
3497 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3498 clear_tv(&rettv);
3499 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003500 return;
3501 }
3502
Bram Moolenaar65639032016-03-16 21:40:30 +01003503 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003504 if (fudi.fd_newkey != NULL)
3505 {
3506 /* Still need to give an error message for missing key. */
3507 EMSG2(_(e_dictkey), fudi.fd_newkey);
3508 vim_free(fudi.fd_newkey);
3509 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003510 if (tofree == NULL)
3511 return;
3512
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003513 /* Increase refcount on dictionary, it could get deleted when evaluating
3514 * the arguments. */
3515 if (fudi.fd_dict != NULL)
3516 ++fudi.fd_dict->dv_refcount;
3517
Bram Moolenaar65639032016-03-16 21:40:30 +01003518 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3519 * contents. For VAR_PARTIAL get its partial, unless we already have one
3520 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003521 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003522 name = deref_func_name(tofree, &len,
3523 partial != NULL ? NULL : &partial, FALSE);
3524
Bram Moolenaar532c7802005-01-27 14:44:31 +00003525 /* Skip white space to allow ":call func ()". Not good, but required for
3526 * backward compatibility. */
3527 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003528 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 if (*startarg != '(')
3531 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003532 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 goto end;
3534 }
3535
3536 /*
3537 * When skipping, evaluate the function once, to find the end of the
3538 * arguments.
3539 * When the function takes a range, this is discovered after the first
3540 * call, and the loop is broken.
3541 */
3542 if (eap->skip)
3543 {
3544 ++emsg_skip;
3545 lnum = eap->line2; /* do it once, also with an invalid range */
3546 }
3547 else
3548 lnum = eap->line1;
3549 for ( ; lnum <= eap->line2; ++lnum)
3550 {
3551 if (!eap->skip && eap->addr_count > 0)
3552 {
3553 curwin->w_cursor.lnum = lnum;
3554 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003555#ifdef FEAT_VIRTUALEDIT
3556 curwin->w_cursor.coladd = 0;
3557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003560 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003561 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003562 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
3564 failed = TRUE;
3565 break;
3566 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003567
3568 /* Handle a function returning a Funcref, Dictionary or List. */
3569 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3570 {
3571 failed = TRUE;
3572 break;
3573 }
3574
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003575 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 if (doesrange || eap->skip)
3577 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003578
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003580 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003581 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003582 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (aborting())
3584 break;
3585 }
3586 if (eap->skip)
3587 --emsg_skip;
3588
3589 if (!failed)
3590 {
3591 /* Check for trailing illegal characters and a following command. */
3592 if (!ends_excmd(*arg))
3593 {
3594 emsg_severe = TRUE;
3595 EMSG(_(e_trailing));
3596 }
3597 else
3598 eap->nextcmd = check_nextcmd(arg);
3599 }
3600
3601end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003602 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003603 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604}
3605
3606/*
3607 * ":unlet[!] var1 ... " command.
3608 */
3609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003610ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 ex_unletlock(eap, eap->arg, 0);
3613}
3614
3615/*
3616 * ":lockvar" and ":unlockvar" commands
3617 */
3618 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003619ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003620{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003622 int deep = 2;
3623
3624 if (eap->forceit)
3625 deep = -1;
3626 else if (vim_isdigit(*arg))
3627 {
3628 deep = getdigits(&arg);
3629 arg = skipwhite(arg);
3630 }
3631
3632 ex_unletlock(eap, arg, deep);
3633}
3634
3635/*
3636 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3637 */
3638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003639ex_unletlock(
3640 exarg_T *eap,
3641 char_u *argstart,
3642 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643{
3644 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
3649 do
3650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003652 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003653 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 if (lv.ll_name == NULL)
3655 error = TRUE; /* error but continue parsing */
3656 if (name_end == NULL || (!vim_iswhite(*name_end)
3657 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 if (name_end != NULL)
3660 {
3661 emsg_severe = TRUE;
3662 EMSG(_(e_trailing));
3663 }
3664 if (!(eap->skip || error))
3665 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 break;
3667 }
3668
3669 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 {
3671 if (eap->cmdidx == CMD_unlet)
3672 {
3673 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3674 error = TRUE;
3675 }
3676 else
3677 {
3678 if (do_lock_var(&lv, name_end, deep,
3679 eap->cmdidx == CMD_lockvar) == FAIL)
3680 error = TRUE;
3681 }
3682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003684 if (!eap->skip)
3685 clear_lval(&lv);
3686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 arg = skipwhite(name_end);
3688 } while (!ends_excmd(*arg));
3689
3690 eap->nextcmd = check_nextcmd(arg);
3691}
3692
Bram Moolenaar8c711452005-01-14 21:53:12 +00003693 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003694do_unlet_var(
3695 lval_T *lp,
3696 char_u *name_end,
3697 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003698{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003699 int ret = OK;
3700 int cc;
3701
3702 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003704 cc = *name_end;
3705 *name_end = NUL;
3706
3707 /* Normal name or expanded name. */
3708 if (check_changedtick(lp->ll_name))
3709 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003710 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003713 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003714 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003715 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003716 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003717 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003718 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 else if (lp->ll_range)
3720 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003722 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003723 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003724
3725 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3726 {
3727 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003728 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003729 return FAIL;
3730 ll_li = li;
3731 ++ll_n1;
3732 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003733
3734 /* Delete a range of List items. */
3735 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3736 {
3737 li = lp->ll_li->li_next;
3738 listitem_remove(lp->ll_list, lp->ll_li);
3739 lp->ll_li = li;
3740 ++lp->ll_n1;
3741 }
3742 }
3743 else
3744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003745 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003746 /* unlet a List item. */
3747 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003748 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003749 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003750 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003751 }
3752
3753 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003754}
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756/*
3757 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003758 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 */
3760 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003761do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762{
Bram Moolenaar33570922005-01-25 22:26:29 +00003763 hashtab_T *ht;
3764 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003765 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003766 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003767 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
Bram Moolenaar33570922005-01-25 22:26:29 +00003769 ht = find_var_ht(name, &varname);
3770 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003772 if (ht == &globvarht)
3773 d = &globvardict;
3774 else if (current_funccal != NULL
3775 && ht == &current_funccal->l_vars.dv_hashtab)
3776 d = &current_funccal->l_vars;
3777 else if (ht == &compat_hashtab)
3778 d = &vimvardict;
3779 else
3780 {
3781 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3782 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3783 }
3784 if (d == NULL)
3785 {
3786 EMSG2(_(e_intern2), "do_unlet()");
3787 return FAIL;
3788 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003789 hi = hash_find(ht, varname);
3790 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003791 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003792 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003793 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003794 || var_check_ro(di->di_flags, name, FALSE)
3795 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003796 return FAIL;
3797
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003798 delete_var(ht, hi);
3799 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003802 if (forceit)
3803 return OK;
3804 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 return FAIL;
3806}
3807
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003808/*
3809 * Lock or unlock variable indicated by "lp".
3810 * "deep" is the levels to go (-1 for unlimited);
3811 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3812 */
3813 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003814do_lock_var(
3815 lval_T *lp,
3816 char_u *name_end,
3817 int deep,
3818 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003819{
3820 int ret = OK;
3821 int cc;
3822 dictitem_T *di;
3823
3824 if (deep == 0) /* nothing to do */
3825 return OK;
3826
3827 if (lp->ll_tv == NULL)
3828 {
3829 cc = *name_end;
3830 *name_end = NUL;
3831
3832 /* Normal name or expanded name. */
3833 if (check_changedtick(lp->ll_name))
3834 ret = FAIL;
3835 else
3836 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003837 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003838 if (di == NULL)
3839 ret = FAIL;
3840 else
3841 {
3842 if (lock)
3843 di->di_flags |= DI_FLAGS_LOCK;
3844 else
3845 di->di_flags &= ~DI_FLAGS_LOCK;
3846 item_lock(&di->di_tv, deep, lock);
3847 }
3848 }
3849 *name_end = cc;
3850 }
3851 else if (lp->ll_range)
3852 {
3853 listitem_T *li = lp->ll_li;
3854
3855 /* (un)lock a range of List items. */
3856 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3857 {
3858 item_lock(&li->li_tv, deep, lock);
3859 li = li->li_next;
3860 ++lp->ll_n1;
3861 }
3862 }
3863 else if (lp->ll_list != NULL)
3864 /* (un)lock a List item. */
3865 item_lock(&lp->ll_li->li_tv, deep, lock);
3866 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003867 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003868 item_lock(&lp->ll_di->di_tv, deep, lock);
3869
3870 return ret;
3871}
3872
3873/*
3874 * Lock or unlock an item. "deep" is nr of levels to go.
3875 */
3876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003877item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003878{
3879 static int recurse = 0;
3880 list_T *l;
3881 listitem_T *li;
3882 dict_T *d;
3883 hashitem_T *hi;
3884 int todo;
3885
3886 if (recurse >= DICT_MAXNEST)
3887 {
3888 EMSG(_("E743: variable nested too deep for (un)lock"));
3889 return;
3890 }
3891 if (deep == 0)
3892 return;
3893 ++recurse;
3894
3895 /* lock/unlock the item itself */
3896 if (lock)
3897 tv->v_lock |= VAR_LOCKED;
3898 else
3899 tv->v_lock &= ~VAR_LOCKED;
3900
3901 switch (tv->v_type)
3902 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003903 case VAR_UNKNOWN:
3904 case VAR_NUMBER:
3905 case VAR_STRING:
3906 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003907 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003908 case VAR_FLOAT:
3909 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003910 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003911 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003912 break;
3913
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003914 case VAR_LIST:
3915 if ((l = tv->vval.v_list) != NULL)
3916 {
3917 if (lock)
3918 l->lv_lock |= VAR_LOCKED;
3919 else
3920 l->lv_lock &= ~VAR_LOCKED;
3921 if (deep < 0 || deep > 1)
3922 /* recursive: lock/unlock the items the List contains */
3923 for (li = l->lv_first; li != NULL; li = li->li_next)
3924 item_lock(&li->li_tv, deep - 1, lock);
3925 }
3926 break;
3927 case VAR_DICT:
3928 if ((d = tv->vval.v_dict) != NULL)
3929 {
3930 if (lock)
3931 d->dv_lock |= VAR_LOCKED;
3932 else
3933 d->dv_lock &= ~VAR_LOCKED;
3934 if (deep < 0 || deep > 1)
3935 {
3936 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003937 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003938 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3939 {
3940 if (!HASHITEM_EMPTY(hi))
3941 {
3942 --todo;
3943 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3944 }
3945 }
3946 }
3947 }
3948 }
3949 --recurse;
3950}
3951
Bram Moolenaara40058a2005-07-11 22:42:07 +00003952/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003953 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3954 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003955 */
3956 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003957tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003958{
3959 return (tv->v_lock & VAR_LOCKED)
3960 || (tv->v_type == VAR_LIST
3961 && tv->vval.v_list != NULL
3962 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3963 || (tv->v_type == VAR_DICT
3964 && tv->vval.v_dict != NULL
3965 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3966}
3967
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3969/*
3970 * Delete all "menutrans_" variables.
3971 */
3972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003973del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
Bram Moolenaar33570922005-01-25 22:26:29 +00003975 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003976 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
Bram Moolenaar33570922005-01-25 22:26:29 +00003978 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003979 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003981 {
3982 if (!HASHITEM_EMPTY(hi))
3983 {
3984 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003985 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3986 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003987 }
3988 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003989 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990}
3991#endif
3992
3993#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3994
3995/*
3996 * Local string buffer for the next two functions to store a variable name
3997 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3998 * get_user_var_name().
3999 */
4000
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004001static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
4003static char_u *varnamebuf = NULL;
4004static int varnamebuflen = 0;
4005
4006/*
4007 * Function to concatenate a prefix and a variable name.
4008 */
4009 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004010cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011{
4012 int len;
4013
4014 len = (int)STRLEN(name) + 3;
4015 if (len > varnamebuflen)
4016 {
4017 vim_free(varnamebuf);
4018 len += 10; /* some additional space */
4019 varnamebuf = alloc(len);
4020 if (varnamebuf == NULL)
4021 {
4022 varnamebuflen = 0;
4023 return NULL;
4024 }
4025 varnamebuflen = len;
4026 }
4027 *varnamebuf = prefix;
4028 varnamebuf[1] = ':';
4029 STRCPY(varnamebuf + 2, name);
4030 return varnamebuf;
4031}
4032
4033/*
4034 * Function given to ExpandGeneric() to obtain the list of user defined
4035 * (global/buffer/window/built-in) variable names.
4036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004038get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004040 static long_u gdone;
4041 static long_u bdone;
4042 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004043#ifdef FEAT_WINDOWS
4044 static long_u tdone;
4045#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004046 static int vidx;
4047 static hashitem_T *hi;
4048 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
4050 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004051 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004053#ifdef FEAT_WINDOWS
4054 tdone = 0;
4055#endif
4056 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004057
4058 /* Global variables */
4059 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004061 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 else
4064 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004065 while (HASHITEM_EMPTY(hi))
4066 ++hi;
4067 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4068 return cat_prefix_varname('g', hi->hi_key);
4069 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004071
4072 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004073 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004074 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004076 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004077 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004078 else
4079 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004080 while (HASHITEM_EMPTY(hi))
4081 ++hi;
4082 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004084 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004086 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 return (char_u *)"b:changedtick";
4088 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004089
4090 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004091 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004092 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004094 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004095 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004096 else
4097 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004098 while (HASHITEM_EMPTY(hi))
4099 ++hi;
4100 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004102
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004103#ifdef FEAT_WINDOWS
4104 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004105 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004106 if (tdone < ht->ht_used)
4107 {
4108 if (tdone++ == 0)
4109 hi = ht->ht_array;
4110 else
4111 ++hi;
4112 while (HASHITEM_EMPTY(hi))
4113 ++hi;
4114 return cat_prefix_varname('t', hi->hi_key);
4115 }
4116#endif
4117
Bram Moolenaar33570922005-01-25 22:26:29 +00004118 /* v: variables */
4119 if (vidx < VV_LEN)
4120 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
4122 vim_free(varnamebuf);
4123 varnamebuf = NULL;
4124 varnamebuflen = 0;
4125 return NULL;
4126}
4127
4128#endif /* FEAT_CMDL_COMPL */
4129
4130/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004131 * Return TRUE if "pat" matches "text".
4132 * Does not use 'cpo' and always uses 'magic'.
4133 */
4134 static int
4135pattern_match(char_u *pat, char_u *text, int ic)
4136{
4137 int matches = FALSE;
4138 char_u *save_cpo;
4139 regmatch_T regmatch;
4140
4141 /* avoid 'l' flag in 'cpoptions' */
4142 save_cpo = p_cpo;
4143 p_cpo = (char_u *)"";
4144 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4145 if (regmatch.regprog != NULL)
4146 {
4147 regmatch.rm_ic = ic;
4148 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4149 vim_regfree(regmatch.regprog);
4150 }
4151 p_cpo = save_cpo;
4152 return matches;
4153}
4154
4155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 * types for expressions.
4157 */
4158typedef enum
4159{
4160 TYPE_UNKNOWN = 0
4161 , TYPE_EQUAL /* == */
4162 , TYPE_NEQUAL /* != */
4163 , TYPE_GREATER /* > */
4164 , TYPE_GEQUAL /* >= */
4165 , TYPE_SMALLER /* < */
4166 , TYPE_SEQUAL /* <= */
4167 , TYPE_MATCH /* =~ */
4168 , TYPE_NOMATCH /* !~ */
4169} exptype_T;
4170
4171/*
4172 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4175 */
4176
4177/*
4178 * Handle zero level expression.
4179 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004181 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 * Return OK or FAIL.
4183 */
4184 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004185eval0(
4186 char_u *arg,
4187 typval_T *rettv,
4188 char_u **nextcmd,
4189 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190{
4191 int ret;
4192 char_u *p;
4193
4194 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 if (ret == FAIL || !ends_excmd(*p))
4197 {
4198 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 /*
4201 * Report the invalid expression unless the expression evaluation has
4202 * been cancelled due to an aborting error, an interrupt, or an
4203 * exception.
4204 */
4205 if (!aborting())
4206 EMSG2(_(e_invexpr2), arg);
4207 ret = FAIL;
4208 }
4209 if (nextcmd != NULL)
4210 *nextcmd = check_nextcmd(p);
4211
4212 return ret;
4213}
4214
4215/*
4216 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004217 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 *
4219 * "arg" must point to the first non-white of the expression.
4220 * "arg" is advanced to the next non-white after the recognized expression.
4221 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004222 * Note: "rettv.v_lock" is not set.
4223 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 * Return OK or FAIL.
4225 */
4226 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004227eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228{
4229 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004230 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
4232 /*
4233 * Get the first variable.
4234 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004235 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 return FAIL;
4237
4238 if ((*arg)[0] == '?')
4239 {
4240 result = FALSE;
4241 if (evaluate)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 int error = FALSE;
4244
4245 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 if (error)
4249 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251
4252 /*
4253 * Get the second variable.
4254 */
4255 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 return FAIL;
4258
4259 /*
4260 * Check for the ":".
4261 */
4262 if ((*arg)[0] != ':')
4263 {
4264 EMSG(_("E109: Missing ':' after '?'"));
4265 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 return FAIL;
4268 }
4269
4270 /*
4271 * Get the third variable.
4272 */
4273 *arg = skipwhite(*arg + 1);
4274 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4275 {
4276 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 return FAIL;
4279 }
4280 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283
4284 return OK;
4285}
4286
4287/*
4288 * Handle first level expression:
4289 * expr2 || expr2 || expr2 logical OR
4290 *
4291 * "arg" must point to the first non-white of the expression.
4292 * "arg" is advanced to the next non-white after the recognized expression.
4293 *
4294 * Return OK or FAIL.
4295 */
4296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004297eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298{
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 long result;
4301 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004302 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304 /*
4305 * Get the first variable.
4306 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004307 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 return FAIL;
4309
4310 /*
4311 * Repeat until there is no following "||".
4312 */
4313 first = TRUE;
4314 result = FALSE;
4315 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4316 {
4317 if (evaluate && first)
4318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 if (error)
4323 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 first = FALSE;
4325 }
4326
4327 /*
4328 * Get the second variable.
4329 */
4330 *arg = skipwhite(*arg + 2);
4331 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4332 return FAIL;
4333
4334 /*
4335 * Compute the result.
4336 */
4337 if (evaluate && !result)
4338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (error)
4343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 if (evaluate)
4346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004347 rettv->v_type = VAR_NUMBER;
4348 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 }
4351
4352 return OK;
4353}
4354
4355/*
4356 * Handle second level expression:
4357 * expr3 && expr3 && expr3 logical AND
4358 *
4359 * "arg" must point to the first non-white of the expression.
4360 * "arg" is advanced to the next non-white after the recognized expression.
4361 *
4362 * Return OK or FAIL.
4363 */
4364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004365eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366{
Bram Moolenaar33570922005-01-25 22:26:29 +00004367 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 long result;
4369 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004370 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
4372 /*
4373 * Get the first variable.
4374 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004375 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 return FAIL;
4377
4378 /*
4379 * Repeat until there is no following "&&".
4380 */
4381 first = TRUE;
4382 result = TRUE;
4383 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4384 {
4385 if (evaluate && first)
4386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004387 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004389 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004390 if (error)
4391 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 first = FALSE;
4393 }
4394
4395 /*
4396 * Get the second variable.
4397 */
4398 *arg = skipwhite(*arg + 2);
4399 if (eval4(arg, &var2, evaluate && result) == FAIL)
4400 return FAIL;
4401
4402 /*
4403 * Compute the result.
4404 */
4405 if (evaluate && result)
4406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004407 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004410 if (error)
4411 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413 if (evaluate)
4414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004415 rettv->v_type = VAR_NUMBER;
4416 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418 }
4419
4420 return OK;
4421}
4422
4423/*
4424 * Handle third level expression:
4425 * var1 == var2
4426 * var1 =~ var2
4427 * var1 != var2
4428 * var1 !~ var2
4429 * var1 > var2
4430 * var1 >= var2
4431 * var1 < var2
4432 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 * var1 is var2
4434 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 *
4436 * "arg" must point to the first non-white of the expression.
4437 * "arg" is advanced to the next non-white after the recognized expression.
4438 *
4439 * Return OK or FAIL.
4440 */
4441 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004442eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
Bram Moolenaar33570922005-01-25 22:26:29 +00004444 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 char_u *p;
4446 int i;
4447 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 int len = 2;
4450 long n1, n2;
4451 char_u *s1, *s2;
4452 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454
4455 /*
4456 * Get the first variable.
4457 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 return FAIL;
4460
4461 p = *arg;
4462 switch (p[0])
4463 {
4464 case '=': if (p[1] == '=')
4465 type = TYPE_EQUAL;
4466 else if (p[1] == '~')
4467 type = TYPE_MATCH;
4468 break;
4469 case '!': if (p[1] == '=')
4470 type = TYPE_NEQUAL;
4471 else if (p[1] == '~')
4472 type = TYPE_NOMATCH;
4473 break;
4474 case '>': if (p[1] != '=')
4475 {
4476 type = TYPE_GREATER;
4477 len = 1;
4478 }
4479 else
4480 type = TYPE_GEQUAL;
4481 break;
4482 case '<': if (p[1] != '=')
4483 {
4484 type = TYPE_SMALLER;
4485 len = 1;
4486 }
4487 else
4488 type = TYPE_SEQUAL;
4489 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004490 case 'i': if (p[1] == 's')
4491 {
4492 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4493 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004494 i = p[len];
4495 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004496 {
4497 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4498 type_is = TRUE;
4499 }
4500 }
4501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
4503
4504 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004505 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 */
4507 if (type != TYPE_UNKNOWN)
4508 {
4509 /* extra question mark appended: ignore case */
4510 if (p[len] == '?')
4511 {
4512 ic = TRUE;
4513 ++len;
4514 }
4515 /* extra '#' appended: match case */
4516 else if (p[len] == '#')
4517 {
4518 ic = FALSE;
4519 ++len;
4520 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004521 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 else
4523 ic = p_ic;
4524
4525 /*
4526 * Get the second variable.
4527 */
4528 *arg = skipwhite(p + len);
4529 if (eval5(arg, &var2, evaluate) == FAIL)
4530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004531 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 return FAIL;
4533 }
4534
4535 if (evaluate)
4536 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004537 if (type_is && rettv->v_type != var2.v_type)
4538 {
4539 /* For "is" a different type always means FALSE, for "notis"
4540 * it means TRUE. */
4541 n1 = (type == TYPE_NEQUAL);
4542 }
4543 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4544 {
4545 if (type_is)
4546 {
4547 n1 = (rettv->v_type == var2.v_type
4548 && rettv->vval.v_list == var2.vval.v_list);
4549 if (type == TYPE_NEQUAL)
4550 n1 = !n1;
4551 }
4552 else if (rettv->v_type != var2.v_type
4553 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4554 {
4555 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004556 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004557 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004558 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 clear_tv(rettv);
4560 clear_tv(&var2);
4561 return FAIL;
4562 }
4563 else
4564 {
4565 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004566 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4567 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 if (type == TYPE_NEQUAL)
4569 n1 = !n1;
4570 }
4571 }
4572
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004573 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4574 {
4575 if (type_is)
4576 {
4577 n1 = (rettv->v_type == var2.v_type
4578 && rettv->vval.v_dict == var2.vval.v_dict);
4579 if (type == TYPE_NEQUAL)
4580 n1 = !n1;
4581 }
4582 else if (rettv->v_type != var2.v_type
4583 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4584 {
4585 if (rettv->v_type != var2.v_type)
4586 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4587 else
4588 EMSG(_("E736: Invalid operation for Dictionary"));
4589 clear_tv(rettv);
4590 clear_tv(&var2);
4591 return FAIL;
4592 }
4593 else
4594 {
4595 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004596 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4597 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004598 if (type == TYPE_NEQUAL)
4599 n1 = !n1;
4600 }
4601 }
4602
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004603 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4604 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004605 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004606 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004607 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004608 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004609 clear_tv(rettv);
4610 clear_tv(&var2);
4611 return FAIL;
4612 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004613 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004614 if (type == TYPE_NEQUAL)
4615 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004616 }
4617
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004618#ifdef FEAT_FLOAT
4619 /*
4620 * If one of the two variables is a float, compare as a float.
4621 * When using "=~" or "!~", always compare as string.
4622 */
4623 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4624 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4625 {
4626 float_T f1, f2;
4627
4628 if (rettv->v_type == VAR_FLOAT)
4629 f1 = rettv->vval.v_float;
4630 else
4631 f1 = get_tv_number(rettv);
4632 if (var2.v_type == VAR_FLOAT)
4633 f2 = var2.vval.v_float;
4634 else
4635 f2 = get_tv_number(&var2);
4636 n1 = FALSE;
4637 switch (type)
4638 {
4639 case TYPE_EQUAL: n1 = (f1 == f2); break;
4640 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4641 case TYPE_GREATER: n1 = (f1 > f2); break;
4642 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4643 case TYPE_SMALLER: n1 = (f1 < f2); break;
4644 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4645 case TYPE_UNKNOWN:
4646 case TYPE_MATCH:
4647 case TYPE_NOMATCH: break; /* avoid gcc warning */
4648 }
4649 }
4650#endif
4651
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 /*
4653 * If one of the two variables is a number, compare as a number.
4654 * When using "=~" or "!~", always compare as string.
4655 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004656 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 n1 = get_tv_number(rettv);
4660 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 switch (type)
4662 {
4663 case TYPE_EQUAL: n1 = (n1 == n2); break;
4664 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4665 case TYPE_GREATER: n1 = (n1 > n2); break;
4666 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4667 case TYPE_SMALLER: n1 = (n1 < n2); break;
4668 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4669 case TYPE_UNKNOWN:
4670 case TYPE_MATCH:
4671 case TYPE_NOMATCH: break; /* avoid gcc warning */
4672 }
4673 }
4674 else
4675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004676 s1 = get_tv_string_buf(rettv, buf1);
4677 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4679 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4680 else
4681 i = 0;
4682 n1 = FALSE;
4683 switch (type)
4684 {
4685 case TYPE_EQUAL: n1 = (i == 0); break;
4686 case TYPE_NEQUAL: n1 = (i != 0); break;
4687 case TYPE_GREATER: n1 = (i > 0); break;
4688 case TYPE_GEQUAL: n1 = (i >= 0); break;
4689 case TYPE_SMALLER: n1 = (i < 0); break;
4690 case TYPE_SEQUAL: n1 = (i <= 0); break;
4691
4692 case TYPE_MATCH:
4693 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004694 n1 = pattern_match(s2, s1, ic);
4695 if (type == TYPE_NOMATCH)
4696 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 break;
4698
4699 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4700 }
4701 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004702 clear_tv(rettv);
4703 clear_tv(&var2);
4704 rettv->v_type = VAR_NUMBER;
4705 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707 }
4708
4709 return OK;
4710}
4711
4712/*
4713 * Handle fourth level expression:
4714 * + number addition
4715 * - number subtraction
4716 * . string concatenation
4717 *
4718 * "arg" must point to the first non-white of the expression.
4719 * "arg" is advanced to the next non-white after the recognized expression.
4720 *
4721 * Return OK or FAIL.
4722 */
4723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004724eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
Bram Moolenaar33570922005-01-25 22:26:29 +00004726 typval_T var2;
4727 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 int op;
4729 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004730#ifdef FEAT_FLOAT
4731 float_T f1 = 0, f2 = 0;
4732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 char_u *s1, *s2;
4734 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4735 char_u *p;
4736
4737 /*
4738 * Get the first variable.
4739 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004740 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 return FAIL;
4742
4743 /*
4744 * Repeat computing, until no '+', '-' or '.' is following.
4745 */
4746 for (;;)
4747 {
4748 op = **arg;
4749 if (op != '+' && op != '-' && op != '.')
4750 break;
4751
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004752 if ((op != '+' || rettv->v_type != VAR_LIST)
4753#ifdef FEAT_FLOAT
4754 && (op == '.' || rettv->v_type != VAR_FLOAT)
4755#endif
4756 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004757 {
4758 /* For "list + ...", an illegal use of the first operand as
4759 * a number cannot be determined before evaluating the 2nd
4760 * operand: if this is also a list, all is ok.
4761 * For "something . ...", "something - ..." or "non-list + ...",
4762 * we know that the first operand needs to be a string or number
4763 * without evaluating the 2nd operand. So check before to avoid
4764 * side effects after an error. */
4765 if (evaluate && get_tv_string_chk(rettv) == NULL)
4766 {
4767 clear_tv(rettv);
4768 return FAIL;
4769 }
4770 }
4771
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 /*
4773 * Get the second variable.
4774 */
4775 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004776 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004778 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 return FAIL;
4780 }
4781
4782 if (evaluate)
4783 {
4784 /*
4785 * Compute the result.
4786 */
4787 if (op == '.')
4788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4790 s2 = get_tv_string_buf_chk(&var2, buf2);
4791 if (s2 == NULL) /* type error ? */
4792 {
4793 clear_tv(rettv);
4794 clear_tv(&var2);
4795 return FAIL;
4796 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004797 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004798 clear_tv(rettv);
4799 rettv->v_type = VAR_STRING;
4800 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004802 else if (op == '+' && rettv->v_type == VAR_LIST
4803 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004804 {
4805 /* concatenate Lists */
4806 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4807 &var3) == FAIL)
4808 {
4809 clear_tv(rettv);
4810 clear_tv(&var2);
4811 return FAIL;
4812 }
4813 clear_tv(rettv);
4814 *rettv = var3;
4815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 else
4817 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004818 int error = FALSE;
4819
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004820#ifdef FEAT_FLOAT
4821 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004822 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004823 f1 = rettv->vval.v_float;
4824 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004825 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004826 else
4827#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004829 n1 = get_tv_number_chk(rettv, &error);
4830 if (error)
4831 {
4832 /* This can only happen for "list + non-list". For
4833 * "non-list + ..." or "something - ...", we returned
4834 * before evaluating the 2nd operand. */
4835 clear_tv(rettv);
4836 return FAIL;
4837 }
4838#ifdef FEAT_FLOAT
4839 if (var2.v_type == VAR_FLOAT)
4840 f1 = n1;
4841#endif
4842 }
4843#ifdef FEAT_FLOAT
4844 if (var2.v_type == VAR_FLOAT)
4845 {
4846 f2 = var2.vval.v_float;
4847 n2 = 0;
4848 }
4849 else
4850#endif
4851 {
4852 n2 = get_tv_number_chk(&var2, &error);
4853 if (error)
4854 {
4855 clear_tv(rettv);
4856 clear_tv(&var2);
4857 return FAIL;
4858 }
4859#ifdef FEAT_FLOAT
4860 if (rettv->v_type == VAR_FLOAT)
4861 f2 = n2;
4862#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004865
4866#ifdef FEAT_FLOAT
4867 /* If there is a float on either side the result is a float. */
4868 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4869 {
4870 if (op == '+')
4871 f1 = f1 + f2;
4872 else
4873 f1 = f1 - f2;
4874 rettv->v_type = VAR_FLOAT;
4875 rettv->vval.v_float = f1;
4876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878#endif
4879 {
4880 if (op == '+')
4881 n1 = n1 + n2;
4882 else
4883 n1 = n1 - n2;
4884 rettv->v_type = VAR_NUMBER;
4885 rettv->vval.v_number = n1;
4886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004888 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890 }
4891 return OK;
4892}
4893
4894/*
4895 * Handle fifth level expression:
4896 * * number multiplication
4897 * / number division
4898 * % number modulo
4899 *
4900 * "arg" must point to the first non-white of the expression.
4901 * "arg" is advanced to the next non-white after the recognized expression.
4902 *
4903 * Return OK or FAIL.
4904 */
4905 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004906eval6(
4907 char_u **arg,
4908 typval_T *rettv,
4909 int evaluate,
4910 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911{
Bram Moolenaar33570922005-01-25 22:26:29 +00004912 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 int op;
4914 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915#ifdef FEAT_FLOAT
4916 int use_float = FALSE;
4917 float_T f1 = 0, f2;
4918#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004919 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921 /*
4922 * Get the first variable.
4923 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004924 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return FAIL;
4926
4927 /*
4928 * Repeat computing, until no '*', '/' or '%' is following.
4929 */
4930 for (;;)
4931 {
4932 op = **arg;
4933 if (op != '*' && op != '/' && op != '%')
4934 break;
4935
4936 if (evaluate)
4937 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004938#ifdef FEAT_FLOAT
4939 if (rettv->v_type == VAR_FLOAT)
4940 {
4941 f1 = rettv->vval.v_float;
4942 use_float = TRUE;
4943 n1 = 0;
4944 }
4945 else
4946#endif
4947 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004948 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004949 if (error)
4950 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952 else
4953 n1 = 0;
4954
4955 /*
4956 * Get the second variable.
4957 */
4958 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004959 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 return FAIL;
4961
4962 if (evaluate)
4963 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004964#ifdef FEAT_FLOAT
4965 if (var2.v_type == VAR_FLOAT)
4966 {
4967 if (!use_float)
4968 {
4969 f1 = n1;
4970 use_float = TRUE;
4971 }
4972 f2 = var2.vval.v_float;
4973 n2 = 0;
4974 }
4975 else
4976#endif
4977 {
4978 n2 = get_tv_number_chk(&var2, &error);
4979 clear_tv(&var2);
4980 if (error)
4981 return FAIL;
4982#ifdef FEAT_FLOAT
4983 if (use_float)
4984 f2 = n2;
4985#endif
4986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987
4988 /*
4989 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992#ifdef FEAT_FLOAT
4993 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 if (op == '*')
4996 f1 = f1 * f2;
4997 else if (op == '/')
4998 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004999# ifdef VMS
5000 /* VMS crashes on divide by zero, work around it */
5001 if (f2 == 0.0)
5002 {
5003 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005004 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005005 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005006 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005007 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005008 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005009 }
5010 else
5011 f1 = f1 / f2;
5012# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005013 /* We rely on the floating point library to handle divide
5014 * by zero to result in "inf" and not a crash. */
5015 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005016# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005019 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005020 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005021 return FAIL;
5022 }
5023 rettv->v_type = VAR_FLOAT;
5024 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029 if (op == '*')
5030 n1 = n1 * n2;
5031 else if (op == '/')
5032 {
5033 if (n2 == 0) /* give an error message? */
5034 {
5035 if (n1 == 0)
5036 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5037 else if (n1 < 0)
5038 n1 = -0x7fffffffL;
5039 else
5040 n1 = 0x7fffffffL;
5041 }
5042 else
5043 n1 = n1 / n2;
5044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005046 {
5047 if (n2 == 0) /* give an error message? */
5048 n1 = 0;
5049 else
5050 n1 = n1 % n2;
5051 }
5052 rettv->v_type = VAR_NUMBER;
5053 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
5056 }
5057
5058 return OK;
5059}
5060
5061/*
5062 * Handle sixth level expression:
5063 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005064 * "string" string constant
5065 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 * &option-name option value
5067 * @r register contents
5068 * identifier variable value
5069 * function() function call
5070 * $VAR environment variable
5071 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005072 * [expr, expr] List
5073 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 *
5075 * Also handle:
5076 * ! in front logical NOT
5077 * - in front unary minus
5078 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 * trailing [] subscript in String or List
5080 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 *
5082 * "arg" must point to the first non-white of the expression.
5083 * "arg" is advanced to the next non-white after the recognized expression.
5084 *
5085 * Return OK or FAIL.
5086 */
5087 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005088eval7(
5089 char_u **arg,
5090 typval_T *rettv,
5091 int evaluate,
5092 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 long n;
5095 int len;
5096 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 char_u *start_leader, *end_leader;
5098 int ret = OK;
5099 char_u *alias;
5100
5101 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005103 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005105 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107 /*
5108 * Skip '!' and '-' characters. They are handled later.
5109 */
5110 start_leader = *arg;
5111 while (**arg == '!' || **arg == '-' || **arg == '+')
5112 *arg = skipwhite(*arg + 1);
5113 end_leader = *arg;
5114
5115 switch (**arg)
5116 {
5117 /*
5118 * Number constant.
5119 */
5120 case '0':
5121 case '1':
5122 case '2':
5123 case '3':
5124 case '4':
5125 case '5':
5126 case '6':
5127 case '7':
5128 case '8':
5129 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005130 {
5131#ifdef FEAT_FLOAT
5132 char_u *p = skipdigits(*arg + 1);
5133 int get_float = FALSE;
5134
5135 /* We accept a float when the format matches
5136 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005137 * strict to avoid backwards compatibility problems.
5138 * Don't look for a float after the "." operator, so that
5139 * ":let vers = 1.2.3" doesn't fail. */
5140 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005142 get_float = TRUE;
5143 p = skipdigits(p + 2);
5144 if (*p == 'e' || *p == 'E')
5145 {
5146 ++p;
5147 if (*p == '-' || *p == '+')
5148 ++p;
5149 if (!vim_isdigit(*p))
5150 get_float = FALSE;
5151 else
5152 p = skipdigits(p + 1);
5153 }
5154 if (ASCII_ISALPHA(*p) || *p == '.')
5155 get_float = FALSE;
5156 }
5157 if (get_float)
5158 {
5159 float_T f;
5160
5161 *arg += string2float(*arg, &f);
5162 if (evaluate)
5163 {
5164 rettv->v_type = VAR_FLOAT;
5165 rettv->vval.v_float = f;
5166 }
5167 }
5168 else
5169#endif
5170 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005171 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005172 *arg += len;
5173 if (evaluate)
5174 {
5175 rettv->v_type = VAR_NUMBER;
5176 rettv->vval.v_number = n;
5177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181
5182 /*
5183 * String constant: "string".
5184 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 break;
5187
5188 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005191 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005192 break;
5193
5194 /*
5195 * List: [expr, expr]
5196 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005197 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199
5200 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 * Dictionary: {key: val, key: val}
5202 */
5203 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5204 break;
5205
5206 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005207 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005209 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 break;
5211
5212 /*
5213 * Environment variable: $VAR.
5214 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005215 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 break;
5217
5218 /*
5219 * Register contents: @r.
5220 */
5221 case '@': ++*arg;
5222 if (evaluate)
5223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005225 rettv->vval.v_string = get_reg_contents(**arg,
5226 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
5228 if (**arg != NUL)
5229 ++*arg;
5230 break;
5231
5232 /*
5233 * nested expression: (expression).
5234 */
5235 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005236 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 if (**arg == ')')
5238 ++*arg;
5239 else if (ret == OK)
5240 {
5241 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005242 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 ret = FAIL;
5244 }
5245 break;
5246
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 break;
5249 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250
5251 if (ret == NOTDONE)
5252 {
5253 /*
5254 * Must be a variable or function name.
5255 * Can also be a curly-braces kind of name: {expr}.
5256 */
5257 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005258 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 if (alias != NULL)
5260 s = alias;
5261
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005262 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 ret = FAIL;
5264 else
5265 {
5266 if (**arg == '(') /* recursive! */
5267 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005268 partial_T *partial;
5269
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 /* If "s" is the name of a variable of type VAR_FUNC
5271 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005272 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273
5274 /* Invoke the function. */
5275 ret = get_func_tv(s, len, rettv, arg,
5276 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005277 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005278
5279 /* If evaluate is FALSE rettv->v_type was not set in
5280 * get_func_tv, but it's needed in handle_subscript() to parse
5281 * what follows. So set it here. */
5282 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5283 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005284 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005285 rettv->v_type = VAR_FUNC;
5286 }
5287
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288 /* Stop the expression evaluation when immediately
5289 * aborting on error, or when an interrupt occurred or
5290 * an exception was thrown but not caught. */
5291 if (aborting())
5292 {
5293 if (ret == OK)
5294 clear_tv(rettv);
5295 ret = FAIL;
5296 }
5297 }
5298 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005299 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005300 else
5301 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005303 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 }
5305
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 *arg = skipwhite(*arg);
5307
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005308 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5309 * expr(expr). */
5310 if (ret == OK)
5311 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312
5313 /*
5314 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5315 */
5316 if (ret == OK && evaluate && end_leader > start_leader)
5317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005319 int val = 0;
5320#ifdef FEAT_FLOAT
5321 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005323 if (rettv->v_type == VAR_FLOAT)
5324 f = rettv->vval.v_float;
5325 else
5326#endif
5327 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005330 clear_tv(rettv);
5331 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005333 else
5334 {
5335 while (end_leader > start_leader)
5336 {
5337 --end_leader;
5338 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005339 {
5340#ifdef FEAT_FLOAT
5341 if (rettv->v_type == VAR_FLOAT)
5342 f = !f;
5343 else
5344#endif
5345 val = !val;
5346 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005347 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005348 {
5349#ifdef FEAT_FLOAT
5350 if (rettv->v_type == VAR_FLOAT)
5351 f = -f;
5352 else
5353#endif
5354 val = -val;
5355 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005356 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005357#ifdef FEAT_FLOAT
5358 if (rettv->v_type == VAR_FLOAT)
5359 {
5360 clear_tv(rettv);
5361 rettv->vval.v_float = f;
5362 }
5363 else
5364#endif
5365 {
5366 clear_tv(rettv);
5367 rettv->v_type = VAR_NUMBER;
5368 rettv->vval.v_number = val;
5369 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
5372
5373 return ret;
5374}
5375
5376/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005377 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5378 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5380 */
5381 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005382eval_index(
5383 char_u **arg,
5384 typval_T *rettv,
5385 int evaluate,
5386 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387{
5388 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005389 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 long len = -1;
5392 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395
Bram Moolenaara03f2332016-02-06 18:09:59 +01005396 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005398 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005399 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005400 if (verbose)
5401 EMSG(_("E695: Cannot index a Funcref"));
5402 return FAIL;
5403 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005404#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005405 if (verbose)
5406 EMSG(_(e_float_as_string));
5407 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005408#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005409 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005410 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005411 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005412 if (verbose)
5413 EMSG(_("E909: Cannot index a special variable"));
5414 return FAIL;
5415 case VAR_UNKNOWN:
5416 if (evaluate)
5417 return FAIL;
5418 /* FALLTHROUGH */
5419
5420 case VAR_STRING:
5421 case VAR_NUMBER:
5422 case VAR_LIST:
5423 case VAR_DICT:
5424 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005425 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005427 init_tv(&var1);
5428 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005429 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 /*
5432 * dict.name
5433 */
5434 key = *arg + 1;
5435 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5436 ;
5437 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 }
5441 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443 /*
5444 * something[idx]
5445 *
5446 * Get the (first) variable from inside the [].
5447 */
5448 *arg = skipwhite(*arg + 1);
5449 if (**arg == ':')
5450 empty1 = TRUE;
5451 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5452 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005453 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5454 {
5455 /* not a number or string */
5456 clear_tv(&var1);
5457 return FAIL;
5458 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459
5460 /*
5461 * Get the second variable from inside the [:].
5462 */
5463 if (**arg == ':')
5464 {
5465 range = TRUE;
5466 *arg = skipwhite(*arg + 1);
5467 if (**arg == ']')
5468 empty2 = TRUE;
5469 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005471 if (!empty1)
5472 clear_tv(&var1);
5473 return FAIL;
5474 }
5475 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5476 {
5477 /* not a number or string */
5478 if (!empty1)
5479 clear_tv(&var1);
5480 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481 return FAIL;
5482 }
5483 }
5484
5485 /* Check for the ']'. */
5486 if (**arg != ']')
5487 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005488 if (verbose)
5489 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490 clear_tv(&var1);
5491 if (range)
5492 clear_tv(&var2);
5493 return FAIL;
5494 }
5495 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 }
5497
5498 if (evaluate)
5499 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005500 n1 = 0;
5501 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 n1 = get_tv_number(&var1);
5504 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 }
5506 if (range)
5507 {
5508 if (empty2)
5509 n2 = -1;
5510 else
5511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512 n2 = get_tv_number(&var2);
5513 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514 }
5515 }
5516
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005519 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005520 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005521 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005522 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005523 case VAR_SPECIAL:
5524 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005525 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005526 break; /* not evaluating, skipping over subscript */
5527
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005528 case VAR_NUMBER:
5529 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 len = (long)STRLEN(s);
5532 if (range)
5533 {
5534 /* The resulting variable is a substring. If the indexes
5535 * are out of range the result is empty. */
5536 if (n1 < 0)
5537 {
5538 n1 = len + n1;
5539 if (n1 < 0)
5540 n1 = 0;
5541 }
5542 if (n2 < 0)
5543 n2 = len + n2;
5544 else if (n2 >= len)
5545 n2 = len;
5546 if (n1 >= len || n2 < 0 || n1 > n2)
5547 s = NULL;
5548 else
5549 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5550 }
5551 else
5552 {
5553 /* The resulting variable is a string of a single
5554 * character. If the index is too big or negative the
5555 * result is empty. */
5556 if (n1 >= len || n1 < 0)
5557 s = NULL;
5558 else
5559 s = vim_strnsave(s + n1, 1);
5560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 clear_tv(rettv);
5562 rettv->v_type = VAR_STRING;
5563 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 break;
5565
5566 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 if (n1 < 0)
5569 n1 = len + n1;
5570 if (!empty1 && (n1 < 0 || n1 >= len))
5571 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005572 /* For a range we allow invalid values and return an empty
5573 * list. A list index out of range is an error. */
5574 if (!range)
5575 {
5576 if (verbose)
5577 EMSGN(_(e_listidx), n1);
5578 return FAIL;
5579 }
5580 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 }
5582 if (range)
5583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005584 list_T *l;
5585 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005586
5587 if (n2 < 0)
5588 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005589 else if (n2 >= len)
5590 n2 = len - 1;
5591 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005592 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 l = list_alloc();
5594 if (l == NULL)
5595 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005596 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 n1 <= n2; ++n1)
5598 {
5599 if (list_append_tv(l, &item->li_tv) == FAIL)
5600 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005601 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 return FAIL;
5603 }
5604 item = item->li_next;
5605 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005606 clear_tv(rettv);
5607 rettv->v_type = VAR_LIST;
5608 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005609 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005610 }
5611 else
5612 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005613 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005614 clear_tv(rettv);
5615 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 }
5617 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005618
5619 case VAR_DICT:
5620 if (range)
5621 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005622 if (verbose)
5623 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005624 if (len == -1)
5625 clear_tv(&var1);
5626 return FAIL;
5627 }
5628 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630
5631 if (len == -1)
5632 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005633 key = get_tv_string_chk(&var1);
5634 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 clear_tv(&var1);
5637 return FAIL;
5638 }
5639 }
5640
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005641 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005642
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005643 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005644 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005645 if (len == -1)
5646 clear_tv(&var1);
5647 if (item == NULL)
5648 return FAIL;
5649
5650 copy_tv(&item->di_tv, &var1);
5651 clear_tv(rettv);
5652 *rettv = var1;
5653 }
5654 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005655 }
5656 }
5657
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005658 return OK;
5659}
5660
5661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 * Get an option value.
5663 * "arg" points to the '&' or '+' before the option name.
5664 * "arg" is advanced to character after the option name.
5665 * Return OK or FAIL.
5666 */
5667 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005668get_option_tv(
5669 char_u **arg,
5670 typval_T *rettv, /* when NULL, only check if option exists */
5671 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 char_u *option_end;
5674 long numval;
5675 char_u *stringval;
5676 int opt_type;
5677 int c;
5678 int working = (**arg == '+'); /* has("+option") */
5679 int ret = OK;
5680 int opt_flags;
5681
5682 /*
5683 * Isolate the option name and find its value.
5684 */
5685 option_end = find_option_end(arg, &opt_flags);
5686 if (option_end == NULL)
5687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 EMSG2(_("E112: Option name missing: %s"), *arg);
5690 return FAIL;
5691 }
5692
5693 if (!evaluate)
5694 {
5695 *arg = option_end;
5696 return OK;
5697 }
5698
5699 c = *option_end;
5700 *option_end = NUL;
5701 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005702 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703
5704 if (opt_type == -3) /* invalid name */
5705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005706 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 EMSG2(_("E113: Unknown option: %s"), *arg);
5708 ret = FAIL;
5709 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005710 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
5712 if (opt_type == -2) /* hidden string option */
5713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005714 rettv->v_type = VAR_STRING;
5715 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717 else if (opt_type == -1) /* hidden number option */
5718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005719 rettv->v_type = VAR_NUMBER;
5720 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 else if (opt_type == 1) /* number option */
5723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005724 rettv->v_type = VAR_NUMBER;
5725 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727 else /* string option */
5728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005729 rettv->v_type = VAR_STRING;
5730 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 }
5732 }
5733 else if (working && (opt_type == -2 || opt_type == -1))
5734 ret = FAIL;
5735
5736 *option_end = c; /* put back for error messages */
5737 *arg = option_end;
5738
5739 return ret;
5740}
5741
5742/*
5743 * Allocate a variable for a string constant.
5744 * Return OK or FAIL.
5745 */
5746 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005747get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
5749 char_u *p;
5750 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 int extra = 0;
5752
5753 /*
5754 * Find the end of the string, skipping backslashed characters.
5755 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
5758 if (*p == '\\' && p[1] != NUL)
5759 {
5760 ++p;
5761 /* A "\<x>" form occupies at least 4 characters, and produces up
5762 * to 6 characters: reserve space for 2 extra */
5763 if (*p == '<')
5764 extra += 2;
5765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767
5768 if (*p != '"')
5769 {
5770 EMSG2(_("E114: Missing quote: %s"), *arg);
5771 return FAIL;
5772 }
5773
5774 /* If only parsing, set *arg and return here */
5775 if (!evaluate)
5776 {
5777 *arg = p + 1;
5778 return OK;
5779 }
5780
5781 /*
5782 * Copy the string into allocated memory, handling backslashed
5783 * characters.
5784 */
5785 name = alloc((unsigned)(p - *arg + extra));
5786 if (name == NULL)
5787 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 rettv->v_type = VAR_STRING;
5789 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {
5793 if (*p == '\\')
5794 {
5795 switch (*++p)
5796 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 case 'b': *name++ = BS; ++p; break;
5798 case 'e': *name++ = ESC; ++p; break;
5799 case 'f': *name++ = FF; ++p; break;
5800 case 'n': *name++ = NL; ++p; break;
5801 case 'r': *name++ = CAR; ++p; break;
5802 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803
5804 case 'X': /* hex: "\x1", "\x12" */
5805 case 'x':
5806 case 'u': /* Unicode: "\u0023" */
5807 case 'U':
5808 if (vim_isxdigit(p[1]))
5809 {
5810 int n, nr;
5811 int c = toupper(*p);
5812
5813 if (c == 'X')
5814 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005815 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005817 else
5818 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 nr = 0;
5820 while (--n >= 0 && vim_isxdigit(p[1]))
5821 {
5822 ++p;
5823 nr = (nr << 4) + hex2nr(*p);
5824 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#ifdef FEAT_MBYTE
5827 /* For "\u" store the number according to
5828 * 'encoding'. */
5829 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 else
5832#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 break;
5836
5837 /* octal: "\1", "\12", "\123" */
5838 case '0':
5839 case '1':
5840 case '2':
5841 case '3':
5842 case '4':
5843 case '5':
5844 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005845 case '7': *name = *p++ - '0';
5846 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 *name = (*name << 3) + *p++ - '0';
5849 if (*p >= '0' && *p <= '7')
5850 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 break;
5854
5855 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 if (extra != 0)
5858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 break;
5861 }
5862 /* FALLTHROUGH */
5863
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 break;
5866 }
5867 }
5868 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 *arg = p + 1;
5874
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 return OK;
5876}
5877
5878/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 * Return OK or FAIL.
5881 */
5882 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005883get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884{
5885 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 int reduce = 0;
5888
5889 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 */
5892 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 break;
5898 ++reduce;
5899 ++p;
5900 }
5901 }
5902
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 return FAIL;
5907 }
5908
Bram Moolenaar8c711452005-01-14 21:53:12 +00005909 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005910 if (!evaluate)
5911 {
5912 *arg = p + 1;
5913 return OK;
5914 }
5915
5916 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005917 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005918 */
5919 str = alloc((unsigned)((p - *arg) - reduce));
5920 if (str == NULL)
5921 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 rettv->v_type = VAR_STRING;
5923 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005930 break;
5931 ++p;
5932 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005935 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936 *arg = p + 1;
5937
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005938 return OK;
5939}
5940
Bram Moolenaarddecc252016-04-06 22:59:37 +02005941 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005942partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005943{
5944 int i;
5945
5946 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005947 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005948 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005949 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005950 func_unref(pt->pt_name);
5951 vim_free(pt->pt_name);
5952 vim_free(pt);
5953}
5954
5955/*
5956 * Unreference a closure: decrement the reference count and free it when it
5957 * becomes zero.
5958 */
5959 void
5960partial_unref(partial_T *pt)
5961{
5962 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005963 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005964}
5965
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005966/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 * Allocate a variable for a List and fill it from "*arg".
5968 * Return OK or FAIL.
5969 */
5970 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972{
Bram Moolenaar33570922005-01-25 22:26:29 +00005973 list_T *l = NULL;
5974 typval_T tv;
5975 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976
5977 if (evaluate)
5978 {
5979 l = list_alloc();
5980 if (l == NULL)
5981 return FAIL;
5982 }
5983
5984 *arg = skipwhite(*arg + 1);
5985 while (**arg != ']' && **arg != NUL)
5986 {
5987 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5988 goto failret;
5989 if (evaluate)
5990 {
5991 item = listitem_alloc();
5992 if (item != NULL)
5993 {
5994 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005995 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 list_append(l, item);
5997 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005998 else
5999 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000 }
6001
6002 if (**arg == ']')
6003 break;
6004 if (**arg != ',')
6005 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006006 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 goto failret;
6008 }
6009 *arg = skipwhite(*arg + 1);
6010 }
6011
6012 if (**arg != ']')
6013 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006014 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015failret:
6016 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006017 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018 return FAIL;
6019 }
6020
6021 *arg = skipwhite(*arg + 1);
6022 if (evaluate)
6023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006024 rettv->v_type = VAR_LIST;
6025 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 ++l->lv_refcount;
6027 }
6028
6029 return OK;
6030}
6031
6032/*
6033 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006034 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006036 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006037list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006039 list_T *l;
6040
6041 l = (list_T *)alloc_clear(sizeof(list_T));
6042 if (l != NULL)
6043 {
6044 /* Prepend the list to the list of lists for garbage collection. */
6045 if (first_list != NULL)
6046 first_list->lv_used_prev = l;
6047 l->lv_used_prev = NULL;
6048 l->lv_used_next = first_list;
6049 first_list = l;
6050 }
6051 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052}
6053
6054/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006055 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006056 * Returns OK or FAIL.
6057 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006058 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006059rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006060{
6061 list_T *l = list_alloc();
6062
6063 if (l == NULL)
6064 return FAIL;
6065
6066 rettv->vval.v_list = l;
6067 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006068 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006069 ++l->lv_refcount;
6070 return OK;
6071}
6072
6073/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 * Unreference a list: decrement the reference count and free it when it
6075 * becomes zero.
6076 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006078list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006080 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006081 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082}
6083
6084/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006085 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 * Ignores the reference count.
6087 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006088 static void
6089list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090{
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006093 for (item = l->lv_first; item != NULL; item = l->lv_first)
6094 {
6095 /* Remove the item before deleting it. */
6096 l->lv_first = item->li_next;
6097 clear_tv(&item->li_tv);
6098 vim_free(item);
6099 }
6100}
6101
6102 static void
6103list_free_list(list_T *l)
6104{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006105 /* Remove the list from the list of lists for garbage collection. */
6106 if (l->lv_used_prev == NULL)
6107 first_list = l->lv_used_next;
6108 else
6109 l->lv_used_prev->lv_used_next = l->lv_used_next;
6110 if (l->lv_used_next != NULL)
6111 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6112
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006113 vim_free(l);
6114}
6115
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006116 void
6117list_free(list_T *l)
6118{
6119 if (!in_free_unref_items)
6120 {
6121 list_free_contents(l);
6122 list_free_list(l);
6123 }
6124}
6125
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006126/*
6127 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006128 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006130 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006131listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006132{
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006134}
6135
6136/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006137 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006138 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006140listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006142 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143 vim_free(item);
6144}
6145
6146/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006147 * Remove a list item from a List and free it. Also clears the value.
6148 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006150listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006151{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006152 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006153 listitem_free(item);
6154}
6155
6156/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157 * Get the number of items in a list.
6158 */
6159 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006160list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006161{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162 if (l == NULL)
6163 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006164 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165}
6166
6167/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006168 * Return TRUE when two lists have exactly the same values.
6169 */
6170 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006171list_equal(
6172 list_T *l1,
6173 list_T *l2,
6174 int ic, /* ignore case for strings */
6175 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176{
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006179 if (l1 == NULL || l2 == NULL)
6180 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006181 if (l1 == l2)
6182 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006183 if (list_len(l1) != list_len(l2))
6184 return FALSE;
6185
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186 for (item1 = l1->lv_first, item2 = l2->lv_first;
6187 item1 != NULL && item2 != NULL;
6188 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006190 return FALSE;
6191 return item1 == NULL && item2 == NULL;
6192}
6193
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006194/*
6195 * Return the dictitem that an entry in a hashtable points to.
6196 */
6197 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006198dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006199{
6200 return HI2DI(hi);
6201}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006202
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006203/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006204 * Return TRUE when two dictionaries have exactly the same key/values.
6205 */
6206 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006207dict_equal(
6208 dict_T *d1,
6209 dict_T *d2,
6210 int ic, /* ignore case for strings */
6211 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006212{
Bram Moolenaar33570922005-01-25 22:26:29 +00006213 hashitem_T *hi;
6214 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006215 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006216
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006217 if (d1 == NULL || d2 == NULL)
6218 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006219 if (d1 == d2)
6220 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006221 if (dict_len(d1) != dict_len(d2))
6222 return FALSE;
6223
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006224 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006225 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006226 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006227 if (!HASHITEM_EMPTY(hi))
6228 {
6229 item2 = dict_find(d2, hi->hi_key, -1);
6230 if (item2 == NULL)
6231 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006232 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006233 return FALSE;
6234 --todo;
6235 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006236 }
6237 return TRUE;
6238}
6239
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006240static int tv_equal_recurse_limit;
6241
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006242/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006243 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006244 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006245 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006246 */
6247 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006248tv_equal(
6249 typval_T *tv1,
6250 typval_T *tv2,
6251 int ic, /* ignore case */
6252 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253{
6254 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006255 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006256 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006257 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006258
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006259 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6260 if ((tv1->v_type == VAR_FUNC
6261 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6262 && (tv2->v_type == VAR_FUNC
6263 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6264 {
6265 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6266 : tv1->vval.v_partial->pt_name;
6267 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6268 : tv2->vval.v_partial->pt_name;
6269 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6270 }
6271
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006272 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006273 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006274
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006275 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006276 * recursiveness to a limit. We guess they are equal then.
6277 * A fixed limit has the problem of still taking an awful long time.
6278 * Reduce the limit every time running into it. That should work fine for
6279 * deeply linked structures that are not recursively linked and catch
6280 * recursiveness quickly. */
6281 if (!recursive)
6282 tv_equal_recurse_limit = 1000;
6283 if (recursive_cnt >= tv_equal_recurse_limit)
6284 {
6285 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006286 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006287 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006288
6289 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006290 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006291 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006292 ++recursive_cnt;
6293 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6294 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006295 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006296
6297 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006298 ++recursive_cnt;
6299 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6300 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006301 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006302
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006303 case VAR_NUMBER:
6304 return tv1->vval.v_number == tv2->vval.v_number;
6305
6306 case VAR_STRING:
6307 s1 = get_tv_string_buf(tv1, buf1);
6308 s2 = get_tv_string_buf(tv2, buf2);
6309 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006310
6311 case VAR_SPECIAL:
6312 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006313
6314 case VAR_FLOAT:
6315#ifdef FEAT_FLOAT
6316 return tv1->vval.v_float == tv2->vval.v_float;
6317#endif
6318 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006319#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006320 return tv1->vval.v_job == tv2->vval.v_job;
6321#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006322 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006323#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006324 return tv1->vval.v_channel == tv2->vval.v_channel;
6325#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006326 case VAR_FUNC:
6327 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006328 case VAR_UNKNOWN:
6329 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006330 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006331
Bram Moolenaara03f2332016-02-06 18:09:59 +01006332 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6333 * does not equal anything, not even itself. */
6334 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006335}
6336
6337/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006338 * Locate item with index "n" in list "l" and return it.
6339 * A negative index is counted from the end; -1 is the last item.
6340 * Returns NULL when "n" is out of range.
6341 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006342 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006343list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344{
Bram Moolenaar33570922005-01-25 22:26:29 +00006345 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346 long idx;
6347
6348 if (l == NULL)
6349 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006350
6351 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006352 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006353 n = l->lv_len + n;
6354
6355 /* Check for index out of range. */
6356 if (n < 0 || n >= l->lv_len)
6357 return NULL;
6358
6359 /* When there is a cached index may start search from there. */
6360 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006361 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006362 if (n < l->lv_idx / 2)
6363 {
6364 /* closest to the start of the list */
6365 item = l->lv_first;
6366 idx = 0;
6367 }
6368 else if (n > (l->lv_idx + l->lv_len) / 2)
6369 {
6370 /* closest to the end of the list */
6371 item = l->lv_last;
6372 idx = l->lv_len - 1;
6373 }
6374 else
6375 {
6376 /* closest to the cached index */
6377 item = l->lv_idx_item;
6378 idx = l->lv_idx;
6379 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380 }
6381 else
6382 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006383 if (n < l->lv_len / 2)
6384 {
6385 /* closest to the start of the list */
6386 item = l->lv_first;
6387 idx = 0;
6388 }
6389 else
6390 {
6391 /* closest to the end of the list */
6392 item = l->lv_last;
6393 idx = l->lv_len - 1;
6394 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006395 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006396
6397 while (n > idx)
6398 {
6399 /* search forward */
6400 item = item->li_next;
6401 ++idx;
6402 }
6403 while (n < idx)
6404 {
6405 /* search backward */
6406 item = item->li_prev;
6407 --idx;
6408 }
6409
6410 /* cache the used index */
6411 l->lv_idx = idx;
6412 l->lv_idx_item = item;
6413
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 return item;
6415}
6416
6417/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006418 * Get list item "l[idx]" as a number.
6419 */
6420 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006421list_find_nr(
6422 list_T *l,
6423 long idx,
6424 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006425{
6426 listitem_T *li;
6427
6428 li = list_find(l, idx);
6429 if (li == NULL)
6430 {
6431 if (errorp != NULL)
6432 *errorp = TRUE;
6433 return -1L;
6434 }
6435 return get_tv_number_chk(&li->li_tv, errorp);
6436}
6437
6438/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006439 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6440 */
6441 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006442list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006443{
6444 listitem_T *li;
6445
6446 li = list_find(l, idx - 1);
6447 if (li == NULL)
6448 {
6449 EMSGN(_(e_listidx), idx);
6450 return NULL;
6451 }
6452 return get_tv_string(&li->li_tv);
6453}
6454
6455/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006456 * Locate "item" list "l" and return its index.
6457 * Returns -1 when "item" is not in the list.
6458 */
6459 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006460list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006461{
6462 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006463 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006464
6465 if (l == NULL)
6466 return -1;
6467 idx = 0;
6468 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6469 ++idx;
6470 if (li == NULL)
6471 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006472 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006473}
6474
6475/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006476 * Append item "item" to the end of list "l".
6477 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006479list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006480{
6481 if (l->lv_last == NULL)
6482 {
6483 /* empty list */
6484 l->lv_first = item;
6485 l->lv_last = item;
6486 item->li_prev = NULL;
6487 }
6488 else
6489 {
6490 l->lv_last->li_next = item;
6491 item->li_prev = l->lv_last;
6492 l->lv_last = item;
6493 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006494 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495 item->li_next = NULL;
6496}
6497
6498/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006501 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006503list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006505 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506
Bram Moolenaar05159a02005-02-26 23:04:13 +00006507 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006509 copy_tv(tv, &li->li_tv);
6510 list_append(l, li);
6511 return OK;
6512}
6513
6514/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006515 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006516 * Return FAIL when out of memory.
6517 */
6518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006519list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006520{
6521 listitem_T *li = listitem_alloc();
6522
6523 if (li == NULL)
6524 return FAIL;
6525 li->li_tv.v_type = VAR_DICT;
6526 li->li_tv.v_lock = 0;
6527 li->li_tv.vval.v_dict = dict;
6528 list_append(list, li);
6529 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530 return OK;
6531}
6532
6533/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006534 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006535 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006536 * Returns FAIL when out of memory.
6537 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006539list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006540{
6541 listitem_T *li = listitem_alloc();
6542
6543 if (li == NULL)
6544 return FAIL;
6545 list_append(l, li);
6546 li->li_tv.v_type = VAR_STRING;
6547 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006548 if (str == NULL)
6549 li->li_tv.vval.v_string = NULL;
6550 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006551 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006552 return FAIL;
6553 return OK;
6554}
6555
6556/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006557 * Append "n" to list "l".
6558 * Returns FAIL when out of memory.
6559 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006560 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006561list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006562{
6563 listitem_T *li;
6564
6565 li = listitem_alloc();
6566 if (li == NULL)
6567 return FAIL;
6568 li->li_tv.v_type = VAR_NUMBER;
6569 li->li_tv.v_lock = 0;
6570 li->li_tv.vval.v_number = n;
6571 list_append(l, li);
6572 return OK;
6573}
6574
6575/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577 * If "item" is NULL append at the end.
6578 * Return FAIL when out of memory.
6579 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006580 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006581list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582{
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584
6585 if (ni == NULL)
6586 return FAIL;
6587 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006588 list_insert(l, ni, item);
6589 return OK;
6590}
6591
6592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006593list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006594{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595 if (item == NULL)
6596 /* Append new item at end of list. */
6597 list_append(l, ni);
6598 else
6599 {
6600 /* Insert new item before existing item. */
6601 ni->li_prev = item->li_prev;
6602 ni->li_next = item;
6603 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006604 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006605 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006606 ++l->lv_idx;
6607 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006608 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006609 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006610 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006611 l->lv_idx_item = NULL;
6612 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006613 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006614 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006615 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006616}
6617
6618/*
6619 * Extend "l1" with "l2".
6620 * If "bef" is NULL append at the end, otherwise insert before this item.
6621 * Returns FAIL when out of memory.
6622 */
6623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006624list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006625{
Bram Moolenaar33570922005-01-25 22:26:29 +00006626 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006627 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006628
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006629 /* We also quit the loop when we have inserted the original item count of
6630 * the list, avoid a hang when we extend a list with itself. */
6631 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006632 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6633 return FAIL;
6634 return OK;
6635}
6636
6637/*
6638 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6639 * Return FAIL when out of memory.
6640 */
6641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006642list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006643{
Bram Moolenaar33570922005-01-25 22:26:29 +00006644 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006645
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006646 if (l1 == NULL || l2 == NULL)
6647 return FAIL;
6648
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006649 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006650 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651 if (l == NULL)
6652 return FAIL;
6653 tv->v_type = VAR_LIST;
6654 tv->vval.v_list = l;
6655
6656 /* append all items from the second list */
6657 return list_extend(l, l2, NULL);
6658}
6659
6660/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006661 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006663 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664 * Returns NULL when out of memory.
6665 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006666 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006667list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668{
Bram Moolenaar33570922005-01-25 22:26:29 +00006669 list_T *copy;
6670 listitem_T *item;
6671 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672
6673 if (orig == NULL)
6674 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675
6676 copy = list_alloc();
6677 if (copy != NULL)
6678 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006679 if (copyID != 0)
6680 {
6681 /* Do this before adding the items, because one of the items may
6682 * refer back to this list. */
6683 orig->lv_copyID = copyID;
6684 orig->lv_copylist = copy;
6685 }
6686 for (item = orig->lv_first; item != NULL && !got_int;
6687 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006688 {
6689 ni = listitem_alloc();
6690 if (ni == NULL)
6691 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006692 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006693 {
6694 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6695 {
6696 vim_free(ni);
6697 break;
6698 }
6699 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006701 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006702 list_append(copy, ni);
6703 }
6704 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006705 if (item != NULL)
6706 {
6707 list_unref(copy);
6708 copy = NULL;
6709 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006710 }
6711
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006712 return copy;
6713}
6714
6715/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006716 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006717 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006718 * This used to be called list_remove, but that conflicts with a Sun header
6719 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006720 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006722vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006723{
Bram Moolenaar33570922005-01-25 22:26:29 +00006724 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006725
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006726 /* notify watchers */
6727 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006728 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006729 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006730 list_fix_watch(l, ip);
6731 if (ip == item2)
6732 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006733 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006734
6735 if (item2->li_next == NULL)
6736 l->lv_last = item->li_prev;
6737 else
6738 item2->li_next->li_prev = item->li_prev;
6739 if (item->li_prev == NULL)
6740 l->lv_first = item2->li_next;
6741 else
6742 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006743 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744}
6745
6746/*
6747 * Return an allocated string with the string representation of a list.
6748 * May return NULL.
6749 */
6750 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006751list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006752{
6753 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006754
6755 if (tv->vval.v_list == NULL)
6756 return NULL;
6757 ga_init2(&ga, (int)sizeof(char), 80);
6758 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006759 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006760 {
6761 vim_free(ga.ga_data);
6762 return NULL;
6763 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006764 ga_append(&ga, ']');
6765 ga_append(&ga, NUL);
6766 return (char_u *)ga.ga_data;
6767}
6768
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006769typedef struct join_S {
6770 char_u *s;
6771 char_u *tofree;
6772} join_T;
6773
6774 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006775list_join_inner(
6776 garray_T *gap, /* to store the result in */
6777 list_T *l,
6778 char_u *sep,
6779 int echo_style,
6780 int copyID,
6781 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006782{
6783 int i;
6784 join_T *p;
6785 int len;
6786 int sumlen = 0;
6787 int first = TRUE;
6788 char_u *tofree;
6789 char_u numbuf[NUMBUFLEN];
6790 listitem_T *item;
6791 char_u *s;
6792
6793 /* Stringify each item in the list. */
6794 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6795 {
6796 if (echo_style)
6797 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6798 else
6799 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6800 if (s == NULL)
6801 return FAIL;
6802
6803 len = (int)STRLEN(s);
6804 sumlen += len;
6805
Bram Moolenaarcde88542015-08-11 19:14:00 +02006806 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006807 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6808 if (tofree != NULL || s != numbuf)
6809 {
6810 p->s = s;
6811 p->tofree = tofree;
6812 }
6813 else
6814 {
6815 p->s = vim_strnsave(s, len);
6816 p->tofree = p->s;
6817 }
6818
6819 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006820 if (did_echo_string_emsg) /* recursion error, bail out */
6821 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006822 }
6823
6824 /* Allocate result buffer with its total size, avoid re-allocation and
6825 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6826 if (join_gap->ga_len >= 2)
6827 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6828 if (ga_grow(gap, sumlen + 2) == FAIL)
6829 return FAIL;
6830
6831 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6832 {
6833 if (first)
6834 first = FALSE;
6835 else
6836 ga_concat(gap, sep);
6837 p = ((join_T *)join_gap->ga_data) + i;
6838
6839 if (p->s != NULL)
6840 ga_concat(gap, p->s);
6841 line_breakcheck();
6842 }
6843
6844 return OK;
6845}
6846
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006847/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006848 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006849 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006850 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006851 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006852 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006853list_join(
6854 garray_T *gap,
6855 list_T *l,
6856 char_u *sep,
6857 int echo_style,
6858 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006859{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006860 garray_T join_ga;
6861 int retval;
6862 join_T *p;
6863 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006864
Bram Moolenaard39a7512015-04-16 22:51:22 +02006865 if (l->lv_len < 1)
6866 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006867 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6868 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6869
6870 /* Dispose each item in join_ga. */
6871 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006872 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006873 p = (join_T *)join_ga.ga_data;
6874 for (i = 0; i < join_ga.ga_len; ++i)
6875 {
6876 vim_free(p->tofree);
6877 ++p;
6878 }
6879 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006880 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006881
6882 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006883}
6884
6885/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006886 * Return the next (unique) copy ID.
6887 * Used for serializing nested structures.
6888 */
6889 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006890get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006891{
6892 current_copyID += COPYID_INC;
6893 return current_copyID;
6894}
6895
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006896/* Used by get_func_tv() */
6897static garray_T funcargs = GA_EMPTY;
6898
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006899/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 * Garbage collection for lists and dictionaries.
6901 *
6902 * We use reference counts to be able to free most items right away when they
6903 * are no longer used. But for composite items it's possible that it becomes
6904 * unused while the reference count is > 0: When there is a recursive
6905 * reference. Example:
6906 * :let l = [1, 2, 3]
6907 * :let d = {9: l}
6908 * :let l[1] = d
6909 *
6910 * Since this is quite unusual we handle this with garbage collection: every
6911 * once in a while find out which lists and dicts are not referenced from any
6912 * variable.
6913 *
6914 * Here is a good reference text about garbage collection (refers to Python
6915 * but it applies to all reference-counting mechanisms):
6916 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006917 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006918
6919/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 * Do garbage collection for lists and dicts.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006921 * When "testing" is TRUE this is called from garbagecollect_for_testing().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006923 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006925garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006926{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006927 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 buf_T *buf;
6930 win_T *wp;
6931 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006932 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006933 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006934 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006935#ifdef FEAT_WINDOWS
6936 tabpage_T *tp;
6937#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006939 if (!testing)
6940 {
6941 /* Only do this once. */
6942 want_garbage_collect = FALSE;
6943 may_garbage_collect = FALSE;
6944 garbage_collect_at_exit = FALSE;
6945 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006946
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006947 /* We advance by two because we add one for items referenced through
6948 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006949 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006950
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951 /*
6952 * 1. Go through all accessible variables and mark all lists and dicts
6953 * with copyID.
6954 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955
6956 /* Don't free variables in the previous_funccal list unless they are only
6957 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006958 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6960 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006961 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6962 NULL);
6963 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6964 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 }
6966
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 /* script-local variables */
6968 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006969 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006970
6971 /* buffer-local variables */
6972 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006973 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6974 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975
6976 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006977 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006978 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6979 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006980#ifdef FEAT_AUTOCMD
6981 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006982 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6983 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006984#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006986#ifdef FEAT_WINDOWS
6987 /* tabpage-local variables */
6988 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006989 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6990 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006991#endif
6992
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006994 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995
6996 /* function-local variables */
6997 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6998 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006999 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7000 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 }
7002
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007003 /* function call arguments, if v:testing is set. */
7004 for (i = 0; i < funcargs.ga_len; ++i)
7005 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7006 copyID, NULL, NULL);
7007
Bram Moolenaard812df62008-11-09 12:46:09 +00007008 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007009 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007010
Bram Moolenaar1dced572012-04-05 16:54:08 +02007011#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007012 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007013#endif
7014
Bram Moolenaardb913952012-06-29 12:54:53 +02007015#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007016 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007017#endif
7018
7019#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007021#endif
7022
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007023#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007024 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007025#endif
7026
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007028 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 /*
7030 * 2. Free lists and dictionaries that are not referenced.
7031 */
7032 did_free = free_unref_items(copyID);
7033
7034 /*
7035 * 3. Check if any funccal can be freed now.
7036 */
7037 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007038 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 if (can_free_funccal(*pfc, copyID))
7040 {
7041 fc = *pfc;
7042 *pfc = fc->caller;
7043 free_funccal(fc, TRUE);
7044 did_free = TRUE;
7045 did_free_funccal = TRUE;
7046 }
7047 else
7048 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007049 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 if (did_free_funccal)
7051 /* When a funccal was freed some more items might be garbage
7052 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007053 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007054 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007055 else if (p_verbose > 0)
7056 {
7057 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7058 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007059
7060 return did_free;
7061}
7062
7063/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007064 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007065 */
7066 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007067free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007068{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007069 dict_T *dd, *dd_next;
7070 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007071 int did_free = FALSE;
7072
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007073 /* Let all "free" functions know that we are here. This means no
7074 * dictionaries, lists, channels or jobs are to be freed, because we will
7075 * do that here. */
7076 in_free_unref_items = TRUE;
7077
7078 /*
7079 * PASS 1: free the contents of the items. We don't free the items
7080 * themselves yet, so that it is possible to decrement refcount counters
7081 */
7082
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007083 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007084 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007086 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007087 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007088 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007089 /* Free the Dictionary and ordinary items it contains, but don't
7090 * recurse into Lists and Dictionaries, they will be in the list
7091 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007092 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095
7096 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007097 * Go through the list of lists and free items without the copyID.
7098 * But don't free a list that has a watcher (used in a for loop), these
7099 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007101 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7102 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7103 && ll->lv_watch == NULL)
7104 {
7105 /* Free the List and ordinary items it contains, but don't recurse
7106 * into Lists and Dictionaries, they will be in the list of dicts
7107 * or list of lists. */
7108 list_free_contents(ll);
7109 did_free = TRUE;
7110 }
7111
7112#ifdef FEAT_JOB_CHANNEL
7113 /* Go through the list of jobs and free items without the copyID. This
7114 * must happen before doing channels, because jobs refer to channels, but
7115 * the reference from the channel to the job isn't tracked. */
7116 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7117
7118 /* Go through the list of channels and free items without the copyID. */
7119 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7120#endif
7121
7122 /*
7123 * PASS 2: free the items themselves.
7124 */
7125 for (dd = first_dict; dd != NULL; dd = dd_next)
7126 {
7127 dd_next = dd->dv_used_next;
7128 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7129 dict_free_dict(dd);
7130 }
7131
7132 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007133 {
7134 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007135 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7136 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007137 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007138 /* Free the List and ordinary items it contains, but don't recurse
7139 * into Lists and Dictionaries, they will be in the list of dicts
7140 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007141 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007142 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007143 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007144
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007145#ifdef FEAT_JOB_CHANNEL
7146 /* Go through the list of jobs and free items without the copyID. This
7147 * must happen before doing channels, because jobs refer to channels, but
7148 * the reference from the channel to the job isn't tracked. */
7149 free_unused_jobs(copyID, COPYID_MASK);
7150
7151 /* Go through the list of channels and free items without the copyID. */
7152 free_unused_channels(copyID, COPYID_MASK);
7153#endif
7154
7155 in_free_unref_items = FALSE;
7156
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007157 return did_free;
7158}
7159
7160/*
7161 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007162 * "list_stack" is used to add lists to be marked. Can be NULL.
7163 *
7164 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007165 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007167set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007168{
7169 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007170 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007171 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007172 hashtab_T *cur_ht;
7173 ht_stack_T *ht_stack = NULL;
7174 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007175
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007176 cur_ht = ht;
7177 for (;;)
7178 {
7179 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007180 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007181 /* Mark each item in the hashtab. If the item contains a hashtab
7182 * it is added to ht_stack, if it contains a list it is added to
7183 * list_stack. */
7184 todo = (int)cur_ht->ht_used;
7185 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7186 if (!HASHITEM_EMPTY(hi))
7187 {
7188 --todo;
7189 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7190 &ht_stack, list_stack);
7191 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007192 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007193
7194 if (ht_stack == NULL)
7195 break;
7196
7197 /* take an item from the stack */
7198 cur_ht = ht_stack->ht;
7199 tempitem = ht_stack;
7200 ht_stack = ht_stack->prev;
7201 free(tempitem);
7202 }
7203
7204 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007205}
7206
7207/*
7208 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007209 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7210 *
7211 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007212 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007213 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007214set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007215{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007216 listitem_T *li;
7217 int abort = FALSE;
7218 list_T *cur_l;
7219 list_stack_T *list_stack = NULL;
7220 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007221
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007222 cur_l = l;
7223 for (;;)
7224 {
7225 if (!abort)
7226 /* Mark each item in the list. If the item contains a hashtab
7227 * it is added to ht_stack, if it contains a list it is added to
7228 * list_stack. */
7229 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7230 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7231 ht_stack, &list_stack);
7232 if (list_stack == NULL)
7233 break;
7234
7235 /* take an item from the stack */
7236 cur_l = list_stack->list;
7237 tempitem = list_stack;
7238 list_stack = list_stack->prev;
7239 free(tempitem);
7240 }
7241
7242 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007243}
7244
7245/*
7246 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007247 * "list_stack" is used to add lists to be marked. Can be NULL.
7248 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7249 *
7250 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007251 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007252 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007253set_ref_in_item(
7254 typval_T *tv,
7255 int copyID,
7256 ht_stack_T **ht_stack,
7257 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007258{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007259 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007260
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007261 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007262 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007263 dict_T *dd = tv->vval.v_dict;
7264
Bram Moolenaara03f2332016-02-06 18:09:59 +01007265 if (dd != NULL && dd->dv_copyID != copyID)
7266 {
7267 /* Didn't see this dict yet. */
7268 dd->dv_copyID = copyID;
7269 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007270 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007271 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7272 }
7273 else
7274 {
7275 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7276 if (newitem == NULL)
7277 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007278 else
7279 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007280 newitem->ht = &dd->dv_hashtab;
7281 newitem->prev = *ht_stack;
7282 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007283 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007284 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007285 }
7286 }
7287 else if (tv->v_type == VAR_LIST)
7288 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007289 list_T *ll = tv->vval.v_list;
7290
Bram Moolenaara03f2332016-02-06 18:09:59 +01007291 if (ll != NULL && ll->lv_copyID != copyID)
7292 {
7293 /* Didn't see this list yet. */
7294 ll->lv_copyID = copyID;
7295 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007296 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007297 abort = set_ref_in_list(ll, copyID, ht_stack);
7298 }
7299 else
7300 {
7301 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007302 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007303 if (newitem == NULL)
7304 abort = TRUE;
7305 else
7306 {
7307 newitem->list = ll;
7308 newitem->prev = *list_stack;
7309 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007310 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007311 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007312 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007313 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007314 else if (tv->v_type == VAR_PARTIAL)
7315 {
7316 partial_T *pt = tv->vval.v_partial;
7317 int i;
7318
7319 /* A partial does not have a copyID, because it cannot contain itself.
7320 */
7321 if (pt != NULL)
7322 {
7323 if (pt->pt_dict != NULL)
7324 {
7325 typval_T dtv;
7326
7327 dtv.v_type = VAR_DICT;
7328 dtv.vval.v_dict = pt->pt_dict;
7329 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7330 }
7331
7332 for (i = 0; i < pt->pt_argc; ++i)
7333 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7334 ht_stack, list_stack);
7335 }
7336 }
7337#ifdef FEAT_JOB_CHANNEL
7338 else if (tv->v_type == VAR_JOB)
7339 {
7340 job_T *job = tv->vval.v_job;
7341 typval_T dtv;
7342
7343 if (job != NULL && job->jv_copyID != copyID)
7344 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007345 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007346 if (job->jv_channel != NULL)
7347 {
7348 dtv.v_type = VAR_CHANNEL;
7349 dtv.vval.v_channel = job->jv_channel;
7350 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7351 }
7352 if (job->jv_exit_partial != NULL)
7353 {
7354 dtv.v_type = VAR_PARTIAL;
7355 dtv.vval.v_partial = job->jv_exit_partial;
7356 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7357 }
7358 }
7359 }
7360 else if (tv->v_type == VAR_CHANNEL)
7361 {
7362 channel_T *ch =tv->vval.v_channel;
7363 int part;
7364 typval_T dtv;
7365 jsonq_T *jq;
7366 cbq_T *cq;
7367
7368 if (ch != NULL && ch->ch_copyID != copyID)
7369 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007370 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007371 for (part = PART_SOCK; part <= PART_IN; ++part)
7372 {
7373 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7374 jq = jq->jq_next)
7375 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7376 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7377 cq = cq->cq_next)
7378 if (cq->cq_partial != NULL)
7379 {
7380 dtv.v_type = VAR_PARTIAL;
7381 dtv.vval.v_partial = cq->cq_partial;
7382 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7383 }
7384 if (ch->ch_part[part].ch_partial != NULL)
7385 {
7386 dtv.v_type = VAR_PARTIAL;
7387 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7388 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7389 }
7390 }
7391 if (ch->ch_partial != NULL)
7392 {
7393 dtv.v_type = VAR_PARTIAL;
7394 dtv.vval.v_partial = ch->ch_partial;
7395 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7396 }
7397 if (ch->ch_close_partial != NULL)
7398 {
7399 dtv.v_type = VAR_PARTIAL;
7400 dtv.vval.v_partial = ch->ch_close_partial;
7401 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7402 }
7403 }
7404 }
7405#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007406 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007407}
7408
7409/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 * Allocate an empty header for a dictionary.
7411 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007412 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007413dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414{
Bram Moolenaar33570922005-01-25 22:26:29 +00007415 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007416
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007418 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007419 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007420 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007421 if (first_dict != NULL)
7422 first_dict->dv_used_prev = d;
7423 d->dv_used_next = first_dict;
7424 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007425 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007426
Bram Moolenaar33570922005-01-25 22:26:29 +00007427 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007428 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007429 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007430 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007431 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007432 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007433 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434}
7435
7436/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007437 * Allocate an empty dict for a return value.
7438 * Returns OK or FAIL.
7439 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007440 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007441rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007442{
7443 dict_T *d = dict_alloc();
7444
7445 if (d == NULL)
7446 return FAIL;
7447
7448 rettv->vval.v_dict = d;
7449 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007450 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007451 ++d->dv_refcount;
7452 return OK;
7453}
7454
7455
7456/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457 * Unreference a Dictionary: decrement the reference count and free it when it
7458 * becomes zero.
7459 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007460 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007461dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007463 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007464 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007465}
7466
7467/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007468 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007469 * Ignores the reference count.
7470 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007471 static void
7472dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007474 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007475 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007476 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007478 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007479 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007480 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007483 if (!HASHITEM_EMPTY(hi))
7484 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007485 /* Remove the item before deleting it, just in case there is
7486 * something recursive causing trouble. */
7487 di = HI2DI(hi);
7488 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007489 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007490 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007491 --todo;
7492 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007494 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007495}
7496
7497 static void
7498dict_free_dict(dict_T *d)
7499{
7500 /* Remove the dict from the list of dicts for garbage collection. */
7501 if (d->dv_used_prev == NULL)
7502 first_dict = d->dv_used_next;
7503 else
7504 d->dv_used_prev->dv_used_next = d->dv_used_next;
7505 if (d->dv_used_next != NULL)
7506 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 vim_free(d);
7508}
7509
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007510 void
7511dict_free(dict_T *d)
7512{
7513 if (!in_free_unref_items)
7514 {
7515 dict_free_contents(d);
7516 dict_free_dict(d);
7517 }
7518}
7519
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520/*
7521 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007522 * The "key" is copied to the new item.
7523 * Note that the value of the item "di_tv" still needs to be initialized!
7524 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007526 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007527dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528{
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007530
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007531 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007533 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007534 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007535 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007536 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007537 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538}
7539
7540/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007541 * Make a copy of a Dictionary item.
7542 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007544dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007545{
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007547
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007548 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7549 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007550 if (di != NULL)
7551 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007553 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007554 copy_tv(&org->di_tv, &di->di_tv);
7555 }
7556 return di;
7557}
7558
7559/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 * Remove item "item" from Dictionary "dict" and free it.
7561 */
7562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007563dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007564{
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007568 if (HASHITEM_EMPTY(hi))
7569 EMSG2(_(e_intern2), "dictitem_remove()");
7570 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 dictitem_free(item);
7573}
7574
7575/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 * Free a dict item. Also clears the value.
7577 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007579dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007582 if (item->di_flags & DI_FLAGS_ALLOC)
7583 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584}
7585
7586/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007587 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7588 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007589 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007590 * Returns NULL when out of memory.
7591 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007592 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007593dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007594{
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 dict_T *copy;
7596 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007597 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007599
7600 if (orig == NULL)
7601 return NULL;
7602
7603 copy = dict_alloc();
7604 if (copy != NULL)
7605 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007606 if (copyID != 0)
7607 {
7608 orig->dv_copyID = copyID;
7609 orig->dv_copydict = copy;
7610 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007611 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007612 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007613 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007616 --todo;
7617
7618 di = dictitem_alloc(hi->hi_key);
7619 if (di == NULL)
7620 break;
7621 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007622 {
7623 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7624 copyID) == FAIL)
7625 {
7626 vim_free(di);
7627 break;
7628 }
7629 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 else
7631 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7632 if (dict_add(copy, di) == FAIL)
7633 {
7634 dictitem_free(di);
7635 break;
7636 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007639
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007641 if (todo > 0)
7642 {
7643 dict_unref(copy);
7644 copy = NULL;
7645 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007646 }
7647
7648 return copy;
7649}
7650
7651/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007653 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007656dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657{
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659}
7660
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007662 * Add a number or string entry to dictionary "d".
7663 * When "str" is NULL use number "nr", otherwise use "str".
7664 * Returns FAIL when out of memory and when key already exists.
7665 */
7666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007667dict_add_nr_str(
7668 dict_T *d,
7669 char *key,
7670 long nr,
7671 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007672{
7673 dictitem_T *item;
7674
7675 item = dictitem_alloc((char_u *)key);
7676 if (item == NULL)
7677 return FAIL;
7678 item->di_tv.v_lock = 0;
7679 if (str == NULL)
7680 {
7681 item->di_tv.v_type = VAR_NUMBER;
7682 item->di_tv.vval.v_number = nr;
7683 }
7684 else
7685 {
7686 item->di_tv.v_type = VAR_STRING;
7687 item->di_tv.vval.v_string = vim_strsave(str);
7688 }
7689 if (dict_add(d, item) == FAIL)
7690 {
7691 dictitem_free(item);
7692 return FAIL;
7693 }
7694 return OK;
7695}
7696
7697/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007698 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007699 * Returns FAIL when out of memory and when key already exists.
7700 */
7701 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007702dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007703{
7704 dictitem_T *item;
7705
7706 item = dictitem_alloc((char_u *)key);
7707 if (item == NULL)
7708 return FAIL;
7709 item->di_tv.v_lock = 0;
7710 item->di_tv.v_type = VAR_LIST;
7711 item->di_tv.vval.v_list = list;
7712 if (dict_add(d, item) == FAIL)
7713 {
7714 dictitem_free(item);
7715 return FAIL;
7716 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007717 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007718 return OK;
7719}
7720
7721/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 * Get the number of items in a Dictionary.
7723 */
7724 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007725dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007726{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007727 if (d == NULL)
7728 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007729 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007730}
7731
7732/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 * Find item "key[len]" in Dictionary "d".
7734 * If "len" is negative use strlen(key).
7735 * Returns NULL when not found.
7736 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007737 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007738dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007740#define AKEYLEN 200
7741 char_u buf[AKEYLEN];
7742 char_u *akey;
7743 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007744 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007746 if (len < 0)
7747 akey = key;
7748 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007749 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007750 tofree = akey = vim_strnsave(key, len);
7751 if (akey == NULL)
7752 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007753 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007754 else
7755 {
7756 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007757 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007758 akey = buf;
7759 }
7760
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007762 vim_free(tofree);
7763 if (HASHITEM_EMPTY(hi))
7764 return NULL;
7765 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766}
7767
7768/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007769 * Get a string item from a dictionary.
7770 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007771 * Returns NULL if the entry doesn't exist or out of memory.
7772 */
7773 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007774get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007775{
7776 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007777 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007778
7779 di = dict_find(d, key, -1);
7780 if (di == NULL)
7781 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007782 s = get_tv_string(&di->di_tv);
7783 if (save && s != NULL)
7784 s = vim_strsave(s);
7785 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007786}
7787
7788/*
7789 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007790 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007791 */
7792 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007793get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007794{
7795 dictitem_T *di;
7796
7797 di = dict_find(d, key, -1);
7798 if (di == NULL)
7799 return 0;
7800 return get_tv_number(&di->di_tv);
7801}
7802
7803/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007804 * Return an allocated string with the string representation of a Dictionary.
7805 * May return NULL.
7806 */
7807 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007808dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007809{
7810 garray_T ga;
7811 int first = TRUE;
7812 char_u *tofree;
7813 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007814 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007815 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007816 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007817 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007818
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007819 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007820 return NULL;
7821 ga_init2(&ga, (int)sizeof(char), 80);
7822 ga_append(&ga, '{');
7823
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007824 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007825 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007826 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007827 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007828 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007829 --todo;
7830
7831 if (first)
7832 first = FALSE;
7833 else
7834 ga_concat(&ga, (char_u *)", ");
7835
7836 tofree = string_quote(hi->hi_key, FALSE);
7837 if (tofree != NULL)
7838 {
7839 ga_concat(&ga, tofree);
7840 vim_free(tofree);
7841 }
7842 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007843 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007844 if (s != NULL)
7845 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007846 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007847 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007848 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007849 line_breakcheck();
7850
Bram Moolenaar8c711452005-01-14 21:53:12 +00007851 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007852 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007853 if (todo > 0)
7854 {
7855 vim_free(ga.ga_data);
7856 return NULL;
7857 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007858
7859 ga_append(&ga, '}');
7860 ga_append(&ga, NUL);
7861 return (char_u *)ga.ga_data;
7862}
7863
7864/*
7865 * Allocate a variable for a Dictionary and fill it from "*arg".
7866 * Return OK or FAIL. Returns NOTDONE for {expr}.
7867 */
7868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007869get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007870{
Bram Moolenaar33570922005-01-25 22:26:29 +00007871 dict_T *d = NULL;
7872 typval_T tvkey;
7873 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007874 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007876 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007877 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007878
7879 /*
7880 * First check if it's not a curly-braces thing: {expr}.
7881 * Must do this without evaluating, otherwise a function may be called
7882 * twice. Unfortunately this means we need to call eval1() twice for the
7883 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007884 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007885 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007886 if (*start != '}')
7887 {
7888 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7889 return FAIL;
7890 if (*start == '}')
7891 return NOTDONE;
7892 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007893
7894 if (evaluate)
7895 {
7896 d = dict_alloc();
7897 if (d == NULL)
7898 return FAIL;
7899 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007900 tvkey.v_type = VAR_UNKNOWN;
7901 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007902
7903 *arg = skipwhite(*arg + 1);
7904 while (**arg != '}' && **arg != NUL)
7905 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007906 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007907 goto failret;
7908 if (**arg != ':')
7909 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007910 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007911 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007912 goto failret;
7913 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007914 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007915 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007916 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007917 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007918 {
7919 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007920 clear_tv(&tvkey);
7921 goto failret;
7922 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007923 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007924
7925 *arg = skipwhite(*arg + 1);
7926 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7927 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007928 if (evaluate)
7929 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007930 goto failret;
7931 }
7932 if (evaluate)
7933 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007934 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 if (item != NULL)
7936 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007937 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007938 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007939 clear_tv(&tv);
7940 goto failret;
7941 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007942 item = dictitem_alloc(key);
7943 clear_tv(&tvkey);
7944 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007947 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007948 if (dict_add(d, item) == FAIL)
7949 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 }
7951 }
7952
7953 if (**arg == '}')
7954 break;
7955 if (**arg != ',')
7956 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007957 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007958 goto failret;
7959 }
7960 *arg = skipwhite(*arg + 1);
7961 }
7962
7963 if (**arg != '}')
7964 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007965 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007966failret:
7967 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007968 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007969 return FAIL;
7970 }
7971
7972 *arg = skipwhite(*arg + 1);
7973 if (evaluate)
7974 {
7975 rettv->v_type = VAR_DICT;
7976 rettv->vval.v_dict = d;
7977 ++d->dv_refcount;
7978 }
7979
7980 return OK;
7981}
7982
Bram Moolenaar17a13432016-01-24 14:22:10 +01007983 static char *
7984get_var_special_name(int nr)
7985{
7986 switch (nr)
7987 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007988 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007989 case VVAL_TRUE: return "v:true";
7990 case VVAL_NONE: return "v:none";
7991 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007992 }
7993 EMSG2(_(e_intern2), "get_var_special_name()");
7994 return "42";
7995}
7996
Bram Moolenaar8c711452005-01-14 21:53:12 +00007997/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007998 * Return a string with the string representation of a variable.
7999 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008000 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008001 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008002 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008003 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008004 */
8005 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008006echo_string(
8007 typval_T *tv,
8008 char_u **tofree,
8009 char_u *numbuf,
8010 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008011{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008012 static int recurse = 0;
8013 char_u *r = NULL;
8014
Bram Moolenaar33570922005-01-25 22:26:29 +00008015 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008017 if (!did_echo_string_emsg)
8018 {
8019 /* Only give this message once for a recursive call to avoid
8020 * flooding the user with errors. And stop iterating over lists
8021 * and dicts. */
8022 did_echo_string_emsg = TRUE;
8023 EMSG(_("E724: variable nested too deep for displaying"));
8024 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008025 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008026 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008027 }
8028 ++recurse;
8029
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008030 switch (tv->v_type)
8031 {
8032 case VAR_FUNC:
8033 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008034 r = tv->vval.v_string;
8035 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008036
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008037 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008038 {
8039 partial_T *pt = tv->vval.v_partial;
8040 char_u *fname = string_quote(pt == NULL ? NULL
8041 : pt->pt_name, FALSE);
8042 garray_T ga;
8043 int i;
8044 char_u *tf;
8045
8046 ga_init2(&ga, 1, 100);
8047 ga_concat(&ga, (char_u *)"function(");
8048 if (fname != NULL)
8049 {
8050 ga_concat(&ga, fname);
8051 vim_free(fname);
8052 }
8053 if (pt != NULL && pt->pt_argc > 0)
8054 {
8055 ga_concat(&ga, (char_u *)", [");
8056 for (i = 0; i < pt->pt_argc; ++i)
8057 {
8058 if (i > 0)
8059 ga_concat(&ga, (char_u *)", ");
8060 ga_concat(&ga,
8061 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8062 vim_free(tf);
8063 }
8064 ga_concat(&ga, (char_u *)"]");
8065 }
8066 if (pt != NULL && pt->pt_dict != NULL)
8067 {
8068 typval_T dtv;
8069
8070 ga_concat(&ga, (char_u *)", ");
8071 dtv.v_type = VAR_DICT;
8072 dtv.vval.v_dict = pt->pt_dict;
8073 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8074 vim_free(tf);
8075 }
8076 ga_concat(&ga, (char_u *)")");
8077
8078 *tofree = ga.ga_data;
8079 r = *tofree;
8080 break;
8081 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008082
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008083 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008084 if (tv->vval.v_list == NULL)
8085 {
8086 *tofree = NULL;
8087 r = NULL;
8088 }
8089 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8090 {
8091 *tofree = NULL;
8092 r = (char_u *)"[...]";
8093 }
8094 else
8095 {
8096 tv->vval.v_list->lv_copyID = copyID;
8097 *tofree = list2string(tv, copyID);
8098 r = *tofree;
8099 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008100 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008101
Bram Moolenaar8c711452005-01-14 21:53:12 +00008102 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008103 if (tv->vval.v_dict == NULL)
8104 {
8105 *tofree = NULL;
8106 r = NULL;
8107 }
8108 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8109 {
8110 *tofree = NULL;
8111 r = (char_u *)"{...}";
8112 }
8113 else
8114 {
8115 tv->vval.v_dict->dv_copyID = copyID;
8116 *tofree = dict2string(tv, copyID);
8117 r = *tofree;
8118 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008119 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008120
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008121 case VAR_STRING:
8122 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008123 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008124 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008125 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008126 *tofree = NULL;
8127 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008128 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008129
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008130 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008131#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008132 *tofree = NULL;
8133 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8134 r = numbuf;
8135 break;
8136#endif
8137
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008138 case VAR_SPECIAL:
8139 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008140 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008141 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008142 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008143
Bram Moolenaar8502c702014-06-17 12:51:16 +02008144 if (--recurse == 0)
8145 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008146 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008147}
8148
8149/*
8150 * Return a string with the string representation of a variable.
8151 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8152 * "numbuf" is used for a number.
8153 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008154 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008155 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008156 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008157tv2string(
8158 typval_T *tv,
8159 char_u **tofree,
8160 char_u *numbuf,
8161 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008162{
8163 switch (tv->v_type)
8164 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008165 case VAR_FUNC:
8166 *tofree = string_quote(tv->vval.v_string, TRUE);
8167 return *tofree;
8168 case VAR_STRING:
8169 *tofree = string_quote(tv->vval.v_string, FALSE);
8170 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008171 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008172#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008173 *tofree = NULL;
8174 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8175 return numbuf;
8176#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008177 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008178 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008179 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008180 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008181 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008182 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008183 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008184 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008185 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008186 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008187 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008188}
8189
8190/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008191 * Return string "str" in ' quotes, doubling ' characters.
8192 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008193 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008194 */
8195 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008196string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008197{
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008199 char_u *p, *r, *s;
8200
Bram Moolenaar33570922005-01-25 22:26:29 +00008201 len = (function ? 13 : 3);
8202 if (str != NULL)
8203 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008204 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008205 for (p = str; *p != NUL; mb_ptr_adv(p))
8206 if (*p == '\'')
8207 ++len;
8208 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008209 s = r = alloc(len);
8210 if (r != NULL)
8211 {
8212 if (function)
8213 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008214 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008215 r += 10;
8216 }
8217 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008218 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008219 if (str != NULL)
8220 for (p = str; *p != NUL; )
8221 {
8222 if (*p == '\'')
8223 *r++ = '\'';
8224 MB_COPY_CHAR(p, r);
8225 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008226 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008227 if (function)
8228 *r++ = ')';
8229 *r++ = NUL;
8230 }
8231 return s;
8232}
8233
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008234#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008235/*
8236 * Convert the string "text" to a floating point number.
8237 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8238 * this always uses a decimal point.
8239 * Returns the length of the text that was consumed.
8240 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008241 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008242string2float(
8243 char_u *text,
8244 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008245{
8246 char *s = (char *)text;
8247 float_T f;
8248
8249 f = strtod(s, &s);
8250 *value = f;
8251 return (int)((char_u *)s - text);
8252}
8253#endif
8254
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 * Get the value of an environment variable.
8257 * "arg" is pointing to the '$'. It is advanced to after the name.
8258 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008259 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 */
8261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008262get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263{
8264 char_u *string = NULL;
8265 int len;
8266 int cc;
8267 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008268 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269
8270 ++*arg;
8271 name = *arg;
8272 len = get_env_len(arg);
8273 if (evaluate)
8274 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008275 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008276 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008277
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008278 cc = name[len];
8279 name[len] = NUL;
8280 /* first try vim_getenv(), fast for normal environment vars */
8281 string = vim_getenv(name, &mustfree);
8282 if (string != NULL && *string != NUL)
8283 {
8284 if (!mustfree)
8285 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008287 else
8288 {
8289 if (mustfree)
8290 vim_free(string);
8291
8292 /* next try expanding things like $VIM and ${HOME} */
8293 string = expand_env_save(name - 1);
8294 if (string != NULL && *string == '$')
8295 {
8296 vim_free(string);
8297 string = NULL;
8298 }
8299 }
8300 name[len] = cc;
8301
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008302 rettv->v_type = VAR_STRING;
8303 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 }
8305
8306 return OK;
8307}
8308
8309/*
8310 * Array with names and number of arguments of all internal functions
8311 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8312 */
8313static struct fst
8314{
8315 char *f_name; /* function name */
8316 char f_min_argc; /* minimal number of arguments */
8317 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008318 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008319 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320} functions[] =
8321{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008322#ifdef FEAT_FLOAT
8323 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008324 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008325#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008326 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008327 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008328 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {"append", 2, 2, f_append},
8330 {"argc", 0, 0, f_argc},
8331 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008332 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008333 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008334#ifdef FEAT_FLOAT
8335 {"asin", 1, 1, f_asin}, /* WJMc */
8336#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008337 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008338 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008339 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008340 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008341 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008342 {"assert_notequal", 2, 3, f_assert_notequal},
8343 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008344 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008345#ifdef FEAT_FLOAT
8346 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008347 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008350 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {"bufexists", 1, 1, f_bufexists},
8352 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8353 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8354 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8355 {"buflisted", 1, 1, f_buflisted},
8356 {"bufloaded", 1, 1, f_bufloaded},
8357 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008358 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {"bufwinnr", 1, 1, f_bufwinnr},
8360 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008361 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008362 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008363 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008364#ifdef FEAT_FLOAT
8365 {"ceil", 1, 1, f_ceil},
8366#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008367#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008368 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008369 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8370 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008371 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008372 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008373 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008374 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008375 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008376 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008377 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008378 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008379 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8380 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008381 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008382 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008383#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008384 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008385 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008387 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008389#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008390 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008391 {"complete_add", 1, 1, f_complete_add},
8392 {"complete_check", 0, 0, f_complete_check},
8393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008395 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008396#ifdef FEAT_FLOAT
8397 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008398 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008399#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008400 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008402 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008403 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008404 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008406 {"diff_filler", 1, 1, f_diff_filler},
8407 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008408 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008409 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008411 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {"eventhandler", 0, 0, f_eventhandler},
8413 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008414 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008416#ifdef FEAT_FLOAT
8417 {"exp", 1, 1, f_exp},
8418#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008419 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008420 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008421 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8423 {"filereadable", 1, 1, f_filereadable},
8424 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008425 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008426 {"finddir", 1, 3, f_finddir},
8427 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008428#ifdef FEAT_FLOAT
8429 {"float2nr", 1, 1, f_float2nr},
8430 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008431 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008432#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008433 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 {"fnamemodify", 2, 2, f_fnamemodify},
8435 {"foldclosed", 1, 1, f_foldclosed},
8436 {"foldclosedend", 1, 1, f_foldclosedend},
8437 {"foldlevel", 1, 1, f_foldlevel},
8438 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008439 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008441 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008442 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008443 {"garbagecollect_for_testing", 0, 0, f_garbagecollect_for_testing},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008444 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008445 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008446 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {"getchar", 0, 1, f_getchar},
8448 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008449 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {"getcmdline", 0, 0, f_getcmdline},
8451 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008452 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008453 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008454 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008455 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008456 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008457 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {"getfsize", 1, 1, f_getfsize},
8459 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008460 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008461 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008462 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008463 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008464 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008465 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008466 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008467 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008469 {"gettabvar", 2, 3, f_gettabvar},
8470 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {"getwinposx", 0, 0, f_getwinposx},
8472 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008473 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008474 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008475 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008476 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008478 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008479 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008480 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8482 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8483 {"histadd", 2, 2, f_histadd},
8484 {"histdel", 1, 2, f_histdel},
8485 {"histget", 1, 2, f_histget},
8486 {"histnr", 1, 1, f_histnr},
8487 {"hlID", 1, 1, f_hlID},
8488 {"hlexists", 1, 1, f_hlexists},
8489 {"hostname", 0, 0, f_hostname},
8490 {"iconv", 3, 3, f_iconv},
8491 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008492 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008493 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008495 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {"inputrestore", 0, 0, f_inputrestore},
8497 {"inputsave", 0, 0, f_inputsave},
8498 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008500 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008502 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008503#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8504 {"isnan", 1, 1, f_isnan},
8505#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008506 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008507#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008508 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008509 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008510 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008511 {"job_start", 1, 2, f_job_start},
8512 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008513 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008514#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008515 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008516 {"js_decode", 1, 1, f_js_decode},
8517 {"js_encode", 1, 1, f_js_encode},
8518 {"json_decode", 1, 1, f_json_decode},
8519 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008520 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {"libcall", 3, 3, f_libcall},
8524 {"libcallnr", 3, 3, f_libcallnr},
8525 {"line", 1, 1, f_line},
8526 {"line2byte", 1, 1, f_line2byte},
8527 {"lispindent", 1, 1, f_lispindent},
8528 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008529#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008530 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008531 {"log10", 1, 1, f_log10},
8532#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008533#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008534 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008535#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008536 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008537 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008538 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008539 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008540 {"matchadd", 2, 5, f_matchadd},
8541 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008542 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008543 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008544 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008545 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008546 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008547 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008548 {"max", 1, 1, f_max},
8549 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008550#ifdef vim_mkdir
8551 {"mkdir", 1, 3, f_mkdir},
8552#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008553 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008554#ifdef FEAT_MZSCHEME
8555 {"mzeval", 1, 1, f_mzeval},
8556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008558 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008559 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008560 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008561#ifdef FEAT_PERL
8562 {"perleval", 1, 1, f_perleval},
8563#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008564#ifdef FEAT_FLOAT
8565 {"pow", 2, 2, f_pow},
8566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008568 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008569 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008570#ifdef FEAT_PYTHON3
8571 {"py3eval", 1, 1, f_py3eval},
8572#endif
8573#ifdef FEAT_PYTHON
8574 {"pyeval", 1, 1, f_pyeval},
8575#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008576 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008577 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008578 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008579#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008580 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008581#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008582 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 {"remote_expr", 2, 3, f_remote_expr},
8584 {"remote_foreground", 1, 1, f_remote_foreground},
8585 {"remote_peek", 1, 2, f_remote_peek},
8586 {"remote_read", 1, 1, f_remote_read},
8587 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008588 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008590 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008592 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008593#ifdef FEAT_FLOAT
8594 {"round", 1, 1, f_round},
8595#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008596 {"screenattr", 2, 2, f_screenattr},
8597 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008598 {"screencol", 0, 0, f_screencol},
8599 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008600 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008601 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008602 {"searchpair", 3, 7, f_searchpair},
8603 {"searchpairpos", 3, 7, f_searchpairpos},
8604 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 {"server2client", 2, 2, f_server2client},
8606 {"serverlist", 0, 0, f_serverlist},
8607 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008608 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008610 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008612 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008613 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008614 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008615 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008617 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008618 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008620#ifdef FEAT_CRYPT
8621 {"sha256", 1, 1, f_sha256},
8622#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008623 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008624 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008626#ifdef FEAT_FLOAT
8627 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008628 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008629#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008630 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008631 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008632 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008633 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008634 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008635#ifdef FEAT_FLOAT
8636 {"sqrt", 1, 1, f_sqrt},
8637 {"str2float", 1, 1, f_str2float},
8638#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008639 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008640 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008641 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008642 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#ifdef HAVE_STRFTIME
8644 {"strftime", 1, 2, f_strftime},
8645#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008646 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008648 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 {"strlen", 1, 1, f_strlen},
8650 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008651 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008653 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008654 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 {"substitute", 4, 4, f_substitute},
8656 {"synID", 3, 3, f_synID},
8657 {"synIDattr", 2, 3, f_synIDattr},
8658 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008659 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008660 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008661 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008662 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008663 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008664 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008665 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008666 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008667 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008668#ifdef FEAT_FLOAT
8669 {"tan", 1, 1, f_tan},
8670 {"tanh", 1, 1, f_tanh},
8671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008673 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008674#ifdef FEAT_TIMERS
8675 {"timer_start", 2, 3, f_timer_start},
8676 {"timer_stop", 1, 1, f_timer_stop},
8677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {"tolower", 1, 1, f_tolower},
8679 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008680 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008681#ifdef FEAT_FLOAT
8682 {"trunc", 1, 1, f_trunc},
8683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008685 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008686 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008687 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008688 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 {"virtcol", 1, 1, f_virtcol},
8690 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008691 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008692 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008693 {"win_getid", 0, 2, f_win_getid},
8694 {"win_gotoid", 1, 1, f_win_gotoid},
8695 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8696 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {"winbufnr", 1, 1, f_winbufnr},
8698 {"wincol", 0, 0, f_wincol},
8699 {"winheight", 1, 1, f_winheight},
8700 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008701 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008703 {"winrestview", 1, 1, f_winrestview},
8704 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008706 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008707 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008708 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709};
8710
8711#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8712
8713/*
8714 * Function given to ExpandGeneric() to obtain the list of internal
8715 * or user defined function names.
8716 */
8717 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008718get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719{
8720 static int intidx = -1;
8721 char_u *name;
8722
8723 if (idx == 0)
8724 intidx = -1;
8725 if (intidx < 0)
8726 {
8727 name = get_user_func_name(xp, idx);
8728 if (name != NULL)
8729 return name;
8730 }
8731 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8732 {
8733 STRCPY(IObuff, functions[intidx].f_name);
8734 STRCAT(IObuff, "(");
8735 if (functions[intidx].f_max_argc == 0)
8736 STRCAT(IObuff, ")");
8737 return IObuff;
8738 }
8739
8740 return NULL;
8741}
8742
8743/*
8744 * Function given to ExpandGeneric() to obtain the list of internal or
8745 * user defined variable or function names.
8746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008748get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
8750 static int intidx = -1;
8751 char_u *name;
8752
8753 if (idx == 0)
8754 intidx = -1;
8755 if (intidx < 0)
8756 {
8757 name = get_function_name(xp, idx);
8758 if (name != NULL)
8759 return name;
8760 }
8761 return get_user_var_name(xp, ++intidx);
8762}
8763
8764#endif /* FEAT_CMDL_COMPL */
8765
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008766#if defined(EBCDIC) || defined(PROTO)
8767/*
8768 * Compare struct fst by function name.
8769 */
8770 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008771compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008772{
8773 struct fst *p1 = (struct fst *)s1;
8774 struct fst *p2 = (struct fst *)s2;
8775
8776 return STRCMP(p1->f_name, p2->f_name);
8777}
8778
8779/*
8780 * Sort the function table by function name.
8781 * The sorting of the table above is ASCII dependant.
8782 * On machines using EBCDIC we have to sort it.
8783 */
8784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008785sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008786{
8787 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8788
8789 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8790}
8791#endif
8792
8793
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794/*
8795 * Find internal function in table above.
8796 * Return index, or -1 if not found
8797 */
8798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008799find_internal_func(
8800 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
8802 int first = 0;
8803 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8804 int cmp;
8805 int x;
8806
8807 /*
8808 * Find the function name in the table. Binary search.
8809 */
8810 while (first <= last)
8811 {
8812 x = first + ((unsigned)(last - first) >> 1);
8813 cmp = STRCMP(name, functions[x].f_name);
8814 if (cmp < 0)
8815 last = x - 1;
8816 else if (cmp > 0)
8817 first = x + 1;
8818 else
8819 return x;
8820 }
8821 return -1;
8822}
8823
8824/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008825 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8826 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008827 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8828 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008829 */
8830 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008831deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008832{
Bram Moolenaar33570922005-01-25 22:26:29 +00008833 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008834 int cc;
8835
Bram Moolenaar65639032016-03-16 21:40:30 +01008836 if (partialp != NULL)
8837 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008838
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008839 cc = name[*lenp];
8840 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008841 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008842 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008843 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008844 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008846 {
8847 *lenp = 0;
8848 return (char_u *)""; /* just in case */
8849 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008850 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008851 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008852 }
8853
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008854 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8855 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008856 partial_T *pt = v->di_tv.vval.v_partial;
8857
8858 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008859 {
8860 *lenp = 0;
8861 return (char_u *)""; /* just in case */
8862 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008863 if (partialp != NULL)
8864 *partialp = pt;
8865 *lenp = (int)STRLEN(pt->pt_name);
8866 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008867 }
8868
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008869 return name;
8870}
8871
8872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 * Allocate a variable for the result of a function.
8874 * Return OK or FAIL.
8875 */
8876 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008877get_func_tv(
8878 char_u *name, /* name of the function */
8879 int len, /* length of "name" */
8880 typval_T *rettv,
8881 char_u **arg, /* argument, pointing to the '(' */
8882 linenr_T firstline, /* first line of range */
8883 linenr_T lastline, /* last line of range */
8884 int *doesrange, /* return: function handled range */
8885 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008886 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008887 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 char_u *argp;
8890 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008891 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 int argcount = 0; /* number of arguments found */
8893
8894 /*
8895 * Get the arguments.
8896 */
8897 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008898 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 {
8900 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8901 if (*argp == ')' || *argp == ',' || *argp == NUL)
8902 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8904 {
8905 ret = FAIL;
8906 break;
8907 }
8908 ++argcount;
8909 if (*argp != ',')
8910 break;
8911 }
8912 if (*argp == ')')
8913 ++argp;
8914 else
8915 ret = FAIL;
8916
8917 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008918 {
8919 int i = 0;
8920
8921 if (get_vim_var_nr(VV_TESTING))
8922 {
8923 /* Prepare for calling garbagecollect_for_testing(), need to know
8924 * what variables are used on the call stack. */
8925 if (funcargs.ga_itemsize == 0)
8926 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8927 for (i = 0; i < argcount; ++i)
8928 if (ga_grow(&funcargs, 1) == OK)
8929 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8930 &argvars[i];
8931 }
8932
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008934 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008935
8936 funcargs.ga_len -= i;
8937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008939 {
8940 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008941 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008942 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008943 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
8946 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
8949 *arg = skipwhite(argp);
8950 return ret;
8951}
8952
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008953#define ERROR_UNKNOWN 0
8954#define ERROR_TOOMANY 1
8955#define ERROR_TOOFEW 2
8956#define ERROR_SCRIPT 3
8957#define ERROR_DICT 4
8958#define ERROR_NONE 5
8959#define ERROR_OTHER 6
8960#define FLEN_FIXED 40
8961
8962/*
8963 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8964 * Change <SNR>123_name() to K_SNR 123_name().
8965 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8966 * (slow).
8967 */
8968 static char_u *
8969fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8970{
8971 int llen;
8972 char_u *fname;
8973 int i;
8974
8975 llen = eval_fname_script(name);
8976 if (llen > 0)
8977 {
8978 fname_buf[0] = K_SPECIAL;
8979 fname_buf[1] = KS_EXTRA;
8980 fname_buf[2] = (int)KE_SNR;
8981 i = 3;
8982 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8983 {
8984 if (current_SID <= 0)
8985 *error = ERROR_SCRIPT;
8986 else
8987 {
8988 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8989 i = (int)STRLEN(fname_buf);
8990 }
8991 }
8992 if (i + STRLEN(name + llen) < FLEN_FIXED)
8993 {
8994 STRCPY(fname_buf + i, name + llen);
8995 fname = fname_buf;
8996 }
8997 else
8998 {
8999 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9000 if (fname == NULL)
9001 *error = ERROR_OTHER;
9002 else
9003 {
9004 *tofree = fname;
9005 mch_memmove(fname, fname_buf, (size_t)i);
9006 STRCPY(fname + i, name + llen);
9007 }
9008 }
9009 }
9010 else
9011 fname = name;
9012 return fname;
9013}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014
9015/*
9016 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009017 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009018 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009020 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009021call_func(
9022 char_u *funcname, /* name of the function */
9023 int len, /* length of "name" */
9024 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009025 int argcount_in, /* number of "argvars" */
9026 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009027 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009028 linenr_T firstline, /* first line of range */
9029 linenr_T lastline, /* last line of range */
9030 int *doesrange, /* return: function handled range */
9031 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009032 partial_T *partial, /* optional, can be NULL */
9033 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034{
9035 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 int error = ERROR_NONE;
9037 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009040 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009042 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009043 int argcount = argcount_in;
9044 typval_T *argvars = argvars_in;
9045 dict_T *selfdict = selfdict_in;
9046 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9047 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009048
9049 /* Make a copy of the name, if it comes from a funcref variable it could
9050 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009051 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009052 if (name == NULL)
9053 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009055 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056
9057 *doesrange = FALSE;
9058
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009059 if (partial != NULL)
9060 {
9061 if (partial->pt_dict != NULL)
9062 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009063 /* When the function has a partial with a dict and there is a dict
9064 * argument, use the dict argument. That is backwards compatible.
9065 */
9066 if (selfdict_in == NULL)
9067 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009068 }
9069 if (error == ERROR_NONE && partial->pt_argc > 0)
9070 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009071 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9072 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9073 for (i = 0; i < argcount_in; ++i)
9074 argv[i + argv_clear] = argvars_in[i];
9075 argvars = argv;
9076 argcount = partial->pt_argc + argcount_in;
9077 }
9078 }
9079
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080
9081 /* execute the function if no errors detected and executing */
9082 if (evaluate && error == ERROR_NONE)
9083 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009084 char_u *rfname = fname;
9085
9086 /* Ignore "g:" before a function name. */
9087 if (fname[0] == 'g' && fname[1] == ':')
9088 rfname = fname + 2;
9089
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009090 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9091 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 error = ERROR_UNKNOWN;
9093
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009094 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 {
9096 /*
9097 * User defined function.
9098 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009099 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009100
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009102 /* Trigger FuncUndefined event, may load the function. */
9103 if (fp == NULL
9104 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009105 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009106 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009108 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009109 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 }
9111#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009112 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009113 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009114 {
9115 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009116 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009117 }
9118
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 if (fp != NULL)
9120 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009121 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009123 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009125 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009127 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009128 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 else
9130 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009131 int did_save_redo = FALSE;
9132
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 /*
9134 * Call the user function.
9135 * Save and restore search patterns, script variables and
9136 * redo buffer.
9137 */
9138 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009139#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009140 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009141#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009142 {
9143 saveRedobuff();
9144 did_save_redo = TRUE;
9145 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009146 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009147 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009148 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009149 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9150 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9151 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009152 /* Function was unreferenced while being used, free it
9153 * now. */
9154 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009155 if (did_save_redo)
9156 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 restore_search_patterns();
9158 error = ERROR_NONE;
9159 }
9160 }
9161 }
9162 else
9163 {
9164 /*
9165 * Find the function name in the table, call its implementation.
9166 */
9167 i = find_internal_func(fname);
9168 if (i >= 0)
9169 {
9170 if (argcount < functions[i].f_min_argc)
9171 error = ERROR_TOOFEW;
9172 else if (argcount > functions[i].f_max_argc)
9173 error = ERROR_TOOMANY;
9174 else
9175 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009176 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009177 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 error = ERROR_NONE;
9179 }
9180 }
9181 }
9182 /*
9183 * The function call (or "FuncUndefined" autocommand sequence) might
9184 * have been aborted by an error, an interrupt, or an explicitly thrown
9185 * exception that has not been caught so far. This situation can be
9186 * tested for by calling aborting(). For an error in an internal
9187 * function or for the "E132" error in call_user_func(), however, the
9188 * throw point at which the "force_abort" flag (temporarily reset by
9189 * emsg()) is normally updated has not been reached yet. We need to
9190 * update that flag first to make aborting() reliable.
9191 */
9192 update_force_abort();
9193 }
9194 if (error == ERROR_NONE)
9195 ret = OK;
9196
9197 /*
9198 * Report an error unless the argument evaluation or function call has been
9199 * cancelled due to an aborting error, an interrupt, or an exception.
9200 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009201 if (!aborting())
9202 {
9203 switch (error)
9204 {
9205 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009206 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009207 break;
9208 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009209 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009210 break;
9211 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009212 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009213 name);
9214 break;
9215 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009216 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009217 name);
9218 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009219 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009220 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009221 name);
9222 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009223 }
9224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009226 while (argv_clear > 0)
9227 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009228 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009229 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
9231 return ret;
9232}
9233
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009234/*
9235 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009236 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009237 */
9238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009239emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009240{
9241 char_u *p;
9242
9243 if (*name == K_SPECIAL)
9244 p = concat_str((char_u *)"<SNR>", name + 3);
9245 else
9246 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009247 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009248 if (p != name)
9249 vim_free(p);
9250}
9251
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009252/*
9253 * Return TRUE for a non-zero Number and a non-empty String.
9254 */
9255 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009256non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009257{
9258 return ((argvars[0].v_type == VAR_NUMBER
9259 && argvars[0].vval.v_number != 0)
9260 || (argvars[0].v_type == VAR_STRING
9261 && argvars[0].vval.v_string != NULL
9262 && *argvars[0].vval.v_string != NUL));
9263}
9264
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265/*********************************************
9266 * Implementation of the built-in functions
9267 */
9268
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009269#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009270static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009271
9272/*
9273 * Get the float value of "argvars[0]" into "f".
9274 * Returns FAIL when the argument is not a Number or Float.
9275 */
9276 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009277get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009278{
9279 if (argvars[0].v_type == VAR_FLOAT)
9280 {
9281 *f = argvars[0].vval.v_float;
9282 return OK;
9283 }
9284 if (argvars[0].v_type == VAR_NUMBER)
9285 {
9286 *f = (float_T)argvars[0].vval.v_number;
9287 return OK;
9288 }
9289 EMSG(_("E808: Number or Float required"));
9290 return FAIL;
9291}
9292
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009293/*
9294 * "abs(expr)" function
9295 */
9296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009297f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009298{
9299 if (argvars[0].v_type == VAR_FLOAT)
9300 {
9301 rettv->v_type = VAR_FLOAT;
9302 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9303 }
9304 else
9305 {
9306 varnumber_T n;
9307 int error = FALSE;
9308
9309 n = get_tv_number_chk(&argvars[0], &error);
9310 if (error)
9311 rettv->vval.v_number = -1;
9312 else if (n > 0)
9313 rettv->vval.v_number = n;
9314 else
9315 rettv->vval.v_number = -n;
9316 }
9317}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009318
9319/*
9320 * "acos()" function
9321 */
9322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009323f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009324{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009325 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009326
9327 rettv->v_type = VAR_FLOAT;
9328 if (get_float_arg(argvars, &f) == OK)
9329 rettv->vval.v_float = acos(f);
9330 else
9331 rettv->vval.v_float = 0.0;
9332}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009333#endif
9334
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009336 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 */
9338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009339f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
Bram Moolenaar33570922005-01-25 22:26:29 +00009341 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009344 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009346 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009347 && !tv_check_lock(l->lv_lock,
9348 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009349 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009351 }
9352 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009353 EMSG(_(e_listreq));
9354}
9355
9356/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009357 * "alloc_fail(id, countdown, repeat)" function
9358 */
9359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009360f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009361{
9362 if (argvars[0].v_type != VAR_NUMBER
9363 || argvars[0].vval.v_number <= 0
9364 || argvars[1].v_type != VAR_NUMBER
9365 || argvars[1].vval.v_number < 0
9366 || argvars[2].v_type != VAR_NUMBER)
9367 EMSG(_(e_invarg));
9368 else
9369 {
9370 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009371 if (alloc_fail_id >= aid_last)
9372 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009373 alloc_fail_countdown = argvars[1].vval.v_number;
9374 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009375 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009376 }
9377}
9378
9379/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009380 * "and(expr, expr)" function
9381 */
9382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009383f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009384{
9385 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9386 & get_tv_number_chk(&argvars[1], NULL);
9387}
9388
9389/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009390 * "append(lnum, string/list)" function
9391 */
9392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009393f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009394{
9395 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009396 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009397 list_T *l = NULL;
9398 listitem_T *li = NULL;
9399 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009400 long added = 0;
9401
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009402 /* When coming here from Insert mode, sync undo, so that this can be
9403 * undone separately from what was previously inserted. */
9404 if (u_sync_once == 2)
9405 {
9406 u_sync_once = 1; /* notify that u_sync() was called */
9407 u_sync(TRUE);
9408 }
9409
Bram Moolenaar0d660222005-01-07 21:51:51 +00009410 lnum = get_tv_lnum(argvars);
9411 if (lnum >= 0
9412 && lnum <= curbuf->b_ml.ml_line_count
9413 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009414 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009415 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009416 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009417 l = argvars[1].vval.v_list;
9418 if (l == NULL)
9419 return;
9420 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009421 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009422 for (;;)
9423 {
9424 if (l == NULL)
9425 tv = &argvars[1]; /* append a string */
9426 else if (li == NULL)
9427 break; /* end of list */
9428 else
9429 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009430 line = get_tv_string_chk(tv);
9431 if (line == NULL) /* type error */
9432 {
9433 rettv->vval.v_number = 1; /* Failed */
9434 break;
9435 }
9436 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009437 ++added;
9438 if (l == NULL)
9439 break;
9440 li = li->li_next;
9441 }
9442
9443 appended_lines_mark(lnum, added);
9444 if (curwin->w_cursor.lnum > lnum)
9445 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009447 else
9448 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449}
9450
9451/*
9452 * "argc()" function
9453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009455f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458}
9459
9460/*
9461 * "argidx()" function
9462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009464f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467}
9468
9469/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009470 * "arglistid()" function
9471 */
9472 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009473f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009474{
9475 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009476
9477 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009478 wp = find_tabwin(&argvars[0], &argvars[1]);
9479 if (wp != NULL)
9480 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009481}
9482
9483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 * "argv(nr)" function
9485 */
9486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009487f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
9489 int idx;
9490
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009491 if (argvars[0].v_type != VAR_UNKNOWN)
9492 {
9493 idx = get_tv_number_chk(&argvars[0], NULL);
9494 if (idx >= 0 && idx < ARGCOUNT)
9495 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9496 else
9497 rettv->vval.v_string = NULL;
9498 rettv->v_type = VAR_STRING;
9499 }
9500 else if (rettv_list_alloc(rettv) == OK)
9501 for (idx = 0; idx < ARGCOUNT; ++idx)
9502 list_append_string(rettv->vval.v_list,
9503 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504}
9505
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009506typedef enum
9507{
9508 ASSERT_EQUAL,
9509 ASSERT_NOTEQUAL,
9510 ASSERT_MATCH,
9511 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009512 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009513} assert_type_T;
9514
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009515static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009516static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009517static void assert_error(garray_T *gap);
9518static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009519
9520/*
9521 * Prepare "gap" for an assert error and add the sourcing position.
9522 */
9523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009524prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009525{
9526 char buf[NUMBUFLEN];
9527
9528 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009529 if (sourcing_name != NULL)
9530 {
9531 ga_concat(gap, sourcing_name);
9532 if (sourcing_lnum > 0)
9533 ga_concat(gap, (char_u *)" ");
9534 }
9535 if (sourcing_lnum > 0)
9536 {
9537 sprintf(buf, "line %ld", (long)sourcing_lnum);
9538 ga_concat(gap, (char_u *)buf);
9539 }
9540 if (sourcing_name != NULL || sourcing_lnum > 0)
9541 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009542}
9543
9544/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009545 * Append "str" to "gap", escaping unprintable characters.
9546 * Changes NL to \n, CR to \r, etc.
9547 */
9548 static void
9549ga_concat_esc(garray_T *gap, char_u *str)
9550{
9551 char_u *p;
9552 char_u buf[NUMBUFLEN];
9553
Bram Moolenaarf1551962016-03-15 12:55:58 +01009554 if (str == NULL)
9555 {
9556 ga_concat(gap, (char_u *)"NULL");
9557 return;
9558 }
9559
Bram Moolenaar23689172016-02-15 22:37:37 +01009560 for (p = str; *p != NUL; ++p)
9561 switch (*p)
9562 {
9563 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9564 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9565 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9566 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9567 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9568 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9569 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9570 default:
9571 if (*p < ' ')
9572 {
9573 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9574 ga_concat(gap, buf);
9575 }
9576 else
9577 ga_append(gap, *p);
9578 break;
9579 }
9580}
9581
9582/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009583 * Fill "gap" with information about an assert error.
9584 */
9585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009586fill_assert_error(
9587 garray_T *gap,
9588 typval_T *opt_msg_tv,
9589 char_u *exp_str,
9590 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009591 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009592 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009593{
9594 char_u numbuf[NUMBUFLEN];
9595 char_u *tofree;
9596
9597 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9598 {
9599 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9600 vim_free(tofree);
9601 }
9602 else
9603 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009604 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009605 ga_concat(gap, (char_u *)"Pattern ");
9606 else
9607 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009608 if (exp_str == NULL)
9609 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009610 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009611 vim_free(tofree);
9612 }
9613 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009614 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009615 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009616 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009617 else if (atype == ASSERT_NOTMATCH)
9618 ga_concat(gap, (char_u *)" does match ");
9619 else if (atype == ASSERT_NOTEQUAL)
9620 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009621 else
9622 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009623 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009624 vim_free(tofree);
9625 }
9626}
Bram Moolenaar43345542015-11-29 17:35:35 +01009627
9628/*
9629 * Add an assert error to v:errors.
9630 */
9631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009633{
9634 struct vimvar *vp = &vimvars[VV_ERRORS];
9635
9636 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9637 /* Make sure v:errors is a list. */
9638 set_vim_var_list(VV_ERRORS, list_alloc());
9639 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9640}
9641
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009642 static void
9643assert_equal_common(typval_T *argvars, assert_type_T atype)
9644{
9645 garray_T ga;
9646
9647 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9648 != (atype == ASSERT_EQUAL))
9649 {
9650 prepare_assert_error(&ga);
9651 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9652 atype);
9653 assert_error(&ga);
9654 ga_clear(&ga);
9655 }
9656}
9657
Bram Moolenaar43345542015-11-29 17:35:35 +01009658/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009659 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009660 */
9661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009662f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009663{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009664 assert_equal_common(argvars, ASSERT_EQUAL);
9665}
Bram Moolenaar43345542015-11-29 17:35:35 +01009666
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009667/*
9668 * "assert_notequal(expected, actual[, msg])" function
9669 */
9670 static void
9671f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9672{
9673 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009674}
9675
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009676/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009677 * "assert_exception(string[, msg])" function
9678 */
9679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009680f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009681{
9682 garray_T ga;
9683 char *error;
9684
9685 error = (char *)get_tv_string_chk(&argvars[0]);
9686 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9687 {
9688 prepare_assert_error(&ga);
9689 ga_concat(&ga, (char_u *)"v:exception is not set");
9690 assert_error(&ga);
9691 ga_clear(&ga);
9692 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009693 else if (error != NULL
9694 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009695 {
9696 prepare_assert_error(&ga);
9697 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009698 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009699 assert_error(&ga);
9700 ga_clear(&ga);
9701 }
9702}
9703
9704/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009705 * "assert_fails(cmd [, error])" function
9706 */
9707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009708f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009709{
9710 char_u *cmd = get_tv_string_chk(&argvars[0]);
9711 garray_T ga;
9712
9713 called_emsg = FALSE;
9714 suppress_errthrow = TRUE;
9715 emsg_silent = TRUE;
9716 do_cmdline_cmd(cmd);
9717 if (!called_emsg)
9718 {
9719 prepare_assert_error(&ga);
9720 ga_concat(&ga, (char_u *)"command did not fail: ");
9721 ga_concat(&ga, cmd);
9722 assert_error(&ga);
9723 ga_clear(&ga);
9724 }
9725 else if (argvars[1].v_type != VAR_UNKNOWN)
9726 {
9727 char_u buf[NUMBUFLEN];
9728 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9729
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009730 if (error == NULL
9731 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009732 {
9733 prepare_assert_error(&ga);
9734 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009735 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009736 assert_error(&ga);
9737 ga_clear(&ga);
9738 }
9739 }
9740
9741 called_emsg = FALSE;
9742 suppress_errthrow = FALSE;
9743 emsg_silent = FALSE;
9744 emsg_on_display = FALSE;
9745 set_vim_var_string(VV_ERRMSG, NULL, 0);
9746}
9747
9748/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009749 * Common for assert_true() and assert_false().
9750 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009752assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009753{
9754 int error = FALSE;
9755 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009756
Bram Moolenaar37127922016-02-06 20:29:28 +01009757 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009758 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009759 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009760 if (argvars[0].v_type != VAR_NUMBER
9761 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9762 || error)
9763 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009764 prepare_assert_error(&ga);
9765 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009766 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009767 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009768 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009769 ga_clear(&ga);
9770 }
9771}
9772
9773/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009774 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009775 */
9776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009777f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009778{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009779 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009780}
9781
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009782 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009783assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009784{
9785 garray_T ga;
9786 char_u buf1[NUMBUFLEN];
9787 char_u buf2[NUMBUFLEN];
9788 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9789 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9790
Bram Moolenaar72188e92016-03-28 22:48:29 +02009791 if (pat == NULL || text == NULL)
9792 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009793 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009794 {
9795 prepare_assert_error(&ga);
9796 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009797 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009798 assert_error(&ga);
9799 ga_clear(&ga);
9800 }
9801}
9802
9803/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009804 * "assert_match(pattern, actual[, msg])" function
9805 */
9806 static void
9807f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9808{
9809 assert_match_common(argvars, ASSERT_MATCH);
9810}
9811
9812/*
9813 * "assert_notmatch(pattern, actual[, msg])" function
9814 */
9815 static void
9816f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9817{
9818 assert_match_common(argvars, ASSERT_NOTMATCH);
9819}
9820
9821/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009822 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009823 */
9824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009825f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009826{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009827 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009828}
9829
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009830#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009831/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009832 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009833 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009835f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009836{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009837 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009838
9839 rettv->v_type = VAR_FLOAT;
9840 if (get_float_arg(argvars, &f) == OK)
9841 rettv->vval.v_float = asin(f);
9842 else
9843 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009844}
9845
9846/*
9847 * "atan()" function
9848 */
9849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009850f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009851{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009852 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009853
9854 rettv->v_type = VAR_FLOAT;
9855 if (get_float_arg(argvars, &f) == OK)
9856 rettv->vval.v_float = atan(f);
9857 else
9858 rettv->vval.v_float = 0.0;
9859}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009860
9861/*
9862 * "atan2()" function
9863 */
9864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009865f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009866{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009867 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009868
9869 rettv->v_type = VAR_FLOAT;
9870 if (get_float_arg(argvars, &fx) == OK
9871 && get_float_arg(&argvars[1], &fy) == OK)
9872 rettv->vval.v_float = atan2(fx, fy);
9873 else
9874 rettv->vval.v_float = 0.0;
9875}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009876#endif
9877
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878/*
9879 * "browse(save, title, initdir, default)" function
9880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009882f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883{
9884#ifdef FEAT_BROWSE
9885 int save;
9886 char_u *title;
9887 char_u *initdir;
9888 char_u *defname;
9889 char_u buf[NUMBUFLEN];
9890 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009891 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 save = get_tv_number_chk(&argvars[0], &error);
9894 title = get_tv_string_chk(&argvars[1]);
9895 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9896 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898 if (error || title == NULL || initdir == NULL || defname == NULL)
9899 rettv->vval.v_string = NULL;
9900 else
9901 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009902 do_browse(save ? BROWSE_SAVE : 0,
9903 title, defname, NULL, initdir, NULL, curbuf);
9904#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009905 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009906#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009908}
9909
9910/*
9911 * "browsedir(title, initdir)" function
9912 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009914f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009915{
9916#ifdef FEAT_BROWSE
9917 char_u *title;
9918 char_u *initdir;
9919 char_u buf[NUMBUFLEN];
9920
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009921 title = get_tv_string_chk(&argvars[0]);
9922 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 if (title == NULL || initdir == NULL)
9925 rettv->vval.v_string = NULL;
9926 else
9927 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009928 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933}
9934
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009935static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009936
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937/*
9938 * Find a buffer by number or exact name.
9939 */
9940 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009941find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942{
9943 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009945 if (avar->v_type == VAR_NUMBER)
9946 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009947 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009949 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009950 if (buf == NULL)
9951 {
9952 /* No full path name match, try a match with a URL or a "nofile"
9953 * buffer, these don't use the full path. */
9954 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9955 if (buf->b_fname != NULL
9956 && (path_with_url(buf->b_fname)
9957#ifdef FEAT_QUICKFIX
9958 || bt_nofile(buf)
9959#endif
9960 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009961 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009962 break;
9963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 }
9965 return buf;
9966}
9967
9968/*
9969 * "bufexists(expr)" function
9970 */
9971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009972f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975}
9976
9977/*
9978 * "buflisted(expr)" function
9979 */
9980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009981f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982{
9983 buf_T *buf;
9984
9985 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987}
9988
9989/*
9990 * "bufloaded(expr)" function
9991 */
9992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009993f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994{
9995 buf_T *buf;
9996
9997 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999}
10000
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010001 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010002buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 int save_magic;
10005 char_u *save_cpo;
10006 buf_T *buf;
10007
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10009 save_magic = p_magic;
10010 p_magic = TRUE;
10011 save_cpo = p_cpo;
10012 p_cpo = (char_u *)"";
10013
10014 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010015 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016
10017 p_magic = save_magic;
10018 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010019 return buf;
10020}
10021
10022/*
10023 * Get buffer by number or pattern.
10024 */
10025 static buf_T *
10026get_buf_tv(typval_T *tv, int curtab_only)
10027{
10028 char_u *name = tv->vval.v_string;
10029 buf_T *buf;
10030
10031 if (tv->v_type == VAR_NUMBER)
10032 return buflist_findnr((int)tv->vval.v_number);
10033 if (tv->v_type != VAR_STRING)
10034 return NULL;
10035 if (name == NULL || *name == NUL)
10036 return curbuf;
10037 if (name[0] == '$' && name[1] == NUL)
10038 return lastbuf;
10039
10040 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041
10042 /* If not found, try expanding the name, like done for bufexists(). */
10043 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
10046 return buf;
10047}
10048
10049/*
10050 * "bufname(expr)" function
10051 */
10052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010053f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054{
10055 buf_T *buf;
10056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010057 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010059 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 --emsg_off;
10066}
10067
10068/*
10069 * "bufnr(expr)" function
10070 */
10071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010072f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073{
10074 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010075 int error = FALSE;
10076 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010078 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010080 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010081 --emsg_off;
10082
10083 /* If the buffer isn't found and the second argument is not zero create a
10084 * new buffer. */
10085 if (buf == NULL
10086 && argvars[1].v_type != VAR_UNKNOWN
10087 && get_tv_number_chk(&argvars[1], &error) != 0
10088 && !error
10089 && (name = get_tv_string_chk(&argvars[0])) != NULL
10090 && !error)
10091 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10092
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097}
10098
10099/*
10100 * "bufwinnr(nr)" function
10101 */
10102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010103f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104{
10105#ifdef FEAT_WINDOWS
10106 win_T *wp;
10107 int winnr = 0;
10108#endif
10109 buf_T *buf;
10110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010113 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114#ifdef FEAT_WINDOWS
10115 for (wp = firstwin; wp; wp = wp->w_next)
10116 {
10117 ++winnr;
10118 if (wp->w_buffer == buf)
10119 break;
10120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124#endif
10125 --emsg_off;
10126}
10127
10128/*
10129 * "byte2line(byte)" function
10130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010132f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133{
10134#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136#else
10137 long boff = 0;
10138
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010139 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 (linenr_T)0, &boff);
10145#endif
10146}
10147
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010149byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010150{
10151#ifdef FEAT_MBYTE
10152 char_u *t;
10153#endif
10154 char_u *str;
10155 long idx;
10156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010157 str = get_tv_string_chk(&argvars[0]);
10158 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010161 return;
10162
10163#ifdef FEAT_MBYTE
10164 t = str;
10165 for ( ; idx > 0; idx--)
10166 {
10167 if (*t == NUL) /* EOL reached */
10168 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010169 if (enc_utf8 && comp)
10170 t += utf_ptr2len(t);
10171 else
10172 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010173 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010174 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010175#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010176 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010177 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010178#endif
10179}
10180
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010181/*
10182 * "byteidx()" function
10183 */
10184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010185f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010186{
10187 byteidx(argvars, rettv, FALSE);
10188}
10189
10190/*
10191 * "byteidxcomp()" function
10192 */
10193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010194f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010195{
10196 byteidx(argvars, rettv, TRUE);
10197}
10198
Bram Moolenaardb913952012-06-29 12:54:53 +020010199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010200func_call(
10201 char_u *name,
10202 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010203 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010204 dict_T *selfdict,
10205 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010206{
10207 listitem_T *item;
10208 typval_T argv[MAX_FUNC_ARGS + 1];
10209 int argc = 0;
10210 int dummy;
10211 int r = 0;
10212
10213 for (item = args->vval.v_list->lv_first; item != NULL;
10214 item = item->li_next)
10215 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010216 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010217 {
10218 EMSG(_("E699: Too many arguments"));
10219 break;
10220 }
10221 /* Make a copy of each argument. This is needed to be able to set
10222 * v_lock to VAR_FIXED in the copy without changing the original list.
10223 */
10224 copy_tv(&item->li_tv, &argv[argc++]);
10225 }
10226
10227 if (item == NULL)
10228 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10229 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010230 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010231
10232 /* Free the arguments. */
10233 while (argc > 0)
10234 clear_tv(&argv[--argc]);
10235
10236 return r;
10237}
10238
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010239/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010240 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010241 */
10242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010243f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010244{
10245 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010246 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010248
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010249 if (argvars[1].v_type != VAR_LIST)
10250 {
10251 EMSG(_(e_listreq));
10252 return;
10253 }
10254 if (argvars[1].vval.v_list == NULL)
10255 return;
10256
10257 if (argvars[0].v_type == VAR_FUNC)
10258 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010259 else if (argvars[0].v_type == VAR_PARTIAL)
10260 {
10261 partial = argvars[0].vval.v_partial;
10262 func = partial->pt_name;
10263 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010264 else
10265 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010266 if (*func == NUL)
10267 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010268
Bram Moolenaare9a41262005-01-15 22:18:47 +000010269 if (argvars[2].v_type != VAR_UNKNOWN)
10270 {
10271 if (argvars[2].v_type != VAR_DICT)
10272 {
10273 EMSG(_(e_dictreq));
10274 return;
10275 }
10276 selfdict = argvars[2].vval.v_dict;
10277 }
10278
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010279 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010280}
10281
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010282#ifdef FEAT_FLOAT
10283/*
10284 * "ceil({float})" function
10285 */
10286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010287f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010288{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010289 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010290
10291 rettv->v_type = VAR_FLOAT;
10292 if (get_float_arg(argvars, &f) == OK)
10293 rettv->vval.v_float = ceil(f);
10294 else
10295 rettv->vval.v_float = 0.0;
10296}
10297#endif
10298
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010299#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010300/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010301 * "ch_close()" function
10302 */
10303 static void
10304f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10305{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010306 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010307
10308 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010309 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010310 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010311 channel_clear(channel);
10312 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010313}
10314
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010315/*
10316 * "ch_getbufnr()" function
10317 */
10318 static void
10319f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10320{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010321 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010322
10323 rettv->vval.v_number = -1;
10324 if (channel != NULL)
10325 {
10326 char_u *what = get_tv_string(&argvars[1]);
10327 int part;
10328
10329 if (STRCMP(what, "err") == 0)
10330 part = PART_ERR;
10331 else if (STRCMP(what, "out") == 0)
10332 part = PART_OUT;
10333 else if (STRCMP(what, "in") == 0)
10334 part = PART_IN;
10335 else
10336 part = PART_SOCK;
10337 if (channel->ch_part[part].ch_buffer != NULL)
10338 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10339 }
10340}
10341
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010342/*
10343 * "ch_getjob()" function
10344 */
10345 static void
10346f_ch_getjob(typval_T *argvars, typval_T *rettv)
10347{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010348 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010349
10350 if (channel != NULL)
10351 {
10352 rettv->v_type = VAR_JOB;
10353 rettv->vval.v_job = channel->ch_job;
10354 if (channel->ch_job != NULL)
10355 ++channel->ch_job->jv_refcount;
10356 }
10357}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010358
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010359/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010360 * "ch_info()" function
10361 */
10362 static void
10363f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10364{
10365 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10366
10367 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10368 channel_info(channel, rettv->vval.v_dict);
10369}
10370
10371/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010372 * "ch_log()" function
10373 */
10374 static void
10375f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10376{
10377 char_u *msg = get_tv_string(&argvars[0]);
10378 channel_T *channel = NULL;
10379
10380 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010381 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010382
10383 ch_log(channel, (char *)msg);
10384}
10385
10386/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010387 * "ch_logfile()" function
10388 */
10389 static void
10390f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10391{
10392 char_u *fname;
10393 char_u *opt = (char_u *)"";
10394 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010395
10396 fname = get_tv_string(&argvars[0]);
10397 if (argvars[1].v_type == VAR_STRING)
10398 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010399 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010400}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010401
10402/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010403 * "ch_open()" function
10404 */
10405 static void
10406f_ch_open(typval_T *argvars, typval_T *rettv)
10407{
Bram Moolenaar77073442016-02-13 23:23:53 +010010408 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010409 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010410}
10411
10412/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010413 * "ch_read()" function
10414 */
10415 static void
10416f_ch_read(typval_T *argvars, typval_T *rettv)
10417{
10418 common_channel_read(argvars, rettv, FALSE);
10419}
10420
10421/*
10422 * "ch_readraw()" function
10423 */
10424 static void
10425f_ch_readraw(typval_T *argvars, typval_T *rettv)
10426{
10427 common_channel_read(argvars, rettv, TRUE);
10428}
10429
10430/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010431 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010432 */
10433 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010434f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10435{
10436 ch_expr_common(argvars, rettv, TRUE);
10437}
10438
10439/*
10440 * "ch_sendexpr()" function
10441 */
10442 static void
10443f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10444{
10445 ch_expr_common(argvars, rettv, FALSE);
10446}
10447
10448/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010449 * "ch_evalraw()" function
10450 */
10451 static void
10452f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10453{
10454 ch_raw_common(argvars, rettv, TRUE);
10455}
10456
10457/*
10458 * "ch_sendraw()" function
10459 */
10460 static void
10461f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10462{
10463 ch_raw_common(argvars, rettv, FALSE);
10464}
10465
10466/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010467 * "ch_setoptions()" function
10468 */
10469 static void
10470f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10471{
10472 channel_T *channel;
10473 jobopt_T opt;
10474
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010475 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010476 if (channel == NULL)
10477 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010478 clear_job_options(&opt);
10479 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010480 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10481 channel_set_options(channel, &opt);
10482 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010483}
10484
10485/*
10486 * "ch_status()" function
10487 */
10488 static void
10489f_ch_status(typval_T *argvars, typval_T *rettv)
10490{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010491 channel_T *channel;
10492
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010493 /* return an empty string by default */
10494 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010495 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010496
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010497 channel = get_channel_arg(&argvars[0], FALSE);
10498 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010499}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010500#endif
10501
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010502/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010503 * "changenr()" function
10504 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010506f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010507{
10508 rettv->vval.v_number = curbuf->b_u_seq_cur;
10509}
10510
10511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 * "char2nr(string)" function
10513 */
10514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010515f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516{
10517#ifdef FEAT_MBYTE
10518 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010519 {
10520 int utf8 = 0;
10521
10522 if (argvars[1].v_type != VAR_UNKNOWN)
10523 utf8 = get_tv_number_chk(&argvars[1], NULL);
10524
10525 if (utf8)
10526 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10527 else
10528 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 else
10531#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533}
10534
10535/*
10536 * "cindent(lnum)" function
10537 */
10538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010539f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540{
10541#ifdef FEAT_CINDENT
10542 pos_T pos;
10543 linenr_T lnum;
10544
10545 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10548 {
10549 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 curwin->w_cursor = pos;
10552 }
10553 else
10554#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556}
10557
10558/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010559 * "clearmatches()" function
10560 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010562f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010563{
10564#ifdef FEAT_SEARCH_EXTRA
10565 clear_matches(curwin);
10566#endif
10567}
10568
10569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 * "col(string)" function
10571 */
10572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010573f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574{
10575 colnr_T col = 0;
10576 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010577 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010579 fp = var2fpos(&argvars[0], FALSE, &fnum);
10580 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 {
10582 if (fp->col == MAXCOL)
10583 {
10584 /* '> can be MAXCOL, get the length of the line then */
10585 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010586 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 else
10588 col = MAXCOL;
10589 }
10590 else
10591 {
10592 col = fp->col + 1;
10593#ifdef FEAT_VIRTUALEDIT
10594 /* col(".") when the cursor is on the NUL at the end of the line
10595 * because of "coladd" can be seen as an extra column. */
10596 if (virtual_active() && fp == &curwin->w_cursor)
10597 {
10598 char_u *p = ml_get_cursor();
10599
10600 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10601 curwin->w_virtcol - curwin->w_cursor.coladd))
10602 {
10603# ifdef FEAT_MBYTE
10604 int l;
10605
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010606 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 col += l;
10608# else
10609 if (*p != NUL && p[1] == NUL)
10610 ++col;
10611# endif
10612 }
10613 }
10614#endif
10615 }
10616 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010617 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618}
10619
Bram Moolenaar572cb562005-08-05 21:35:02 +000010620#if defined(FEAT_INS_EXPAND)
10621/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010622 * "complete()" function
10623 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010625f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010626{
10627 int startcol;
10628
10629 if ((State & INSERT) == 0)
10630 {
10631 EMSG(_("E785: complete() can only be used in Insert mode"));
10632 return;
10633 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010634
10635 /* Check for undo allowed here, because if something was already inserted
10636 * the line was already saved for undo and this check isn't done. */
10637 if (!undo_allowed())
10638 return;
10639
Bram Moolenaarade00832006-03-10 21:46:58 +000010640 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10641 {
10642 EMSG(_(e_invarg));
10643 return;
10644 }
10645
10646 startcol = get_tv_number_chk(&argvars[0], NULL);
10647 if (startcol <= 0)
10648 return;
10649
10650 set_completion(startcol - 1, argvars[1].vval.v_list);
10651}
10652
10653/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010654 * "complete_add()" function
10655 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010657f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010658{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010659 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010660}
10661
10662/*
10663 * "complete_check()" function
10664 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010666f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010667{
10668 int saved = RedrawingDisabled;
10669
10670 RedrawingDisabled = 0;
10671 ins_compl_check_keys(0);
10672 rettv->vval.v_number = compl_interrupted;
10673 RedrawingDisabled = saved;
10674}
10675#endif
10676
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677/*
10678 * "confirm(message, buttons[, default [, type]])" function
10679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010681f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682{
10683#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10684 char_u *message;
10685 char_u *buttons = NULL;
10686 char_u buf[NUMBUFLEN];
10687 char_u buf2[NUMBUFLEN];
10688 int def = 1;
10689 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010690 char_u *typestr;
10691 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693 message = get_tv_string_chk(&argvars[0]);
10694 if (message == NULL)
10695 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010696 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10699 if (buttons == NULL)
10700 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010701 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010703 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010704 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010706 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10707 if (typestr == NULL)
10708 error = TRUE;
10709 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010711 switch (TOUPPER_ASC(*typestr))
10712 {
10713 case 'E': type = VIM_ERROR; break;
10714 case 'Q': type = VIM_QUESTION; break;
10715 case 'I': type = VIM_INFO; break;
10716 case 'W': type = VIM_WARNING; break;
10717 case 'G': type = VIM_GENERIC; break;
10718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 }
10720 }
10721 }
10722 }
10723
10724 if (buttons == NULL || *buttons == NUL)
10725 buttons = (char_u *)_("&Ok");
10726
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010727 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010728 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010729 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730#endif
10731}
10732
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010733/*
10734 * "copy()" function
10735 */
10736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010737f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010738{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010739 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010740}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010742#ifdef FEAT_FLOAT
10743/*
10744 * "cos()" function
10745 */
10746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010747f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010748{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010749 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010750
10751 rettv->v_type = VAR_FLOAT;
10752 if (get_float_arg(argvars, &f) == OK)
10753 rettv->vval.v_float = cos(f);
10754 else
10755 rettv->vval.v_float = 0.0;
10756}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010757
10758/*
10759 * "cosh()" function
10760 */
10761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010762f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010763{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010764 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010765
10766 rettv->v_type = VAR_FLOAT;
10767 if (get_float_arg(argvars, &f) == OK)
10768 rettv->vval.v_float = cosh(f);
10769 else
10770 rettv->vval.v_float = 0.0;
10771}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010772#endif
10773
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010775 * "count()" function
10776 */
10777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010778f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010779{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010780 long n = 0;
10781 int ic = FALSE;
10782
Bram Moolenaare9a41262005-01-15 22:18:47 +000010783 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010784 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010785 listitem_T *li;
10786 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010788
Bram Moolenaare9a41262005-01-15 22:18:47 +000010789 if ((l = argvars[0].vval.v_list) != NULL)
10790 {
10791 li = l->lv_first;
10792 if (argvars[2].v_type != VAR_UNKNOWN)
10793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010794 int error = FALSE;
10795
10796 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010797 if (argvars[3].v_type != VAR_UNKNOWN)
10798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010799 idx = get_tv_number_chk(&argvars[3], &error);
10800 if (!error)
10801 {
10802 li = list_find(l, idx);
10803 if (li == NULL)
10804 EMSGN(_(e_listidx), idx);
10805 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010806 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010807 if (error)
10808 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010809 }
10810
10811 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010812 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010813 ++n;
10814 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010815 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 else if (argvars[0].v_type == VAR_DICT)
10817 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010818 int todo;
10819 dict_T *d;
10820 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010821
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010822 if ((d = argvars[0].vval.v_dict) != NULL)
10823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010824 int error = FALSE;
10825
Bram Moolenaare9a41262005-01-15 22:18:47 +000010826 if (argvars[2].v_type != VAR_UNKNOWN)
10827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010829 if (argvars[3].v_type != VAR_UNKNOWN)
10830 EMSG(_(e_invarg));
10831 }
10832
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010833 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010834 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010835 {
10836 if (!HASHITEM_EMPTY(hi))
10837 {
10838 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010839 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010840 ++n;
10841 }
10842 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010843 }
10844 }
10845 else
10846 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010847 rettv->vval.v_number = n;
10848}
10849
10850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10852 *
10853 * Checks the existence of a cscope connection.
10854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010856f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857{
10858#ifdef FEAT_CSCOPE
10859 int num = 0;
10860 char_u *dbpath = NULL;
10861 char_u *prepend = NULL;
10862 char_u buf[NUMBUFLEN];
10863
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010864 if (argvars[0].v_type != VAR_UNKNOWN
10865 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 num = (int)get_tv_number(&argvars[0]);
10868 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010869 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010870 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 }
10872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874#endif
10875}
10876
10877/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010878 * "cursor(lnum, col)" function, or
10879 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010881 * Moves the cursor to the specified line and column.
10882 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010885f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886{
10887 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010888#ifdef FEAT_VIRTUALEDIT
10889 long coladd = 0;
10890#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010891 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010893 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010894 if (argvars[1].v_type == VAR_UNKNOWN)
10895 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010896 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010897 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010898
Bram Moolenaar493c1782014-05-28 14:34:46 +020010899 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010900 {
10901 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010902 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010903 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010904 line = pos.lnum;
10905 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010906#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010907 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010908#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010909 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010910 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010911 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010912 set_curswant = FALSE;
10913 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010914 }
10915 else
10916 {
10917 line = get_tv_lnum(argvars);
10918 col = get_tv_number_chk(&argvars[1], NULL);
10919#ifdef FEAT_VIRTUALEDIT
10920 if (argvars[2].v_type != VAR_UNKNOWN)
10921 coladd = get_tv_number_chk(&argvars[2], NULL);
10922#endif
10923 }
10924 if (line < 0 || col < 0
10925#ifdef FEAT_VIRTUALEDIT
10926 || coladd < 0
10927#endif
10928 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 if (line > 0)
10931 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 if (col > 0)
10933 curwin->w_cursor.col = col - 1;
10934#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010935 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936#endif
10937
10938 /* Make sure the cursor is in a valid position. */
10939 check_cursor();
10940#ifdef FEAT_MBYTE
10941 /* Correct cursor for multi-byte character. */
10942 if (has_mbyte)
10943 mb_adjust_cursor();
10944#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010945
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010946 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010947 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948}
10949
10950/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 */
10953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010954f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010956 int noref = 0;
10957
10958 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010960 if (noref < 0 || noref > 1)
10961 EMSG(_(e_invarg));
10962 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010963 {
10964 current_copyID += COPYID_INC;
10965 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967}
10968
10969/*
10970 * "delete()" function
10971 */
10972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010973f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010975 char_u nbuf[NUMBUFLEN];
10976 char_u *name;
10977 char_u *flags;
10978
10979 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010981 return;
10982
10983 name = get_tv_string(&argvars[0]);
10984 if (name == NULL || *name == NUL)
10985 {
10986 EMSG(_(e_invarg));
10987 return;
10988 }
10989
10990 if (argvars[1].v_type != VAR_UNKNOWN)
10991 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010993 flags = (char_u *)"";
10994
10995 if (*flags == NUL)
10996 /* delete a file */
10997 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10998 else if (STRCMP(flags, "d") == 0)
10999 /* delete an empty directory */
11000 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11001 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011002 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011003 rettv->vval.v_number = delete_recursive(name);
11004 else
11005 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006}
11007
11008/*
11009 * "did_filetype()" function
11010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011012f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013{
11014#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016#endif
11017}
11018
11019/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011020 * "diff_filler()" function
11021 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011023f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011024{
11025#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011026 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011027#endif
11028}
11029
11030/*
11031 * "diff_hlID()" function
11032 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011034f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011035{
11036#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011038 static linenr_T prev_lnum = 0;
11039 static int changedtick = 0;
11040 static int fnum = 0;
11041 static int change_start = 0;
11042 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011043 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011044 int filler_lines;
11045 int col;
11046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 if (lnum < 0) /* ignore type error in {lnum} arg */
11048 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011049 if (lnum != prev_lnum
11050 || changedtick != curbuf->b_changedtick
11051 || fnum != curbuf->b_fnum)
11052 {
11053 /* New line, buffer, change: need to get the values. */
11054 filler_lines = diff_check(curwin, lnum);
11055 if (filler_lines < 0)
11056 {
11057 if (filler_lines == -1)
11058 {
11059 change_start = MAXCOL;
11060 change_end = -1;
11061 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11062 hlID = HLF_ADD; /* added line */
11063 else
11064 hlID = HLF_CHD; /* changed line */
11065 }
11066 else
11067 hlID = HLF_ADD; /* added line */
11068 }
11069 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011070 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011071 prev_lnum = lnum;
11072 changedtick = curbuf->b_changedtick;
11073 fnum = curbuf->b_fnum;
11074 }
11075
11076 if (hlID == HLF_CHD || hlID == HLF_TXD)
11077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011079 if (col >= change_start && col <= change_end)
11080 hlID = HLF_TXD; /* changed text */
11081 else
11082 hlID = HLF_CHD; /* changed line */
11083 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011084 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011085#endif
11086}
11087
11088/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011089 * "disable_char_avail_for_testing({expr})" function
11090 */
11091 static void
11092f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11093{
11094 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11095}
11096
11097/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011098 * "empty({expr})" function
11099 */
11100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011101f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011102{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011103 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011104
11105 switch (argvars[0].v_type)
11106 {
11107 case VAR_STRING:
11108 case VAR_FUNC:
11109 n = argvars[0].vval.v_string == NULL
11110 || *argvars[0].vval.v_string == NUL;
11111 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011112 case VAR_PARTIAL:
11113 n = FALSE;
11114 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011115 case VAR_NUMBER:
11116 n = argvars[0].vval.v_number == 0;
11117 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011118 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011119#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011120 n = argvars[0].vval.v_float == 0.0;
11121 break;
11122#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011123 case VAR_LIST:
11124 n = argvars[0].vval.v_list == NULL
11125 || argvars[0].vval.v_list->lv_first == NULL;
11126 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011127 case VAR_DICT:
11128 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011129 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011131 case VAR_SPECIAL:
11132 n = argvars[0].vval.v_number != VVAL_TRUE;
11133 break;
11134
Bram Moolenaar835dc632016-02-07 14:27:38 +010011135 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011136#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011137 n = argvars[0].vval.v_job == NULL
11138 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11139 break;
11140#endif
11141 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011143 n = argvars[0].vval.v_channel == NULL
11144 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011145 break;
11146#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011147 case VAR_UNKNOWN:
11148 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11149 n = TRUE;
11150 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011151 }
11152
11153 rettv->vval.v_number = n;
11154}
11155
11156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 * "escape({string}, {chars})" function
11158 */
11159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011160f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161{
11162 char_u buf[NUMBUFLEN];
11163
Bram Moolenaar758711c2005-02-02 23:11:38 +000011164 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11165 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011166 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167}
11168
11169/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011170 * "eval()" function
11171 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011173f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011174{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011175 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011177 s = get_tv_string_chk(&argvars[0]);
11178 if (s != NULL)
11179 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011180
Bram Moolenaar615b9972015-01-14 17:15:05 +010011181 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011182 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11183 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011184 if (p != NULL && !aborting())
11185 EMSG2(_(e_invexpr2), p);
11186 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011188 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011189 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011190 else if (*s != NUL)
11191 EMSG(_(e_trailing));
11192}
11193
11194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 * "eventhandler()" function
11196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011198f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201}
11202
11203/*
11204 * "executable()" function
11205 */
11206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011207f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011209 char_u *name = get_tv_string(&argvars[0]);
11210
11211 /* Check in $PATH and also check directly if there is a directory name. */
11212 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11213 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011214}
11215
11216/*
11217 * "exepath()" function
11218 */
11219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011220f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011221{
11222 char_u *p = NULL;
11223
Bram Moolenaarb5971142015-03-21 17:32:19 +010011224 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011225 rettv->v_type = VAR_STRING;
11226 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227}
11228
11229/*
11230 * "exists()" function
11231 */
11232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011233f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234{
11235 char_u *p;
11236 char_u *name;
11237 int n = FALSE;
11238 int len = 0;
11239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 if (*p == '$') /* environment variable */
11242 {
11243 /* first try "normal" environment variables (fast) */
11244 if (mch_getenv(p + 1) != NULL)
11245 n = TRUE;
11246 else
11247 {
11248 /* try expanding things like $VIM and ${HOME} */
11249 p = expand_env_save(p);
11250 if (p != NULL && *p != '$')
11251 n = TRUE;
11252 vim_free(p);
11253 }
11254 }
11255 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011258 if (*skipwhite(p) != NUL)
11259 n = FALSE; /* trailing garbage */
11260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 else if (*p == '*') /* internal or user defined function */
11262 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011263 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 }
11265 else if (*p == ':')
11266 {
11267 n = cmd_exists(p + 1);
11268 }
11269 else if (*p == '#')
11270 {
11271#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011272 if (p[1] == '#')
11273 n = autocmd_supported(p + 2);
11274 else
11275 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276#endif
11277 }
11278 else /* internal variable */
11279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011280 char_u *tofree;
11281 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011283 /* get_name_len() takes care of expanding curly braces */
11284 name = p;
11285 len = get_name_len(&p, &tofree, TRUE, FALSE);
11286 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011288 if (tofree != NULL)
11289 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011290 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011291 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011293 /* handle d.key, l[idx], f(expr) */
11294 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11295 if (n)
11296 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 }
11298 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011299 if (*p != NUL)
11300 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011302 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 }
11304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306}
11307
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011308#ifdef FEAT_FLOAT
11309/*
11310 * "exp()" function
11311 */
11312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011313f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011314{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011315 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011316
11317 rettv->v_type = VAR_FLOAT;
11318 if (get_float_arg(argvars, &f) == OK)
11319 rettv->vval.v_float = exp(f);
11320 else
11321 rettv->vval.v_float = 0.0;
11322}
11323#endif
11324
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325/*
11326 * "expand()" function
11327 */
11328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011329f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330{
11331 char_u *s;
11332 int len;
11333 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011334 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011336 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011337 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011340 if (argvars[1].v_type != VAR_UNKNOWN
11341 && argvars[2].v_type != VAR_UNKNOWN
11342 && get_tv_number_chk(&argvars[2], &error)
11343 && !error)
11344 {
11345 rettv->v_type = VAR_LIST;
11346 rettv->vval.v_list = NULL;
11347 }
11348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 if (*s == '%' || *s == '#' || *s == '<')
11351 {
11352 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011353 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011355 if (rettv->v_type == VAR_LIST)
11356 {
11357 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11358 list_append_string(rettv->vval.v_list, result, -1);
11359 else
11360 vim_free(result);
11361 }
11362 else
11363 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 }
11365 else
11366 {
11367 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011368 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011369 if (argvars[1].v_type != VAR_UNKNOWN
11370 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011371 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011372 if (!error)
11373 {
11374 ExpandInit(&xpc);
11375 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011376 if (p_wic)
11377 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011378 if (rettv->v_type == VAR_STRING)
11379 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11380 options, WILD_ALL);
11381 else if (rettv_list_alloc(rettv) != FAIL)
11382 {
11383 int i;
11384
11385 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11386 for (i = 0; i < xpc.xp_numfiles; i++)
11387 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11388 ExpandCleanup(&xpc);
11389 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011390 }
11391 else
11392 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 }
11394}
11395
11396/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011397 * Go over all entries in "d2" and add them to "d1".
11398 * When "action" is "error" then a duplicate key is an error.
11399 * When "action" is "force" then a duplicate key is overwritten.
11400 * Otherwise duplicate keys are ignored ("action" is "keep").
11401 */
11402 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011403dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011404{
11405 dictitem_T *di1;
11406 hashitem_T *hi2;
11407 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011408 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011409
11410 todo = (int)d2->dv_hashtab.ht_used;
11411 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11412 {
11413 if (!HASHITEM_EMPTY(hi2))
11414 {
11415 --todo;
11416 di1 = dict_find(d1, hi2->hi_key, -1);
11417 if (d1->dv_scope != 0)
11418 {
11419 /* Disallow replacing a builtin function in l: and g:.
11420 * Check the key to be valid when adding to any
11421 * scope. */
11422 if (d1->dv_scope == VAR_DEF_SCOPE
11423 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11424 && var_check_func_name(hi2->hi_key,
11425 di1 == NULL))
11426 break;
11427 if (!valid_varname(hi2->hi_key))
11428 break;
11429 }
11430 if (di1 == NULL)
11431 {
11432 di1 = dictitem_copy(HI2DI(hi2));
11433 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11434 dictitem_free(di1);
11435 }
11436 else if (*action == 'e')
11437 {
11438 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11439 break;
11440 }
11441 else if (*action == 'f' && HI2DI(hi2) != di1)
11442 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011443 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11444 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011445 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011446 clear_tv(&di1->di_tv);
11447 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11448 }
11449 }
11450 }
11451}
11452
11453/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011454 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011455 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011456 */
11457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011458f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011459{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011460 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011461
Bram Moolenaare9a41262005-01-15 22:18:47 +000011462 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011463 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011464 list_T *l1, *l2;
11465 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011466 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011467 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011468
Bram Moolenaare9a41262005-01-15 22:18:47 +000011469 l1 = argvars[0].vval.v_list;
11470 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011471 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011472 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011473 {
11474 if (argvars[2].v_type != VAR_UNKNOWN)
11475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011476 before = get_tv_number_chk(&argvars[2], &error);
11477 if (error)
11478 return; /* type error; errmsg already given */
11479
Bram Moolenaar758711c2005-02-02 23:11:38 +000011480 if (before == l1->lv_len)
11481 item = NULL;
11482 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011483 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011484 item = list_find(l1, before);
11485 if (item == NULL)
11486 {
11487 EMSGN(_(e_listidx), before);
11488 return;
11489 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011490 }
11491 }
11492 else
11493 item = NULL;
11494 list_extend(l1, l2, item);
11495
Bram Moolenaare9a41262005-01-15 22:18:47 +000011496 copy_tv(&argvars[0], rettv);
11497 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011498 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011499 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11500 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011501 dict_T *d1, *d2;
11502 char_u *action;
11503 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011504
11505 d1 = argvars[0].vval.v_dict;
11506 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011507 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011508 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011509 {
11510 /* Check the third argument. */
11511 if (argvars[2].v_type != VAR_UNKNOWN)
11512 {
11513 static char *(av[]) = {"keep", "force", "error"};
11514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011515 action = get_tv_string_chk(&argvars[2]);
11516 if (action == NULL)
11517 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011518 for (i = 0; i < 3; ++i)
11519 if (STRCMP(action, av[i]) == 0)
11520 break;
11521 if (i == 3)
11522 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011523 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011524 return;
11525 }
11526 }
11527 else
11528 action = (char_u *)"force";
11529
Bram Moolenaara9922d62013-05-30 13:01:18 +020011530 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011531
Bram Moolenaare9a41262005-01-15 22:18:47 +000011532 copy_tv(&argvars[0], rettv);
11533 }
11534 }
11535 else
11536 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011537}
11538
11539/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011540 * "feedkeys()" function
11541 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011543f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011544{
11545 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011546 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011547 char_u *keys, *flags;
11548 char_u nbuf[NUMBUFLEN];
11549 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011550 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011551 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011552 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011553
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011554 /* This is not allowed in the sandbox. If the commands would still be
11555 * executed in the sandbox it would be OK, but it probably happens later,
11556 * when "sandbox" is no longer set. */
11557 if (check_secure())
11558 return;
11559
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011560 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011561
11562 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011563 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011564 flags = get_tv_string_buf(&argvars[1], nbuf);
11565 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011566 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011567 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011568 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011569 case 'n': remap = FALSE; break;
11570 case 'm': remap = TRUE; break;
11571 case 't': typed = TRUE; break;
11572 case 'i': insert = TRUE; break;
11573 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011574 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011575 }
11576 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011577 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011578
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011579 if (*keys != NUL || execute)
11580 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011581 /* Need to escape K_SPECIAL and CSI before putting the string in the
11582 * typeahead buffer. */
11583 keys_esc = vim_strsave_escape_csi(keys);
11584 if (keys_esc != NULL)
11585 {
11586 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011587 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011588 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011589 if (vgetc_busy)
11590 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011591 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011592 {
11593 int save_msg_scroll = msg_scroll;
11594
11595 /* Avoid a 1 second delay when the keys start Insert mode. */
11596 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011597
Bram Moolenaar245c4102016-04-20 17:37:41 +020011598 if (!dangerous)
11599 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011600 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011601 if (!dangerous)
11602 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011603 msg_scroll |= save_msg_scroll;
11604 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011605 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011606 }
11607}
11608
11609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610 * "filereadable()" function
11611 */
11612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011613f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011615 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 char_u *p;
11617 int n;
11618
Bram Moolenaarc236c162008-07-13 17:41:49 +000011619#ifndef O_NONBLOCK
11620# define O_NONBLOCK 0
11621#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011623 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11624 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 {
11626 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011627 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 }
11629 else
11630 n = FALSE;
11631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633}
11634
11635/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011636 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 * rights to write into.
11638 */
11639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011640f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011642 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643}
11644
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011645 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011646findfilendir(
11647 typval_T *argvars UNUSED,
11648 typval_T *rettv,
11649 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011650{
11651#ifdef FEAT_SEARCHPATH
11652 char_u *fname;
11653 char_u *fresult = NULL;
11654 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11655 char_u *p;
11656 char_u pathbuf[NUMBUFLEN];
11657 int count = 1;
11658 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011659 int error = FALSE;
11660#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011661
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011662 rettv->vval.v_string = NULL;
11663 rettv->v_type = VAR_STRING;
11664
11665#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011667
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011668 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011670 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11671 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011672 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 else
11674 {
11675 if (*p != NUL)
11676 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011678 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011679 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011680 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011681 }
11682
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011683 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11684 error = TRUE;
11685
11686 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011688 do
11689 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011690 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011691 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011692 fresult = find_file_in_path_option(first ? fname : NULL,
11693 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011694 0, first, path,
11695 find_what,
11696 curbuf->b_ffname,
11697 find_what == FINDFILE_DIR
11698 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011700
11701 if (fresult != NULL && rettv->v_type == VAR_LIST)
11702 list_append_string(rettv->vval.v_list, fresult, -1);
11703
11704 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011706
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011707 if (rettv->v_type == VAR_STRING)
11708 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011709#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011710}
11711
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011712static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11713static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011714
11715/*
11716 * Implementation of map() and filter().
11717 */
11718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011719filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011720{
11721 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011722 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011723 listitem_T *li, *nli;
11724 list_T *l = NULL;
11725 dictitem_T *di;
11726 hashtab_T *ht;
11727 hashitem_T *hi;
11728 dict_T *d = NULL;
11729 typval_T save_val;
11730 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011731 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011732 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011733 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011734 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011735 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011736 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011737 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011738
Bram Moolenaare9a41262005-01-15 22:18:47 +000011739 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011740 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011741 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011742 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011743 return;
11744 }
11745 else if (argvars[0].v_type == VAR_DICT)
11746 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011747 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011748 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011749 return;
11750 }
11751 else
11752 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011753 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011754 return;
11755 }
11756
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011757 expr = get_tv_string_buf_chk(&argvars[1], buf);
11758 /* On type errors, the preceding call has already displayed an error
11759 * message. Avoid a misleading error message for an empty string that
11760 * was not passed as argument. */
11761 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011762 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011763 prepare_vimvar(VV_VAL, &save_val);
11764 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011765
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011766 /* We reset "did_emsg" to be able to detect whether an error
11767 * occurred during evaluation of the expression. */
11768 save_did_emsg = did_emsg;
11769 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011770
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011771 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011772 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011774 vimvars[VV_KEY].vv_type = VAR_STRING;
11775
11776 ht = &d->dv_hashtab;
11777 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011778 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011779 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011781 if (!HASHITEM_EMPTY(hi))
11782 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011783 int r;
11784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 --todo;
11786 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011787 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011788 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11789 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011790 break;
11791 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011792 r = filter_map_one(&di->di_tv, expr, map, &rem);
11793 clear_tv(&vimvars[VV_KEY].vv_tv);
11794 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011795 break;
11796 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011797 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011798 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11799 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011800 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011802 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011803 }
11804 }
11805 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011806 }
11807 else
11808 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011809 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011811 for (li = l->lv_first; li != NULL; li = nli)
11812 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011813 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011814 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011816 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011817 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011818 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011819 break;
11820 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011821 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011822 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011823 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011824 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011825
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011826 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011827 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011828
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011829 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011831
11832 copy_tv(&argvars[0], rettv);
11833}
11834
11835 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011836filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011837{
Bram Moolenaar33570922005-01-25 22:26:29 +000011838 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011839 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011840 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011841
Bram Moolenaar33570922005-01-25 22:26:29 +000011842 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011843 s = expr;
11844 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011845 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011846 if (*s != NUL) /* check for trailing chars after expr */
11847 {
11848 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011849 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011850 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011851 }
11852 if (map)
11853 {
11854 /* map(): replace the list item value */
11855 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011856 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011857 *tv = rettv;
11858 }
11859 else
11860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011861 int error = FALSE;
11862
Bram Moolenaare9a41262005-01-15 22:18:47 +000011863 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011864 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011865 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 /* On type error, nothing has been removed; return FAIL to stop the
11867 * loop. The error message was given by get_tv_number_chk(). */
11868 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011869 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011871 retval = OK;
11872theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011873 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011874 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011875}
11876
11877/*
11878 * "filter()" function
11879 */
11880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011881f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011882{
11883 filter_map(argvars, rettv, FALSE);
11884}
11885
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011886/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011887 * "finddir({fname}[, {path}[, {count}]])" function
11888 */
11889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011890f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011891{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011892 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011893}
11894
11895/*
11896 * "findfile({fname}[, {path}[, {count}]])" function
11897 */
11898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011899f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011900{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011901 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011902}
11903
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011904#ifdef FEAT_FLOAT
11905/*
11906 * "float2nr({float})" function
11907 */
11908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011909f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011910{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011911 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011912
11913 if (get_float_arg(argvars, &f) == OK)
11914 {
11915 if (f < -0x7fffffff)
11916 rettv->vval.v_number = -0x7fffffff;
11917 else if (f > 0x7fffffff)
11918 rettv->vval.v_number = 0x7fffffff;
11919 else
11920 rettv->vval.v_number = (varnumber_T)f;
11921 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011922}
11923
11924/*
11925 * "floor({float})" function
11926 */
11927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011928f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011929{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011930 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011931
11932 rettv->v_type = VAR_FLOAT;
11933 if (get_float_arg(argvars, &f) == OK)
11934 rettv->vval.v_float = floor(f);
11935 else
11936 rettv->vval.v_float = 0.0;
11937}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011938
11939/*
11940 * "fmod()" function
11941 */
11942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011943f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011944{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011945 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011946
11947 rettv->v_type = VAR_FLOAT;
11948 if (get_float_arg(argvars, &fx) == OK
11949 && get_float_arg(&argvars[1], &fy) == OK)
11950 rettv->vval.v_float = fmod(fx, fy);
11951 else
11952 rettv->vval.v_float = 0.0;
11953}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011954#endif
11955
Bram Moolenaar0d660222005-01-07 21:51:51 +000011956/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011957 * "fnameescape({string})" function
11958 */
11959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011960f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011961{
11962 rettv->vval.v_string = vim_strsave_fnameescape(
11963 get_tv_string(&argvars[0]), FALSE);
11964 rettv->v_type = VAR_STRING;
11965}
11966
11967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 * "fnamemodify({fname}, {mods})" function
11969 */
11970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011971f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972{
11973 char_u *fname;
11974 char_u *mods;
11975 int usedlen = 0;
11976 int len;
11977 char_u *fbuf = NULL;
11978 char_u buf[NUMBUFLEN];
11979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011980 fname = get_tv_string_chk(&argvars[0]);
11981 mods = get_tv_string_buf_chk(&argvars[1], buf);
11982 if (fname == NULL || mods == NULL)
11983 fname = NULL;
11984 else
11985 {
11986 len = (int)STRLEN(fname);
11987 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011990 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011992 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011994 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 vim_free(fbuf);
11996}
11997
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011998static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999
12000/*
12001 * "foldclosed()" function
12002 */
12003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012004foldclosed_both(
12005 typval_T *argvars UNUSED,
12006 typval_T *rettv,
12007 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008{
12009#ifdef FEAT_FOLDING
12010 linenr_T lnum;
12011 linenr_T first, last;
12012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012013 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12015 {
12016 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12017 {
12018 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 return;
12023 }
12024 }
12025#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027}
12028
12029/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012030 * "foldclosed()" function
12031 */
12032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012033f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012034{
12035 foldclosed_both(argvars, rettv, FALSE);
12036}
12037
12038/*
12039 * "foldclosedend()" function
12040 */
12041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012042f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012043{
12044 foldclosed_both(argvars, rettv, TRUE);
12045}
12046
12047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 * "foldlevel()" function
12049 */
12050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012051f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052{
12053#ifdef FEAT_FOLDING
12054 linenr_T lnum;
12055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012056 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060}
12061
12062/*
12063 * "foldtext()" function
12064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012066f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067{
12068#ifdef FEAT_FOLDING
12069 linenr_T lnum;
12070 char_u *s;
12071 char_u *r;
12072 int len;
12073 char *txt;
12074#endif
12075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076 rettv->v_type = VAR_STRING;
12077 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012079 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12080 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12081 <= curbuf->b_ml.ml_line_count
12082 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 {
12084 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012085 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12086 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 {
12088 if (!linewhite(lnum))
12089 break;
12090 ++lnum;
12091 }
12092
12093 /* Find interesting text in this line. */
12094 s = skipwhite(ml_get(lnum));
12095 /* skip C comment-start */
12096 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012099 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012100 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012101 {
12102 s = skipwhite(ml_get(lnum + 1));
12103 if (*s == '*')
12104 s = skipwhite(s + 1);
12105 }
12106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 txt = _("+-%s%3ld lines: ");
12108 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012109 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110 + 20 /* for %3ld */
12111 + STRLEN(s))); /* concatenated */
12112 if (r != NULL)
12113 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012114 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12115 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12116 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117 len = (int)STRLEN(r);
12118 STRCAT(r, s);
12119 /* remove 'foldmarker' and 'commentstring' */
12120 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 }
12123 }
12124#endif
12125}
12126
12127/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012128 * "foldtextresult(lnum)" function
12129 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012131f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012132{
12133#ifdef FEAT_FOLDING
12134 linenr_T lnum;
12135 char_u *text;
12136 char_u buf[51];
12137 foldinfo_T foldinfo;
12138 int fold_count;
12139#endif
12140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012141 rettv->v_type = VAR_STRING;
12142 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012143#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012144 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012145 /* treat illegal types and illegal string values for {lnum} the same */
12146 if (lnum < 0)
12147 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012148 fold_count = foldedCount(curwin, lnum, &foldinfo);
12149 if (fold_count > 0)
12150 {
12151 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12152 &foldinfo, buf);
12153 if (text == buf)
12154 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012156 }
12157#endif
12158}
12159
12160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 * "foreground()" function
12162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012164f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166#ifdef FEAT_GUI
12167 if (gui.in_use)
12168 gui_mch_set_foreground();
12169#else
12170# ifdef WIN32
12171 win32_set_foreground();
12172# endif
12173#endif
12174}
12175
12176/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012177 * "function()" function
12178 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012180f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012181{
12182 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012183 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012184 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012185 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012186
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012187 if (argvars[0].v_type == VAR_FUNC)
12188 {
12189 /* function(MyFunc, [arg], dict) */
12190 s = argvars[0].vval.v_string;
12191 }
12192 else if (argvars[0].v_type == VAR_PARTIAL
12193 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012194 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012195 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012196 arg_pt = argvars[0].vval.v_partial;
12197 s = arg_pt->pt_name;
12198 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012199 else
12200 {
12201 /* function('MyFunc', [arg], dict) */
12202 s = get_tv_string(&argvars[0]);
12203 use_string = TRUE;
12204 }
12205
12206 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012207 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012208 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012209 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12210 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012211 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012212 else
12213 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012214 int dict_idx = 0;
12215 int arg_idx = 0;
12216 list_T *list = NULL;
12217
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012218 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012219 {
12220 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012221 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012222
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012223 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12224 * also be called from another script. Using trans_function_name()
12225 * would also work, but some plugins depend on the name being
12226 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012227 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012228 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12229 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012230 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012231 STRCPY(name, sid_buf);
12232 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012233 }
12234 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012235 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012236 name = vim_strsave(s);
12237
12238 if (argvars[1].v_type != VAR_UNKNOWN)
12239 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012240 if (argvars[2].v_type != VAR_UNKNOWN)
12241 {
12242 /* function(name, [args], dict) */
12243 arg_idx = 1;
12244 dict_idx = 2;
12245 }
12246 else if (argvars[1].v_type == VAR_DICT)
12247 /* function(name, dict) */
12248 dict_idx = 1;
12249 else
12250 /* function(name, [args]) */
12251 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012252 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012253 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012254 if (argvars[dict_idx].v_type != VAR_DICT)
12255 {
12256 EMSG(_("E922: expected a dict"));
12257 vim_free(name);
12258 return;
12259 }
12260 if (argvars[dict_idx].vval.v_dict == NULL)
12261 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012262 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012263 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012264 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012265 if (argvars[arg_idx].v_type != VAR_LIST)
12266 {
12267 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12268 vim_free(name);
12269 return;
12270 }
12271 list = argvars[arg_idx].vval.v_list;
12272 if (list == NULL || list->lv_len == 0)
12273 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012274 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012275 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012276 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012277 {
12278 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012279
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012280 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012281 if (pt == NULL)
12282 vim_free(name);
12283 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012284 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012285 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012286 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012287 listitem_T *li;
12288 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012289 int arg_len = 0;
12290 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012291
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012292 if (arg_pt != NULL)
12293 arg_len = arg_pt->pt_argc;
12294 if (list != NULL)
12295 lv_len = list->lv_len;
12296 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012297 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012298 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012299 if (pt->pt_argv == NULL)
12300 {
12301 vim_free(pt);
12302 vim_free(name);
12303 return;
12304 }
12305 else
12306 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012307 for (i = 0; i < arg_len; i++)
12308 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12309 if (lv_len > 0)
12310 for (li = list->lv_first; li != NULL;
12311 li = li->li_next)
12312 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012313 }
12314 }
12315
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012316 /* For "function(dict.func, [], dict)" and "func" is a partial
12317 * use "dict". That is backwards compatible. */
12318 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012319 {
12320 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12321 ++pt->pt_dict->dv_refcount;
12322 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012323 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012324 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012325 pt->pt_dict = arg_pt->pt_dict;
12326 if (pt->pt_dict != NULL)
12327 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012328 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012329
12330 pt->pt_refcount = 1;
12331 pt->pt_name = name;
12332 func_ref(pt->pt_name);
12333 }
12334 rettv->v_type = VAR_PARTIAL;
12335 rettv->vval.v_partial = pt;
12336 }
12337 else
12338 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012339 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012340 rettv->v_type = VAR_FUNC;
12341 rettv->vval.v_string = name;
12342 func_ref(name);
12343 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012344 }
12345}
12346
12347/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012348 * "garbagecollect()" function
12349 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012351f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012352{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012353 /* This is postponed until we are back at the toplevel, because we may be
12354 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12355 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012356
12357 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12358 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012359}
12360
12361/*
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +020012362 * "garbagecollect_for_testing()" function
12363 */
12364 static void
12365f_garbagecollect_for_testing(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12366{
12367 /* This is dangerous, any Lists and Dicts used internally may be freed
12368 * while still in use. */
12369 garbage_collect(TRUE);
12370}
12371
12372/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012373 * "get()" function
12374 */
12375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012376f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012377{
Bram Moolenaar33570922005-01-25 22:26:29 +000012378 listitem_T *li;
12379 list_T *l;
12380 dictitem_T *di;
12381 dict_T *d;
12382 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012383
Bram Moolenaare9a41262005-01-15 22:18:47 +000012384 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012385 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012386 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 int error = FALSE;
12389
12390 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12391 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012392 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012393 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012394 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012395 else if (argvars[0].v_type == VAR_DICT)
12396 {
12397 if ((d = argvars[0].vval.v_dict) != NULL)
12398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012399 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012400 if (di != NULL)
12401 tv = &di->di_tv;
12402 }
12403 }
12404 else
12405 EMSG2(_(e_listdictarg), "get()");
12406
12407 if (tv == NULL)
12408 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012409 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012410 copy_tv(&argvars[2], rettv);
12411 }
12412 else
12413 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012414}
12415
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012416static 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 +000012417
12418/*
12419 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012420 * Return a range (from start to end) of lines in rettv from the specified
12421 * buffer.
12422 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012423 */
12424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012425get_buffer_lines(
12426 buf_T *buf,
12427 linenr_T start,
12428 linenr_T end,
12429 int retlist,
12430 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012431{
12432 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012433
Bram Moolenaar959a1432013-12-14 12:17:38 +010012434 rettv->v_type = VAR_STRING;
12435 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012436 if (retlist && rettv_list_alloc(rettv) == FAIL)
12437 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012438
12439 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12440 return;
12441
12442 if (!retlist)
12443 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012444 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12445 p = ml_get_buf(buf, start, FALSE);
12446 else
12447 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012448 rettv->vval.v_string = vim_strsave(p);
12449 }
12450 else
12451 {
12452 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012453 return;
12454
12455 if (start < 1)
12456 start = 1;
12457 if (end > buf->b_ml.ml_line_count)
12458 end = buf->b_ml.ml_line_count;
12459 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012460 if (list_append_string(rettv->vval.v_list,
12461 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012462 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012463 }
12464}
12465
12466/*
12467 * "getbufline()" function
12468 */
12469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012470f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012471{
12472 linenr_T lnum;
12473 linenr_T end;
12474 buf_T *buf;
12475
12476 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12477 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012478 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012479 --emsg_off;
12480
Bram Moolenaar661b1822005-07-28 22:36:45 +000012481 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012482 if (argvars[2].v_type == VAR_UNKNOWN)
12483 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012484 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012485 end = get_tv_lnum_buf(&argvars[2], buf);
12486
Bram Moolenaar342337a2005-07-21 21:11:17 +000012487 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012488}
12489
Bram Moolenaar0d660222005-01-07 21:51:51 +000012490/*
12491 * "getbufvar()" function
12492 */
12493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012494f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012495{
12496 buf_T *buf;
12497 buf_T *save_curbuf;
12498 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012499 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012500 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012502 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12503 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012504 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012505 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012506
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012507 rettv->v_type = VAR_STRING;
12508 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012509
12510 if (buf != NULL && varname != NULL)
12511 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012512 /* set curbuf to be our buf, temporarily */
12513 save_curbuf = curbuf;
12514 curbuf = buf;
12515
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012517 {
12518 if (get_option_tv(&varname, rettv, TRUE) == OK)
12519 done = TRUE;
12520 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012521 else if (STRCMP(varname, "changedtick") == 0)
12522 {
12523 rettv->v_type = VAR_NUMBER;
12524 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012525 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012526 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012527 else
12528 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012529 /* Look up the variable. */
12530 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12531 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12532 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012533 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012534 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012535 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012536 done = TRUE;
12537 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012539
12540 /* restore previous notion of curbuf */
12541 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542 }
12543
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012544 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12545 /* use the default value */
12546 copy_tv(&argvars[2], rettv);
12547
Bram Moolenaar0d660222005-01-07 21:51:51 +000012548 --emsg_off;
12549}
12550
12551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552 * "getchar()" function
12553 */
12554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012555f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556{
12557 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012558 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012560 /* Position the cursor. Needed after a message that ends in a space. */
12561 windgoto(msg_row, msg_col);
12562
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563 ++no_mapping;
12564 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012565 for (;;)
12566 {
12567 if (argvars[0].v_type == VAR_UNKNOWN)
12568 /* getchar(): blocking wait. */
12569 n = safe_vgetc();
12570 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12571 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012572 n = vpeekc_any();
12573 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012574 /* illegal argument or getchar(0) and no char avail: return zero */
12575 n = 0;
12576 else
12577 /* getchar(0) and char avail: return char */
12578 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012579
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012580 if (n == K_IGNORE)
12581 continue;
12582 break;
12583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 --no_mapping;
12585 --allow_keys;
12586
Bram Moolenaar219b8702006-11-01 14:32:36 +000012587 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12588 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12589 vimvars[VV_MOUSE_COL].vv_nr = 0;
12590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 if (IS_SPECIAL(n) || mod_mask != 0)
12593 {
12594 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12595 int i = 0;
12596
12597 /* Turn a special key into three bytes, plus modifier. */
12598 if (mod_mask != 0)
12599 {
12600 temp[i++] = K_SPECIAL;
12601 temp[i++] = KS_MODIFIER;
12602 temp[i++] = mod_mask;
12603 }
12604 if (IS_SPECIAL(n))
12605 {
12606 temp[i++] = K_SPECIAL;
12607 temp[i++] = K_SECOND(n);
12608 temp[i++] = K_THIRD(n);
12609 }
12610#ifdef FEAT_MBYTE
12611 else if (has_mbyte)
12612 i += (*mb_char2bytes)(n, temp + i);
12613#endif
12614 else
12615 temp[i++] = n;
12616 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012617 rettv->v_type = VAR_STRING;
12618 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012619
12620#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012621 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012622 {
12623 int row = mouse_row;
12624 int col = mouse_col;
12625 win_T *win;
12626 linenr_T lnum;
12627# ifdef FEAT_WINDOWS
12628 win_T *wp;
12629# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012630 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012631
12632 if (row >= 0 && col >= 0)
12633 {
12634 /* Find the window at the mouse coordinates and compute the
12635 * text position. */
12636 win = mouse_find_win(&row, &col);
12637 (void)mouse_comp_pos(win, &row, &col, &lnum);
12638# ifdef FEAT_WINDOWS
12639 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012640 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012641# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012642 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012643 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12644 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12645 }
12646 }
12647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 }
12649}
12650
12651/*
12652 * "getcharmod()" function
12653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012655f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658}
12659
12660/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012661 * "getcharsearch()" function
12662 */
12663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012664f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012665{
12666 if (rettv_dict_alloc(rettv) != FAIL)
12667 {
12668 dict_T *dict = rettv->vval.v_dict;
12669
12670 dict_add_nr_str(dict, "char", 0L, last_csearch());
12671 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12672 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12673 }
12674}
12675
12676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 * "getcmdline()" function
12678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012680f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 rettv->v_type = VAR_STRING;
12683 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684}
12685
12686/*
12687 * "getcmdpos()" function
12688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012690f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693}
12694
12695/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012696 * "getcmdtype()" function
12697 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012699f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012700{
12701 rettv->v_type = VAR_STRING;
12702 rettv->vval.v_string = alloc(2);
12703 if (rettv->vval.v_string != NULL)
12704 {
12705 rettv->vval.v_string[0] = get_cmdline_type();
12706 rettv->vval.v_string[1] = NUL;
12707 }
12708}
12709
12710/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012711 * "getcmdwintype()" function
12712 */
12713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012714f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012715{
12716 rettv->v_type = VAR_STRING;
12717 rettv->vval.v_string = NULL;
12718#ifdef FEAT_CMDWIN
12719 rettv->vval.v_string = alloc(2);
12720 if (rettv->vval.v_string != NULL)
12721 {
12722 rettv->vval.v_string[0] = cmdwin_type;
12723 rettv->vval.v_string[1] = NUL;
12724 }
12725#endif
12726}
12727
12728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 * "getcwd()" function
12730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012732f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012734 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012735 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012737 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012738 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012739
12740 wp = find_tabwin(&argvars[0], &argvars[1]);
12741 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012743 if (wp->w_localdir != NULL)
12744 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12745 else if(globaldir != NULL)
12746 rettv->vval.v_string = vim_strsave(globaldir);
12747 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012748 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012749 cwd = alloc(MAXPATHL);
12750 if (cwd != NULL)
12751 {
12752 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12753 rettv->vval.v_string = vim_strsave(cwd);
12754 vim_free(cwd);
12755 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012756 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012757#ifdef BACKSLASH_IN_FILENAME
12758 if (rettv->vval.v_string != NULL)
12759 slash_adjust(rettv->vval.v_string);
12760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 }
12762}
12763
12764/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012765 * "getfontname()" function
12766 */
12767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012768f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012769{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->v_type = VAR_STRING;
12771 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012772#ifdef FEAT_GUI
12773 if (gui.in_use)
12774 {
12775 GuiFont font;
12776 char_u *name = NULL;
12777
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012778 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012779 {
12780 /* Get the "Normal" font. Either the name saved by
12781 * hl_set_font_name() or from the font ID. */
12782 font = gui.norm_font;
12783 name = hl_get_font_name();
12784 }
12785 else
12786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012788 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12789 return;
12790 font = gui_mch_get_font(name, FALSE);
12791 if (font == NOFONT)
12792 return; /* Invalid font name, return empty string. */
12793 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012795 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012796 gui_mch_free_font(font);
12797 }
12798#endif
12799}
12800
12801/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012802 * "getfperm({fname})" function
12803 */
12804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012805f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012806{
12807 char_u *fname;
12808 struct stat st;
12809 char_u *perm = NULL;
12810 char_u flags[] = "rwx";
12811 int i;
12812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012816 if (mch_stat((char *)fname, &st) >= 0)
12817 {
12818 perm = vim_strsave((char_u *)"---------");
12819 if (perm != NULL)
12820 {
12821 for (i = 0; i < 9; i++)
12822 {
12823 if (st.st_mode & (1 << (8 - i)))
12824 perm[i] = flags[i % 3];
12825 }
12826 }
12827 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012829}
12830
12831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832 * "getfsize({fname})" function
12833 */
12834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012835f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836{
12837 char_u *fname;
12838 struct stat st;
12839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012840 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843
12844 if (mch_stat((char *)fname, &st) >= 0)
12845 {
12846 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012851
12852 /* non-perfect check for overflow */
12853 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12854 rettv->vval.v_number = -2;
12855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 }
12857 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012858 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859}
12860
12861/*
12862 * "getftime({fname})" function
12863 */
12864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012865f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866{
12867 char_u *fname;
12868 struct stat st;
12869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012870 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871
12872 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876}
12877
12878/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012879 * "getftype({fname})" function
12880 */
12881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012882f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012883{
12884 char_u *fname;
12885 struct stat st;
12886 char_u *type = NULL;
12887 char *t;
12888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012892 if (mch_lstat((char *)fname, &st) >= 0)
12893 {
12894#ifdef S_ISREG
12895 if (S_ISREG(st.st_mode))
12896 t = "file";
12897 else if (S_ISDIR(st.st_mode))
12898 t = "dir";
12899# ifdef S_ISLNK
12900 else if (S_ISLNK(st.st_mode))
12901 t = "link";
12902# endif
12903# ifdef S_ISBLK
12904 else if (S_ISBLK(st.st_mode))
12905 t = "bdev";
12906# endif
12907# ifdef S_ISCHR
12908 else if (S_ISCHR(st.st_mode))
12909 t = "cdev";
12910# endif
12911# ifdef S_ISFIFO
12912 else if (S_ISFIFO(st.st_mode))
12913 t = "fifo";
12914# endif
12915# ifdef S_ISSOCK
12916 else if (S_ISSOCK(st.st_mode))
12917 t = "fifo";
12918# endif
12919 else
12920 t = "other";
12921#else
12922# ifdef S_IFMT
12923 switch (st.st_mode & S_IFMT)
12924 {
12925 case S_IFREG: t = "file"; break;
12926 case S_IFDIR: t = "dir"; break;
12927# ifdef S_IFLNK
12928 case S_IFLNK: t = "link"; break;
12929# endif
12930# ifdef S_IFBLK
12931 case S_IFBLK: t = "bdev"; break;
12932# endif
12933# ifdef S_IFCHR
12934 case S_IFCHR: t = "cdev"; break;
12935# endif
12936# ifdef S_IFIFO
12937 case S_IFIFO: t = "fifo"; break;
12938# endif
12939# ifdef S_IFSOCK
12940 case S_IFSOCK: t = "socket"; break;
12941# endif
12942 default: t = "other";
12943 }
12944# else
12945 if (mch_isdir(fname))
12946 t = "dir";
12947 else
12948 t = "file";
12949# endif
12950#endif
12951 type = vim_strsave((char_u *)t);
12952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012953 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012954}
12955
12956/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012957 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012958 */
12959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012960f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012961{
12962 linenr_T lnum;
12963 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012964 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012965
12966 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012967 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012968 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012969 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012970 retlist = FALSE;
12971 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012972 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012973 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012974 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012975 retlist = TRUE;
12976 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012977
Bram Moolenaar342337a2005-07-21 21:11:17 +000012978 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012979}
12980
12981/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012982 * "getmatches()" function
12983 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012985f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012986{
12987#ifdef FEAT_SEARCH_EXTRA
12988 dict_T *dict;
12989 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012990 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012991
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012992 if (rettv_list_alloc(rettv) == OK)
12993 {
12994 while (cur != NULL)
12995 {
12996 dict = dict_alloc();
12997 if (dict == NULL)
12998 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012999 if (cur->match.regprog == NULL)
13000 {
13001 /* match added with matchaddpos() */
13002 for (i = 0; i < MAXPOSMATCH; ++i)
13003 {
13004 llpos_T *llpos;
13005 char buf[6];
13006 list_T *l;
13007
13008 llpos = &cur->pos.pos[i];
13009 if (llpos->lnum == 0)
13010 break;
13011 l = list_alloc();
13012 if (l == NULL)
13013 break;
13014 list_append_number(l, (varnumber_T)llpos->lnum);
13015 if (llpos->col > 0)
13016 {
13017 list_append_number(l, (varnumber_T)llpos->col);
13018 list_append_number(l, (varnumber_T)llpos->len);
13019 }
13020 sprintf(buf, "pos%d", i + 1);
13021 dict_add_list(dict, buf, l);
13022 }
13023 }
13024 else
13025 {
13026 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13027 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013028 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013029 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13030 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013031# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013032 if (cur->conceal_char)
13033 {
13034 char_u buf[MB_MAXBYTES + 1];
13035
13036 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13037 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13038 }
13039# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013040 list_append_dict(rettv->vval.v_list, dict);
13041 cur = cur->next;
13042 }
13043 }
13044#endif
13045}
13046
13047/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013048 * "getpid()" function
13049 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013051f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013052{
13053 rettv->vval.v_number = mch_get_pid();
13054}
13055
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013057getpos_both(
13058 typval_T *argvars,
13059 typval_T *rettv,
13060 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013061{
Bram Moolenaara5525202006-03-02 22:52:09 +000013062 pos_T *fp;
13063 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013064 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013065
13066 if (rettv_list_alloc(rettv) == OK)
13067 {
13068 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013069 if (getcurpos)
13070 fp = &curwin->w_cursor;
13071 else
13072 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013073 if (fnum != -1)
13074 list_append_number(l, (varnumber_T)fnum);
13075 else
13076 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013077 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13078 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013079 list_append_number(l, (fp != NULL)
13080 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013081 : (varnumber_T)0);
13082 list_append_number(l,
13083#ifdef FEAT_VIRTUALEDIT
13084 (fp != NULL) ? (varnumber_T)fp->coladd :
13085#endif
13086 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013087 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013088 {
13089 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013090 list_append_number(l, curwin->w_curswant == MAXCOL ?
13091 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013092 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013093 }
13094 else
13095 rettv->vval.v_number = FALSE;
13096}
13097
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013098
13099/*
13100 * "getcurpos()" function
13101 */
13102 static void
13103f_getcurpos(typval_T *argvars, typval_T *rettv)
13104{
13105 getpos_both(argvars, rettv, TRUE);
13106}
13107
13108/*
13109 * "getpos(string)" function
13110 */
13111 static void
13112f_getpos(typval_T *argvars, typval_T *rettv)
13113{
13114 getpos_both(argvars, rettv, FALSE);
13115}
13116
Bram Moolenaara5525202006-03-02 22:52:09 +000013117/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013118 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013119 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013121f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013122{
13123#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013124 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013125#endif
13126
Bram Moolenaar2641f772005-03-25 21:58:17 +000013127#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013128 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013129 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013130 wp = NULL;
13131 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13132 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013133 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013134 if (wp == NULL)
13135 return;
13136 }
13137
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013138 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013139 }
13140#endif
13141}
13142
13143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 * "getreg()" function
13145 */
13146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013147f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148{
13149 char_u *strregname;
13150 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013151 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013152 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013153 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013155 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013157 strregname = get_tv_string_chk(&argvars[0]);
13158 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013159 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013161 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013162 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13163 return_list = get_tv_number_chk(&argvars[2], &error);
13164 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013167 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013168
13169 if (error)
13170 return;
13171
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 regname = (strregname == NULL ? '"' : *strregname);
13173 if (regname == 0)
13174 regname = '"';
13175
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013176 if (return_list)
13177 {
13178 rettv->v_type = VAR_LIST;
13179 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13180 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013181 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013182 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013183 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013184 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013185 }
13186 else
13187 {
13188 rettv->v_type = VAR_STRING;
13189 rettv->vval.v_string = get_reg_contents(regname,
13190 arg2 ? GREG_EXPR_SRC : 0);
13191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192}
13193
13194/*
13195 * "getregtype()" function
13196 */
13197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013198f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199{
13200 char_u *strregname;
13201 int regname;
13202 char_u buf[NUMBUFLEN + 2];
13203 long reglen = 0;
13204
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013205 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013206 {
13207 strregname = get_tv_string_chk(&argvars[0]);
13208 if (strregname == NULL) /* type error; errmsg already given */
13209 {
13210 rettv->v_type = VAR_STRING;
13211 rettv->vval.v_string = NULL;
13212 return;
13213 }
13214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 else
13216 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013217 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218
13219 regname = (strregname == NULL ? '"' : *strregname);
13220 if (regname == 0)
13221 regname = '"';
13222
13223 buf[0] = NUL;
13224 buf[1] = NUL;
13225 switch (get_reg_type(regname, &reglen))
13226 {
13227 case MLINE: buf[0] = 'V'; break;
13228 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 case MBLOCK:
13230 buf[0] = Ctrl_V;
13231 sprintf((char *)buf + 1, "%ld", reglen + 1);
13232 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234 rettv->v_type = VAR_STRING;
13235 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236}
13237
13238/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013239 * "gettabvar()" function
13240 */
13241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013242f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013243{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013244 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013245 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013246 dictitem_T *v;
13247 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013248 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013249
13250 rettv->v_type = VAR_STRING;
13251 rettv->vval.v_string = NULL;
13252
13253 varname = get_tv_string_chk(&argvars[1]);
13254 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13255 if (tp != NULL && varname != NULL)
13256 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013257 /* Set tp to be our tabpage, temporarily. Also set the window to the
13258 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013259 if (switch_win(&oldcurwin, &oldtabpage,
13260 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013261 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013262 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013263 /* look up the variable */
13264 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13265 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13266 if (v != NULL)
13267 {
13268 copy_tv(&v->di_tv, rettv);
13269 done = TRUE;
13270 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013271 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013272
13273 /* restore previous notion of curwin */
13274 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013275 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013276
13277 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013278 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013279}
13280
13281/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013282 * "gettabwinvar()" function
13283 */
13284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013285f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013286{
13287 getwinvar(argvars, rettv, 1);
13288}
13289
13290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291 * "getwinposx()" function
13292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013294f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013296 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297#ifdef FEAT_GUI
13298 if (gui.in_use)
13299 {
13300 int x, y;
13301
13302 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013303 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 }
13305#endif
13306}
13307
13308/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013309 * "win_findbuf()" function
13310 */
13311 static void
13312f_win_findbuf(typval_T *argvars, typval_T *rettv)
13313{
13314 if (rettv_list_alloc(rettv) != FAIL)
13315 win_findbuf(argvars, rettv->vval.v_list);
13316}
13317
13318/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013319 * "win_getid()" function
13320 */
13321 static void
13322f_win_getid(typval_T *argvars, typval_T *rettv)
13323{
13324 rettv->vval.v_number = win_getid(argvars);
13325}
13326
13327/*
13328 * "win_gotoid()" function
13329 */
13330 static void
13331f_win_gotoid(typval_T *argvars, typval_T *rettv)
13332{
13333 rettv->vval.v_number = win_gotoid(argvars);
13334}
13335
13336/*
13337 * "win_id2tabwin()" function
13338 */
13339 static void
13340f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13341{
13342 if (rettv_list_alloc(rettv) != FAIL)
13343 win_id2tabwin(argvars, rettv->vval.v_list);
13344}
13345
13346/*
13347 * "win_id2win()" function
13348 */
13349 static void
13350f_win_id2win(typval_T *argvars, typval_T *rettv)
13351{
13352 rettv->vval.v_number = win_id2win(argvars);
13353}
13354
13355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356 * "getwinposy()" function
13357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013359f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362#ifdef FEAT_GUI
13363 if (gui.in_use)
13364 {
13365 int x, y;
13366
13367 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 }
13370#endif
13371}
13372
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013373/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013374 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013375 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013376 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013377find_win_by_nr(
13378 typval_T *vp,
13379 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013380{
13381#ifdef FEAT_WINDOWS
13382 win_T *wp;
13383#endif
13384 int nr;
13385
13386 nr = get_tv_number_chk(vp, NULL);
13387
13388#ifdef FEAT_WINDOWS
13389 if (nr < 0)
13390 return NULL;
13391 if (nr == 0)
13392 return curwin;
13393
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013394 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13395 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013396 if (--nr <= 0)
13397 break;
13398 return wp;
13399#else
13400 if (nr == 0 || nr == 1)
13401 return curwin;
13402 return NULL;
13403#endif
13404}
13405
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013407 * Find window specified by "wvp" in tabpage "tvp".
13408 */
13409 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013410find_tabwin(
13411 typval_T *wvp, /* VAR_UNKNOWN for current window */
13412 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013413{
13414 win_T *wp = NULL;
13415 tabpage_T *tp = NULL;
13416 long n;
13417
13418 if (wvp->v_type != VAR_UNKNOWN)
13419 {
13420 if (tvp->v_type != VAR_UNKNOWN)
13421 {
13422 n = get_tv_number(tvp);
13423 if (n >= 0)
13424 tp = find_tabpage(n);
13425 }
13426 else
13427 tp = curtab;
13428
13429 if (tp != NULL)
13430 wp = find_win_by_nr(wvp, tp);
13431 }
13432 else
13433 wp = curwin;
13434
13435 return wp;
13436}
13437
13438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 * "getwinvar()" function
13440 */
13441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013442f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013444 getwinvar(argvars, rettv, 0);
13445}
13446
13447/*
13448 * getwinvar() and gettabwinvar()
13449 */
13450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013451getwinvar(
13452 typval_T *argvars,
13453 typval_T *rettv,
13454 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013455{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013456 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013458 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013459 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013460 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013461#ifdef FEAT_WINDOWS
13462 win_T *oldcurwin;
13463 tabpage_T *oldtabpage;
13464 int need_switch_win;
13465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013467#ifdef FEAT_WINDOWS
13468 if (off == 1)
13469 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13470 else
13471 tp = curtab;
13472#endif
13473 win = find_win_by_nr(&argvars[off], tp);
13474 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013475 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013477 rettv->v_type = VAR_STRING;
13478 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479
13480 if (win != NULL && varname != NULL)
13481 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013482#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013483 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013484 * otherwise the window is not valid. Only do this when needed,
13485 * autocommands get blocked. */
13486 need_switch_win = !(tp == curtab && win == curwin);
13487 if (!need_switch_win
13488 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13489#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013490 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013491 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013492 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013493 if (get_option_tv(&varname, rettv, 1) == OK)
13494 done = TRUE;
13495 }
13496 else
13497 {
13498 /* Look up the variable. */
13499 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13500 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13501 varname, FALSE);
13502 if (v != NULL)
13503 {
13504 copy_tv(&v->di_tv, rettv);
13505 done = TRUE;
13506 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013509
Bram Moolenaarba117c22015-09-29 16:53:22 +020013510#ifdef FEAT_WINDOWS
13511 if (need_switch_win)
13512 /* restore previous notion of curwin */
13513 restore_win(oldcurwin, oldtabpage, TRUE);
13514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 }
13516
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013517 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13518 /* use the default return value */
13519 copy_tv(&argvars[off + 2], rettv);
13520
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 --emsg_off;
13522}
13523
13524/*
13525 * "glob()" function
13526 */
13527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013528f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013530 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013532 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013534 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013535 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013536 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013537 if (argvars[1].v_type != VAR_UNKNOWN)
13538 {
13539 if (get_tv_number_chk(&argvars[1], &error))
13540 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013541 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013542 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013543 if (get_tv_number_chk(&argvars[2], &error))
13544 {
13545 rettv->v_type = VAR_LIST;
13546 rettv->vval.v_list = NULL;
13547 }
13548 if (argvars[3].v_type != VAR_UNKNOWN
13549 && get_tv_number_chk(&argvars[3], &error))
13550 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013551 }
13552 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013553 if (!error)
13554 {
13555 ExpandInit(&xpc);
13556 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013557 if (p_wic)
13558 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013559 if (rettv->v_type == VAR_STRING)
13560 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013561 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013562 else if (rettv_list_alloc(rettv) != FAIL)
13563 {
13564 int i;
13565
13566 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13567 NULL, options, WILD_ALL_KEEP);
13568 for (i = 0; i < xpc.xp_numfiles; i++)
13569 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13570
13571 ExpandCleanup(&xpc);
13572 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013573 }
13574 else
13575 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576}
13577
13578/*
13579 * "globpath()" function
13580 */
13581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013582f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013584 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013587 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013588 garray_T ga;
13589 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013591 /* When the optional second argument is non-zero, don't remove matches
13592 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013593 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013594 if (argvars[2].v_type != VAR_UNKNOWN)
13595 {
13596 if (get_tv_number_chk(&argvars[2], &error))
13597 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013598 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013599 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013600 if (get_tv_number_chk(&argvars[3], &error))
13601 {
13602 rettv->v_type = VAR_LIST;
13603 rettv->vval.v_list = NULL;
13604 }
13605 if (argvars[4].v_type != VAR_UNKNOWN
13606 && get_tv_number_chk(&argvars[4], &error))
13607 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013608 }
13609 }
13610 if (file != NULL && !error)
13611 {
13612 ga_init2(&ga, (int)sizeof(char_u *), 10);
13613 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13614 if (rettv->v_type == VAR_STRING)
13615 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13616 else if (rettv_list_alloc(rettv) != FAIL)
13617 for (i = 0; i < ga.ga_len; ++i)
13618 list_append_string(rettv->vval.v_list,
13619 ((char_u **)(ga.ga_data))[i], -1);
13620 ga_clear_strings(&ga);
13621 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013622 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013623 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624}
13625
13626/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013627 * "glob2regpat()" function
13628 */
13629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013630f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013631{
13632 char_u *pat = get_tv_string_chk(&argvars[0]);
13633
13634 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013635 rettv->vval.v_string = (pat == NULL)
13636 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013637}
13638
13639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 * "has()" function
13641 */
13642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013643f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644{
13645 int i;
13646 char_u *name;
13647 int n = FALSE;
13648 static char *(has_list[]) =
13649 {
13650#ifdef AMIGA
13651 "amiga",
13652# ifdef FEAT_ARP
13653 "arp",
13654# endif
13655#endif
13656#ifdef __BEOS__
13657 "beos",
13658#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013659#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 "mac",
13661#endif
13662#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013663 "macunix", /* built with 'darwin' enabled */
13664#endif
13665#if defined(__APPLE__) && __APPLE__ == 1
13666 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668#ifdef __QNX__
13669 "qnx",
13670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671#ifdef UNIX
13672 "unix",
13673#endif
13674#ifdef VMS
13675 "vms",
13676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677#ifdef WIN32
13678 "win32",
13679#endif
13680#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13681 "win32unix",
13682#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013683#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684 "win64",
13685#endif
13686#ifdef EBCDIC
13687 "ebcdic",
13688#endif
13689#ifndef CASE_INSENSITIVE_FILENAME
13690 "fname_case",
13691#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013692#ifdef HAVE_ACL
13693 "acl",
13694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695#ifdef FEAT_ARABIC
13696 "arabic",
13697#endif
13698#ifdef FEAT_AUTOCMD
13699 "autocmd",
13700#endif
13701#ifdef FEAT_BEVAL
13702 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013703# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13704 "balloon_multiline",
13705# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706#endif
13707#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13708 "builtin_terms",
13709# ifdef ALL_BUILTIN_TCAPS
13710 "all_builtin_terms",
13711# endif
13712#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013713#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13714 || defined(FEAT_GUI_W32) \
13715 || defined(FEAT_GUI_MOTIF))
13716 "browsefilter",
13717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718#ifdef FEAT_BYTEOFF
13719 "byte_offset",
13720#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013721#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013722 "channel",
13723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724#ifdef FEAT_CINDENT
13725 "cindent",
13726#endif
13727#ifdef FEAT_CLIENTSERVER
13728 "clientserver",
13729#endif
13730#ifdef FEAT_CLIPBOARD
13731 "clipboard",
13732#endif
13733#ifdef FEAT_CMDL_COMPL
13734 "cmdline_compl",
13735#endif
13736#ifdef FEAT_CMDHIST
13737 "cmdline_hist",
13738#endif
13739#ifdef FEAT_COMMENTS
13740 "comments",
13741#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013742#ifdef FEAT_CONCEAL
13743 "conceal",
13744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745#ifdef FEAT_CRYPT
13746 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013747 "crypt-blowfish",
13748 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749#endif
13750#ifdef FEAT_CSCOPE
13751 "cscope",
13752#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013753#ifdef FEAT_CURSORBIND
13754 "cursorbind",
13755#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013756#ifdef CURSOR_SHAPE
13757 "cursorshape",
13758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759#ifdef DEBUG
13760 "debug",
13761#endif
13762#ifdef FEAT_CON_DIALOG
13763 "dialog_con",
13764#endif
13765#ifdef FEAT_GUI_DIALOG
13766 "dialog_gui",
13767#endif
13768#ifdef FEAT_DIFF
13769 "diff",
13770#endif
13771#ifdef FEAT_DIGRAPHS
13772 "digraphs",
13773#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013774#ifdef FEAT_DIRECTX
13775 "directx",
13776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777#ifdef FEAT_DND
13778 "dnd",
13779#endif
13780#ifdef FEAT_EMACS_TAGS
13781 "emacs_tags",
13782#endif
13783 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013784 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785#ifdef FEAT_SEARCH_EXTRA
13786 "extra_search",
13787#endif
13788#ifdef FEAT_FKMAP
13789 "farsi",
13790#endif
13791#ifdef FEAT_SEARCHPATH
13792 "file_in_path",
13793#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013794#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013795 "filterpipe",
13796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797#ifdef FEAT_FIND_ID
13798 "find_in_path",
13799#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013800#ifdef FEAT_FLOAT
13801 "float",
13802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803#ifdef FEAT_FOLDING
13804 "folding",
13805#endif
13806#ifdef FEAT_FOOTER
13807 "footer",
13808#endif
13809#if !defined(USE_SYSTEM) && defined(UNIX)
13810 "fork",
13811#endif
13812#ifdef FEAT_GETTEXT
13813 "gettext",
13814#endif
13815#ifdef FEAT_GUI
13816 "gui",
13817#endif
13818#ifdef FEAT_GUI_ATHENA
13819# ifdef FEAT_GUI_NEXTAW
13820 "gui_neXtaw",
13821# else
13822 "gui_athena",
13823# endif
13824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825#ifdef FEAT_GUI_GTK
13826 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013827# ifdef USE_GTK3
13828 "gui_gtk3",
13829# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013833#ifdef FEAT_GUI_GNOME
13834 "gui_gnome",
13835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836#ifdef FEAT_GUI_MAC
13837 "gui_mac",
13838#endif
13839#ifdef FEAT_GUI_MOTIF
13840 "gui_motif",
13841#endif
13842#ifdef FEAT_GUI_PHOTON
13843 "gui_photon",
13844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845#ifdef FEAT_GUI_W32
13846 "gui_win32",
13847#endif
13848#ifdef FEAT_HANGULIN
13849 "hangul_input",
13850#endif
13851#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13852 "iconv",
13853#endif
13854#ifdef FEAT_INS_EXPAND
13855 "insert_expand",
13856#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013857#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013858 "job",
13859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860#ifdef FEAT_JUMPLIST
13861 "jumplist",
13862#endif
13863#ifdef FEAT_KEYMAP
13864 "keymap",
13865#endif
13866#ifdef FEAT_LANGMAP
13867 "langmap",
13868#endif
13869#ifdef FEAT_LIBCALL
13870 "libcall",
13871#endif
13872#ifdef FEAT_LINEBREAK
13873 "linebreak",
13874#endif
13875#ifdef FEAT_LISP
13876 "lispindent",
13877#endif
13878#ifdef FEAT_LISTCMDS
13879 "listcmds",
13880#endif
13881#ifdef FEAT_LOCALMAP
13882 "localmap",
13883#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013884#ifdef FEAT_LUA
13885# ifndef DYNAMIC_LUA
13886 "lua",
13887# endif
13888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889#ifdef FEAT_MENU
13890 "menu",
13891#endif
13892#ifdef FEAT_SESSION
13893 "mksession",
13894#endif
13895#ifdef FEAT_MODIFY_FNAME
13896 "modify_fname",
13897#endif
13898#ifdef FEAT_MOUSE
13899 "mouse",
13900#endif
13901#ifdef FEAT_MOUSESHAPE
13902 "mouseshape",
13903#endif
13904#if defined(UNIX) || defined(VMS)
13905# ifdef FEAT_MOUSE_DEC
13906 "mouse_dec",
13907# endif
13908# ifdef FEAT_MOUSE_GPM
13909 "mouse_gpm",
13910# endif
13911# ifdef FEAT_MOUSE_JSB
13912 "mouse_jsbterm",
13913# endif
13914# ifdef FEAT_MOUSE_NET
13915 "mouse_netterm",
13916# endif
13917# ifdef FEAT_MOUSE_PTERM
13918 "mouse_pterm",
13919# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013920# ifdef FEAT_MOUSE_SGR
13921 "mouse_sgr",
13922# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013923# ifdef FEAT_SYSMOUSE
13924 "mouse_sysmouse",
13925# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013926# ifdef FEAT_MOUSE_URXVT
13927 "mouse_urxvt",
13928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929# ifdef FEAT_MOUSE_XTERM
13930 "mouse_xterm",
13931# endif
13932#endif
13933#ifdef FEAT_MBYTE
13934 "multi_byte",
13935#endif
13936#ifdef FEAT_MBYTE_IME
13937 "multi_byte_ime",
13938#endif
13939#ifdef FEAT_MULTI_LANG
13940 "multi_lang",
13941#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013942#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013943#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013944 "mzscheme",
13945#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947#ifdef FEAT_OLE
13948 "ole",
13949#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013950 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951#ifdef FEAT_PATH_EXTRA
13952 "path_extra",
13953#endif
13954#ifdef FEAT_PERL
13955#ifndef DYNAMIC_PERL
13956 "perl",
13957#endif
13958#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013959#ifdef FEAT_PERSISTENT_UNDO
13960 "persistent_undo",
13961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962#ifdef FEAT_PYTHON
13963#ifndef DYNAMIC_PYTHON
13964 "python",
13965#endif
13966#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013967#ifdef FEAT_PYTHON3
13968#ifndef DYNAMIC_PYTHON3
13969 "python3",
13970#endif
13971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972#ifdef FEAT_POSTSCRIPT
13973 "postscript",
13974#endif
13975#ifdef FEAT_PRINTER
13976 "printer",
13977#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013978#ifdef FEAT_PROFILE
13979 "profile",
13980#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013981#ifdef FEAT_RELTIME
13982 "reltime",
13983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984#ifdef FEAT_QUICKFIX
13985 "quickfix",
13986#endif
13987#ifdef FEAT_RIGHTLEFT
13988 "rightleft",
13989#endif
13990#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13991 "ruby",
13992#endif
13993#ifdef FEAT_SCROLLBIND
13994 "scrollbind",
13995#endif
13996#ifdef FEAT_CMDL_INFO
13997 "showcmd",
13998 "cmdline_info",
13999#endif
14000#ifdef FEAT_SIGNS
14001 "signs",
14002#endif
14003#ifdef FEAT_SMARTINDENT
14004 "smartindent",
14005#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014006#ifdef STARTUPTIME
14007 "startuptime",
14008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009#ifdef FEAT_STL_OPT
14010 "statusline",
14011#endif
14012#ifdef FEAT_SUN_WORKSHOP
14013 "sun_workshop",
14014#endif
14015#ifdef FEAT_NETBEANS_INTG
14016 "netbeans_intg",
14017#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014018#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014019 "spell",
14020#endif
14021#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 "syntax",
14023#endif
14024#if defined(USE_SYSTEM) || !defined(UNIX)
14025 "system",
14026#endif
14027#ifdef FEAT_TAG_BINS
14028 "tag_binary",
14029#endif
14030#ifdef FEAT_TAG_OLDSTATIC
14031 "tag_old_static",
14032#endif
14033#ifdef FEAT_TAG_ANYWHITE
14034 "tag_any_white",
14035#endif
14036#ifdef FEAT_TCL
14037# ifndef DYNAMIC_TCL
14038 "tcl",
14039# endif
14040#endif
14041#ifdef TERMINFO
14042 "terminfo",
14043#endif
14044#ifdef FEAT_TERMRESPONSE
14045 "termresponse",
14046#endif
14047#ifdef FEAT_TEXTOBJ
14048 "textobjects",
14049#endif
14050#ifdef HAVE_TGETENT
14051 "tgetent",
14052#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014053#ifdef FEAT_TIMERS
14054 "timers",
14055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056#ifdef FEAT_TITLE
14057 "title",
14058#endif
14059#ifdef FEAT_TOOLBAR
14060 "toolbar",
14061#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014062#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14063 "unnamedplus",
14064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065#ifdef FEAT_USR_CMDS
14066 "user-commands", /* was accidentally included in 5.4 */
14067 "user_commands",
14068#endif
14069#ifdef FEAT_VIMINFO
14070 "viminfo",
14071#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014072#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 "vertsplit",
14074#endif
14075#ifdef FEAT_VIRTUALEDIT
14076 "virtualedit",
14077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079#ifdef FEAT_VISUALEXTRA
14080 "visualextra",
14081#endif
14082#ifdef FEAT_VREPLACE
14083 "vreplace",
14084#endif
14085#ifdef FEAT_WILDIGN
14086 "wildignore",
14087#endif
14088#ifdef FEAT_WILDMENU
14089 "wildmenu",
14090#endif
14091#ifdef FEAT_WINDOWS
14092 "windows",
14093#endif
14094#ifdef FEAT_WAK
14095 "winaltkeys",
14096#endif
14097#ifdef FEAT_WRITEBACKUP
14098 "writebackup",
14099#endif
14100#ifdef FEAT_XIM
14101 "xim",
14102#endif
14103#ifdef FEAT_XFONTSET
14104 "xfontset",
14105#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014106#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014107 "xpm",
14108 "xpm_w32", /* for backward compatibility */
14109#else
14110# if defined(HAVE_XPM)
14111 "xpm",
14112# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114#ifdef USE_XSMP
14115 "xsmp",
14116#endif
14117#ifdef USE_XSMP_INTERACT
14118 "xsmp_interact",
14119#endif
14120#ifdef FEAT_XCLIPBOARD
14121 "xterm_clipboard",
14122#endif
14123#ifdef FEAT_XTERM_SAVE
14124 "xterm_save",
14125#endif
14126#if defined(UNIX) && defined(FEAT_X11)
14127 "X11",
14128#endif
14129 NULL
14130 };
14131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133 for (i = 0; has_list[i] != NULL; ++i)
14134 if (STRICMP(name, has_list[i]) == 0)
14135 {
14136 n = TRUE;
14137 break;
14138 }
14139
14140 if (n == FALSE)
14141 {
14142 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014143 {
14144 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014145 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014146 && vim_isdigit(name[6])
14147 && vim_isdigit(name[8])
14148 && vim_isdigit(name[10]))
14149 {
14150 int major = atoi((char *)name + 6);
14151 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014152
14153 /* Expect "patch-9.9.01234". */
14154 n = (major < VIM_VERSION_MAJOR
14155 || (major == VIM_VERSION_MAJOR
14156 && (minor < VIM_VERSION_MINOR
14157 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014158 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014159 }
14160 else
14161 n = has_patch(atoi((char *)name + 5));
14162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 else if (STRICMP(name, "vim_starting") == 0)
14164 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014165#ifdef FEAT_MBYTE
14166 else if (STRICMP(name, "multi_byte_encoding") == 0)
14167 n = has_mbyte;
14168#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014169#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14170 else if (STRICMP(name, "balloon_multiline") == 0)
14171 n = multiline_balloon_available();
14172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173#ifdef DYNAMIC_TCL
14174 else if (STRICMP(name, "tcl") == 0)
14175 n = tcl_enabled(FALSE);
14176#endif
14177#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14178 else if (STRICMP(name, "iconv") == 0)
14179 n = iconv_enabled(FALSE);
14180#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014181#ifdef DYNAMIC_LUA
14182 else if (STRICMP(name, "lua") == 0)
14183 n = lua_enabled(FALSE);
14184#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014185#ifdef DYNAMIC_MZSCHEME
14186 else if (STRICMP(name, "mzscheme") == 0)
14187 n = mzscheme_enabled(FALSE);
14188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189#ifdef DYNAMIC_RUBY
14190 else if (STRICMP(name, "ruby") == 0)
14191 n = ruby_enabled(FALSE);
14192#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014193#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194#ifdef DYNAMIC_PYTHON
14195 else if (STRICMP(name, "python") == 0)
14196 n = python_enabled(FALSE);
14197#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014198#endif
14199#ifdef FEAT_PYTHON3
14200#ifdef DYNAMIC_PYTHON3
14201 else if (STRICMP(name, "python3") == 0)
14202 n = python3_enabled(FALSE);
14203#endif
14204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205#ifdef DYNAMIC_PERL
14206 else if (STRICMP(name, "perl") == 0)
14207 n = perl_enabled(FALSE);
14208#endif
14209#ifdef FEAT_GUI
14210 else if (STRICMP(name, "gui_running") == 0)
14211 n = (gui.in_use || gui.starting);
14212# ifdef FEAT_GUI_W32
14213 else if (STRICMP(name, "gui_win32s") == 0)
14214 n = gui_is_win32s();
14215# endif
14216# ifdef FEAT_BROWSE
14217 else if (STRICMP(name, "browse") == 0)
14218 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14219# endif
14220#endif
14221#ifdef FEAT_SYN_HL
14222 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014223 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224#endif
14225#if defined(WIN3264)
14226 else if (STRICMP(name, "win95") == 0)
14227 n = mch_windows95();
14228#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014229#ifdef FEAT_NETBEANS_INTG
14230 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014231 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233 }
14234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014235 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236}
14237
14238/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014239 * "has_key()" function
14240 */
14241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014242f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014243{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014244 if (argvars[0].v_type != VAR_DICT)
14245 {
14246 EMSG(_(e_dictreq));
14247 return;
14248 }
14249 if (argvars[0].vval.v_dict == NULL)
14250 return;
14251
14252 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014253 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014254}
14255
14256/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014257 * "haslocaldir()" function
14258 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014260f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014261{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014262 win_T *wp = NULL;
14263
14264 wp = find_tabwin(&argvars[0], &argvars[1]);
14265 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014266}
14267
14268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 * "hasmapto()" function
14270 */
14271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014272f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273{
14274 char_u *name;
14275 char_u *mode;
14276 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014277 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014279 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014280 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281 mode = (char_u *)"nvo";
14282 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014283 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014284 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014285 if (argvars[2].v_type != VAR_UNKNOWN)
14286 abbr = get_tv_number(&argvars[2]);
14287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288
Bram Moolenaar2c932302006-03-18 21:42:09 +000014289 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014290 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014292 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293}
14294
14295/*
14296 * "histadd()" function
14297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014299f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300{
14301#ifdef FEAT_CMDHIST
14302 int histype;
14303 char_u *str;
14304 char_u buf[NUMBUFLEN];
14305#endif
14306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 if (check_restricted() || check_secure())
14309 return;
14310#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14312 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313 if (histype >= 0)
14314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014315 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 if (*str != NUL)
14317 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014318 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014320 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 return;
14322 }
14323 }
14324#endif
14325}
14326
14327/*
14328 * "histdel()" function
14329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014331f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332{
14333#ifdef FEAT_CMDHIST
14334 int n;
14335 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014336 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014338 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14339 if (str == NULL)
14340 n = 0;
14341 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014344 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014346 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014347 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 else
14349 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014350 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351 get_tv_string_buf(&argvars[1], buf));
14352 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353#endif
14354}
14355
14356/*
14357 * "histget()" function
14358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014360f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361{
14362#ifdef FEAT_CMDHIST
14363 int type;
14364 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014365 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014367 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14368 if (str == NULL)
14369 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014371 {
14372 type = get_histtype(str);
14373 if (argvars[1].v_type == VAR_UNKNOWN)
14374 idx = get_history_idx(type);
14375 else
14376 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14377 /* -1 on type error */
14378 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014381 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014383 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384}
14385
14386/*
14387 * "histnr()" function
14388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014390f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391{
14392 int i;
14393
14394#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014395 char_u *history = get_tv_string_chk(&argvars[0]);
14396
14397 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398 if (i >= HIST_CMD && i < HIST_COUNT)
14399 i = get_history_idx(i);
14400 else
14401#endif
14402 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404}
14405
14406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 * "highlightID(name)" function
14408 */
14409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014410f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014412 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413}
14414
14415/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014416 * "highlight_exists()" function
14417 */
14418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014419f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014420{
14421 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14422}
14423
14424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 * "hostname()" function
14426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014428f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429{
14430 char_u hostname[256];
14431
14432 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 rettv->v_type = VAR_STRING;
14434 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435}
14436
14437/*
14438 * iconv() function
14439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014441f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442{
14443#ifdef FEAT_MBYTE
14444 char_u buf1[NUMBUFLEN];
14445 char_u buf2[NUMBUFLEN];
14446 char_u *from, *to, *str;
14447 vimconv_T vimconv;
14448#endif
14449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014450 rettv->v_type = VAR_STRING;
14451 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452
14453#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454 str = get_tv_string(&argvars[0]);
14455 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14456 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 vimconv.vc_type = CONV_NONE;
14458 convert_setup(&vimconv, from, to);
14459
14460 /* If the encodings are equal, no conversion needed. */
14461 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465
14466 convert_setup(&vimconv, NULL, NULL);
14467 vim_free(from);
14468 vim_free(to);
14469#endif
14470}
14471
14472/*
14473 * "indent()" function
14474 */
14475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014476f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477{
14478 linenr_T lnum;
14479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014482 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485}
14486
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014487/*
14488 * "index()" function
14489 */
14490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014491f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014492{
Bram Moolenaar33570922005-01-25 22:26:29 +000014493 list_T *l;
14494 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014495 long idx = 0;
14496 int ic = FALSE;
14497
14498 rettv->vval.v_number = -1;
14499 if (argvars[0].v_type != VAR_LIST)
14500 {
14501 EMSG(_(e_listreq));
14502 return;
14503 }
14504 l = argvars[0].vval.v_list;
14505 if (l != NULL)
14506 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014507 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014508 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014510 int error = FALSE;
14511
Bram Moolenaar758711c2005-02-02 23:11:38 +000014512 /* Start at specified item. Use the cached index that list_find()
14513 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014514 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014515 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014516 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014517 ic = get_tv_number_chk(&argvars[3], &error);
14518 if (error)
14519 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014520 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014521
Bram Moolenaar758711c2005-02-02 23:11:38 +000014522 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014523 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014524 {
14525 rettv->vval.v_number = idx;
14526 break;
14527 }
14528 }
14529}
14530
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531static int inputsecret_flag = 0;
14532
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014533static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014534
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014536 * This function is used by f_input() and f_inputdialog() functions. The third
14537 * argument to f_input() specifies the type of completion to use at the
14538 * prompt. The third argument to f_inputdialog() specifies the value to return
14539 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 */
14541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014542get_user_input(
14543 typval_T *argvars,
14544 typval_T *rettv,
14545 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014547 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 char_u *p = NULL;
14549 int c;
14550 char_u buf[NUMBUFLEN];
14551 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014552 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014553 int xp_type = EXPAND_NOTHING;
14554 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014556 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014557 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558
14559#ifdef NO_CONSOLE_INPUT
14560 /* While starting up, there is no place to enter text. */
14561 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563#endif
14564
14565 cmd_silent = FALSE; /* Want to see the prompt. */
14566 if (prompt != NULL)
14567 {
14568 /* Only the part of the message after the last NL is considered as
14569 * prompt for the command line */
14570 p = vim_strrchr(prompt, '\n');
14571 if (p == NULL)
14572 p = prompt;
14573 else
14574 {
14575 ++p;
14576 c = *p;
14577 *p = NUL;
14578 msg_start();
14579 msg_clr_eos();
14580 msg_puts_attr(prompt, echo_attr);
14581 msg_didout = FALSE;
14582 msg_starthere();
14583 *p = c;
14584 }
14585 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014587 if (argvars[1].v_type != VAR_UNKNOWN)
14588 {
14589 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14590 if (defstr != NULL)
14591 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014593 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014594 {
14595 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014596 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014597 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014598
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014599 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014600 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014601
Bram Moolenaar4463f292005-09-25 22:20:24 +000014602 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14603 if (xp_name == NULL)
14604 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014605
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014606 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014607
Bram Moolenaar4463f292005-09-25 22:20:24 +000014608 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14609 &xp_arg) == FAIL)
14610 return;
14611 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014612 }
14613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014614 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014615 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014616 int save_ex_normal_busy = ex_normal_busy;
14617 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014618 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014619 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14620 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014621 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014622 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014623 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014624 && argvars[1].v_type != VAR_UNKNOWN
14625 && argvars[2].v_type != VAR_UNKNOWN)
14626 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14627 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014628
14629 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014631 /* since the user typed this, no need to wait for return */
14632 need_wait_return = FALSE;
14633 msg_didout = FALSE;
14634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635 cmd_silent = cmd_silent_save;
14636}
14637
14638/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014639 * "input()" function
14640 * Also handles inputsecret() when inputsecret is set.
14641 */
14642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014643f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014644{
14645 get_user_input(argvars, rettv, FALSE);
14646}
14647
14648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649 * "inputdialog()" function
14650 */
14651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014652f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653{
14654#if defined(FEAT_GUI_TEXTDIALOG)
14655 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14656 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14657 {
14658 char_u *message;
14659 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014660 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014662 message = get_tv_string_chk(&argvars[0]);
14663 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014664 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014665 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 else
14667 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014668 if (message != NULL && defstr != NULL
14669 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014670 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014671 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 else
14673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014674 if (message != NULL && defstr != NULL
14675 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014676 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014677 rettv->vval.v_string = vim_strsave(
14678 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014680 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 }
14684 else
14685#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014686 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687}
14688
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014689/*
14690 * "inputlist()" function
14691 */
14692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014693f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014694{
14695 listitem_T *li;
14696 int selected;
14697 int mouse_used;
14698
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014699#ifdef NO_CONSOLE_INPUT
14700 /* While starting up, there is no place to enter text. */
14701 if (no_console_input())
14702 return;
14703#endif
14704 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14705 {
14706 EMSG2(_(e_listarg), "inputlist()");
14707 return;
14708 }
14709
14710 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014711 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014712 lines_left = Rows; /* avoid more prompt */
14713 msg_scroll = TRUE;
14714 msg_clr_eos();
14715
14716 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14717 {
14718 msg_puts(get_tv_string(&li->li_tv));
14719 msg_putchar('\n');
14720 }
14721
14722 /* Ask for choice. */
14723 selected = prompt_for_number(&mouse_used);
14724 if (mouse_used)
14725 selected -= lines_left;
14726
14727 rettv->vval.v_number = selected;
14728}
14729
14730
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14732
14733/*
14734 * "inputrestore()" function
14735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014737f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738{
14739 if (ga_userinput.ga_len > 0)
14740 {
14741 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14743 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014744 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 }
14746 else if (p_verbose > 1)
14747 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014748 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014749 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 }
14751}
14752
14753/*
14754 * "inputsave()" function
14755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014757f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014759 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 if (ga_grow(&ga_userinput, 1) == OK)
14761 {
14762 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14763 + ga_userinput.ga_len);
14764 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014765 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766 }
14767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014768 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769}
14770
14771/*
14772 * "inputsecret()" function
14773 */
14774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014775f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776{
14777 ++cmdline_star;
14778 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014779 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780 --cmdline_star;
14781 --inputsecret_flag;
14782}
14783
14784/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014785 * "insert()" function
14786 */
14787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014788f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014789{
14790 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014791 listitem_T *item;
14792 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014793 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014794
14795 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014796 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014797 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014798 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014799 {
14800 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014801 before = get_tv_number_chk(&argvars[2], &error);
14802 if (error)
14803 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014804
Bram Moolenaar758711c2005-02-02 23:11:38 +000014805 if (before == l->lv_len)
14806 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014807 else
14808 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014809 item = list_find(l, before);
14810 if (item == NULL)
14811 {
14812 EMSGN(_(e_listidx), before);
14813 l = NULL;
14814 }
14815 }
14816 if (l != NULL)
14817 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014818 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014819 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014820 }
14821 }
14822}
14823
14824/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014825 * "invert(expr)" function
14826 */
14827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014828f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014829{
14830 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14831}
14832
14833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 * "isdirectory()" function
14835 */
14836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014837f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014839 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840}
14841
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014842/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014843 * "islocked()" function
14844 */
14845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014846f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014847{
14848 lval_T lv;
14849 char_u *end;
14850 dictitem_T *di;
14851
14852 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014853 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14854 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014855 if (end != NULL && lv.ll_name != NULL)
14856 {
14857 if (*end != NUL)
14858 EMSG(_(e_trailing));
14859 else
14860 {
14861 if (lv.ll_tv == NULL)
14862 {
14863 if (check_changedtick(lv.ll_name))
14864 rettv->vval.v_number = 1; /* always locked */
14865 else
14866 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014867 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014868 if (di != NULL)
14869 {
14870 /* Consider a variable locked when:
14871 * 1. the variable itself is locked
14872 * 2. the value of the variable is locked.
14873 * 3. the List or Dict value is locked.
14874 */
14875 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14876 || tv_islocked(&di->di_tv));
14877 }
14878 }
14879 }
14880 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014881 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014882 else if (lv.ll_newkey != NULL)
14883 EMSG2(_(e_dictkey), lv.ll_newkey);
14884 else if (lv.ll_list != NULL)
14885 /* List item. */
14886 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14887 else
14888 /* Dictionary item. */
14889 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14890 }
14891 }
14892
14893 clear_lval(&lv);
14894}
14895
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014896#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14897/*
14898 * "isnan()" function
14899 */
14900 static void
14901f_isnan(typval_T *argvars, typval_T *rettv)
14902{
14903 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14904 && isnan(argvars[0].vval.v_float);
14905}
14906#endif
14907
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014908static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014909
14910/*
14911 * Turn a dict into a list:
14912 * "what" == 0: list of keys
14913 * "what" == 1: list of values
14914 * "what" == 2: list of items
14915 */
14916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014917dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014918{
Bram Moolenaar33570922005-01-25 22:26:29 +000014919 list_T *l2;
14920 dictitem_T *di;
14921 hashitem_T *hi;
14922 listitem_T *li;
14923 listitem_T *li2;
14924 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014925 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014926
Bram Moolenaar8c711452005-01-14 21:53:12 +000014927 if (argvars[0].v_type != VAR_DICT)
14928 {
14929 EMSG(_(e_dictreq));
14930 return;
14931 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014932 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014933 return;
14934
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014935 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014936 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014937
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014938 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014939 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014940 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014941 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014942 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014943 --todo;
14944 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014945
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014946 li = listitem_alloc();
14947 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014948 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014949 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014950
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014951 if (what == 0)
14952 {
14953 /* keys() */
14954 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014955 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014956 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14957 }
14958 else if (what == 1)
14959 {
14960 /* values() */
14961 copy_tv(&di->di_tv, &li->li_tv);
14962 }
14963 else
14964 {
14965 /* items() */
14966 l2 = list_alloc();
14967 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014968 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014969 li->li_tv.vval.v_list = l2;
14970 if (l2 == NULL)
14971 break;
14972 ++l2->lv_refcount;
14973
14974 li2 = listitem_alloc();
14975 if (li2 == NULL)
14976 break;
14977 list_append(l2, li2);
14978 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014979 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014980 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14981
14982 li2 = listitem_alloc();
14983 if (li2 == NULL)
14984 break;
14985 list_append(l2, li2);
14986 copy_tv(&di->di_tv, &li2->li_tv);
14987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014988 }
14989 }
14990}
14991
14992/*
14993 * "items(dict)" function
14994 */
14995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014996f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014997{
14998 dict_list(argvars, rettv, 2);
14999}
15000
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015001#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015002/*
15003 * Get the job from the argument.
15004 * Returns NULL if the job is invalid.
15005 */
15006 static job_T *
15007get_job_arg(typval_T *tv)
15008{
15009 job_T *job;
15010
15011 if (tv->v_type != VAR_JOB)
15012 {
15013 EMSG2(_(e_invarg2), get_tv_string(tv));
15014 return NULL;
15015 }
15016 job = tv->vval.v_job;
15017
15018 if (job == NULL)
15019 EMSG(_("E916: not a valid job"));
15020 return job;
15021}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015022
Bram Moolenaar835dc632016-02-07 14:27:38 +010015023/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015024 * "job_getchannel()" function
15025 */
15026 static void
15027f_job_getchannel(typval_T *argvars, typval_T *rettv)
15028{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015029 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015030
Bram Moolenaar65edff82016-02-21 16:40:11 +010015031 if (job != NULL)
15032 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015033 rettv->v_type = VAR_CHANNEL;
15034 rettv->vval.v_channel = job->jv_channel;
15035 if (job->jv_channel != NULL)
15036 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015037 }
15038}
15039
15040/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015041 * "job_info()" function
15042 */
15043 static void
15044f_job_info(typval_T *argvars, typval_T *rettv)
15045{
15046 job_T *job = get_job_arg(&argvars[0]);
15047
15048 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15049 job_info(job, rettv->vval.v_dict);
15050}
15051
15052/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015053 * "job_setoptions()" function
15054 */
15055 static void
15056f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15057{
15058 job_T *job = get_job_arg(&argvars[0]);
15059 jobopt_T opt;
15060
15061 if (job == NULL)
15062 return;
15063 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015064 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15065 job_set_options(job, &opt);
15066 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015067}
15068
15069/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015070 * "job_start()" function
15071 */
15072 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015073f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015074{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015075 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015076 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015077}
15078
15079/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015080 * "job_status()" function
15081 */
15082 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015083f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015084{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015085 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015086
Bram Moolenaar65edff82016-02-21 16:40:11 +010015087 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015088 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015089 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015090 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015091 }
15092}
15093
15094/*
15095 * "job_stop()" function
15096 */
15097 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015098f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015099{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015100 job_T *job = get_job_arg(&argvars[0]);
15101
15102 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015103 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015104}
15105#endif
15106
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015108 * "join()" function
15109 */
15110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015111f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015112{
15113 garray_T ga;
15114 char_u *sep;
15115
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015116 if (argvars[0].v_type != VAR_LIST)
15117 {
15118 EMSG(_(e_listreq));
15119 return;
15120 }
15121 if (argvars[0].vval.v_list == NULL)
15122 return;
15123 if (argvars[1].v_type == VAR_UNKNOWN)
15124 sep = (char_u *)" ";
15125 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015126 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015127
15128 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015129
15130 if (sep != NULL)
15131 {
15132 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015133 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015134 ga_append(&ga, NUL);
15135 rettv->vval.v_string = (char_u *)ga.ga_data;
15136 }
15137 else
15138 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015139}
15140
15141/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015142 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015143 */
15144 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015145f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015146{
15147 js_read_T reader;
15148
15149 reader.js_buf = get_tv_string(&argvars[0]);
15150 reader.js_fill = NULL;
15151 reader.js_used = 0;
15152 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15153 EMSG(_(e_invarg));
15154}
15155
15156/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015157 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015158 */
15159 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015160f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015161{
15162 rettv->v_type = VAR_STRING;
15163 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15164}
15165
15166/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015167 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015168 */
15169 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015170f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015171{
15172 js_read_T reader;
15173
15174 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015175 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015176 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015177 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015178 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015179}
15180
15181/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015182 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015183 */
15184 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015185f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015186{
15187 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015188 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015189}
15190
15191/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015192 * "keys()" function
15193 */
15194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015195f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015196{
15197 dict_list(argvars, rettv, 0);
15198}
15199
15200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201 * "last_buffer_nr()" function.
15202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015204f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205{
15206 int n = 0;
15207 buf_T *buf;
15208
15209 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15210 if (n < buf->b_fnum)
15211 n = buf->b_fnum;
15212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015213 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015214}
15215
15216/*
15217 * "len()" function
15218 */
15219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015220f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015221{
15222 switch (argvars[0].v_type)
15223 {
15224 case VAR_STRING:
15225 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015226 rettv->vval.v_number = (varnumber_T)STRLEN(
15227 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015228 break;
15229 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015230 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015231 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015232 case VAR_DICT:
15233 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15234 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015235 case VAR_UNKNOWN:
15236 case VAR_SPECIAL:
15237 case VAR_FLOAT:
15238 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015239 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015240 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015241 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015242 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015243 break;
15244 }
15245}
15246
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015247static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015248
15249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015250libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015251{
15252#ifdef FEAT_LIBCALL
15253 char_u *string_in;
15254 char_u **string_result;
15255 int nr_result;
15256#endif
15257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015258 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015259 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015260 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015261
15262 if (check_restricted() || check_secure())
15263 return;
15264
15265#ifdef FEAT_LIBCALL
15266 /* The first two args must be strings, otherwise its meaningless */
15267 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15268 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015269 string_in = NULL;
15270 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015271 string_in = argvars[2].vval.v_string;
15272 if (type == VAR_NUMBER)
15273 string_result = NULL;
15274 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015275 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015276 if (mch_libcall(argvars[0].vval.v_string,
15277 argvars[1].vval.v_string,
15278 string_in,
15279 argvars[2].vval.v_number,
15280 string_result,
15281 &nr_result) == OK
15282 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015283 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015284 }
15285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286}
15287
15288/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015289 * "libcall()" function
15290 */
15291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015292f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015293{
15294 libcall_common(argvars, rettv, VAR_STRING);
15295}
15296
15297/*
15298 * "libcallnr()" function
15299 */
15300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015301f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015302{
15303 libcall_common(argvars, rettv, VAR_NUMBER);
15304}
15305
15306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307 * "line(string)" function
15308 */
15309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015310f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311{
15312 linenr_T lnum = 0;
15313 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015314 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015316 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317 if (fp != NULL)
15318 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015319 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320}
15321
15322/*
15323 * "line2byte(lnum)" function
15324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015326f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327{
15328#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015329 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330#else
15331 linenr_T lnum;
15332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015333 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015335 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015337 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15338 if (rettv->vval.v_number >= 0)
15339 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340#endif
15341}
15342
15343/*
15344 * "lispindent(lnum)" function
15345 */
15346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015347f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348{
15349#ifdef FEAT_LISP
15350 pos_T pos;
15351 linenr_T lnum;
15352
15353 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015354 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15356 {
15357 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015358 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 curwin->w_cursor = pos;
15360 }
15361 else
15362#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364}
15365
15366/*
15367 * "localtime()" function
15368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015370f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373}
15374
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015375static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376
15377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015378get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379{
15380 char_u *keys;
15381 char_u *which;
15382 char_u buf[NUMBUFLEN];
15383 char_u *keys_buf = NULL;
15384 char_u *rhs;
15385 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015386 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015387 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015388 mapblock_T *mp;
15389 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390
15391 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015392 rettv->v_type = VAR_STRING;
15393 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 if (*keys == NUL)
15397 return;
15398
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015399 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015401 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015402 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015403 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015404 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015405 if (argvars[3].v_type != VAR_UNKNOWN)
15406 get_dict = get_tv_number(&argvars[3]);
15407 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409 else
15410 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015411 if (which == NULL)
15412 return;
15413
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 mode = get_map_mode(&which, 0);
15415
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015416 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015417 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015419
15420 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015422 /* Return a string. */
15423 if (rhs != NULL)
15424 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425
Bram Moolenaarbd743252010-10-20 21:23:33 +020015426 }
15427 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15428 {
15429 /* Return a dictionary. */
15430 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15431 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15432 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433
Bram Moolenaarbd743252010-10-20 21:23:33 +020015434 dict_add_nr_str(dict, "lhs", 0L, lhs);
15435 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15436 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15437 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15438 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15439 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15440 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015441 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015442 dict_add_nr_str(dict, "mode", 0L, mapmode);
15443
15444 vim_free(lhs);
15445 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446 }
15447}
15448
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015449#ifdef FEAT_FLOAT
15450/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015451 * "log()" function
15452 */
15453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015454f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015455{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015456 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015457
15458 rettv->v_type = VAR_FLOAT;
15459 if (get_float_arg(argvars, &f) == OK)
15460 rettv->vval.v_float = log(f);
15461 else
15462 rettv->vval.v_float = 0.0;
15463}
15464
15465/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015466 * "log10()" function
15467 */
15468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015469f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015470{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015471 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015472
15473 rettv->v_type = VAR_FLOAT;
15474 if (get_float_arg(argvars, &f) == OK)
15475 rettv->vval.v_float = log10(f);
15476 else
15477 rettv->vval.v_float = 0.0;
15478}
15479#endif
15480
Bram Moolenaar1dced572012-04-05 16:54:08 +020015481#ifdef FEAT_LUA
15482/*
15483 * "luaeval()" function
15484 */
15485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015486f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015487{
15488 char_u *str;
15489 char_u buf[NUMBUFLEN];
15490
15491 str = get_tv_string_buf(&argvars[0], buf);
15492 do_luaeval(str, argvars + 1, rettv);
15493}
15494#endif
15495
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015497 * "map()" function
15498 */
15499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015500f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015501{
15502 filter_map(argvars, rettv, TRUE);
15503}
15504
15505/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015506 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 */
15508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015509f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015511 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512}
15513
15514/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015515 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 */
15517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015518f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015520 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521}
15522
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015523static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524
15525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015526find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015528 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015529 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015530 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531 char_u *pat;
15532 regmatch_T regmatch;
15533 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015534 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 char_u *save_cpo;
15536 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015537 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015538 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015539 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015540 list_T *l = NULL;
15541 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015542 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015543 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544
15545 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15546 save_cpo = p_cpo;
15547 p_cpo = (char_u *)"";
15548
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015549 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015550 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015551 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015552 /* type 3: return empty list when there are no matches.
15553 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015554 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015555 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015556 if (type == 4
15557 && (list_append_string(rettv->vval.v_list,
15558 (char_u *)"", 0) == FAIL
15559 || list_append_number(rettv->vval.v_list,
15560 (varnumber_T)-1) == FAIL
15561 || list_append_number(rettv->vval.v_list,
15562 (varnumber_T)-1) == FAIL
15563 || list_append_number(rettv->vval.v_list,
15564 (varnumber_T)-1) == FAIL))
15565 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015566 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015567 rettv->vval.v_list = NULL;
15568 goto theend;
15569 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015570 }
15571 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573 rettv->v_type = VAR_STRING;
15574 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015577 if (argvars[0].v_type == VAR_LIST)
15578 {
15579 if ((l = argvars[0].vval.v_list) == NULL)
15580 goto theend;
15581 li = l->lv_first;
15582 }
15583 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015584 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015585 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015586 len = (long)STRLEN(str);
15587 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015589 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15590 if (pat == NULL)
15591 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015592
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015593 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015595 int error = FALSE;
15596
15597 start = get_tv_number_chk(&argvars[2], &error);
15598 if (error)
15599 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015600 if (l != NULL)
15601 {
15602 li = list_find(l, start);
15603 if (li == NULL)
15604 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015605 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015606 }
15607 else
15608 {
15609 if (start < 0)
15610 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015611 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015612 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015613 /* When "count" argument is there ignore matches before "start",
15614 * otherwise skip part of the string. Differs when pattern is "^"
15615 * or "\<". */
15616 if (argvars[3].v_type != VAR_UNKNOWN)
15617 startcol = start;
15618 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015619 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015620 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015621 len -= start;
15622 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015623 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015624
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015625 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015626 nth = get_tv_number_chk(&argvars[3], &error);
15627 if (error)
15628 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629 }
15630
15631 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15632 if (regmatch.regprog != NULL)
15633 {
15634 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015635
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015636 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015637 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015638 if (l != NULL)
15639 {
15640 if (li == NULL)
15641 {
15642 match = FALSE;
15643 break;
15644 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015645 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015646 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015647 if (str == NULL)
15648 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015649 }
15650
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015651 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015652
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015653 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015654 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015655 if (l == NULL && !match)
15656 break;
15657
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015658 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015659 if (l != NULL)
15660 {
15661 li = li->li_next;
15662 ++idx;
15663 }
15664 else
15665 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015666#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015667 startcol = (colnr_T)(regmatch.startp[0]
15668 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015669#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015670 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015671#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015672 if (startcol > (colnr_T)len
15673 || str + startcol <= regmatch.startp[0])
15674 {
15675 match = FALSE;
15676 break;
15677 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015678 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015679 }
15680
15681 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015683 if (type == 4)
15684 {
15685 listitem_T *li1 = rettv->vval.v_list->lv_first;
15686 listitem_T *li2 = li1->li_next;
15687 listitem_T *li3 = li2->li_next;
15688 listitem_T *li4 = li3->li_next;
15689
15690 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15691 (int)(regmatch.endp[0] - regmatch.startp[0]));
15692 li3->li_tv.vval.v_number =
15693 (varnumber_T)(regmatch.startp[0] - expr);
15694 li4->li_tv.vval.v_number =
15695 (varnumber_T)(regmatch.endp[0] - expr);
15696 if (l != NULL)
15697 li2->li_tv.vval.v_number = (varnumber_T)idx;
15698 }
15699 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015700 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015701 int i;
15702
15703 /* return list with matched string and submatches */
15704 for (i = 0; i < NSUBEXP; ++i)
15705 {
15706 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015707 {
15708 if (list_append_string(rettv->vval.v_list,
15709 (char_u *)"", 0) == FAIL)
15710 break;
15711 }
15712 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015713 regmatch.startp[i],
15714 (int)(regmatch.endp[i] - regmatch.startp[i]))
15715 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015716 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015717 }
15718 }
15719 else if (type == 2)
15720 {
15721 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015722 if (l != NULL)
15723 copy_tv(&li->li_tv, rettv);
15724 else
15725 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015727 }
15728 else if (l != NULL)
15729 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 else
15731 {
15732 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015733 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 (varnumber_T)(regmatch.startp[0] - str);
15735 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015736 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015738 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 }
15740 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015741 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 }
15743
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015744 if (type == 4 && l == NULL)
15745 /* matchstrpos() without a list: drop the second item. */
15746 listitem_remove(rettv->vval.v_list,
15747 rettv->vval.v_list->lv_first->li_next);
15748
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015750 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751 p_cpo = save_cpo;
15752}
15753
15754/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015755 * "match()" function
15756 */
15757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015758f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015759{
15760 find_some_match(argvars, rettv, 1);
15761}
15762
15763/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015764 * "matchadd()" function
15765 */
15766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015767f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015768{
15769#ifdef FEAT_SEARCH_EXTRA
15770 char_u buf[NUMBUFLEN];
15771 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15772 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15773 int prio = 10; /* default priority */
15774 int id = -1;
15775 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015776 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015777
15778 rettv->vval.v_number = -1;
15779
15780 if (grp == NULL || pat == NULL)
15781 return;
15782 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015783 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015784 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015785 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015786 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015787 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015788 if (argvars[4].v_type != VAR_UNKNOWN)
15789 {
15790 if (argvars[4].v_type != VAR_DICT)
15791 {
15792 EMSG(_(e_dictreq));
15793 return;
15794 }
15795 if (dict_find(argvars[4].vval.v_dict,
15796 (char_u *)"conceal", -1) != NULL)
15797 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15798 (char_u *)"conceal", FALSE);
15799 }
15800 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015801 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015802 if (error == TRUE)
15803 return;
15804 if (id >= 1 && id <= 3)
15805 {
15806 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15807 return;
15808 }
15809
Bram Moolenaar6561d522015-07-21 15:48:27 +020015810 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15811 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015812#endif
15813}
15814
15815/*
15816 * "matchaddpos()" function
15817 */
15818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015819f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015820{
15821#ifdef FEAT_SEARCH_EXTRA
15822 char_u buf[NUMBUFLEN];
15823 char_u *group;
15824 int prio = 10;
15825 int id = -1;
15826 int error = FALSE;
15827 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015828 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015829
15830 rettv->vval.v_number = -1;
15831
15832 group = get_tv_string_buf_chk(&argvars[0], buf);
15833 if (group == NULL)
15834 return;
15835
15836 if (argvars[1].v_type != VAR_LIST)
15837 {
15838 EMSG2(_(e_listarg), "matchaddpos()");
15839 return;
15840 }
15841 l = argvars[1].vval.v_list;
15842 if (l == NULL)
15843 return;
15844
15845 if (argvars[2].v_type != VAR_UNKNOWN)
15846 {
15847 prio = get_tv_number_chk(&argvars[2], &error);
15848 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015849 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015850 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015851 if (argvars[4].v_type != VAR_UNKNOWN)
15852 {
15853 if (argvars[4].v_type != VAR_DICT)
15854 {
15855 EMSG(_(e_dictreq));
15856 return;
15857 }
15858 if (dict_find(argvars[4].vval.v_dict,
15859 (char_u *)"conceal", -1) != NULL)
15860 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15861 (char_u *)"conceal", FALSE);
15862 }
15863 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015864 }
15865 if (error == TRUE)
15866 return;
15867
15868 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15869 if (id == 1 || id == 2)
15870 {
15871 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15872 return;
15873 }
15874
Bram Moolenaar6561d522015-07-21 15:48:27 +020015875 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15876 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015877#endif
15878}
15879
15880/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015881 * "matcharg()" function
15882 */
15883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015884f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015885{
15886 if (rettv_list_alloc(rettv) == OK)
15887 {
15888#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015889 int id = get_tv_number(&argvars[0]);
15890 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015891
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015892 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015893 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015894 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15895 {
15896 list_append_string(rettv->vval.v_list,
15897 syn_id2name(m->hlg_id), -1);
15898 list_append_string(rettv->vval.v_list, m->pattern, -1);
15899 }
15900 else
15901 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015902 list_append_string(rettv->vval.v_list, NULL, -1);
15903 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015904 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015905 }
15906#endif
15907 }
15908}
15909
15910/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015911 * "matchdelete()" function
15912 */
15913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015914f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015915{
15916#ifdef FEAT_SEARCH_EXTRA
15917 rettv->vval.v_number = match_delete(curwin,
15918 (int)get_tv_number(&argvars[0]), TRUE);
15919#endif
15920}
15921
15922/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015923 * "matchend()" function
15924 */
15925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015926f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015927{
15928 find_some_match(argvars, rettv, 0);
15929}
15930
15931/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015932 * "matchlist()" function
15933 */
15934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015935f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015936{
15937 find_some_match(argvars, rettv, 3);
15938}
15939
15940/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015941 * "matchstr()" function
15942 */
15943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015944f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015945{
15946 find_some_match(argvars, rettv, 2);
15947}
15948
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015949/*
15950 * "matchstrpos()" function
15951 */
15952 static void
15953f_matchstrpos(typval_T *argvars, typval_T *rettv)
15954{
15955 find_some_match(argvars, rettv, 4);
15956}
15957
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015958static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015959
15960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015961max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015962{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015963 long n = 0;
15964 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015965 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015966
15967 if (argvars[0].v_type == VAR_LIST)
15968 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015969 list_T *l;
15970 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015971
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015972 l = argvars[0].vval.v_list;
15973 if (l != NULL)
15974 {
15975 li = l->lv_first;
15976 if (li != NULL)
15977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015978 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015979 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015980 {
15981 li = li->li_next;
15982 if (li == NULL)
15983 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015984 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015985 if (domax ? i > n : i < n)
15986 n = i;
15987 }
15988 }
15989 }
15990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015991 else if (argvars[0].v_type == VAR_DICT)
15992 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015993 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015994 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015995 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015996 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015997
15998 d = argvars[0].vval.v_dict;
15999 if (d != NULL)
16000 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016001 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016003 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016004 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016005 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016006 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016007 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016008 if (first)
16009 {
16010 n = i;
16011 first = FALSE;
16012 }
16013 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016014 n = i;
16015 }
16016 }
16017 }
16018 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016019 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016020 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016021 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016022}
16023
16024/*
16025 * "max()" function
16026 */
16027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016028f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016029{
16030 max_min(argvars, rettv, TRUE);
16031}
16032
16033/*
16034 * "min()" function
16035 */
16036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016037f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016038{
16039 max_min(argvars, rettv, FALSE);
16040}
16041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016042static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016043
16044/*
16045 * Create the directory in which "dir" is located, and higher levels when
16046 * needed.
16047 */
16048 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016049mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016050{
16051 char_u *p;
16052 char_u *updir;
16053 int r = FAIL;
16054
16055 /* Get end of directory name in "dir".
16056 * We're done when it's "/" or "c:/". */
16057 p = gettail_sep(dir);
16058 if (p <= get_past_head(dir))
16059 return OK;
16060
16061 /* If the directory exists we're done. Otherwise: create it.*/
16062 updir = vim_strnsave(dir, (int)(p - dir));
16063 if (updir == NULL)
16064 return FAIL;
16065 if (mch_isdir(updir))
16066 r = OK;
16067 else if (mkdir_recurse(updir, prot) == OK)
16068 r = vim_mkdir_emsg(updir, prot);
16069 vim_free(updir);
16070 return r;
16071}
16072
16073#ifdef vim_mkdir
16074/*
16075 * "mkdir()" function
16076 */
16077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016078f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016079{
16080 char_u *dir;
16081 char_u buf[NUMBUFLEN];
16082 int prot = 0755;
16083
16084 rettv->vval.v_number = FAIL;
16085 if (check_restricted() || check_secure())
16086 return;
16087
16088 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016089 if (*dir == NUL)
16090 rettv->vval.v_number = FAIL;
16091 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016092 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016093 if (*gettail(dir) == NUL)
16094 /* remove trailing slashes */
16095 *gettail_sep(dir) = NUL;
16096
16097 if (argvars[1].v_type != VAR_UNKNOWN)
16098 {
16099 if (argvars[2].v_type != VAR_UNKNOWN)
16100 prot = get_tv_number_chk(&argvars[2], NULL);
16101 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16102 mkdir_recurse(dir, prot);
16103 }
16104 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016105 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016106}
16107#endif
16108
Bram Moolenaar0d660222005-01-07 21:51:51 +000016109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110 * "mode()" function
16111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016113f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016115 char_u buf[3];
16116
16117 buf[1] = NUL;
16118 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 if (VIsual_active)
16121 {
16122 if (VIsual_select)
16123 buf[0] = VIsual_mode + 's' - 'v';
16124 else
16125 buf[0] = VIsual_mode;
16126 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016127 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016128 || State == CONFIRM)
16129 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016131 if (State == ASKMORE)
16132 buf[1] = 'm';
16133 else if (State == CONFIRM)
16134 buf[1] = '?';
16135 }
16136 else if (State == EXTERNCMD)
16137 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138 else if (State & INSERT)
16139 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016140#ifdef FEAT_VREPLACE
16141 if (State & VREPLACE_FLAG)
16142 {
16143 buf[0] = 'R';
16144 buf[1] = 'v';
16145 }
16146 else
16147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 if (State & REPLACE_FLAG)
16149 buf[0] = 'R';
16150 else
16151 buf[0] = 'i';
16152 }
16153 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016156 if (exmode_active)
16157 buf[1] = 'v';
16158 }
16159 else if (exmode_active)
16160 {
16161 buf[0] = 'c';
16162 buf[1] = 'e';
16163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016167 if (finish_op)
16168 buf[1] = 'o';
16169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016171 /* Clear out the minor mode when the argument is not a non-zero number or
16172 * non-empty string. */
16173 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016174 buf[1] = NUL;
16175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016176 rettv->vval.v_string = vim_strsave(buf);
16177 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178}
16179
Bram Moolenaar429fa852013-04-15 12:27:36 +020016180#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016181/*
16182 * "mzeval()" function
16183 */
16184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016185f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016186{
16187 char_u *str;
16188 char_u buf[NUMBUFLEN];
16189
16190 str = get_tv_string_buf(&argvars[0], buf);
16191 do_mzeval(str, rettv);
16192}
Bram Moolenaar75676462013-01-30 14:55:42 +010016193
16194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016195mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016196{
16197 typval_T argvars[3];
16198
16199 argvars[0].v_type = VAR_STRING;
16200 argvars[0].vval.v_string = name;
16201 copy_tv(args, &argvars[1]);
16202 argvars[2].v_type = VAR_UNKNOWN;
16203 f_call(argvars, rettv);
16204 clear_tv(&argvars[1]);
16205}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016206#endif
16207
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016209 * "nextnonblank()" function
16210 */
16211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016212f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016213{
16214 linenr_T lnum;
16215
16216 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016218 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219 {
16220 lnum = 0;
16221 break;
16222 }
16223 if (*skipwhite(ml_get(lnum)) != NUL)
16224 break;
16225 }
16226 rettv->vval.v_number = lnum;
16227}
16228
16229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 * "nr2char()" function
16231 */
16232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016233f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234{
16235 char_u buf[NUMBUFLEN];
16236
16237#ifdef FEAT_MBYTE
16238 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016239 {
16240 int utf8 = 0;
16241
16242 if (argvars[1].v_type != VAR_UNKNOWN)
16243 utf8 = get_tv_number_chk(&argvars[1], NULL);
16244 if (utf8)
16245 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16246 else
16247 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249 else
16250#endif
16251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016252 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 buf[1] = NUL;
16254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016255 rettv->v_type = VAR_STRING;
16256 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016257}
16258
16259/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016260 * "or(expr, expr)" function
16261 */
16262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016263f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016264{
16265 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16266 | get_tv_number_chk(&argvars[1], NULL);
16267}
16268
16269/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016270 * "pathshorten()" function
16271 */
16272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016273f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016274{
16275 char_u *p;
16276
16277 rettv->v_type = VAR_STRING;
16278 p = get_tv_string_chk(&argvars[0]);
16279 if (p == NULL)
16280 rettv->vval.v_string = NULL;
16281 else
16282 {
16283 p = vim_strsave(p);
16284 rettv->vval.v_string = p;
16285 if (p != NULL)
16286 shorten_dir(p);
16287 }
16288}
16289
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016290#ifdef FEAT_PERL
16291/*
16292 * "perleval()" function
16293 */
16294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016295f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016296{
16297 char_u *str;
16298 char_u buf[NUMBUFLEN];
16299
16300 str = get_tv_string_buf(&argvars[0], buf);
16301 do_perleval(str, rettv);
16302}
16303#endif
16304
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016305#ifdef FEAT_FLOAT
16306/*
16307 * "pow()" function
16308 */
16309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016310f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016311{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016312 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016313
16314 rettv->v_type = VAR_FLOAT;
16315 if (get_float_arg(argvars, &fx) == OK
16316 && get_float_arg(&argvars[1], &fy) == OK)
16317 rettv->vval.v_float = pow(fx, fy);
16318 else
16319 rettv->vval.v_float = 0.0;
16320}
16321#endif
16322
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016323/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016324 * "prevnonblank()" function
16325 */
16326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016327f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016328{
16329 linenr_T lnum;
16330
16331 lnum = get_tv_lnum(argvars);
16332 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16333 lnum = 0;
16334 else
16335 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16336 --lnum;
16337 rettv->vval.v_number = lnum;
16338}
16339
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016340/* This dummy va_list is here because:
16341 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16342 * - locally in the function results in a "used before set" warning
16343 * - using va_start() to initialize it gives "function with fixed args" error */
16344static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016345
Bram Moolenaar8c711452005-01-14 21:53:12 +000016346/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016347 * "printf()" function
16348 */
16349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016350f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016351{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016352 char_u buf[NUMBUFLEN];
16353 int len;
16354 char_u *s;
16355 int saved_did_emsg = did_emsg;
16356 char *fmt;
16357
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016358 rettv->v_type = VAR_STRING;
16359 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016360
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016361 /* Get the required length, allocate the buffer and do it for real. */
16362 did_emsg = FALSE;
16363 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16364 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16365 if (!did_emsg)
16366 {
16367 s = alloc(len + 1);
16368 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016369 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016370 rettv->vval.v_string = s;
16371 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016372 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016373 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016374 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016375}
16376
16377/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016378 * "pumvisible()" function
16379 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016381f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016382{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016383#ifdef FEAT_INS_EXPAND
16384 if (pum_visible())
16385 rettv->vval.v_number = 1;
16386#endif
16387}
16388
Bram Moolenaardb913952012-06-29 12:54:53 +020016389#ifdef FEAT_PYTHON3
16390/*
16391 * "py3eval()" function
16392 */
16393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016394f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016395{
16396 char_u *str;
16397 char_u buf[NUMBUFLEN];
16398
16399 str = get_tv_string_buf(&argvars[0], buf);
16400 do_py3eval(str, rettv);
16401}
16402#endif
16403
16404#ifdef FEAT_PYTHON
16405/*
16406 * "pyeval()" function
16407 */
16408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016409f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016410{
16411 char_u *str;
16412 char_u buf[NUMBUFLEN];
16413
16414 str = get_tv_string_buf(&argvars[0], buf);
16415 do_pyeval(str, rettv);
16416}
16417#endif
16418
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016419/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016420 * "range()" function
16421 */
16422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016423f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016424{
16425 long start;
16426 long end;
16427 long stride = 1;
16428 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016429 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016431 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016432 if (argvars[1].v_type == VAR_UNKNOWN)
16433 {
16434 end = start - 1;
16435 start = 0;
16436 }
16437 else
16438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016439 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016440 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016442 }
16443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016444 if (error)
16445 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016446 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016447 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016448 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016449 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016450 else
16451 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016452 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016453 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016454 if (list_append_number(rettv->vval.v_list,
16455 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016456 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016457 }
16458}
16459
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016460/*
16461 * "readfile()" function
16462 */
16463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016464f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016465{
16466 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016467 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016468 char_u *fname;
16469 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016470 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16471 int io_size = sizeof(buf);
16472 int readlen; /* size of last fread() */
16473 char_u *prev = NULL; /* previously read bytes, if any */
16474 long prevlen = 0; /* length of data in prev */
16475 long prevsize = 0; /* size of prev buffer */
16476 long maxline = MAXLNUM;
16477 long cnt = 0;
16478 char_u *p; /* position in buf */
16479 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016480
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016481 if (argvars[1].v_type != VAR_UNKNOWN)
16482 {
16483 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16484 binary = TRUE;
16485 if (argvars[2].v_type != VAR_UNKNOWN)
16486 maxline = get_tv_number(&argvars[2]);
16487 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016488
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016489 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016490 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016491
16492 /* Always open the file in binary mode, library functions have a mind of
16493 * their own about CR-LF conversion. */
16494 fname = get_tv_string(&argvars[0]);
16495 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16496 {
16497 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16498 return;
16499 }
16500
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016501 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016502 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016503 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016504
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016505 /* This for loop processes what was read, but is also entered at end
16506 * of file so that either:
16507 * - an incomplete line gets written
16508 * - a "binary" file gets an empty line at the end if it ends in a
16509 * newline. */
16510 for (p = buf, start = buf;
16511 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16512 ++p)
16513 {
16514 if (*p == '\n' || readlen <= 0)
16515 {
16516 listitem_T *li;
16517 char_u *s = NULL;
16518 long_u len = p - start;
16519
16520 /* Finished a line. Remove CRs before NL. */
16521 if (readlen > 0 && !binary)
16522 {
16523 while (len > 0 && start[len - 1] == '\r')
16524 --len;
16525 /* removal may cross back to the "prev" string */
16526 if (len == 0)
16527 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16528 --prevlen;
16529 }
16530 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016531 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016532 else
16533 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016534 /* Change "prev" buffer to be the right size. This way
16535 * the bytes are only copied once, and very long lines are
16536 * allocated only once. */
16537 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016538 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016539 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016540 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016541 prev = NULL; /* the list will own the string */
16542 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016543 }
16544 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016545 if (s == NULL)
16546 {
16547 do_outofmem_msg((long_u) prevlen + len + 1);
16548 failed = TRUE;
16549 break;
16550 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016551
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016552 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016553 {
16554 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016555 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556 break;
16557 }
16558 li->li_tv.v_type = VAR_STRING;
16559 li->li_tv.v_lock = 0;
16560 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016561 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016562
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016563 start = p + 1; /* step over newline */
16564 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016565 break;
16566 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016567 else if (*p == NUL)
16568 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016569#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016570 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16571 * when finding the BF and check the previous two bytes. */
16572 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016573 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016574 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16575 * + 1, these may be in the "prev" string. */
16576 char_u back1 = p >= buf + 1 ? p[-1]
16577 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16578 char_u back2 = p >= buf + 2 ? p[-2]
16579 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16580 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016581
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016582 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016583 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016584 char_u *dest = p - 2;
16585
16586 /* Usually a BOM is at the beginning of a file, and so at
16587 * the beginning of a line; then we can just step over it.
16588 */
16589 if (start == dest)
16590 start = p + 1;
16591 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016592 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016593 /* have to shuffle buf to close gap */
16594 int adjust_prevlen = 0;
16595
16596 if (dest < buf)
16597 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016598 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016599 dest = buf;
16600 }
16601 if (readlen > p - buf + 1)
16602 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16603 readlen -= 3 - adjust_prevlen;
16604 prevlen -= adjust_prevlen;
16605 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016606 }
16607 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016608 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016609#endif
16610 } /* for */
16611
16612 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16613 break;
16614 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016615 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016616 /* There's part of a line in buf, store it in "prev". */
16617 if (p - start + prevlen >= prevsize)
16618 {
16619 /* need bigger "prev" buffer */
16620 char_u *newprev;
16621
16622 /* A common use case is ordinary text files and "prev" gets a
16623 * fragment of a line, so the first allocation is made
16624 * small, to avoid repeatedly 'allocing' large and
16625 * 'reallocing' small. */
16626 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016627 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016628 else
16629 {
16630 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016631 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016632 prevsize = grow50pc > growmin ? grow50pc : growmin;
16633 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016634 newprev = prev == NULL ? alloc(prevsize)
16635 : vim_realloc(prev, prevsize);
16636 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016637 {
16638 do_outofmem_msg((long_u)prevsize);
16639 failed = TRUE;
16640 break;
16641 }
16642 prev = newprev;
16643 }
16644 /* Add the line part to end of "prev". */
16645 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016646 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016647 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016648 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016649
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016650 /*
16651 * For a negative line count use only the lines at the end of the file,
16652 * free the rest.
16653 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016654 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016655 while (cnt > -maxline)
16656 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016657 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016658 --cnt;
16659 }
16660
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016661 if (failed)
16662 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016663 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016664 /* readfile doc says an empty list is returned on error */
16665 rettv->vval.v_list = list_alloc();
16666 }
16667
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016668 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016669 fclose(fd);
16670}
16671
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016672#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016673static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016674
16675/*
16676 * Convert a List to proftime_T.
16677 * Return FAIL when there is something wrong.
16678 */
16679 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016680list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016681{
16682 long n1, n2;
16683 int error = FALSE;
16684
16685 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16686 || arg->vval.v_list->lv_len != 2)
16687 return FAIL;
16688 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16689 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16690# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016691 tm->HighPart = n1;
16692 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016693# else
16694 tm->tv_sec = n1;
16695 tm->tv_usec = n2;
16696# endif
16697 return error ? FAIL : OK;
16698}
16699#endif /* FEAT_RELTIME */
16700
16701/*
16702 * "reltime()" function
16703 */
16704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016705f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016706{
16707#ifdef FEAT_RELTIME
16708 proftime_T res;
16709 proftime_T start;
16710
16711 if (argvars[0].v_type == VAR_UNKNOWN)
16712 {
16713 /* No arguments: get current time. */
16714 profile_start(&res);
16715 }
16716 else if (argvars[1].v_type == VAR_UNKNOWN)
16717 {
16718 if (list2proftime(&argvars[0], &res) == FAIL)
16719 return;
16720 profile_end(&res);
16721 }
16722 else
16723 {
16724 /* Two arguments: compute the difference. */
16725 if (list2proftime(&argvars[0], &start) == FAIL
16726 || list2proftime(&argvars[1], &res) == FAIL)
16727 return;
16728 profile_sub(&res, &start);
16729 }
16730
16731 if (rettv_list_alloc(rettv) == OK)
16732 {
16733 long n1, n2;
16734
16735# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016736 n1 = res.HighPart;
16737 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016738# else
16739 n1 = res.tv_sec;
16740 n2 = res.tv_usec;
16741# endif
16742 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16743 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16744 }
16745#endif
16746}
16747
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016748#ifdef FEAT_FLOAT
16749/*
16750 * "reltimefloat()" function
16751 */
16752 static void
16753f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16754{
16755# ifdef FEAT_RELTIME
16756 proftime_T tm;
16757# endif
16758
16759 rettv->v_type = VAR_FLOAT;
16760 rettv->vval.v_float = 0;
16761# ifdef FEAT_RELTIME
16762 if (list2proftime(&argvars[0], &tm) == OK)
16763 rettv->vval.v_float = profile_float(&tm);
16764# endif
16765}
16766#endif
16767
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016768/*
16769 * "reltimestr()" function
16770 */
16771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016772f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016773{
16774#ifdef FEAT_RELTIME
16775 proftime_T tm;
16776#endif
16777
16778 rettv->v_type = VAR_STRING;
16779 rettv->vval.v_string = NULL;
16780#ifdef FEAT_RELTIME
16781 if (list2proftime(&argvars[0], &tm) == OK)
16782 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16783#endif
16784}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016785
Bram Moolenaar0d660222005-01-07 21:51:51 +000016786#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016787static void make_connection(void);
16788static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016789
16790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016791make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016792{
16793 if (X_DISPLAY == NULL
16794# ifdef FEAT_GUI
16795 && !gui.in_use
16796# endif
16797 )
16798 {
16799 x_force_connect = TRUE;
16800 setup_term_clip();
16801 x_force_connect = FALSE;
16802 }
16803}
16804
16805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016806check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016807{
16808 make_connection();
16809 if (X_DISPLAY == NULL)
16810 {
16811 EMSG(_("E240: No connection to Vim server"));
16812 return FAIL;
16813 }
16814 return OK;
16815}
16816#endif
16817
16818#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016819static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016820
16821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016822remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016823{
16824 char_u *server_name;
16825 char_u *keys;
16826 char_u *r = NULL;
16827 char_u buf[NUMBUFLEN];
16828# ifdef WIN32
16829 HWND w;
16830# else
16831 Window w;
16832# endif
16833
16834 if (check_restricted() || check_secure())
16835 return;
16836
16837# ifdef FEAT_X11
16838 if (check_connection() == FAIL)
16839 return;
16840# endif
16841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016842 server_name = get_tv_string_chk(&argvars[0]);
16843 if (server_name == NULL)
16844 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016845 keys = get_tv_string_buf(&argvars[1], buf);
16846# ifdef WIN32
16847 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16848# else
16849 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16850 < 0)
16851# endif
16852 {
16853 if (r != NULL)
16854 EMSG(r); /* sending worked but evaluation failed */
16855 else
16856 EMSG2(_("E241: Unable to send to %s"), server_name);
16857 return;
16858 }
16859
16860 rettv->vval.v_string = r;
16861
16862 if (argvars[2].v_type != VAR_UNKNOWN)
16863 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016864 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016865 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016866 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016867
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016868 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016869 v.di_tv.v_type = VAR_STRING;
16870 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016871 idvar = get_tv_string_chk(&argvars[2]);
16872 if (idvar != NULL)
16873 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016874 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875 }
16876}
16877#endif
16878
16879/*
16880 * "remote_expr()" function
16881 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016883f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016884{
16885 rettv->v_type = VAR_STRING;
16886 rettv->vval.v_string = NULL;
16887#ifdef FEAT_CLIENTSERVER
16888 remote_common(argvars, rettv, TRUE);
16889#endif
16890}
16891
16892/*
16893 * "remote_foreground()" function
16894 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016896f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016897{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016898#ifdef FEAT_CLIENTSERVER
16899# ifdef WIN32
16900 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016901 {
16902 char_u *server_name = get_tv_string_chk(&argvars[0]);
16903
16904 if (server_name != NULL)
16905 serverForeground(server_name);
16906 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016907# else
16908 /* Send a foreground() expression to the server. */
16909 argvars[1].v_type = VAR_STRING;
16910 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16911 argvars[2].v_type = VAR_UNKNOWN;
16912 remote_common(argvars, rettv, TRUE);
16913 vim_free(argvars[1].vval.v_string);
16914# endif
16915#endif
16916}
16917
Bram Moolenaar0d660222005-01-07 21:51:51 +000016918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016919f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016920{
16921#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016922 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 char_u *s = NULL;
16924# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016925 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016926# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016927 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928
16929 if (check_restricted() || check_secure())
16930 {
16931 rettv->vval.v_number = -1;
16932 return;
16933 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016934 serverid = get_tv_string_chk(&argvars[0]);
16935 if (serverid == NULL)
16936 {
16937 rettv->vval.v_number = -1;
16938 return; /* type error; errmsg already given */
16939 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016940# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016941 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942 if (n == 0)
16943 rettv->vval.v_number = -1;
16944 else
16945 {
16946 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16947 rettv->vval.v_number = (s != NULL);
16948 }
16949# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016950 if (check_connection() == FAIL)
16951 return;
16952
16953 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016954 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955# endif
16956
16957 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016959 char_u *retvar;
16960
Bram Moolenaar33570922005-01-25 22:26:29 +000016961 v.di_tv.v_type = VAR_STRING;
16962 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016963 retvar = get_tv_string_chk(&argvars[1]);
16964 if (retvar != NULL)
16965 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016966 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 }
16968#else
16969 rettv->vval.v_number = -1;
16970#endif
16971}
16972
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016974f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975{
16976 char_u *r = NULL;
16977
16978#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016979 char_u *serverid = get_tv_string_chk(&argvars[0]);
16980
16981 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 {
16983# ifdef WIN32
16984 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016985 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016987 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016988 if (n != 0)
16989 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16990 if (r == NULL)
16991# else
16992 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016993 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994# endif
16995 EMSG(_("E277: Unable to read a server reply"));
16996 }
16997#endif
16998 rettv->v_type = VAR_STRING;
16999 rettv->vval.v_string = r;
17000}
17001
17002/*
17003 * "remote_send()" function
17004 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017006f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017007{
17008 rettv->v_type = VAR_STRING;
17009 rettv->vval.v_string = NULL;
17010#ifdef FEAT_CLIENTSERVER
17011 remote_common(argvars, rettv, FALSE);
17012#endif
17013}
17014
17015/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017016 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017017 */
17018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017019f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017020{
Bram Moolenaar33570922005-01-25 22:26:29 +000017021 list_T *l;
17022 listitem_T *item, *item2;
17023 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017024 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017025 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017026 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017027 dict_T *d;
17028 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017029 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017030
Bram Moolenaar8c711452005-01-14 21:53:12 +000017031 if (argvars[0].v_type == VAR_DICT)
17032 {
17033 if (argvars[2].v_type != VAR_UNKNOWN)
17034 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017035 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017036 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017038 key = get_tv_string_chk(&argvars[1]);
17039 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017041 di = dict_find(d, key, -1);
17042 if (di == NULL)
17043 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017044 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17045 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017046 {
17047 *rettv = di->di_tv;
17048 init_tv(&di->di_tv);
17049 dictitem_remove(d, di);
17050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017052 }
17053 }
17054 else if (argvars[0].v_type != VAR_LIST)
17055 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017056 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017057 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017059 int error = FALSE;
17060
17061 idx = get_tv_number_chk(&argvars[1], &error);
17062 if (error)
17063 ; /* type error: do nothing, errmsg already given */
17064 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017065 EMSGN(_(e_listidx), idx);
17066 else
17067 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017068 if (argvars[2].v_type == VAR_UNKNOWN)
17069 {
17070 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017071 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017072 *rettv = item->li_tv;
17073 vim_free(item);
17074 }
17075 else
17076 {
17077 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078 end = get_tv_number_chk(&argvars[2], &error);
17079 if (error)
17080 ; /* type error: do nothing */
17081 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017082 EMSGN(_(e_listidx), end);
17083 else
17084 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017085 int cnt = 0;
17086
17087 for (li = item; li != NULL; li = li->li_next)
17088 {
17089 ++cnt;
17090 if (li == item2)
17091 break;
17092 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017093 if (li == NULL) /* didn't find "item2" after "item" */
17094 EMSG(_(e_invrange));
17095 else
17096 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017097 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017098 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017099 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017100 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017101 l->lv_first = item;
17102 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017103 item->li_prev = NULL;
17104 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017105 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017106 }
17107 }
17108 }
17109 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017110 }
17111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112}
17113
17114/*
17115 * "rename({from}, {to})" function
17116 */
17117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017118f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119{
17120 char_u buf[NUMBUFLEN];
17121
17122 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017123 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017125 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17126 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127}
17128
17129/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017130 * "repeat()" function
17131 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017133f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017134{
17135 char_u *p;
17136 int n;
17137 int slen;
17138 int len;
17139 char_u *r;
17140 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017141
17142 n = get_tv_number(&argvars[1]);
17143 if (argvars[0].v_type == VAR_LIST)
17144 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017145 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017146 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017147 if (list_extend(rettv->vval.v_list,
17148 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017149 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017150 }
17151 else
17152 {
17153 p = get_tv_string(&argvars[0]);
17154 rettv->v_type = VAR_STRING;
17155 rettv->vval.v_string = NULL;
17156
17157 slen = (int)STRLEN(p);
17158 len = slen * n;
17159 if (len <= 0)
17160 return;
17161
17162 r = alloc(len + 1);
17163 if (r != NULL)
17164 {
17165 for (i = 0; i < n; i++)
17166 mch_memmove(r + i * slen, p, (size_t)slen);
17167 r[len] = NUL;
17168 }
17169
17170 rettv->vval.v_string = r;
17171 }
17172}
17173
17174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 * "resolve()" function
17176 */
17177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017178f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179{
17180 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017181#ifdef HAVE_READLINK
17182 char_u *buf = NULL;
17183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017185 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186#ifdef FEAT_SHORTCUT
17187 {
17188 char_u *v = NULL;
17189
17190 v = mch_resolve_shortcut(p);
17191 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017192 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017194 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 }
17196#else
17197# ifdef HAVE_READLINK
17198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 char_u *cpy;
17200 int len;
17201 char_u *remain = NULL;
17202 char_u *q;
17203 int is_relative_to_current = FALSE;
17204 int has_trailing_pathsep = FALSE;
17205 int limit = 100;
17206
17207 p = vim_strsave(p);
17208
17209 if (p[0] == '.' && (vim_ispathsep(p[1])
17210 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17211 is_relative_to_current = TRUE;
17212
17213 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017214 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017217 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219
17220 q = getnextcomp(p);
17221 if (*q != NUL)
17222 {
17223 /* Separate the first path component in "p", and keep the
17224 * remainder (beginning with the path separator). */
17225 remain = vim_strsave(q - 1);
17226 q[-1] = NUL;
17227 }
17228
Bram Moolenaard9462e32011-04-11 21:35:11 +020017229 buf = alloc(MAXPATHL + 1);
17230 if (buf == NULL)
17231 goto fail;
17232
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233 for (;;)
17234 {
17235 for (;;)
17236 {
17237 len = readlink((char *)p, (char *)buf, MAXPATHL);
17238 if (len <= 0)
17239 break;
17240 buf[len] = NUL;
17241
17242 if (limit-- == 0)
17243 {
17244 vim_free(p);
17245 vim_free(remain);
17246 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017247 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 goto fail;
17249 }
17250
17251 /* Ensure that the result will have a trailing path separator
17252 * if the argument has one. */
17253 if (remain == NULL && has_trailing_pathsep)
17254 add_pathsep(buf);
17255
17256 /* Separate the first path component in the link value and
17257 * concatenate the remainders. */
17258 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17259 if (*q != NUL)
17260 {
17261 if (remain == NULL)
17262 remain = vim_strsave(q - 1);
17263 else
17264 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017265 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 if (cpy != NULL)
17267 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 vim_free(remain);
17269 remain = cpy;
17270 }
17271 }
17272 q[-1] = NUL;
17273 }
17274
17275 q = gettail(p);
17276 if (q > p && *q == NUL)
17277 {
17278 /* Ignore trailing path separator. */
17279 q[-1] = NUL;
17280 q = gettail(p);
17281 }
17282 if (q > p && !mch_isFullName(buf))
17283 {
17284 /* symlink is relative to directory of argument */
17285 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17286 if (cpy != NULL)
17287 {
17288 STRCPY(cpy, p);
17289 STRCPY(gettail(cpy), buf);
17290 vim_free(p);
17291 p = cpy;
17292 }
17293 }
17294 else
17295 {
17296 vim_free(p);
17297 p = vim_strsave(buf);
17298 }
17299 }
17300
17301 if (remain == NULL)
17302 break;
17303
17304 /* Append the first path component of "remain" to "p". */
17305 q = getnextcomp(remain + 1);
17306 len = q - remain - (*q != NUL);
17307 cpy = vim_strnsave(p, STRLEN(p) + len);
17308 if (cpy != NULL)
17309 {
17310 STRNCAT(cpy, remain, len);
17311 vim_free(p);
17312 p = cpy;
17313 }
17314 /* Shorten "remain". */
17315 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017316 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 else
17318 {
17319 vim_free(remain);
17320 remain = NULL;
17321 }
17322 }
17323
17324 /* If the result is a relative path name, make it explicitly relative to
17325 * the current directory if and only if the argument had this form. */
17326 if (!vim_ispathsep(*p))
17327 {
17328 if (is_relative_to_current
17329 && *p != NUL
17330 && !(p[0] == '.'
17331 && (p[1] == NUL
17332 || vim_ispathsep(p[1])
17333 || (p[1] == '.'
17334 && (p[2] == NUL
17335 || vim_ispathsep(p[2]))))))
17336 {
17337 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017338 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 if (cpy != NULL)
17340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341 vim_free(p);
17342 p = cpy;
17343 }
17344 }
17345 else if (!is_relative_to_current)
17346 {
17347 /* Strip leading "./". */
17348 q = p;
17349 while (q[0] == '.' && vim_ispathsep(q[1]))
17350 q += 2;
17351 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017352 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 }
17354 }
17355
17356 /* Ensure that the result will have no trailing path separator
17357 * if the argument had none. But keep "/" or "//". */
17358 if (!has_trailing_pathsep)
17359 {
17360 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017361 if (after_pathsep(p, q))
17362 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 }
17364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017365 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 }
17367# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017368 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369# endif
17370#endif
17371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017372 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373
17374#ifdef HAVE_READLINK
17375fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017376 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017378 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379}
17380
17381/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017382 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 */
17384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017385f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386{
Bram Moolenaar33570922005-01-25 22:26:29 +000017387 list_T *l;
17388 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389
Bram Moolenaar0d660222005-01-07 21:51:51 +000017390 if (argvars[0].v_type != VAR_LIST)
17391 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017392 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017393 && !tv_check_lock(l->lv_lock,
17394 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017395 {
17396 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017397 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017398 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017399 while (li != NULL)
17400 {
17401 ni = li->li_prev;
17402 list_append(l, li);
17403 li = ni;
17404 }
17405 rettv->vval.v_list = l;
17406 rettv->v_type = VAR_LIST;
17407 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017408 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410}
17411
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017412#define SP_NOMOVE 0x01 /* don't move cursor */
17413#define SP_REPEAT 0x02 /* repeat to find outer pair */
17414#define SP_RETCOUNT 0x04 /* return matchcount */
17415#define SP_SETPCMARK 0x08 /* set previous context mark */
17416#define SP_START 0x10 /* accept match at start position */
17417#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17418#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017419#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017420
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017421static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017422
17423/*
17424 * Get flags for a search function.
17425 * Possibly sets "p_ws".
17426 * Returns BACKWARD, FORWARD or zero (for an error).
17427 */
17428 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017429get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017430{
17431 int dir = FORWARD;
17432 char_u *flags;
17433 char_u nbuf[NUMBUFLEN];
17434 int mask;
17435
17436 if (varp->v_type != VAR_UNKNOWN)
17437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017438 flags = get_tv_string_buf_chk(varp, nbuf);
17439 if (flags == NULL)
17440 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017441 while (*flags != NUL)
17442 {
17443 switch (*flags)
17444 {
17445 case 'b': dir = BACKWARD; break;
17446 case 'w': p_ws = TRUE; break;
17447 case 'W': p_ws = FALSE; break;
17448 default: mask = 0;
17449 if (flagsp != NULL)
17450 switch (*flags)
17451 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017452 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017453 case 'e': mask = SP_END; break;
17454 case 'm': mask = SP_RETCOUNT; break;
17455 case 'n': mask = SP_NOMOVE; break;
17456 case 'p': mask = SP_SUBPAT; break;
17457 case 'r': mask = SP_REPEAT; break;
17458 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017459 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017460 }
17461 if (mask == 0)
17462 {
17463 EMSG2(_(e_invarg2), flags);
17464 dir = 0;
17465 }
17466 else
17467 *flagsp |= mask;
17468 }
17469 if (dir == 0)
17470 break;
17471 ++flags;
17472 }
17473 }
17474 return dir;
17475}
17476
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017478 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017480 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017481search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017483 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 char_u *pat;
17485 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017486 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 int save_p_ws = p_ws;
17488 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017489 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017490 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017491 proftime_T tm;
17492#ifdef FEAT_RELTIME
17493 long time_limit = 0;
17494#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017495 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017496 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017498 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017499 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017500 if (dir == 0)
17501 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017502 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017503 if (flags & SP_START)
17504 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017505 if (flags & SP_END)
17506 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017507 if (flags & SP_COLUMN)
17508 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017509
Bram Moolenaar76929292008-01-06 19:07:36 +000017510 /* Optional arguments: line number to stop searching and timeout. */
17511 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017512 {
17513 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17514 if (lnum_stop < 0)
17515 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017516#ifdef FEAT_RELTIME
17517 if (argvars[3].v_type != VAR_UNKNOWN)
17518 {
17519 time_limit = get_tv_number_chk(&argvars[3], NULL);
17520 if (time_limit < 0)
17521 goto theend;
17522 }
17523#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017524 }
17525
Bram Moolenaar76929292008-01-06 19:07:36 +000017526#ifdef FEAT_RELTIME
17527 /* Set the time limit, if there is one. */
17528 profile_setlimit(time_limit, &tm);
17529#endif
17530
Bram Moolenaar231334e2005-07-25 20:46:57 +000017531 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017532 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017533 * Check to make sure only those flags are set.
17534 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17535 * flags cannot be set. Check for that condition also.
17536 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017537 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017538 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017540 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017541 goto theend;
17542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017544 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017545 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017546 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017547 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017549 if (flags & SP_SUBPAT)
17550 retval = subpatnum;
17551 else
17552 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017553 if (flags & SP_SETPCMARK)
17554 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017556 if (match_pos != NULL)
17557 {
17558 /* Store the match cursor position */
17559 match_pos->lnum = pos.lnum;
17560 match_pos->col = pos.col + 1;
17561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 /* "/$" will put the cursor after the end of the line, may need to
17563 * correct that here */
17564 check_cursor();
17565 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017566
17567 /* If 'n' flag is used: restore cursor position. */
17568 if (flags & SP_NOMOVE)
17569 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017570 else
17571 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017572theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017574
17575 return retval;
17576}
17577
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017578#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017579
17580/*
17581 * round() is not in C90, use ceil() or floor() instead.
17582 */
17583 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017584vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017585{
17586 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17587}
17588
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017589/*
17590 * "round({float})" function
17591 */
17592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017593f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017594{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017595 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017596
17597 rettv->v_type = VAR_FLOAT;
17598 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017599 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017600 else
17601 rettv->vval.v_float = 0.0;
17602}
17603#endif
17604
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017605/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017606 * "screenattr()" function
17607 */
17608 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017609f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017610{
17611 int row;
17612 int col;
17613 int c;
17614
17615 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17616 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17617 if (row < 0 || row >= screen_Rows
17618 || col < 0 || col >= screen_Columns)
17619 c = -1;
17620 else
17621 c = ScreenAttrs[LineOffset[row] + col];
17622 rettv->vval.v_number = c;
17623}
17624
17625/*
17626 * "screenchar()" function
17627 */
17628 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017629f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017630{
17631 int row;
17632 int col;
17633 int off;
17634 int c;
17635
17636 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17637 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17638 if (row < 0 || row >= screen_Rows
17639 || col < 0 || col >= screen_Columns)
17640 c = -1;
17641 else
17642 {
17643 off = LineOffset[row] + col;
17644#ifdef FEAT_MBYTE
17645 if (enc_utf8 && ScreenLinesUC[off] != 0)
17646 c = ScreenLinesUC[off];
17647 else
17648#endif
17649 c = ScreenLines[off];
17650 }
17651 rettv->vval.v_number = c;
17652}
17653
17654/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017655 * "screencol()" function
17656 *
17657 * First column is 1 to be consistent with virtcol().
17658 */
17659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017660f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017661{
17662 rettv->vval.v_number = screen_screencol() + 1;
17663}
17664
17665/*
17666 * "screenrow()" function
17667 */
17668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017669f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017670{
17671 rettv->vval.v_number = screen_screenrow() + 1;
17672}
17673
17674/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017675 * "search()" function
17676 */
17677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017678f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017679{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017680 int flags = 0;
17681
17682 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683}
17684
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017686 * "searchdecl()" function
17687 */
17688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017689f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017690{
17691 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017692 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017693 int error = FALSE;
17694 char_u *name;
17695
17696 rettv->vval.v_number = 1; /* default: FAIL */
17697
17698 name = get_tv_string_chk(&argvars[0]);
17699 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017700 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017701 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017702 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17703 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17704 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017705 if (!error && name != NULL)
17706 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017707 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017708}
17709
17710/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017711 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017713 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017714searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715{
17716 char_u *spat, *mpat, *epat;
17717 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 int dir;
17720 int flags = 0;
17721 char_u nbuf1[NUMBUFLEN];
17722 char_u nbuf2[NUMBUFLEN];
17723 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017724 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017725 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017726 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017729 spat = get_tv_string_chk(&argvars[0]);
17730 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17731 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17732 if (spat == NULL || mpat == NULL || epat == NULL)
17733 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735 /* Handle the optional fourth argument: flags */
17736 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017737 if (dir == 0)
17738 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017739
17740 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017741 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17742 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017743 if ((flags & (SP_END | SP_SUBPAT)) != 0
17744 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017745 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017746 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017747 goto theend;
17748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017750 /* Using 'r' implies 'W', otherwise it doesn't work. */
17751 if (flags & SP_REPEAT)
17752 p_ws = FALSE;
17753
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017754 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017755 if (argvars[3].v_type == VAR_UNKNOWN
17756 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 skip = (char_u *)"";
17758 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017760 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017761 if (argvars[5].v_type != VAR_UNKNOWN)
17762 {
17763 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17764 if (lnum_stop < 0)
17765 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017766#ifdef FEAT_RELTIME
17767 if (argvars[6].v_type != VAR_UNKNOWN)
17768 {
17769 time_limit = get_tv_number_chk(&argvars[6], NULL);
17770 if (time_limit < 0)
17771 goto theend;
17772 }
17773#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017774 }
17775 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017776 if (skip == NULL)
17777 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017779 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017780 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017781
17782theend:
17783 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017784
17785 return retval;
17786}
17787
17788/*
17789 * "searchpair()" function
17790 */
17791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017792f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017793{
17794 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17795}
17796
17797/*
17798 * "searchpairpos()" function
17799 */
17800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017801f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017802{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017803 pos_T match_pos;
17804 int lnum = 0;
17805 int col = 0;
17806
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017807 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017808 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017809
17810 if (searchpair_cmn(argvars, &match_pos) > 0)
17811 {
17812 lnum = match_pos.lnum;
17813 col = match_pos.col;
17814 }
17815
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017816 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17817 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017818}
17819
17820/*
17821 * Search for a start/middle/end thing.
17822 * Used by searchpair(), see its documentation for the details.
17823 * Returns 0 or -1 for no match,
17824 */
17825 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017826do_searchpair(
17827 char_u *spat, /* start pattern */
17828 char_u *mpat, /* middle pattern */
17829 char_u *epat, /* end pattern */
17830 int dir, /* BACKWARD or FORWARD */
17831 char_u *skip, /* skip expression */
17832 int flags, /* SP_SETPCMARK and other SP_ values */
17833 pos_T *match_pos,
17834 linenr_T lnum_stop, /* stop at this line if not zero */
17835 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017836{
17837 char_u *save_cpo;
17838 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17839 long retval = 0;
17840 pos_T pos;
17841 pos_T firstpos;
17842 pos_T foundpos;
17843 pos_T save_cursor;
17844 pos_T save_pos;
17845 int n;
17846 int r;
17847 int nest = 1;
17848 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017849 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017850 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017851
17852 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17853 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017854 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017855
Bram Moolenaar76929292008-01-06 19:07:36 +000017856#ifdef FEAT_RELTIME
17857 /* Set the time limit, if there is one. */
17858 profile_setlimit(time_limit, &tm);
17859#endif
17860
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017861 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17862 * start/middle/end (pat3, for the top pair). */
17863 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17864 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17865 if (pat2 == NULL || pat3 == NULL)
17866 goto theend;
17867 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17868 if (*mpat == NUL)
17869 STRCPY(pat3, pat2);
17870 else
17871 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17872 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017873 if (flags & SP_START)
17874 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017875
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876 save_cursor = curwin->w_cursor;
17877 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017878 clearpos(&firstpos);
17879 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880 pat = pat3;
17881 for (;;)
17882 {
17883 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017884 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17886 /* didn't find it or found the first match again: FAIL */
17887 break;
17888
17889 if (firstpos.lnum == 0)
17890 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017891 if (equalpos(pos, foundpos))
17892 {
17893 /* Found the same position again. Can happen with a pattern that
17894 * has "\zs" at the end and searching backwards. Advance one
17895 * character and try again. */
17896 if (dir == BACKWARD)
17897 decl(&pos);
17898 else
17899 incl(&pos);
17900 }
17901 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017903 /* clear the start flag to avoid getting stuck here */
17904 options &= ~SEARCH_START;
17905
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906 /* If the skip pattern matches, ignore this match. */
17907 if (*skip != NUL)
17908 {
17909 save_pos = curwin->w_cursor;
17910 curwin->w_cursor = pos;
17911 r = eval_to_bool(skip, &err, NULL, FALSE);
17912 curwin->w_cursor = save_pos;
17913 if (err)
17914 {
17915 /* Evaluating {skip} caused an error, break here. */
17916 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017917 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 break;
17919 }
17920 if (r)
17921 continue;
17922 }
17923
17924 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17925 {
17926 /* Found end when searching backwards or start when searching
17927 * forward: nested pair. */
17928 ++nest;
17929 pat = pat2; /* nested, don't search for middle */
17930 }
17931 else
17932 {
17933 /* Found end when searching forward or start when searching
17934 * backward: end of (nested) pair; or found middle in outer pair. */
17935 if (--nest == 1)
17936 pat = pat3; /* outer level, search for middle */
17937 }
17938
17939 if (nest == 0)
17940 {
17941 /* Found the match: return matchcount or line number. */
17942 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017943 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017945 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017946 if (flags & SP_SETPCMARK)
17947 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 curwin->w_cursor = pos;
17949 if (!(flags & SP_REPEAT))
17950 break;
17951 nest = 1; /* search for next unmatched */
17952 }
17953 }
17954
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017955 if (match_pos != NULL)
17956 {
17957 /* Store the match cursor position */
17958 match_pos->lnum = curwin->w_cursor.lnum;
17959 match_pos->col = curwin->w_cursor.col + 1;
17960 }
17961
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017963 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 curwin->w_cursor = save_cursor;
17965
17966theend:
17967 vim_free(pat2);
17968 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017969 if (p_cpo == empty_option)
17970 p_cpo = save_cpo;
17971 else
17972 /* Darn, evaluating the {skip} expression changed the value. */
17973 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017974
17975 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976}
17977
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017978/*
17979 * "searchpos()" function
17980 */
17981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017982f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017983{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017984 pos_T match_pos;
17985 int lnum = 0;
17986 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017987 int n;
17988 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017989
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017990 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017991 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017992
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017993 n = search_cmn(argvars, &match_pos, &flags);
17994 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017995 {
17996 lnum = match_pos.lnum;
17997 col = match_pos.col;
17998 }
17999
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018000 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18001 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018002 if (flags & SP_SUBPAT)
18003 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018004}
18005
Bram Moolenaar0d660222005-01-07 21:51:51 +000018006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018007f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018009#ifdef FEAT_CLIENTSERVER
18010 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018011 char_u *server = get_tv_string_chk(&argvars[0]);
18012 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013
Bram Moolenaar0d660222005-01-07 21:51:51 +000018014 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018015 if (server == NULL || reply == NULL)
18016 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018017 if (check_restricted() || check_secure())
18018 return;
18019# ifdef FEAT_X11
18020 if (check_connection() == FAIL)
18021 return;
18022# endif
18023
18024 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018026 EMSG(_("E258: Unable to send to client"));
18027 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018029 rettv->vval.v_number = 0;
18030#else
18031 rettv->vval.v_number = -1;
18032#endif
18033}
18034
Bram Moolenaar0d660222005-01-07 21:51:51 +000018035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018036f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018037{
18038 char_u *r = NULL;
18039
18040#ifdef FEAT_CLIENTSERVER
18041# ifdef WIN32
18042 r = serverGetVimNames();
18043# else
18044 make_connection();
18045 if (X_DISPLAY != NULL)
18046 r = serverGetVimNames(X_DISPLAY);
18047# endif
18048#endif
18049 rettv->v_type = VAR_STRING;
18050 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051}
18052
18053/*
18054 * "setbufvar()" function
18055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018057f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058{
18059 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018062 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 char_u nbuf[NUMBUFLEN];
18064
18065 if (check_restricted() || check_secure())
18066 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018067 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18068 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018069 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 varp = &argvars[2];
18071
18072 if (buf != NULL && varname != NULL && varp != NULL)
18073 {
18074 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076
18077 if (*varname == '&')
18078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018079 long numval;
18080 char_u *strval;
18081 int error = FALSE;
18082
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018084 numval = get_tv_number_chk(varp, &error);
18085 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018086 if (!error && strval != NULL)
18087 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 }
18089 else
18090 {
18091 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18092 if (bufvarname != NULL)
18093 {
18094 STRCPY(bufvarname, "b:");
18095 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018096 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 vim_free(bufvarname);
18098 }
18099 }
18100
18101 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104}
18105
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018107f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018108{
18109 dict_T *d;
18110 dictitem_T *di;
18111 char_u *csearch;
18112
18113 if (argvars[0].v_type != VAR_DICT)
18114 {
18115 EMSG(_(e_dictreq));
18116 return;
18117 }
18118
18119 if ((d = argvars[0].vval.v_dict) != NULL)
18120 {
18121 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18122 if (csearch != NULL)
18123 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018124#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018125 if (enc_utf8)
18126 {
18127 int pcc[MAX_MCO];
18128 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018129
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018130 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18131 }
18132 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018133#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018134 set_last_csearch(PTR2CHAR(csearch),
18135 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018136 }
18137
18138 di = dict_find(d, (char_u *)"forward", -1);
18139 if (di != NULL)
18140 set_csearch_direction(get_tv_number(&di->di_tv)
18141 ? FORWARD : BACKWARD);
18142
18143 di = dict_find(d, (char_u *)"until", -1);
18144 if (di != NULL)
18145 set_csearch_until(!!get_tv_number(&di->di_tv));
18146 }
18147}
18148
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149/*
18150 * "setcmdpos()" function
18151 */
18152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018153f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018155 int pos = (int)get_tv_number(&argvars[0]) - 1;
18156
18157 if (pos >= 0)
18158 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159}
18160
18161/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018162 * "setfperm({fname}, {mode})" function
18163 */
18164 static void
18165f_setfperm(typval_T *argvars, typval_T *rettv)
18166{
18167 char_u *fname;
18168 char_u modebuf[NUMBUFLEN];
18169 char_u *mode_str;
18170 int i;
18171 int mask;
18172 int mode = 0;
18173
18174 rettv->vval.v_number = 0;
18175 fname = get_tv_string_chk(&argvars[0]);
18176 if (fname == NULL)
18177 return;
18178 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18179 if (mode_str == NULL)
18180 return;
18181 if (STRLEN(mode_str) != 9)
18182 {
18183 EMSG2(_(e_invarg2), mode_str);
18184 return;
18185 }
18186
18187 mask = 1;
18188 for (i = 8; i >= 0; --i)
18189 {
18190 if (mode_str[i] != '-')
18191 mode |= mask;
18192 mask = mask << 1;
18193 }
18194 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18195}
18196
18197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198 * "setline()" function
18199 */
18200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018201f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202{
18203 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018204 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018205 list_T *l = NULL;
18206 listitem_T *li = NULL;
18207 long added = 0;
18208 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018210 lnum = get_tv_lnum(&argvars[0]);
18211 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018213 l = argvars[1].vval.v_list;
18214 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018216 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018217 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018218
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018219 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018220 for (;;)
18221 {
18222 if (l != NULL)
18223 {
18224 /* list argument, get next string */
18225 if (li == NULL)
18226 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018227 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018228 li = li->li_next;
18229 }
18230
18231 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018232 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018233 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018234
18235 /* When coming here from Insert mode, sync undo, so that this can be
18236 * undone separately from what was previously inserted. */
18237 if (u_sync_once == 2)
18238 {
18239 u_sync_once = 1; /* notify that u_sync() was called */
18240 u_sync(TRUE);
18241 }
18242
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018243 if (lnum <= curbuf->b_ml.ml_line_count)
18244 {
18245 /* existing line, replace it */
18246 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18247 {
18248 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018249 if (lnum == curwin->w_cursor.lnum)
18250 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018251 rettv->vval.v_number = 0; /* OK */
18252 }
18253 }
18254 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18255 {
18256 /* lnum is one past the last line, append the line */
18257 ++added;
18258 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18259 rettv->vval.v_number = 0; /* OK */
18260 }
18261
18262 if (l == NULL) /* only one string argument */
18263 break;
18264 ++lnum;
18265 }
18266
18267 if (added > 0)
18268 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269}
18270
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018271static 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 +000018272
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018274 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018275 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018277set_qf_ll_list(
18278 win_T *wp UNUSED,
18279 typval_T *list_arg UNUSED,
18280 typval_T *action_arg UNUSED,
18281 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018282{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018283#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018284 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018285 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018286 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018287#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018288
Bram Moolenaar2641f772005-03-25 21:58:17 +000018289 rettv->vval.v_number = -1;
18290
18291#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018292 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018293 EMSG(_(e_listreq));
18294 else
18295 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018296 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018297
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018298 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018299 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018300 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018301 if (act == NULL)
18302 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018303 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018304 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018305 else
18306 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018307 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018308 else if (action_arg->v_type == VAR_UNKNOWN)
18309 action = ' ';
18310 else
18311 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018312
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018313 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018314 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018315 rettv->vval.v_number = 0;
18316 }
18317#endif
18318}
18319
18320/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018321 * "setloclist()" function
18322 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018324f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018325{
18326 win_T *win;
18327
18328 rettv->vval.v_number = -1;
18329
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018330 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018331 if (win != NULL)
18332 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18333}
18334
18335/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018336 * "setmatches()" function
18337 */
18338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018339f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018340{
18341#ifdef FEAT_SEARCH_EXTRA
18342 list_T *l;
18343 listitem_T *li;
18344 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018345 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018346
18347 rettv->vval.v_number = -1;
18348 if (argvars[0].v_type != VAR_LIST)
18349 {
18350 EMSG(_(e_listreq));
18351 return;
18352 }
18353 if ((l = argvars[0].vval.v_list) != NULL)
18354 {
18355
18356 /* To some extent make sure that we are dealing with a list from
18357 * "getmatches()". */
18358 li = l->lv_first;
18359 while (li != NULL)
18360 {
18361 if (li->li_tv.v_type != VAR_DICT
18362 || (d = li->li_tv.vval.v_dict) == NULL)
18363 {
18364 EMSG(_(e_invarg));
18365 return;
18366 }
18367 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018368 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18369 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018370 && dict_find(d, (char_u *)"priority", -1) != NULL
18371 && dict_find(d, (char_u *)"id", -1) != NULL))
18372 {
18373 EMSG(_(e_invarg));
18374 return;
18375 }
18376 li = li->li_next;
18377 }
18378
18379 clear_matches(curwin);
18380 li = l->lv_first;
18381 while (li != NULL)
18382 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018383 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018384 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018385 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018386 char_u *group;
18387 int priority;
18388 int id;
18389 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018390
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018391 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018392 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18393 {
18394 if (s == NULL)
18395 {
18396 s = list_alloc();
18397 if (s == NULL)
18398 return;
18399 }
18400
18401 /* match from matchaddpos() */
18402 for (i = 1; i < 9; i++)
18403 {
18404 sprintf((char *)buf, (char *)"pos%d", i);
18405 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18406 {
18407 if (di->di_tv.v_type != VAR_LIST)
18408 return;
18409
18410 list_append_tv(s, &di->di_tv);
18411 s->lv_refcount++;
18412 }
18413 else
18414 break;
18415 }
18416 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018417
18418 group = get_dict_string(d, (char_u *)"group", FALSE);
18419 priority = (int)get_dict_number(d, (char_u *)"priority");
18420 id = (int)get_dict_number(d, (char_u *)"id");
18421 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18422 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18423 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018424 if (i == 0)
18425 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018426 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018427 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018428 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018429 }
18430 else
18431 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018432 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018433 list_unref(s);
18434 s = NULL;
18435 }
18436
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018437 li = li->li_next;
18438 }
18439 rettv->vval.v_number = 0;
18440 }
18441#endif
18442}
18443
18444/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018445 * "setpos()" function
18446 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018448f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018449{
18450 pos_T pos;
18451 int fnum;
18452 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018453 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018454
Bram Moolenaar08250432008-02-13 11:42:46 +000018455 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018456 name = get_tv_string_chk(argvars);
18457 if (name != NULL)
18458 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018459 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018460 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018461 if (--pos.col < 0)
18462 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018463 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018464 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018465 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018466 if (fnum == curbuf->b_fnum)
18467 {
18468 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018469 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018470 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018471 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018472 curwin->w_set_curswant = FALSE;
18473 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018474 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018475 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018476 }
18477 else
18478 EMSG(_(e_invarg));
18479 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018480 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18481 {
18482 /* set mark */
18483 if (setmark_pos(name[1], &pos, fnum) == OK)
18484 rettv->vval.v_number = 0;
18485 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018486 else
18487 EMSG(_(e_invarg));
18488 }
18489 }
18490}
18491
18492/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018493 * "setqflist()" function
18494 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018496f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018497{
18498 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18499}
18500
18501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502 * "setreg()" function
18503 */
18504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018505f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506{
18507 int regname;
18508 char_u *strregname;
18509 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018510 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 int append;
18512 char_u yank_type;
18513 long block_len;
18514
18515 block_len = -1;
18516 yank_type = MAUTO;
18517 append = FALSE;
18518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018519 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018520 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018522 if (strregname == NULL)
18523 return; /* type error; errmsg already given */
18524 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 if (regname == 0 || regname == '@')
18526 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018528 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018530 stropt = get_tv_string_chk(&argvars[2]);
18531 if (stropt == NULL)
18532 return; /* type error */
18533 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 switch (*stropt)
18535 {
18536 case 'a': case 'A': /* append */
18537 append = TRUE;
18538 break;
18539 case 'v': case 'c': /* character-wise selection */
18540 yank_type = MCHAR;
18541 break;
18542 case 'V': case 'l': /* line-wise selection */
18543 yank_type = MLINE;
18544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 case 'b': case Ctrl_V: /* block-wise selection */
18546 yank_type = MBLOCK;
18547 if (VIM_ISDIGIT(stropt[1]))
18548 {
18549 ++stropt;
18550 block_len = getdigits(&stropt) - 1;
18551 --stropt;
18552 }
18553 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554 }
18555 }
18556
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018557 if (argvars[1].v_type == VAR_LIST)
18558 {
18559 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018560 char_u **allocval;
18561 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018562 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018563 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018564 int len = argvars[1].vval.v_list->lv_len;
18565 listitem_T *li;
18566
Bram Moolenaar7d647822014-04-05 21:28:56 +020018567 /* First half: use for pointers to result lines; second half: use for
18568 * pointers to allocated copies. */
18569 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018570 if (lstval == NULL)
18571 return;
18572 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018573 allocval = lstval + len + 2;
18574 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018575
18576 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18577 li = li->li_next)
18578 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018579 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018580 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018581 goto free_lstval;
18582 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018583 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018584 /* Need to make a copy, next get_tv_string_buf_chk() will
18585 * overwrite the string. */
18586 strval = vim_strsave(buf);
18587 if (strval == NULL)
18588 goto free_lstval;
18589 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018590 }
18591 *curval++ = strval;
18592 }
18593 *curval++ = NULL;
18594
18595 write_reg_contents_lst(regname, lstval, -1,
18596 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018597free_lstval:
18598 while (curallocval > allocval)
18599 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018600 vim_free(lstval);
18601 }
18602 else
18603 {
18604 strval = get_tv_string_chk(&argvars[1]);
18605 if (strval == NULL)
18606 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018607 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018609 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018610 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611}
18612
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018613/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018614 * "settabvar()" function
18615 */
18616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018617f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018618{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018619#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018620 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018621 tabpage_T *tp;
18622#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018623 char_u *varname, *tabvarname;
18624 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018625
18626 rettv->vval.v_number = 0;
18627
18628 if (check_restricted() || check_secure())
18629 return;
18630
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018631#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018632 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018633#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018634 varname = get_tv_string_chk(&argvars[1]);
18635 varp = &argvars[2];
18636
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018637 if (varname != NULL && varp != NULL
18638#ifdef FEAT_WINDOWS
18639 && tp != NULL
18640#endif
18641 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018642 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018643#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018644 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018645 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018646#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018647
18648 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18649 if (tabvarname != NULL)
18650 {
18651 STRCPY(tabvarname, "t:");
18652 STRCPY(tabvarname + 2, varname);
18653 set_var(tabvarname, varp, TRUE);
18654 vim_free(tabvarname);
18655 }
18656
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018657#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018658 /* Restore current tabpage */
18659 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018660 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018661#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018662 }
18663}
18664
18665/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018666 * "settabwinvar()" function
18667 */
18668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018669f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018670{
18671 setwinvar(argvars, rettv, 1);
18672}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673
18674/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018675 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018678f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018680 setwinvar(argvars, rettv, 0);
18681}
18682
18683/*
18684 * "setwinvar()" and "settabwinvar()" functions
18685 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018686
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018688setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018689{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 win_T *win;
18691#ifdef FEAT_WINDOWS
18692 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018693 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018694 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695#endif
18696 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018697 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018699 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700
18701 if (check_restricted() || check_secure())
18702 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018703
18704#ifdef FEAT_WINDOWS
18705 if (off == 1)
18706 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18707 else
18708 tp = curtab;
18709#endif
18710 win = find_win_by_nr(&argvars[off], tp);
18711 varname = get_tv_string_chk(&argvars[off + 1]);
18712 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713
18714 if (win != NULL && varname != NULL && varp != NULL)
18715 {
18716#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018717 need_switch_win = !(tp == curtab && win == curwin);
18718 if (!need_switch_win
18719 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018722 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018724 long numval;
18725 char_u *strval;
18726 int error = FALSE;
18727
18728 ++varname;
18729 numval = get_tv_number_chk(varp, &error);
18730 strval = get_tv_string_buf_chk(varp, nbuf);
18731 if (!error && strval != NULL)
18732 set_option_value(varname, numval, strval, OPT_LOCAL);
18733 }
18734 else
18735 {
18736 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18737 if (winvarname != NULL)
18738 {
18739 STRCPY(winvarname, "w:");
18740 STRCPY(winvarname + 2, varname);
18741 set_var(winvarname, varp, TRUE);
18742 vim_free(winvarname);
18743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 }
18745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018747 if (need_switch_win)
18748 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749#endif
18750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751}
18752
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018753#ifdef FEAT_CRYPT
18754/*
18755 * "sha256({string})" function
18756 */
18757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018758f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018759{
18760 char_u *p;
18761
18762 p = get_tv_string(&argvars[0]);
18763 rettv->vval.v_string = vim_strsave(
18764 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18765 rettv->v_type = VAR_STRING;
18766}
18767#endif /* FEAT_CRYPT */
18768
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018770 * "shellescape({string})" function
18771 */
18772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018773f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018774{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018775 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018776 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018777 rettv->v_type = VAR_STRING;
18778}
18779
18780/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018781 * shiftwidth() function
18782 */
18783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018784f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018785{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018786 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018787}
18788
18789/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018790 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 */
18792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018793f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018795 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796
Bram Moolenaar0d660222005-01-07 21:51:51 +000018797 p = get_tv_string(&argvars[0]);
18798 rettv->vval.v_string = vim_strsave(p);
18799 simplify_filename(rettv->vval.v_string); /* simplify in place */
18800 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801}
18802
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018803#ifdef FEAT_FLOAT
18804/*
18805 * "sin()" function
18806 */
18807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018808f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018809{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018810 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018811
18812 rettv->v_type = VAR_FLOAT;
18813 if (get_float_arg(argvars, &f) == OK)
18814 rettv->vval.v_float = sin(f);
18815 else
18816 rettv->vval.v_float = 0.0;
18817}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018818
18819/*
18820 * "sinh()" function
18821 */
18822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018823f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018824{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018825 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018826
18827 rettv->v_type = VAR_FLOAT;
18828 if (get_float_arg(argvars, &f) == OK)
18829 rettv->vval.v_float = sinh(f);
18830 else
18831 rettv->vval.v_float = 0.0;
18832}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018833#endif
18834
Bram Moolenaar0d660222005-01-07 21:51:51 +000018835static int
18836#ifdef __BORLANDC__
18837 _RTLENTRYF
18838#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018839 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018840static int
18841#ifdef __BORLANDC__
18842 _RTLENTRYF
18843#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018844 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018845
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018846/* struct used in the array that's given to qsort() */
18847typedef struct
18848{
18849 listitem_T *item;
18850 int idx;
18851} sortItem_T;
18852
Bram Moolenaar0b962472016-02-22 22:51:33 +010018853/* struct storing information about current sort */
18854typedef struct
18855{
18856 int item_compare_ic;
18857 int item_compare_numeric;
18858 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018859#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018860 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018861#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018862 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018863 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018864 dict_T *item_compare_selfdict;
18865 int item_compare_func_err;
18866 int item_compare_keep_zero;
18867} sortinfo_T;
18868static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018869static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018870#define ITEM_COMPARE_FAIL 999
18871
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018873 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018875 static int
18876#ifdef __BORLANDC__
18877_RTLENTRYF
18878#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018879item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018881 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018882 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018883 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018884 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018885 int res;
18886 char_u numbuf1[NUMBUFLEN];
18887 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018889 si1 = (sortItem_T *)s1;
18890 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018891 tv1 = &si1->item->li_tv;
18892 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018893
Bram Moolenaar0b962472016-02-22 22:51:33 +010018894 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018895 {
18896 long v1 = get_tv_number(tv1);
18897 long v2 = get_tv_number(tv2);
18898
18899 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18900 }
18901
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018902#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018903 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018904 {
18905 float_T v1 = get_tv_float(tv1);
18906 float_T v2 = get_tv_float(tv2);
18907
18908 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18909 }
18910#endif
18911
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018912 /* tv2string() puts quotes around a string and allocates memory. Don't do
18913 * that for string variables. Use a single quote when comparing with a
18914 * non-string to do what the docs promise. */
18915 if (tv1->v_type == VAR_STRING)
18916 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018917 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018918 p1 = (char_u *)"'";
18919 else
18920 p1 = tv1->vval.v_string;
18921 }
18922 else
18923 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18924 if (tv2->v_type == VAR_STRING)
18925 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018926 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018927 p2 = (char_u *)"'";
18928 else
18929 p2 = tv2->vval.v_string;
18930 }
18931 else
18932 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018933 if (p1 == NULL)
18934 p1 = (char_u *)"";
18935 if (p2 == NULL)
18936 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018937 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018938 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018939 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018940 res = STRICMP(p1, p2);
18941 else
18942 res = STRCMP(p1, p2);
18943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018945 {
18946 double n1, n2;
18947 n1 = strtod((char *)p1, (char **)&p1);
18948 n2 = strtod((char *)p2, (char **)&p2);
18949 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18950 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018951
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018952 /* When the result would be zero, compare the item indexes. Makes the
18953 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018954 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018955 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018956
Bram Moolenaar0d660222005-01-07 21:51:51 +000018957 vim_free(tofree1);
18958 vim_free(tofree2);
18959 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960}
18961
18962 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963#ifdef __BORLANDC__
18964_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018966item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018967{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018968 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018969 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018970 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018971 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018972 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018973 char_u *func_name;
18974 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018976 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018977 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018978 return 0;
18979
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018980 si1 = (sortItem_T *)s1;
18981 si2 = (sortItem_T *)s2;
18982
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018983 if (partial == NULL)
18984 func_name = sortinfo->item_compare_func;
18985 else
18986 func_name = partial->pt_name;
18987
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018988 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018989 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018990 copy_tv(&si1->item->li_tv, &argv[0]);
18991 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018992
18993 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018994 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018995 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018996 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018997 clear_tv(&argv[0]);
18998 clear_tv(&argv[1]);
18999
19000 if (res == FAIL)
19001 res = ITEM_COMPARE_FAIL;
19002 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019003 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19004 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019005 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019006 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019007
19008 /* When the result would be zero, compare the pointers themselves. Makes
19009 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019010 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019011 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019012
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013 return res;
19014}
19015
19016/*
19017 * "sort({list})" function
19018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019020do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021{
Bram Moolenaar33570922005-01-25 22:26:29 +000019022 list_T *l;
19023 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019024 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019025 sortinfo_T *old_sortinfo;
19026 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019027 long len;
19028 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029
Bram Moolenaar0b962472016-02-22 22:51:33 +010019030 /* Pointer to current info struct used in compare function. Save and
19031 * restore the current one for nested calls. */
19032 old_sortinfo = sortinfo;
19033 sortinfo = &info;
19034
Bram Moolenaar0d660222005-01-07 21:51:51 +000019035 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019036 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 else
19038 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019039 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019040 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019041 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19042 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019043 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019044 rettv->vval.v_list = l;
19045 rettv->v_type = VAR_LIST;
19046 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047
Bram Moolenaar0d660222005-01-07 21:51:51 +000019048 len = list_len(l);
19049 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019050 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051
Bram Moolenaar0b962472016-02-22 22:51:33 +010019052 info.item_compare_ic = FALSE;
19053 info.item_compare_numeric = FALSE;
19054 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019055#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019056 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019057#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019058 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019059 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019060 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019061 if (argvars[1].v_type != VAR_UNKNOWN)
19062 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019063 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019064 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019065 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019066 else if (argvars[1].v_type == VAR_PARTIAL)
19067 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019068 else
19069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019070 int error = FALSE;
19071
19072 i = get_tv_number_chk(&argvars[1], &error);
19073 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019074 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019075 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019076 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019077 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019078 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019079 else if (i != 0)
19080 {
19081 EMSG(_(e_invarg));
19082 goto theend;
19083 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019084 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019085 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019086 if (*info.item_compare_func == NUL)
19087 {
19088 /* empty string means default sort */
19089 info.item_compare_func = NULL;
19090 }
19091 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019092 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019093 info.item_compare_func = NULL;
19094 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019095 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019096 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019097 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019098 info.item_compare_func = NULL;
19099 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019100 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019101#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019102 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019103 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019104 info.item_compare_func = NULL;
19105 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019106 }
19107#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019108 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019109 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019110 info.item_compare_func = NULL;
19111 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019112 }
19113 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019114 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019115
19116 if (argvars[2].v_type != VAR_UNKNOWN)
19117 {
19118 /* optional third argument: {dict} */
19119 if (argvars[2].v_type != VAR_DICT)
19120 {
19121 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019122 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019123 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019124 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127
Bram Moolenaar0d660222005-01-07 21:51:51 +000019128 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019129 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019130 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019131 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132
Bram Moolenaar327aa022014-03-25 18:24:23 +010019133 i = 0;
19134 if (sort)
19135 {
19136 /* sort(): ptrs will be the list to sort */
19137 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019138 {
19139 ptrs[i].item = li;
19140 ptrs[i].idx = i;
19141 ++i;
19142 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019143
Bram Moolenaar0b962472016-02-22 22:51:33 +010019144 info.item_compare_func_err = FALSE;
19145 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019146 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019147 if ((info.item_compare_func != NULL
19148 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019149 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019150 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019151 EMSG(_("E702: Sort compare function failed"));
19152 else
19153 {
19154 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019155 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019156 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019157 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019158 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019159
Bram Moolenaar0b962472016-02-22 22:51:33 +010019160 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019161 {
19162 /* Clear the List and append the items in sorted order. */
19163 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19164 l->lv_len = 0;
19165 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019166 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019167 }
19168 }
19169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019171 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019172 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019173
19174 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019175 info.item_compare_func_err = FALSE;
19176 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019177 item_compare_func_ptr = info.item_compare_func != NULL
19178 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019179 ? item_compare2 : item_compare;
19180
19181 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19182 li = li->li_next)
19183 {
19184 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19185 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019186 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019187 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019188 {
19189 EMSG(_("E882: Uniq compare function failed"));
19190 break;
19191 }
19192 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019193
Bram Moolenaar0b962472016-02-22 22:51:33 +010019194 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019195 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019196 while (--i >= 0)
19197 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019198 li = ptrs[i].item->li_next;
19199 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019200 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019201 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019202 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019203 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019204 list_fix_watch(l, li);
19205 listitem_free(li);
19206 l->lv_len--;
19207 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019208 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019209 }
19210
19211 vim_free(ptrs);
19212 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019213theend:
19214 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019215}
19216
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019217/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019218 * "sort({list})" function
19219 */
19220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019221f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019222{
19223 do_sort_uniq(argvars, rettv, TRUE);
19224}
19225
19226/*
19227 * "uniq({list})" function
19228 */
19229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019230f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019231{
19232 do_sort_uniq(argvars, rettv, FALSE);
19233}
19234
19235/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019236 * "soundfold({word})" function
19237 */
19238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019239f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019240{
19241 char_u *s;
19242
19243 rettv->v_type = VAR_STRING;
19244 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019245#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019246 rettv->vval.v_string = eval_soundfold(s);
19247#else
19248 rettv->vval.v_string = vim_strsave(s);
19249#endif
19250}
19251
19252/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019253 * "spellbadword()" function
19254 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019256f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019257{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019258 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019259 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019260 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019261
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019262 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019263 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019264
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019265#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019266 if (argvars[0].v_type == VAR_UNKNOWN)
19267 {
19268 /* Find the start and length of the badly spelled word. */
19269 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19270 if (len != 0)
19271 word = ml_get_cursor();
19272 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019273 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019274 {
19275 char_u *str = get_tv_string_chk(&argvars[0]);
19276 int capcol = -1;
19277
19278 if (str != NULL)
19279 {
19280 /* Check the argument for spelling. */
19281 while (*str != NUL)
19282 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019283 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019284 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019285 {
19286 word = str;
19287 break;
19288 }
19289 str += len;
19290 }
19291 }
19292 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019293#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019294
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019295 list_append_string(rettv->vval.v_list, word, len);
19296 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019297 attr == HLF_SPB ? "bad" :
19298 attr == HLF_SPR ? "rare" :
19299 attr == HLF_SPL ? "local" :
19300 attr == HLF_SPC ? "caps" :
19301 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019302}
19303
19304/*
19305 * "spellsuggest()" function
19306 */
19307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019308f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019309{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019310#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019311 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019312 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019313 int maxcount;
19314 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019315 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019316 listitem_T *li;
19317 int need_capital = FALSE;
19318#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019319
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019320 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019321 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019322
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019323#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019324 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019325 {
19326 str = get_tv_string(&argvars[0]);
19327 if (argvars[1].v_type != VAR_UNKNOWN)
19328 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019329 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019330 if (maxcount <= 0)
19331 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019332 if (argvars[2].v_type != VAR_UNKNOWN)
19333 {
19334 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19335 if (typeerr)
19336 return;
19337 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019338 }
19339 else
19340 maxcount = 25;
19341
Bram Moolenaar4770d092006-01-12 23:22:24 +000019342 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019343
19344 for (i = 0; i < ga.ga_len; ++i)
19345 {
19346 str = ((char_u **)ga.ga_data)[i];
19347
19348 li = listitem_alloc();
19349 if (li == NULL)
19350 vim_free(str);
19351 else
19352 {
19353 li->li_tv.v_type = VAR_STRING;
19354 li->li_tv.v_lock = 0;
19355 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019356 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019357 }
19358 }
19359 ga_clear(&ga);
19360 }
19361#endif
19362}
19363
Bram Moolenaar0d660222005-01-07 21:51:51 +000019364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019365f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019366{
19367 char_u *str;
19368 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019369 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019370 regmatch_T regmatch;
19371 char_u patbuf[NUMBUFLEN];
19372 char_u *save_cpo;
19373 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019374 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019375 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019376 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019377
19378 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19379 save_cpo = p_cpo;
19380 p_cpo = (char_u *)"";
19381
19382 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019383 if (argvars[1].v_type != VAR_UNKNOWN)
19384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019385 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19386 if (pat == NULL)
19387 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019388 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019389 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019390 }
19391 if (pat == NULL || *pat == NUL)
19392 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019393
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019394 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019395 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019396 if (typeerr)
19397 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398
Bram Moolenaar0d660222005-01-07 21:51:51 +000019399 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19400 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019402 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019403 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019404 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019405 if (*str == NUL)
19406 match = FALSE; /* empty item at the end */
19407 else
19408 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019409 if (match)
19410 end = regmatch.startp[0];
19411 else
19412 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019413 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19414 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019415 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019416 if (list_append_string(rettv->vval.v_list, str,
19417 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019418 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019419 }
19420 if (!match)
19421 break;
19422 /* Advance to just after the match. */
19423 if (regmatch.endp[0] > str)
19424 col = 0;
19425 else
19426 {
19427 /* Don't get stuck at the same match. */
19428#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019429 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019430#else
19431 col = 1;
19432#endif
19433 }
19434 str = regmatch.endp[0];
19435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436
Bram Moolenaar473de612013-06-08 18:19:48 +020019437 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439
Bram Moolenaar0d660222005-01-07 21:51:51 +000019440 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441}
19442
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019443#ifdef FEAT_FLOAT
19444/*
19445 * "sqrt()" function
19446 */
19447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019448f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019449{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019450 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019451
19452 rettv->v_type = VAR_FLOAT;
19453 if (get_float_arg(argvars, &f) == OK)
19454 rettv->vval.v_float = sqrt(f);
19455 else
19456 rettv->vval.v_float = 0.0;
19457}
19458
19459/*
19460 * "str2float()" function
19461 */
19462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019463f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019464{
19465 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19466
19467 if (*p == '+')
19468 p = skipwhite(p + 1);
19469 (void)string2float(p, &rettv->vval.v_float);
19470 rettv->v_type = VAR_FLOAT;
19471}
19472#endif
19473
Bram Moolenaar2c932302006-03-18 21:42:09 +000019474/*
19475 * "str2nr()" function
19476 */
19477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019478f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019479{
19480 int base = 10;
19481 char_u *p;
19482 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019483 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019484
19485 if (argvars[1].v_type != VAR_UNKNOWN)
19486 {
19487 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019488 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019489 {
19490 EMSG(_(e_invarg));
19491 return;
19492 }
19493 }
19494
19495 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019496 if (*p == '+')
19497 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019498 switch (base)
19499 {
19500 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19501 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19502 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19503 default: what = 0;
19504 }
19505 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019506 rettv->vval.v_number = n;
19507}
19508
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509#ifdef HAVE_STRFTIME
19510/*
19511 * "strftime({format}[, {time}])" function
19512 */
19513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019514f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515{
19516 char_u result_buf[256];
19517 struct tm *curtime;
19518 time_t seconds;
19519 char_u *p;
19520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019521 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019524 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 seconds = time(NULL);
19526 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019527 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 curtime = localtime(&seconds);
19529 /* MSVC returns NULL for an invalid value of seconds. */
19530 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019531 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 else
19533 {
19534# ifdef FEAT_MBYTE
19535 vimconv_T conv;
19536 char_u *enc;
19537
19538 conv.vc_type = CONV_NONE;
19539 enc = enc_locale();
19540 convert_setup(&conv, p_enc, enc);
19541 if (conv.vc_type != CONV_NONE)
19542 p = string_convert(&conv, p, NULL);
19543# endif
19544 if (p != NULL)
19545 (void)strftime((char *)result_buf, sizeof(result_buf),
19546 (char *)p, curtime);
19547 else
19548 result_buf[0] = NUL;
19549
19550# ifdef FEAT_MBYTE
19551 if (conv.vc_type != CONV_NONE)
19552 vim_free(p);
19553 convert_setup(&conv, enc, p_enc);
19554 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019555 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 else
19557# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019558 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559
19560# ifdef FEAT_MBYTE
19561 /* Release conversion descriptors */
19562 convert_setup(&conv, NULL, NULL);
19563 vim_free(enc);
19564# endif
19565 }
19566}
19567#endif
19568
19569/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019570 * "strgetchar()" function
19571 */
19572 static void
19573f_strgetchar(typval_T *argvars, typval_T *rettv)
19574{
19575 char_u *str;
19576 int len;
19577 int error = FALSE;
19578 int charidx;
19579
19580 rettv->vval.v_number = -1;
19581 str = get_tv_string_chk(&argvars[0]);
19582 if (str == NULL)
19583 return;
19584 len = (int)STRLEN(str);
19585 charidx = get_tv_number_chk(&argvars[1], &error);
19586 if (error)
19587 return;
19588#ifdef FEAT_MBYTE
19589 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019590 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019591
19592 while (charidx >= 0 && byteidx < len)
19593 {
19594 if (charidx == 0)
19595 {
19596 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19597 break;
19598 }
19599 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019600 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019601 }
19602 }
19603#else
19604 if (charidx < len)
19605 rettv->vval.v_number = str[charidx];
19606#endif
19607}
19608
19609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 * "stridx()" function
19611 */
19612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019613f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614{
19615 char_u buf[NUMBUFLEN];
19616 char_u *needle;
19617 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019620 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019622 needle = get_tv_string_chk(&argvars[1]);
19623 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019624 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019625 if (needle == NULL || haystack == NULL)
19626 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627
Bram Moolenaar33570922005-01-25 22:26:29 +000019628 if (argvars[2].v_type != VAR_UNKNOWN)
19629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019630 int error = FALSE;
19631
19632 start_idx = get_tv_number_chk(&argvars[2], &error);
19633 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019634 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019635 if (start_idx >= 0)
19636 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019637 }
19638
19639 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19640 if (pos != NULL)
19641 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642}
19643
19644/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019645 * "string()" function
19646 */
19647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019648f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019649{
19650 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019651 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019653 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019654 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19655 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019656 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019657 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019658 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659}
19660
19661/*
19662 * "strlen()" function
19663 */
19664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019665f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019667 rettv->vval.v_number = (varnumber_T)(STRLEN(
19668 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669}
19670
19671/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019672 * "strchars()" function
19673 */
19674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019675f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019676{
19677 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019678 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019679#ifdef FEAT_MBYTE
19680 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019681 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019682#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019683
19684 if (argvars[1].v_type != VAR_UNKNOWN)
19685 skipcc = get_tv_number_chk(&argvars[1], NULL);
19686 if (skipcc < 0 || skipcc > 1)
19687 EMSG(_(e_invarg));
19688 else
19689 {
19690#ifdef FEAT_MBYTE
19691 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19692 while (*s != NUL)
19693 {
19694 func_mb_ptr2char_adv(&s);
19695 ++len;
19696 }
19697 rettv->vval.v_number = len;
19698#else
19699 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19700#endif
19701 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019702}
19703
19704/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019705 * "strdisplaywidth()" function
19706 */
19707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019708f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019709{
19710 char_u *s = get_tv_string(&argvars[0]);
19711 int col = 0;
19712
19713 if (argvars[1].v_type != VAR_UNKNOWN)
19714 col = get_tv_number(&argvars[1]);
19715
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019716 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019717}
19718
19719/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019720 * "strwidth()" function
19721 */
19722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019723f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019724{
19725 char_u *s = get_tv_string(&argvars[0]);
19726
19727 rettv->vval.v_number = (varnumber_T)(
19728#ifdef FEAT_MBYTE
19729 mb_string2cells(s, -1)
19730#else
19731 STRLEN(s)
19732#endif
19733 );
19734}
19735
19736/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019737 * "strcharpart()" function
19738 */
19739 static void
19740f_strcharpart(typval_T *argvars, typval_T *rettv)
19741{
19742#ifdef FEAT_MBYTE
19743 char_u *p;
19744 int nchar;
19745 int nbyte = 0;
19746 int charlen;
19747 int len = 0;
19748 int slen;
19749 int error = FALSE;
19750
19751 p = get_tv_string(&argvars[0]);
19752 slen = (int)STRLEN(p);
19753
19754 nchar = get_tv_number_chk(&argvars[1], &error);
19755 if (!error)
19756 {
19757 if (nchar > 0)
19758 while (nchar > 0 && nbyte < slen)
19759 {
19760 nbyte += mb_char2len(p[nbyte]);
19761 --nchar;
19762 }
19763 else
19764 nbyte = nchar;
19765 if (argvars[2].v_type != VAR_UNKNOWN)
19766 {
19767 charlen = get_tv_number(&argvars[2]);
19768 while (charlen > 0 && nbyte + len < slen)
19769 {
19770 len += mb_char2len(p[nbyte + len]);
19771 --charlen;
19772 }
19773 }
19774 else
19775 len = slen - nbyte; /* default: all bytes that are available. */
19776 }
19777
19778 /*
19779 * Only return the overlap between the specified part and the actual
19780 * string.
19781 */
19782 if (nbyte < 0)
19783 {
19784 len += nbyte;
19785 nbyte = 0;
19786 }
19787 else if (nbyte > slen)
19788 nbyte = slen;
19789 if (len < 0)
19790 len = 0;
19791 else if (nbyte + len > slen)
19792 len = slen - nbyte;
19793
19794 rettv->v_type = VAR_STRING;
19795 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19796#else
19797 f_strpart(argvars, rettv);
19798#endif
19799}
19800
19801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 * "strpart()" function
19803 */
19804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019805f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806{
19807 char_u *p;
19808 int n;
19809 int len;
19810 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019811 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019813 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 slen = (int)STRLEN(p);
19815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019816 n = get_tv_number_chk(&argvars[1], &error);
19817 if (error)
19818 len = 0;
19819 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019820 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 else
19822 len = slen - n; /* default len: all bytes that are available. */
19823
19824 /*
19825 * Only return the overlap between the specified part and the actual
19826 * string.
19827 */
19828 if (n < 0)
19829 {
19830 len += n;
19831 n = 0;
19832 }
19833 else if (n > slen)
19834 n = slen;
19835 if (len < 0)
19836 len = 0;
19837 else if (n + len > slen)
19838 len = slen - n;
19839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019840 rettv->v_type = VAR_STRING;
19841 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842}
19843
19844/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019845 * "strridx()" function
19846 */
19847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019848f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019849{
19850 char_u buf[NUMBUFLEN];
19851 char_u *needle;
19852 char_u *haystack;
19853 char_u *rest;
19854 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019855 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019857 needle = get_tv_string_chk(&argvars[1]);
19858 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019859
19860 rettv->vval.v_number = -1;
19861 if (needle == NULL || haystack == NULL)
19862 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019863
19864 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019865 if (argvars[2].v_type != VAR_UNKNOWN)
19866 {
19867 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019868 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019869 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019870 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019871 }
19872 else
19873 end_idx = haystack_len;
19874
Bram Moolenaar0d660222005-01-07 21:51:51 +000019875 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019876 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019877 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019878 lastmatch = haystack + end_idx;
19879 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019880 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019881 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019882 for (rest = haystack; *rest != '\0'; ++rest)
19883 {
19884 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019885 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019886 break;
19887 lastmatch = rest;
19888 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019889 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019890
19891 if (lastmatch == NULL)
19892 rettv->vval.v_number = -1;
19893 else
19894 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19895}
19896
19897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898 * "strtrans()" function
19899 */
19900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019901f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019903 rettv->v_type = VAR_STRING;
19904 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905}
19906
19907/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019908 * "submatch()" function
19909 */
19910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019911f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019912{
Bram Moolenaar41571762014-04-02 19:00:58 +020019913 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019914 int no;
19915 int retList = 0;
19916
19917 no = (int)get_tv_number_chk(&argvars[0], &error);
19918 if (error)
19919 return;
19920 error = FALSE;
19921 if (argvars[1].v_type != VAR_UNKNOWN)
19922 retList = get_tv_number_chk(&argvars[1], &error);
19923 if (error)
19924 return;
19925
19926 if (retList == 0)
19927 {
19928 rettv->v_type = VAR_STRING;
19929 rettv->vval.v_string = reg_submatch(no);
19930 }
19931 else
19932 {
19933 rettv->v_type = VAR_LIST;
19934 rettv->vval.v_list = reg_submatch_list(no);
19935 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019936}
19937
19938/*
19939 * "substitute()" function
19940 */
19941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019942f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019943{
19944 char_u patbuf[NUMBUFLEN];
19945 char_u subbuf[NUMBUFLEN];
19946 char_u flagsbuf[NUMBUFLEN];
19947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019948 char_u *str = get_tv_string_chk(&argvars[0]);
19949 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19950 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19951 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19952
Bram Moolenaar0d660222005-01-07 21:51:51 +000019953 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019954 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19955 rettv->vval.v_string = NULL;
19956 else
19957 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019958}
19959
19960/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019961 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019964f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965{
19966 int id = 0;
19967#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019968 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 long col;
19970 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019971 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019973 lnum = get_tv_lnum(argvars); /* -1 on type error */
19974 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19975 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019977 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019978 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019979 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980#endif
19981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019982 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983}
19984
19985/*
19986 * "synIDattr(id, what [, mode])" function
19987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019989f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990{
19991 char_u *p = NULL;
19992#ifdef FEAT_SYN_HL
19993 int id;
19994 char_u *what;
19995 char_u *mode;
19996 char_u modebuf[NUMBUFLEN];
19997 int modec;
19998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019999 id = get_tv_number(&argvars[0]);
20000 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020001 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020003 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020005 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 modec = 0; /* replace invalid with current */
20007 }
20008 else
20009 {
20010#ifdef FEAT_GUI
20011 if (gui.in_use)
20012 modec = 'g';
20013 else
20014#endif
20015 if (t_colors > 1)
20016 modec = 'c';
20017 else
20018 modec = 't';
20019 }
20020
20021
20022 switch (TOLOWER_ASC(what[0]))
20023 {
20024 case 'b':
20025 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20026 p = highlight_color(id, what, modec);
20027 else /* bold */
20028 p = highlight_has_attr(id, HL_BOLD, modec);
20029 break;
20030
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020031 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 p = highlight_color(id, what, modec);
20033 break;
20034
20035 case 'i':
20036 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20037 p = highlight_has_attr(id, HL_INVERSE, modec);
20038 else /* italic */
20039 p = highlight_has_attr(id, HL_ITALIC, modec);
20040 break;
20041
20042 case 'n': /* name */
20043 p = get_highlight_name(NULL, id - 1);
20044 break;
20045
20046 case 'r': /* reverse */
20047 p = highlight_has_attr(id, HL_INVERSE, modec);
20048 break;
20049
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020050 case 's':
20051 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20052 p = highlight_color(id, what, modec);
20053 else /* standout */
20054 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 break;
20056
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020057 case 'u':
20058 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20059 /* underline */
20060 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20061 else
20062 /* undercurl */
20063 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 break;
20065 }
20066
20067 if (p != NULL)
20068 p = vim_strsave(p);
20069#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020070 rettv->v_type = VAR_STRING;
20071 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072}
20073
20074/*
20075 * "synIDtrans(id)" function
20076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020078f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079{
20080 int id;
20081
20082#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020083 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084
20085 if (id > 0)
20086 id = syn_get_final_id(id);
20087 else
20088#endif
20089 id = 0;
20090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020091 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092}
20093
20094/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020095 * "synconcealed(lnum, col)" function
20096 */
20097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020098f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020099{
20100#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20101 long lnum;
20102 long col;
20103 int syntax_flags = 0;
20104 int cchar;
20105 int matchid = 0;
20106 char_u str[NUMBUFLEN];
20107#endif
20108
20109 rettv->v_type = VAR_LIST;
20110 rettv->vval.v_list = NULL;
20111
20112#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20113 lnum = get_tv_lnum(argvars); /* -1 on type error */
20114 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20115
20116 vim_memset(str, NUL, sizeof(str));
20117
20118 if (rettv_list_alloc(rettv) != FAIL)
20119 {
20120 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20121 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20122 && curwin->w_p_cole > 0)
20123 {
20124 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20125 syntax_flags = get_syntax_info(&matchid);
20126
20127 /* get the conceal character */
20128 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20129 {
20130 cchar = syn_get_sub_char();
20131 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20132 cchar = lcs_conceal;
20133 if (cchar != NUL)
20134 {
20135# ifdef FEAT_MBYTE
20136 if (has_mbyte)
20137 (*mb_char2bytes)(cchar, str);
20138 else
20139# endif
20140 str[0] = cchar;
20141 }
20142 }
20143 }
20144
20145 list_append_number(rettv->vval.v_list,
20146 (syntax_flags & HL_CONCEAL) != 0);
20147 /* -1 to auto-determine strlen */
20148 list_append_string(rettv->vval.v_list, str, -1);
20149 list_append_number(rettv->vval.v_list, matchid);
20150 }
20151#endif
20152}
20153
20154/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020155 * "synstack(lnum, col)" function
20156 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020158f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020159{
20160#ifdef FEAT_SYN_HL
20161 long lnum;
20162 long col;
20163 int i;
20164 int id;
20165#endif
20166
20167 rettv->v_type = VAR_LIST;
20168 rettv->vval.v_list = NULL;
20169
20170#ifdef FEAT_SYN_HL
20171 lnum = get_tv_lnum(argvars); /* -1 on type error */
20172 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20173
20174 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020175 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020176 && rettv_list_alloc(rettv) != FAIL)
20177 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020178 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020179 for (i = 0; ; ++i)
20180 {
20181 id = syn_get_stack_item(i);
20182 if (id < 0)
20183 break;
20184 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20185 break;
20186 }
20187 }
20188#endif
20189}
20190
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020192get_cmd_output_as_rettv(
20193 typval_T *argvars,
20194 typval_T *rettv,
20195 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020197 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020199 char_u *infile = NULL;
20200 char_u buf[NUMBUFLEN];
20201 int err = FALSE;
20202 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020203 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020204 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020206 rettv->v_type = VAR_STRING;
20207 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020208 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020209 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020210
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020211 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020212 {
20213 /*
20214 * Write the string to a temp file, to be used for input of the shell
20215 * command.
20216 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020217 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020218 {
20219 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020220 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020221 }
20222
20223 fd = mch_fopen((char *)infile, WRITEBIN);
20224 if (fd == NULL)
20225 {
20226 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020227 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020228 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020229 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020230 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020231 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20232 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020233 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020234 else
20235 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020236 size_t len;
20237
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020238 p = get_tv_string_buf_chk(&argvars[1], buf);
20239 if (p == NULL)
20240 {
20241 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020242 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020243 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020244 len = STRLEN(p);
20245 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020246 err = TRUE;
20247 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020248 if (fclose(fd) != 0)
20249 err = TRUE;
20250 if (err)
20251 {
20252 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020253 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020254 }
20255 }
20256
Bram Moolenaar52a72462014-08-29 15:53:52 +020020257 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20258 * echoes typeahead, that messes up the display. */
20259 if (!msg_silent)
20260 flags += SHELL_COOKED;
20261
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020262 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020264 int len;
20265 listitem_T *li;
20266 char_u *s = NULL;
20267 char_u *start;
20268 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020269 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270
Bram Moolenaar52a72462014-08-29 15:53:52 +020020271 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020272 if (res == NULL)
20273 goto errret;
20274
20275 list = list_alloc();
20276 if (list == NULL)
20277 goto errret;
20278
20279 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020281 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020282 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020283 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020284 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020285
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020286 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020287 if (s == NULL)
20288 goto errret;
20289
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020290 for (p = s; start < end; ++p, ++start)
20291 *p = *start == NUL ? NL : *start;
20292 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020293
20294 li = listitem_alloc();
20295 if (li == NULL)
20296 {
20297 vim_free(s);
20298 goto errret;
20299 }
20300 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020301 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020302 li->li_tv.vval.v_string = s;
20303 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020305
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020306 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020307 rettv->v_type = VAR_LIST;
20308 rettv->vval.v_list = list;
20309 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020311 else
20312 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020313 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020314#ifdef USE_CR
20315 /* translate <CR> into <NL> */
20316 if (res != NULL)
20317 {
20318 char_u *s;
20319
20320 for (s = res; *s; ++s)
20321 {
20322 if (*s == CAR)
20323 *s = NL;
20324 }
20325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326#else
20327# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020328 /* translate <CR><NL> into <NL> */
20329 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020331 char_u *s, *d;
20332
20333 d = res;
20334 for (s = res; *s; ++s)
20335 {
20336 if (s[0] == CAR && s[1] == NL)
20337 ++s;
20338 *d++ = *s;
20339 }
20340 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342# endif
20343#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020344 rettv->vval.v_string = res;
20345 res = NULL;
20346 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020347
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020348errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020349 if (infile != NULL)
20350 {
20351 mch_remove(infile);
20352 vim_free(infile);
20353 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020354 if (res != NULL)
20355 vim_free(res);
20356 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020357 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020358}
20359
20360/*
20361 * "system()" function
20362 */
20363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020364f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020365{
20366 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20367}
20368
20369/*
20370 * "systemlist()" function
20371 */
20372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020373f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020374{
20375 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376}
20377
20378/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020379 * "tabpagebuflist()" function
20380 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020382f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020383{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020384#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020385 tabpage_T *tp;
20386 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020387
20388 if (argvars[0].v_type == VAR_UNKNOWN)
20389 wp = firstwin;
20390 else
20391 {
20392 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20393 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020394 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020395 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020396 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020397 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020398 for (; wp != NULL; wp = wp->w_next)
20399 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020400 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020401 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020402 }
20403#endif
20404}
20405
20406
20407/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020408 * "tabpagenr()" function
20409 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020411f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020412{
20413 int nr = 1;
20414#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020415 char_u *arg;
20416
20417 if (argvars[0].v_type != VAR_UNKNOWN)
20418 {
20419 arg = get_tv_string_chk(&argvars[0]);
20420 nr = 0;
20421 if (arg != NULL)
20422 {
20423 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020424 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020425 else
20426 EMSG2(_(e_invexpr2), arg);
20427 }
20428 }
20429 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020430 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020431#endif
20432 rettv->vval.v_number = nr;
20433}
20434
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020435
20436#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020437static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020438
20439/*
20440 * Common code for tabpagewinnr() and winnr().
20441 */
20442 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020443get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020444{
20445 win_T *twin;
20446 int nr = 1;
20447 win_T *wp;
20448 char_u *arg;
20449
20450 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20451 if (argvar->v_type != VAR_UNKNOWN)
20452 {
20453 arg = get_tv_string_chk(argvar);
20454 if (arg == NULL)
20455 nr = 0; /* type error; errmsg already given */
20456 else if (STRCMP(arg, "$") == 0)
20457 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20458 else if (STRCMP(arg, "#") == 0)
20459 {
20460 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20461 if (twin == NULL)
20462 nr = 0;
20463 }
20464 else
20465 {
20466 EMSG2(_(e_invexpr2), arg);
20467 nr = 0;
20468 }
20469 }
20470
20471 if (nr > 0)
20472 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20473 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020474 {
20475 if (wp == NULL)
20476 {
20477 /* didn't find it in this tabpage */
20478 nr = 0;
20479 break;
20480 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020481 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020482 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020483 return nr;
20484}
20485#endif
20486
20487/*
20488 * "tabpagewinnr()" function
20489 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020491f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020492{
20493 int nr = 1;
20494#ifdef FEAT_WINDOWS
20495 tabpage_T *tp;
20496
20497 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20498 if (tp == NULL)
20499 nr = 0;
20500 else
20501 nr = get_winnr(tp, &argvars[1]);
20502#endif
20503 rettv->vval.v_number = nr;
20504}
20505
20506
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020507/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020508 * "tagfiles()" function
20509 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020511f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020512{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020513 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020514 tagname_T tn;
20515 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020516
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020517 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020518 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020519 fname = alloc(MAXPATHL);
20520 if (fname == NULL)
20521 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020522
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020523 for (first = TRUE; ; first = FALSE)
20524 if (get_tagfname(&tn, first, fname) == FAIL
20525 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020526 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020527 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020528 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020529}
20530
20531/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020532 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020533 */
20534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020535f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020536{
20537 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020538
20539 tag_pattern = get_tv_string(&argvars[0]);
20540
20541 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020542 if (*tag_pattern == NUL)
20543 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020544
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020545 if (rettv_list_alloc(rettv) == OK)
20546 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020547}
20548
20549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 * "tempname()" function
20551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020553f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554{
20555 static int x = 'A';
20556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020557 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020558 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559
20560 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20561 * names. Skip 'I' and 'O', they are used for shell redirection. */
20562 do
20563 {
20564 if (x == 'Z')
20565 x = '0';
20566 else if (x == '9')
20567 x = 'A';
20568 else
20569 {
20570#ifdef EBCDIC
20571 if (x == 'I')
20572 x = 'J';
20573 else if (x == 'R')
20574 x = 'S';
20575 else
20576#endif
20577 ++x;
20578 }
20579 } while (x == 'I' || x == 'O');
20580}
20581
20582/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020583 * "test(list)" function: Just checking the walls...
20584 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020586f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020587{
20588 /* Used for unit testing. Change the code below to your liking. */
20589#if 0
20590 listitem_T *li;
20591 list_T *l;
20592 char_u *bad, *good;
20593
20594 if (argvars[0].v_type != VAR_LIST)
20595 return;
20596 l = argvars[0].vval.v_list;
20597 if (l == NULL)
20598 return;
20599 li = l->lv_first;
20600 if (li == NULL)
20601 return;
20602 bad = get_tv_string(&li->li_tv);
20603 li = li->li_next;
20604 if (li == NULL)
20605 return;
20606 good = get_tv_string(&li->li_tv);
20607 rettv->vval.v_number = test_edit_score(bad, good);
20608#endif
20609}
20610
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020611#ifdef FEAT_FLOAT
20612/*
20613 * "tan()" function
20614 */
20615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020616f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020617{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020618 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020619
20620 rettv->v_type = VAR_FLOAT;
20621 if (get_float_arg(argvars, &f) == OK)
20622 rettv->vval.v_float = tan(f);
20623 else
20624 rettv->vval.v_float = 0.0;
20625}
20626
20627/*
20628 * "tanh()" function
20629 */
20630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020631f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020632{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020633 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020634
20635 rettv->v_type = VAR_FLOAT;
20636 if (get_float_arg(argvars, &f) == OK)
20637 rettv->vval.v_float = tanh(f);
20638 else
20639 rettv->vval.v_float = 0.0;
20640}
20641#endif
20642
Bram Moolenaar975b5272016-03-15 23:10:59 +010020643#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20644/*
20645 * Get a callback from "arg". It can be a Funcref or a function name.
20646 * When "arg" is zero return an empty string.
20647 * Return NULL for an invalid argument.
20648 */
20649 char_u *
20650get_callback(typval_T *arg, partial_T **pp)
20651{
20652 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20653 {
20654 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020655 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020656 return (*pp)->pt_name;
20657 }
20658 *pp = NULL;
20659 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20660 return arg->vval.v_string;
20661 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20662 return (char_u *)"";
20663 EMSG(_("E921: Invalid callback argument"));
20664 return NULL;
20665}
20666#endif
20667
20668#ifdef FEAT_TIMERS
20669/*
20670 * "timer_start(time, callback [, options])" function
20671 */
20672 static void
20673f_timer_start(typval_T *argvars, typval_T *rettv)
20674{
20675 long msec = get_tv_number(&argvars[0]);
20676 timer_T *timer;
20677 int repeat = 0;
20678 char_u *callback;
20679 dict_T *dict;
20680
20681 if (argvars[2].v_type != VAR_UNKNOWN)
20682 {
20683 if (argvars[2].v_type != VAR_DICT
20684 || (dict = argvars[2].vval.v_dict) == NULL)
20685 {
20686 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20687 return;
20688 }
20689 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20690 repeat = get_dict_number(dict, (char_u *)"repeat");
20691 }
20692
20693 timer = create_timer(msec, repeat);
20694 callback = get_callback(&argvars[1], &timer->tr_partial);
20695 if (callback == NULL)
20696 {
20697 stop_timer(timer);
20698 rettv->vval.v_number = -1;
20699 }
20700 else
20701 {
20702 timer->tr_callback = vim_strsave(callback);
20703 rettv->vval.v_number = timer->tr_id;
20704 }
20705}
20706
20707/*
20708 * "timer_stop(timer)" function
20709 */
20710 static void
20711f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20712{
20713 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20714
20715 if (timer != NULL)
20716 stop_timer(timer);
20717}
20718#endif
20719
Bram Moolenaard52d9742005-08-21 22:20:28 +000020720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 * "tolower(string)" function
20722 */
20723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020724f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725{
20726 char_u *p;
20727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020728 p = vim_strsave(get_tv_string(&argvars[0]));
20729 rettv->v_type = VAR_STRING;
20730 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731
20732 if (p != NULL)
20733 while (*p != NUL)
20734 {
20735#ifdef FEAT_MBYTE
20736 int l;
20737
20738 if (enc_utf8)
20739 {
20740 int c, lc;
20741
20742 c = utf_ptr2char(p);
20743 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020744 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 /* TODO: reallocate string when byte count changes. */
20746 if (utf_char2len(lc) == l)
20747 utf_char2bytes(lc, p);
20748 p += l;
20749 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020750 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751 p += l; /* skip multi-byte character */
20752 else
20753#endif
20754 {
20755 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20756 ++p;
20757 }
20758 }
20759}
20760
20761/*
20762 * "toupper(string)" function
20763 */
20764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020765f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020766{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020767 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020768 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769}
20770
20771/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020772 * "tr(string, fromstr, tostr)" function
20773 */
20774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020775f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020776{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020777 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020778 char_u *fromstr;
20779 char_u *tostr;
20780 char_u *p;
20781#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020782 int inlen;
20783 int fromlen;
20784 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020785 int idx;
20786 char_u *cpstr;
20787 int cplen;
20788 int first = TRUE;
20789#endif
20790 char_u buf[NUMBUFLEN];
20791 char_u buf2[NUMBUFLEN];
20792 garray_T ga;
20793
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020794 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020795 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20796 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020797
20798 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020799 rettv->v_type = VAR_STRING;
20800 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020801 if (fromstr == NULL || tostr == NULL)
20802 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020803 ga_init2(&ga, (int)sizeof(char), 80);
20804
20805#ifdef FEAT_MBYTE
20806 if (!has_mbyte)
20807#endif
20808 /* not multi-byte: fromstr and tostr must be the same length */
20809 if (STRLEN(fromstr) != STRLEN(tostr))
20810 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020811#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020812error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020813#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020814 EMSG2(_(e_invarg2), fromstr);
20815 ga_clear(&ga);
20816 return;
20817 }
20818
20819 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020820 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020821 {
20822#ifdef FEAT_MBYTE
20823 if (has_mbyte)
20824 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020825 inlen = (*mb_ptr2len)(in_str);
20826 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020827 cplen = inlen;
20828 idx = 0;
20829 for (p = fromstr; *p != NUL; p += fromlen)
20830 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020831 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020832 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020833 {
20834 for (p = tostr; *p != NUL; p += tolen)
20835 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020836 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020837 if (idx-- == 0)
20838 {
20839 cplen = tolen;
20840 cpstr = p;
20841 break;
20842 }
20843 }
20844 if (*p == NUL) /* tostr is shorter than fromstr */
20845 goto error;
20846 break;
20847 }
20848 ++idx;
20849 }
20850
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020851 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020852 {
20853 /* Check that fromstr and tostr have the same number of
20854 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020855 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020856 first = FALSE;
20857 for (p = tostr; *p != NUL; p += tolen)
20858 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020859 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020860 --idx;
20861 }
20862 if (idx != 0)
20863 goto error;
20864 }
20865
Bram Moolenaarcde88542015-08-11 19:14:00 +020020866 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020867 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020868 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020869
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020870 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020871 }
20872 else
20873#endif
20874 {
20875 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020876 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020877 if (p != NULL)
20878 ga_append(&ga, tostr[p - fromstr]);
20879 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020880 ga_append(&ga, *in_str);
20881 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020882 }
20883 }
20884
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020885 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020886 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020887 ga_append(&ga, NUL);
20888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020889 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020890}
20891
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020892#ifdef FEAT_FLOAT
20893/*
20894 * "trunc({float})" function
20895 */
20896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020897f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020898{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020899 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020900
20901 rettv->v_type = VAR_FLOAT;
20902 if (get_float_arg(argvars, &f) == OK)
20903 /* trunc() is not in C90, use floor() or ceil() instead. */
20904 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20905 else
20906 rettv->vval.v_float = 0.0;
20907}
20908#endif
20909
Bram Moolenaar8299df92004-07-10 09:47:34 +000020910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 * "type(expr)" function
20912 */
20913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020914f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020916 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020917
20918 switch (argvars[0].v_type)
20919 {
20920 case VAR_NUMBER: n = 0; break;
20921 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020922 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020923 case VAR_FUNC: n = 2; break;
20924 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020925 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020926 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020927 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020928 if (argvars[0].vval.v_number == VVAL_FALSE
20929 || argvars[0].vval.v_number == VVAL_TRUE)
20930 n = 6;
20931 else
20932 n = 7;
20933 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020934 case VAR_JOB: n = 8; break;
20935 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020936 case VAR_UNKNOWN:
20937 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20938 n = -1;
20939 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020940 }
20941 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942}
20943
20944/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020945 * "undofile(name)" function
20946 */
20947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020948f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020949{
20950 rettv->v_type = VAR_STRING;
20951#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020952 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020953 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020954
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020955 if (*fname == NUL)
20956 {
20957 /* If there is no file name there will be no undo file. */
20958 rettv->vval.v_string = NULL;
20959 }
20960 else
20961 {
20962 char_u *ffname = FullName_save(fname, FALSE);
20963
20964 if (ffname != NULL)
20965 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20966 vim_free(ffname);
20967 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020968 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020969#else
20970 rettv->vval.v_string = NULL;
20971#endif
20972}
20973
20974/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020975 * "undotree()" function
20976 */
20977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020978f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020979{
20980 if (rettv_dict_alloc(rettv) == OK)
20981 {
20982 dict_T *dict = rettv->vval.v_dict;
20983 list_T *list;
20984
Bram Moolenaar730cde92010-06-27 05:18:54 +020020985 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020986 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020987 dict_add_nr_str(dict, "save_last",
20988 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020989 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20990 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020991 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020992
20993 list = list_alloc();
20994 if (list != NULL)
20995 {
20996 u_eval_tree(curbuf->b_u_oldhead, list);
20997 dict_add_list(dict, "entries", list);
20998 }
20999 }
21000}
21001
21002/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021003 * "values(dict)" function
21004 */
21005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021006f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021007{
21008 dict_list(argvars, rettv, 1);
21009}
21010
21011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 * "virtcol(string)" function
21013 */
21014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021015f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016{
21017 colnr_T vcol = 0;
21018 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021019 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021021 fp = var2fpos(&argvars[0], FALSE, &fnum);
21022 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21023 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024 {
21025 getvvcol(curwin, fp, NULL, NULL, &vcol);
21026 ++vcol;
21027 }
21028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021029 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030}
21031
21032/*
21033 * "visualmode()" function
21034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021036f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 char_u str[2];
21039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021040 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 str[0] = curbuf->b_visual_mode_eval;
21042 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021043 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044
21045 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021046 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048}
21049
21050/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021051 * "wildmenumode()" function
21052 */
21053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021054f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021055{
21056#ifdef FEAT_WILDMENU
21057 if (wild_menu_showing)
21058 rettv->vval.v_number = 1;
21059#endif
21060}
21061
21062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 * "winbufnr(nr)" function
21064 */
21065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021066f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067{
21068 win_T *wp;
21069
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021070 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021072 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021074 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075}
21076
21077/*
21078 * "wincol()" function
21079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021081f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082{
21083 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021084 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085}
21086
21087/*
21088 * "winheight(nr)" function
21089 */
21090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021091f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092{
21093 win_T *wp;
21094
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021095 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021097 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021099 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100}
21101
21102/*
21103 * "winline()" function
21104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021106f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107{
21108 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021109 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110}
21111
21112/*
21113 * "winnr()" function
21114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021116f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117{
21118 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021119
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021121 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021123 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124}
21125
21126/*
21127 * "winrestcmd()" function
21128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021130f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131{
21132#ifdef FEAT_WINDOWS
21133 win_T *wp;
21134 int winnr = 1;
21135 garray_T ga;
21136 char_u buf[50];
21137
21138 ga_init2(&ga, (int)sizeof(char), 70);
21139 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21140 {
21141 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21142 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21144 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 ++winnr;
21146 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021147 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021149 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021151 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021153 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154}
21155
21156/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021157 * "winrestview()" function
21158 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021160f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021161{
21162 dict_T *dict;
21163
21164 if (argvars[0].v_type != VAR_DICT
21165 || (dict = argvars[0].vval.v_dict) == NULL)
21166 EMSG(_(e_invarg));
21167 else
21168 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021169 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21170 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21171 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21172 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021173#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021174 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21175 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021176#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021177 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21178 {
21179 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21180 curwin->w_set_curswant = FALSE;
21181 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021182
Bram Moolenaar82c25852014-05-28 16:47:16 +020021183 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21184 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021185#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021186 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21187 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021188#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021189 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21190 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21191 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21192 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021193
21194 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021195 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021196# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021197 win_new_width(curwin, W_WIDTH(curwin));
21198# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021199 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021200
Bram Moolenaarb851a962014-10-31 15:45:52 +010021201 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021202 curwin->w_topline = 1;
21203 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21204 curwin->w_topline = curbuf->b_ml.ml_line_count;
21205#ifdef FEAT_DIFF
21206 check_topfill(curwin, TRUE);
21207#endif
21208 }
21209}
21210
21211/*
21212 * "winsaveview()" function
21213 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021215f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021216{
21217 dict_T *dict;
21218
Bram Moolenaara800b422010-06-27 01:15:55 +020021219 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021220 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021221 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021222
21223 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21224 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21225#ifdef FEAT_VIRTUALEDIT
21226 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21227#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021228 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021229 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21230
21231 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21232#ifdef FEAT_DIFF
21233 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21234#endif
21235 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21236 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21237}
21238
21239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 * "winwidth(nr)" function
21241 */
21242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021243f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244{
21245 win_T *wp;
21246
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021247 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021249 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021251#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021252 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021254 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255#endif
21256}
21257
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021259 * "wordcount()" function
21260 */
21261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021262f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021263{
21264 if (rettv_dict_alloc(rettv) == FAIL)
21265 return;
21266 cursor_pos_info(rettv->vval.v_dict);
21267}
21268
21269/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021270 * Write list of strings to file
21271 */
21272 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021273write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021274{
21275 listitem_T *li;
21276 int c;
21277 int ret = OK;
21278 char_u *s;
21279
21280 for (li = list->lv_first; li != NULL; li = li->li_next)
21281 {
21282 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21283 {
21284 if (*s == '\n')
21285 c = putc(NUL, fd);
21286 else
21287 c = putc(*s, fd);
21288 if (c == EOF)
21289 {
21290 ret = FAIL;
21291 break;
21292 }
21293 }
21294 if (!binary || li->li_next != NULL)
21295 if (putc('\n', fd) == EOF)
21296 {
21297 ret = FAIL;
21298 break;
21299 }
21300 if (ret == FAIL)
21301 {
21302 EMSG(_(e_write));
21303 break;
21304 }
21305 }
21306 return ret;
21307}
21308
21309/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021310 * "writefile()" function
21311 */
21312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021313f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021314{
21315 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021316 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021317 char_u *fname;
21318 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021319 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021320
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021321 if (check_restricted() || check_secure())
21322 return;
21323
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021324 if (argvars[0].v_type != VAR_LIST)
21325 {
21326 EMSG2(_(e_listarg), "writefile()");
21327 return;
21328 }
21329 if (argvars[0].vval.v_list == NULL)
21330 return;
21331
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021332 if (argvars[2].v_type != VAR_UNKNOWN)
21333 {
21334 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21335 binary = TRUE;
21336 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21337 append = TRUE;
21338 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021339
21340 /* Always open the file in binary mode, library functions have a mind of
21341 * their own about CR-LF conversion. */
21342 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021343 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21344 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021345 {
21346 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21347 ret = -1;
21348 }
21349 else
21350 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021351 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21352 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021353 fclose(fd);
21354 }
21355
21356 rettv->vval.v_number = ret;
21357}
21358
21359/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021360 * "xor(expr, expr)" function
21361 */
21362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021363f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021364{
21365 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21366 ^ get_tv_number_chk(&argvars[1], NULL);
21367}
21368
21369
21370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021372 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 */
21374 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021375var2fpos(
21376 typval_T *varp,
21377 int dollar_lnum, /* TRUE when $ is last line */
21378 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021380 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021382 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383
Bram Moolenaara5525202006-03-02 22:52:09 +000021384 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021385 if (varp->v_type == VAR_LIST)
21386 {
21387 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021388 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021389 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021390 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021391
21392 l = varp->vval.v_list;
21393 if (l == NULL)
21394 return NULL;
21395
21396 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021397 pos.lnum = list_find_nr(l, 0L, &error);
21398 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021399 return NULL; /* invalid line number */
21400
21401 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021402 pos.col = list_find_nr(l, 1L, &error);
21403 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021404 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021405 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021406
21407 /* We accept "$" for the column number: last column. */
21408 li = list_find(l, 1L);
21409 if (li != NULL && li->li_tv.v_type == VAR_STRING
21410 && li->li_tv.vval.v_string != NULL
21411 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21412 pos.col = len + 1;
21413
Bram Moolenaara5525202006-03-02 22:52:09 +000021414 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021415 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021416 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021417 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021418
Bram Moolenaara5525202006-03-02 22:52:09 +000021419#ifdef FEAT_VIRTUALEDIT
21420 /* Get the virtual offset. Defaults to zero. */
21421 pos.coladd = list_find_nr(l, 2L, &error);
21422 if (error)
21423 pos.coladd = 0;
21424#endif
21425
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021426 return &pos;
21427 }
21428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021429 name = get_tv_string_chk(varp);
21430 if (name == NULL)
21431 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021432 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021434 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21435 {
21436 if (VIsual_active)
21437 return &VIsual;
21438 return &curwin->w_cursor;
21439 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021440 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021442 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21444 return NULL;
21445 return pp;
21446 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021447
21448#ifdef FEAT_VIRTUALEDIT
21449 pos.coladd = 0;
21450#endif
21451
Bram Moolenaar477933c2007-07-17 14:32:23 +000021452 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021453 {
21454 pos.col = 0;
21455 if (name[1] == '0') /* "w0": first visible line */
21456 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021457 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021458 pos.lnum = curwin->w_topline;
21459 return &pos;
21460 }
21461 else if (name[1] == '$') /* "w$": last visible line */
21462 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021463 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021464 pos.lnum = curwin->w_botline - 1;
21465 return &pos;
21466 }
21467 }
21468 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021470 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 {
21472 pos.lnum = curbuf->b_ml.ml_line_count;
21473 pos.col = 0;
21474 }
21475 else
21476 {
21477 pos.lnum = curwin->w_cursor.lnum;
21478 pos.col = (colnr_T)STRLEN(ml_get_curline());
21479 }
21480 return &pos;
21481 }
21482 return NULL;
21483}
21484
21485/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021486 * Convert list in "arg" into a position and optional file number.
21487 * When "fnump" is NULL there is no file number, only 3 items.
21488 * Note that the column is passed on as-is, the caller may want to decrement
21489 * it to use 1 for the first column.
21490 * Return FAIL when conversion is not possible, doesn't check the position for
21491 * validity.
21492 */
21493 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021494list2fpos(
21495 typval_T *arg,
21496 pos_T *posp,
21497 int *fnump,
21498 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021499{
21500 list_T *l = arg->vval.v_list;
21501 long i = 0;
21502 long n;
21503
Bram Moolenaar493c1782014-05-28 14:34:46 +020021504 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21505 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021506 if (arg->v_type != VAR_LIST
21507 || l == NULL
21508 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021509 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021510 return FAIL;
21511
21512 if (fnump != NULL)
21513 {
21514 n = list_find_nr(l, i++, NULL); /* fnum */
21515 if (n < 0)
21516 return FAIL;
21517 if (n == 0)
21518 n = curbuf->b_fnum; /* current buffer */
21519 *fnump = n;
21520 }
21521
21522 n = list_find_nr(l, i++, NULL); /* lnum */
21523 if (n < 0)
21524 return FAIL;
21525 posp->lnum = n;
21526
21527 n = list_find_nr(l, i++, NULL); /* col */
21528 if (n < 0)
21529 return FAIL;
21530 posp->col = n;
21531
21532#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021533 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021534 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021535 posp->coladd = 0;
21536 else
21537 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021538#endif
21539
Bram Moolenaar493c1782014-05-28 14:34:46 +020021540 if (curswantp != NULL)
21541 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21542
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021543 return OK;
21544}
21545
21546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 * Get the length of an environment variable name.
21548 * Advance "arg" to the first character after the name.
21549 * Return 0 for error.
21550 */
21551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021552get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553{
21554 char_u *p;
21555 int len;
21556
21557 for (p = *arg; vim_isIDc(*p); ++p)
21558 ;
21559 if (p == *arg) /* no name found */
21560 return 0;
21561
21562 len = (int)(p - *arg);
21563 *arg = p;
21564 return len;
21565}
21566
21567/*
21568 * Get the length of the name of a function or internal variable.
21569 * "arg" is advanced to the first non-white character after the name.
21570 * Return 0 if something is wrong.
21571 */
21572 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021573get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574{
21575 char_u *p;
21576 int len;
21577
21578 /* Find the end of the name. */
21579 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021580 {
21581 if (*p == ':')
21582 {
21583 /* "s:" is start of "s:var", but "n:" is not and can be used in
21584 * slice "[n:]". Also "xx:" is not a namespace. */
21585 len = (int)(p - *arg);
21586 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21587 || len > 1)
21588 break;
21589 }
21590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 if (p == *arg) /* no name found */
21592 return 0;
21593
21594 len = (int)(p - *arg);
21595 *arg = skipwhite(p);
21596
21597 return len;
21598}
21599
21600/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021601 * Get the length of the name of a variable or function.
21602 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021604 * Return -1 if curly braces expansion failed.
21605 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 * If the name contains 'magic' {}'s, expand them and return the
21607 * expanded name in an allocated string via 'alias' - caller must free.
21608 */
21609 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021610get_name_len(
21611 char_u **arg,
21612 char_u **alias,
21613 int evaluate,
21614 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615{
21616 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 char_u *p;
21618 char_u *expr_start;
21619 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620
21621 *alias = NULL; /* default to no alias */
21622
21623 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21624 && (*arg)[2] == (int)KE_SNR)
21625 {
21626 /* hard coded <SNR>, already translated */
21627 *arg += 3;
21628 return get_id_len(arg) + 3;
21629 }
21630 len = eval_fname_script(*arg);
21631 if (len > 0)
21632 {
21633 /* literal "<SID>", "s:" or "<SNR>" */
21634 *arg += len;
21635 }
21636
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021638 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021640 p = find_name_end(*arg, &expr_start, &expr_end,
21641 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 if (expr_start != NULL)
21643 {
21644 char_u *temp_string;
21645
21646 if (!evaluate)
21647 {
21648 len += (int)(p - *arg);
21649 *arg = skipwhite(p);
21650 return len;
21651 }
21652
21653 /*
21654 * Include any <SID> etc in the expanded string:
21655 * Thus the -len here.
21656 */
21657 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21658 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021659 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 *alias = temp_string;
21661 *arg = skipwhite(p);
21662 return (int)STRLEN(temp_string);
21663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664
21665 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021666 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 EMSG2(_(e_invexpr2), *arg);
21668
21669 return len;
21670}
21671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021672/*
21673 * Find the end of a variable or function name, taking care of magic braces.
21674 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21675 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021676 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021677 * Return a pointer to just after the name. Equal to "arg" if there is no
21678 * valid name.
21679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021681find_name_end(
21682 char_u *arg,
21683 char_u **expr_start,
21684 char_u **expr_end,
21685 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021687 int mb_nest = 0;
21688 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021690 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021692 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021694 *expr_start = NULL;
21695 *expr_end = NULL;
21696 }
21697
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021698 /* Quick check for valid starting character. */
21699 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21700 return arg;
21701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021702 for (p = arg; *p != NUL
21703 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021704 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021705 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021706 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021707 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021708 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021709 if (*p == '\'')
21710 {
21711 /* skip over 'string' to avoid counting [ and ] inside it. */
21712 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21713 ;
21714 if (*p == NUL)
21715 break;
21716 }
21717 else if (*p == '"')
21718 {
21719 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21720 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21721 if (*p == '\\' && p[1] != NUL)
21722 ++p;
21723 if (*p == NUL)
21724 break;
21725 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021726 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21727 {
21728 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021729 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021730 len = (int)(p - arg);
21731 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021732 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021733 break;
21734 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021736 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021738 if (*p == '[')
21739 ++br_nest;
21740 else if (*p == ']')
21741 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021744 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021746 if (*p == '{')
21747 {
21748 mb_nest++;
21749 if (expr_start != NULL && *expr_start == NULL)
21750 *expr_start = p;
21751 }
21752 else if (*p == '}')
21753 {
21754 mb_nest--;
21755 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21756 *expr_end = p;
21757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 }
21760
21761 return p;
21762}
21763
21764/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021765 * Expands out the 'magic' {}'s in a variable/function name.
21766 * Note that this can call itself recursively, to deal with
21767 * constructs like foo{bar}{baz}{bam}
21768 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21769 * "in_start" ^
21770 * "expr_start" ^
21771 * "expr_end" ^
21772 * "in_end" ^
21773 *
21774 * Returns a new allocated string, which the caller must free.
21775 * Returns NULL for failure.
21776 */
21777 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021778make_expanded_name(
21779 char_u *in_start,
21780 char_u *expr_start,
21781 char_u *expr_end,
21782 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021783{
21784 char_u c1;
21785 char_u *retval = NULL;
21786 char_u *temp_result;
21787 char_u *nextcmd = NULL;
21788
21789 if (expr_end == NULL || in_end == NULL)
21790 return NULL;
21791 *expr_start = NUL;
21792 *expr_end = NUL;
21793 c1 = *in_end;
21794 *in_end = NUL;
21795
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021796 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021797 if (temp_result != NULL && nextcmd == NULL)
21798 {
21799 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21800 + (in_end - expr_end) + 1));
21801 if (retval != NULL)
21802 {
21803 STRCPY(retval, in_start);
21804 STRCAT(retval, temp_result);
21805 STRCAT(retval, expr_end + 1);
21806 }
21807 }
21808 vim_free(temp_result);
21809
21810 *in_end = c1; /* put char back for error messages */
21811 *expr_start = '{';
21812 *expr_end = '}';
21813
21814 if (retval != NULL)
21815 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021816 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021817 if (expr_start != NULL)
21818 {
21819 /* Further expansion! */
21820 temp_result = make_expanded_name(retval, expr_start,
21821 expr_end, temp_result);
21822 vim_free(retval);
21823 retval = temp_result;
21824 }
21825 }
21826
21827 return retval;
21828}
21829
21830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021832 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833 */
21834 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021835eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021837 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21838}
21839
21840/*
21841 * Return TRUE if character "c" can be used as the first character in a
21842 * variable or function name (excluding '{' and '}').
21843 */
21844 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021845eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021846{
21847 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848}
21849
21850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 * Set number v: variable to "val".
21852 */
21853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021854set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021856 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857}
21858
21859/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021860 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 */
21862 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021863get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021865 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866}
21867
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021868/*
21869 * Get string v: variable value. Uses a static buffer, can only be used once.
21870 */
21871 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021872get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021873{
21874 return get_tv_string(&vimvars[idx].vv_tv);
21875}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021876
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021878 * Get List v: variable value. Caller must take care of reference count when
21879 * needed.
21880 */
21881 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021882get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021883{
21884 return vimvars[idx].vv_list;
21885}
21886
21887/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021888 * Set v:char to character "c".
21889 */
21890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021891set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021892{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021893 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021894
21895#ifdef FEAT_MBYTE
21896 if (has_mbyte)
21897 buf[(*mb_char2bytes)(c, buf)] = NUL;
21898 else
21899#endif
21900 {
21901 buf[0] = c;
21902 buf[1] = NUL;
21903 }
21904 set_vim_var_string(VV_CHAR, buf, -1);
21905}
21906
21907/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021908 * Set v:count to "count" and v:count1 to "count1".
21909 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910 */
21911 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021912set_vcount(
21913 long count,
21914 long count1,
21915 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021917 if (set_prevcount)
21918 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021919 vimvars[VV_COUNT].vv_nr = count;
21920 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921}
21922
21923/*
21924 * Set string v: variable to a copy of "val".
21925 */
21926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021927set_vim_var_string(
21928 int idx,
21929 char_u *val,
21930 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931{
Bram Moolenaara542c682016-01-31 16:28:04 +010021932 clear_tv(&vimvars[idx].vv_di.di_tv);
21933 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021935 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021937 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021939 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021940}
21941
21942/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021943 * Set List v: variable to "val".
21944 */
21945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021946set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021947{
Bram Moolenaara542c682016-01-31 16:28:04 +010021948 clear_tv(&vimvars[idx].vv_di.di_tv);
21949 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021950 vimvars[idx].vv_list = val;
21951 if (val != NULL)
21952 ++val->lv_refcount;
21953}
21954
21955/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021956 * Set Dictionary v: variable to "val".
21957 */
21958 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021959set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021960{
21961 int todo;
21962 hashitem_T *hi;
21963
Bram Moolenaara542c682016-01-31 16:28:04 +010021964 clear_tv(&vimvars[idx].vv_di.di_tv);
21965 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021966 vimvars[idx].vv_dict = val;
21967 if (val != NULL)
21968 {
21969 ++val->dv_refcount;
21970
21971 /* Set readonly */
21972 todo = (int)val->dv_hashtab.ht_used;
21973 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21974 {
21975 if (HASHITEM_EMPTY(hi))
21976 continue;
21977 --todo;
21978 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21979 }
21980 }
21981}
21982
21983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 * Set v:register if needed.
21985 */
21986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021987set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988{
21989 char_u regname;
21990
21991 if (c == 0 || c == ' ')
21992 regname = '"';
21993 else
21994 regname = c;
21995 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021996 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 set_vim_var_string(VV_REG, &regname, 1);
21998}
21999
22000/*
22001 * Get or set v:exception. If "oldval" == NULL, return the current value.
22002 * Otherwise, restore the value to "oldval" and return NULL.
22003 * Must always be called in pairs to save and restore v:exception! Does not
22004 * take care of memory allocations.
22005 */
22006 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022007v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008{
22009 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022010 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011
Bram Moolenaare9a41262005-01-15 22:18:47 +000022012 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 return NULL;
22014}
22015
22016/*
22017 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22018 * Otherwise, restore the value to "oldval" and return NULL.
22019 * Must always be called in pairs to save and restore v:throwpoint! Does not
22020 * take care of memory allocations.
22021 */
22022 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022023v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024{
22025 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022026 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027
Bram Moolenaare9a41262005-01-15 22:18:47 +000022028 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 return NULL;
22030}
22031
22032#if defined(FEAT_AUTOCMD) || defined(PROTO)
22033/*
22034 * Set v:cmdarg.
22035 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22036 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22037 * Must always be called in pairs!
22038 */
22039 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022040set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041{
22042 char_u *oldval;
22043 char_u *newval;
22044 unsigned len;
22045
Bram Moolenaare9a41262005-01-15 22:18:47 +000022046 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022047 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022049 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022050 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022051 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 }
22053
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022054 if (eap->force_bin == FORCE_BIN)
22055 len = 6;
22056 else if (eap->force_bin == FORCE_NOBIN)
22057 len = 8;
22058 else
22059 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022060
22061 if (eap->read_edit)
22062 len += 7;
22063
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022064 if (eap->force_ff != 0)
22065 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22066# ifdef FEAT_MBYTE
22067 if (eap->force_enc != 0)
22068 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022069 if (eap->bad_char != 0)
22070 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022071# endif
22072
22073 newval = alloc(len + 1);
22074 if (newval == NULL)
22075 return NULL;
22076
22077 if (eap->force_bin == FORCE_BIN)
22078 sprintf((char *)newval, " ++bin");
22079 else if (eap->force_bin == FORCE_NOBIN)
22080 sprintf((char *)newval, " ++nobin");
22081 else
22082 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022083
22084 if (eap->read_edit)
22085 STRCAT(newval, " ++edit");
22086
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022087 if (eap->force_ff != 0)
22088 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22089 eap->cmd + eap->force_ff);
22090# ifdef FEAT_MBYTE
22091 if (eap->force_enc != 0)
22092 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22093 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022094 if (eap->bad_char == BAD_KEEP)
22095 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22096 else if (eap->bad_char == BAD_DROP)
22097 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22098 else if (eap->bad_char != 0)
22099 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022100# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022101 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022102 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103}
22104#endif
22105
22106/*
22107 * Get the value of internal variable "name".
22108 * Return OK or FAIL.
22109 */
22110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022111get_var_tv(
22112 char_u *name,
22113 int len, /* length of "name" */
22114 typval_T *rettv, /* NULL when only checking existence */
22115 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22116 int verbose, /* may give error message */
22117 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118{
22119 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022120 typval_T *tv = NULL;
22121 typval_T atv;
22122 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124
22125 /* truncate the name, so that we can use strcmp() */
22126 cc = name[len];
22127 name[len] = NUL;
22128
22129 /*
22130 * Check for "b:changedtick".
22131 */
22132 if (STRCMP(name, "b:changedtick") == 0)
22133 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022134 atv.v_type = VAR_NUMBER;
22135 atv.vval.v_number = curbuf->b_changedtick;
22136 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 }
22138
22139 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022140 * Check for user-defined variables.
22141 */
22142 else
22143 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022144 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022146 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022147 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022148 if (dip != NULL)
22149 *dip = v;
22150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 }
22152
Bram Moolenaare9a41262005-01-15 22:18:47 +000022153 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022155 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022156 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 ret = FAIL;
22158 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022159 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022160 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161
22162 name[len] = cc;
22163
22164 return ret;
22165}
22166
22167/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022168 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22169 * Also handle function call with Funcref variable: func(expr)
22170 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22171 */
22172 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022173handle_subscript(
22174 char_u **arg,
22175 typval_T *rettv,
22176 int evaluate, /* do more than finding the end */
22177 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022178{
22179 int ret = OK;
22180 dict_T *selfdict = NULL;
22181 char_u *s;
22182 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022183 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022184
22185 while (ret == OK
22186 && (**arg == '['
22187 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022188 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22189 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022190 && !vim_iswhite(*(*arg - 1)))
22191 {
22192 if (**arg == '(')
22193 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022194 partial_T *pt = NULL;
22195
Bram Moolenaard9fba312005-06-26 22:34:35 +000022196 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022197 if (evaluate)
22198 {
22199 functv = *rettv;
22200 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022201
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022202 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022203 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022204 {
22205 pt = functv.vval.v_partial;
22206 s = pt->pt_name;
22207 }
22208 else
22209 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022210 }
22211 else
22212 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022213 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022214 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022215 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022216
22217 /* Clear the funcref afterwards, so that deleting it while
22218 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022219 if (evaluate)
22220 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022221
22222 /* Stop the expression evaluation when immediately aborting on
22223 * error, or when an interrupt occurred or an exception was thrown
22224 * but not caught. */
22225 if (aborting())
22226 {
22227 if (ret == OK)
22228 clear_tv(rettv);
22229 ret = FAIL;
22230 }
22231 dict_unref(selfdict);
22232 selfdict = NULL;
22233 }
22234 else /* **arg == '[' || **arg == '.' */
22235 {
22236 dict_unref(selfdict);
22237 if (rettv->v_type == VAR_DICT)
22238 {
22239 selfdict = rettv->vval.v_dict;
22240 if (selfdict != NULL)
22241 ++selfdict->dv_refcount;
22242 }
22243 else
22244 selfdict = NULL;
22245 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22246 {
22247 clear_tv(rettv);
22248 ret = FAIL;
22249 }
22250 }
22251 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022252
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022253 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
22254 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022255 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022256 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22257 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022258 char_u *tofree = NULL;
22259 ufunc_T *fp;
22260 char_u fname_buf[FLEN_FIXED + 1];
22261 int error;
22262
22263 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022264 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022265 fp = find_func(fname);
22266 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022267
22268 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010022269 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022270 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022271 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22272
22273 if (pt != NULL)
22274 {
22275 pt->pt_refcount = 1;
22276 pt->pt_dict = selfdict;
22277 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022278 if (rettv->v_type == VAR_FUNC)
22279 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022280 /* Just a function: Take over the function name and use
22281 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022282 pt->pt_name = rettv->vval.v_string;
22283 }
22284 else
22285 {
22286 partial_T *ret_pt = rettv->vval.v_partial;
22287 int i;
22288
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022289 /* Partial: copy the function name, use selfdict and copy
22290 * args. Can't take over name or args, the partial might
22291 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022292 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022293 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022294 if (ret_pt->pt_argc > 0)
22295 {
22296 pt->pt_argv = (typval_T *)alloc(
22297 sizeof(typval_T) * ret_pt->pt_argc);
22298 if (pt->pt_argv == NULL)
22299 /* out of memory: drop the arguments */
22300 pt->pt_argc = 0;
22301 else
22302 {
22303 pt->pt_argc = ret_pt->pt_argc;
22304 for (i = 0; i < pt->pt_argc; i++)
22305 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22306 }
22307 }
22308 partial_unref(ret_pt);
22309 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022310 rettv->v_type = VAR_PARTIAL;
22311 rettv->vval.v_partial = pt;
22312 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022313 }
22314 }
22315
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022316 dict_unref(selfdict);
22317 return ret;
22318}
22319
22320/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022321 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022322 * value).
22323 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022324 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022325alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022326{
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022328}
22329
22330/*
22331 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 * The string "s" must have been allocated, it is consumed.
22333 * Return NULL for out of memory, the variable otherwise.
22334 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022335 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022336alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337{
Bram Moolenaar33570922005-01-25 22:26:29 +000022338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022340 rettv = alloc_tv();
22341 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022343 rettv->v_type = VAR_STRING;
22344 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 }
22346 else
22347 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022348 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349}
22350
22351/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022352 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022355free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356{
22357 if (varp != NULL)
22358 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022359 switch (varp->v_type)
22360 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022361 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022362 func_unref(varp->vval.v_string);
22363 /*FALLTHROUGH*/
22364 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022365 vim_free(varp->vval.v_string);
22366 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022367 case VAR_PARTIAL:
22368 partial_unref(varp->vval.v_partial);
22369 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022370 case VAR_LIST:
22371 list_unref(varp->vval.v_list);
22372 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022373 case VAR_DICT:
22374 dict_unref(varp->vval.v_dict);
22375 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022376 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022377#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022378 job_unref(varp->vval.v_job);
22379 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022380#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022381 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022382#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022383 channel_unref(varp->vval.v_channel);
22384 break;
22385#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022386 case VAR_NUMBER:
22387 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022388 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022389 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022390 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 vim_free(varp);
22393 }
22394}
22395
22396/*
22397 * Free the memory for a variable value and set the value to NULL or 0.
22398 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022399 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022400clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401{
22402 if (varp != NULL)
22403 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022404 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022406 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022407 func_unref(varp->vval.v_string);
22408 /*FALLTHROUGH*/
22409 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022410 vim_free(varp->vval.v_string);
22411 varp->vval.v_string = NULL;
22412 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022413 case VAR_PARTIAL:
22414 partial_unref(varp->vval.v_partial);
22415 varp->vval.v_partial = NULL;
22416 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022417 case VAR_LIST:
22418 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022419 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022420 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022421 case VAR_DICT:
22422 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022423 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022424 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022425 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022426 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022427 varp->vval.v_number = 0;
22428 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022429 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022430#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022431 varp->vval.v_float = 0.0;
22432 break;
22433#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022434 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022435#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022436 job_unref(varp->vval.v_job);
22437 varp->vval.v_job = NULL;
22438#endif
22439 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022440 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022441#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022442 channel_unref(varp->vval.v_channel);
22443 varp->vval.v_channel = NULL;
22444#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022445 case VAR_UNKNOWN:
22446 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022448 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 }
22450}
22451
22452/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022453 * Set the value of a variable to NULL without freeing items.
22454 */
22455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022456init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022457{
22458 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022459 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022460}
22461
22462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 * Get the number value of a variable.
22464 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022465 * For incompatible types, return 0.
22466 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22467 * caller of incompatible types: it sets *denote to TRUE if "denote"
22468 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022470 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022471get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022473 int error = FALSE;
22474
22475 return get_tv_number_chk(varp, &error); /* return 0L on error */
22476}
22477
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022478 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022479get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022480{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022481 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022483 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022485 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022486 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022487 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022488#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022489 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022490 break;
22491#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022492 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022493 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022494 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022495 break;
22496 case VAR_STRING:
22497 if (varp->vval.v_string != NULL)
22498 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022499 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022500 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022501 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022502 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022503 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022504 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022505 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022506 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022507 case VAR_SPECIAL:
22508 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22509 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022510 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022512 EMSG(_("E910: Using a Job as a Number"));
22513 break;
22514#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022515 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022516#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022517 EMSG(_("E913: Using a Channel as a Number"));
22518 break;
22519#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022520 case VAR_UNKNOWN:
22521 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022522 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022524 if (denote == NULL) /* useful for values that must be unsigned */
22525 n = -1;
22526 else
22527 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022528 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529}
22530
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022531#ifdef FEAT_FLOAT
22532 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022533get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022534{
22535 switch (varp->v_type)
22536 {
22537 case VAR_NUMBER:
22538 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022539 case VAR_FLOAT:
22540 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022541 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022542 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022543 EMSG(_("E891: Using a Funcref as a Float"));
22544 break;
22545 case VAR_STRING:
22546 EMSG(_("E892: Using a String as a Float"));
22547 break;
22548 case VAR_LIST:
22549 EMSG(_("E893: Using a List as a Float"));
22550 break;
22551 case VAR_DICT:
22552 EMSG(_("E894: Using a Dictionary as a Float"));
22553 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022554 case VAR_SPECIAL:
22555 EMSG(_("E907: Using a special value as a Float"));
22556 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022557 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022558# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022559 EMSG(_("E911: Using a Job as a Float"));
22560 break;
22561# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022562 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022563# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022564 EMSG(_("E914: Using a Channel as a Float"));
22565 break;
22566# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022567 case VAR_UNKNOWN:
22568 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022569 break;
22570 }
22571 return 0;
22572}
22573#endif
22574
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022576 * Get the lnum from the first argument.
22577 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022578 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 */
22580 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022581get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582{
Bram Moolenaar33570922005-01-25 22:26:29 +000022583 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 linenr_T lnum;
22585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022586 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587 if (lnum == 0) /* no valid number, try using line() */
22588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022589 rettv.v_type = VAR_NUMBER;
22590 f_line(argvars, &rettv);
22591 lnum = rettv.vval.v_number;
22592 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 }
22594 return lnum;
22595}
22596
22597/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022598 * Get the lnum from the first argument.
22599 * Also accepts "$", then "buf" is used.
22600 * Returns 0 on error.
22601 */
22602 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022603get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022604{
22605 if (argvars[0].v_type == VAR_STRING
22606 && argvars[0].vval.v_string != NULL
22607 && argvars[0].vval.v_string[0] == '$'
22608 && buf != NULL)
22609 return buf->b_ml.ml_line_count;
22610 return get_tv_number_chk(&argvars[0], NULL);
22611}
22612
22613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 * Get the string value of a variable.
22615 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022616 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22617 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 * If the String variable has never been set, return an empty string.
22619 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022620 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22621 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022623 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022624get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625{
22626 static char_u mybuf[NUMBUFLEN];
22627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022628 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629}
22630
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022631 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022632get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022634 char_u *res = get_tv_string_buf_chk(varp, buf);
22635
22636 return res != NULL ? res : (char_u *)"";
22637}
22638
Bram Moolenaar7d647822014-04-05 21:28:56 +020022639/*
22640 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22641 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022642 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022643get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022644{
22645 static char_u mybuf[NUMBUFLEN];
22646
22647 return get_tv_string_buf_chk(varp, mybuf);
22648}
22649
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022650 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022651get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022652{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022653 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022655 case VAR_NUMBER:
22656 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22657 return buf;
22658 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022659 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022660 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022661 break;
22662 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022663 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022664 break;
22665 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022666 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022667 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022668 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022669#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022670 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022671 break;
22672#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022673 case VAR_STRING:
22674 if (varp->vval.v_string != NULL)
22675 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022676 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022677 case VAR_SPECIAL:
22678 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22679 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022680 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022681#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022682 {
22683 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022684 char *status;
22685
22686 if (job == NULL)
22687 return (char_u *)"no process";
22688 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022689 : job->jv_status == JOB_ENDED ? "dead"
22690 : "run";
22691# ifdef UNIX
22692 vim_snprintf((char *)buf, NUMBUFLEN,
22693 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022694# elif defined(WIN32)
22695 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022696 "process %ld %s",
22697 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022698 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022699# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022700 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022701 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22702# endif
22703 return buf;
22704 }
22705#endif
22706 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022707 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022708#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022709 {
22710 channel_T *channel = varp->vval.v_channel;
22711 char *status = channel_status(channel);
22712
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022713 if (channel == NULL)
22714 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22715 else
22716 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022717 "channel %d %s", channel->ch_id, status);
22718 return buf;
22719 }
22720#endif
22721 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022722 case VAR_UNKNOWN:
22723 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022724 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022726 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727}
22728
22729/*
22730 * Find variable "name" in the list of variables.
22731 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022732 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022733 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022734 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022736 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022737find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022740 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741
Bram Moolenaara7043832005-01-21 11:56:39 +000022742 ht = find_var_ht(name, &varname);
22743 if (htp != NULL)
22744 *htp = ht;
22745 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022747 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748}
22749
22750/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022751 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022752 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022754 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022755find_var_in_ht(
22756 hashtab_T *ht,
22757 int htname,
22758 char_u *varname,
22759 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022760{
Bram Moolenaar33570922005-01-25 22:26:29 +000022761 hashitem_T *hi;
22762
22763 if (*varname == NUL)
22764 {
22765 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022766 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022767 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022768 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022769 case 'g': return &globvars_var;
22770 case 'v': return &vimvars_var;
22771 case 'b': return &curbuf->b_bufvar;
22772 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022773#ifdef FEAT_WINDOWS
22774 case 't': return &curtab->tp_winvar;
22775#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022776 case 'l': return current_funccal == NULL
22777 ? NULL : &current_funccal->l_vars_var;
22778 case 'a': return current_funccal == NULL
22779 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022780 }
22781 return NULL;
22782 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022783
22784 hi = hash_find(ht, varname);
22785 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022786 {
22787 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022788 * worked find the variable again. Don't auto-load a script if it was
22789 * loaded already, otherwise it would be loaded every time when
22790 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022791 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022792 {
22793 /* Note: script_autoload() may make "hi" invalid. It must either
22794 * be obtained again or not used. */
22795 if (!script_autoload(varname, FALSE) || aborting())
22796 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022797 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022798 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022799 if (HASHITEM_EMPTY(hi))
22800 return NULL;
22801 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022802 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022803}
22804
22805/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022806 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022807 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022808 * Set "varname" to the start of name without ':'.
22809 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022810 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022811find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022813 hashitem_T *hi;
22814
Bram Moolenaar73627d02015-08-11 15:46:09 +020022815 if (name[0] == NUL)
22816 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 if (name[1] != ':')
22818 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022819 /* The name must not start with a colon or #. */
22820 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022821 return NULL;
22822 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022823
22824 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022825 hi = hash_find(&compat_hashtab, name);
22826 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022827 return &compat_hashtab;
22828
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022830 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022831 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832 }
22833 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022834 if (*name == 'g') /* global variable */
22835 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022836 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22837 */
22838 if (vim_strchr(name + 2, ':') != NULL
22839 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022840 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022842 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022844 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022845#ifdef FEAT_WINDOWS
22846 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022847 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022848#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022849 if (*name == 'v') /* v: variable */
22850 return &vimvarht;
22851 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022852 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022853 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022854 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 if (*name == 's' /* script variable */
22856 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22857 return &SCRIPT_VARS(current_SID);
22858 return NULL;
22859}
22860
22861/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022862 * Get function call environment based on bactrace debug level
22863 */
22864 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022865get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022866{
22867 int i;
22868 funccall_T *funccal;
22869 funccall_T *temp_funccal;
22870
22871 funccal = current_funccal;
22872 if (debug_backtrace_level > 0)
22873 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022874 for (i = 0; i < debug_backtrace_level; i++)
22875 {
22876 temp_funccal = funccal->caller;
22877 if (temp_funccal)
22878 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022879 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022880 /* backtrace level overflow. reset to max */
22881 debug_backtrace_level = i;
22882 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022883 }
22884 return funccal;
22885}
22886
22887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022889 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 * Returns NULL when it doesn't exist.
22891 */
22892 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022893get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894{
Bram Moolenaar33570922005-01-25 22:26:29 +000022895 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022897 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 if (v == NULL)
22899 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022900 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901}
22902
22903/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022904 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 * sourcing this script and when executing functions defined in the script.
22906 */
22907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022908new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909{
Bram Moolenaara7043832005-01-21 11:56:39 +000022910 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022911 hashtab_T *ht;
22912 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022913
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22915 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022916 /* Re-allocating ga_data means that an ht_array pointing to
22917 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022918 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022919 for (i = 1; i <= ga_scripts.ga_len; ++i)
22920 {
22921 ht = &SCRIPT_VARS(i);
22922 if (ht->ht_mask == HT_INIT_SIZE - 1)
22923 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022924 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022925 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022926 }
22927
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 while (ga_scripts.ga_len < id)
22929 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022930 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022931 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022932 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934 }
22935 }
22936}
22937
22938/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022939 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22940 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 */
22942 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022943init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944{
Bram Moolenaar33570922005-01-25 22:26:29 +000022945 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022946 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022947 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022948 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022949 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022950 dict_var->di_tv.vval.v_dict = dict;
22951 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022952 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022953 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22954 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955}
22956
22957/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022958 * Unreference a dictionary initialized by init_var_dict().
22959 */
22960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022961unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022962{
22963 /* Now the dict needs to be freed if no one else is using it, go back to
22964 * normal reference counting. */
22965 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22966 dict_unref(dict);
22967}
22968
22969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022971 * Frees all allocated variables and the value they contain.
22972 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 */
22974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022975vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022976{
22977 vars_clear_ext(ht, TRUE);
22978}
22979
22980/*
22981 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22982 */
22983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022984vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985{
Bram Moolenaara7043832005-01-21 11:56:39 +000022986 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022987 hashitem_T *hi;
22988 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989
Bram Moolenaar33570922005-01-25 22:26:29 +000022990 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022991 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022992 for (hi = ht->ht_array; todo > 0; ++hi)
22993 {
22994 if (!HASHITEM_EMPTY(hi))
22995 {
22996 --todo;
22997
Bram Moolenaar33570922005-01-25 22:26:29 +000022998 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022999 * ht_array might change then. hash_clear() takes care of it
23000 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023001 v = HI2DI(hi);
23002 if (free_val)
23003 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023004 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023005 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023006 }
23007 }
23008 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023009 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010}
23011
Bram Moolenaara7043832005-01-21 11:56:39 +000023012/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023013 * Delete a variable from hashtab "ht" at item "hi".
23014 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023017delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018{
Bram Moolenaar33570922005-01-25 22:26:29 +000023019 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023020
23021 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023022 clear_tv(&di->di_tv);
23023 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024}
23025
23026/*
23027 * List the value of one internal variable.
23028 */
23029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023030list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023032 char_u *tofree;
23033 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023034 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023035
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023036 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023037 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023038 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023039 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040}
23041
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023043list_one_var_a(
23044 char_u *prefix,
23045 char_u *name,
23046 int type,
23047 char_u *string,
23048 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049{
Bram Moolenaar31859182007-08-14 20:41:13 +000023050 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23051 msg_start();
23052 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023053 if (name != NULL) /* "a:" vars don't have a name stored */
23054 msg_puts(name);
23055 msg_putchar(' ');
23056 msg_advance(22);
23057 if (type == VAR_NUMBER)
23058 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023059 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023060 msg_putchar('*');
23061 else if (type == VAR_LIST)
23062 {
23063 msg_putchar('[');
23064 if (*string == '[')
23065 ++string;
23066 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023067 else if (type == VAR_DICT)
23068 {
23069 msg_putchar('{');
23070 if (*string == '{')
23071 ++string;
23072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 else
23074 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023075
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023077
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023078 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023079 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023080 if (*first)
23081 {
23082 msg_clr_eos();
23083 *first = FALSE;
23084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085}
23086
23087/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023088 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 * If the variable already exists, the value is updated.
23090 * Otherwise the variable is created.
23091 */
23092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023093set_var(
23094 char_u *name,
23095 typval_T *tv,
23096 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097{
Bram Moolenaar33570922005-01-25 22:26:29 +000023098 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023100 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023102 ht = find_var_ht(name, &varname);
23103 if (ht == NULL || *varname == NUL)
23104 {
23105 EMSG2(_(e_illvar), name);
23106 return;
23107 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023108 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023109
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023110 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23111 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023112 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023113
Bram Moolenaar33570922005-01-25 22:26:29 +000023114 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023116 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023117 if (var_check_ro(v->di_flags, name, FALSE)
23118 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023119 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023120
23121 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023122 * Handle setting internal v: variables separately where needed to
23123 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023124 */
23125 if (ht == &vimvarht)
23126 {
23127 if (v->di_tv.v_type == VAR_STRING)
23128 {
23129 vim_free(v->di_tv.vval.v_string);
23130 if (copy || tv->v_type != VAR_STRING)
23131 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23132 else
23133 {
23134 /* Take over the string to avoid an extra alloc/free. */
23135 v->di_tv.vval.v_string = tv->vval.v_string;
23136 tv->vval.v_string = NULL;
23137 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023138 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023139 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023140 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023141 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023142 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023143 if (STRCMP(varname, "searchforward") == 0)
23144 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023145#ifdef FEAT_SEARCH_EXTRA
23146 else if (STRCMP(varname, "hlsearch") == 0)
23147 {
23148 no_hlsearch = !v->di_tv.vval.v_number;
23149 redraw_all_later(SOME_VALID);
23150 }
23151#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023152 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023153 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023154 else if (v->di_tv.v_type != tv->v_type)
23155 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023156 }
23157
23158 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 }
23160 else /* add a new variable */
23161 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023162 /* Can't add "v:" variable. */
23163 if (ht == &vimvarht)
23164 {
23165 EMSG2(_(e_illvar), name);
23166 return;
23167 }
23168
Bram Moolenaar92124a32005-06-17 22:03:40 +000023169 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023170 if (!valid_varname(varname))
23171 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023172
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023173 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23174 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023175 if (v == NULL)
23176 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023177 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023178 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023180 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 return;
23182 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023183 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023185
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023186 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023187 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023188 else
23189 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023190 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023191 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023192 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194}
23195
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023196/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023197 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023198 * Also give an error message.
23199 */
23200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023201var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023202{
23203 if (flags & DI_FLAGS_RO)
23204 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023205 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023206 return TRUE;
23207 }
23208 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23209 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023210 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023211 return TRUE;
23212 }
23213 return FALSE;
23214}
23215
23216/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023217 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23218 * Also give an error message.
23219 */
23220 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023221var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023222{
23223 if (flags & DI_FLAGS_FIX)
23224 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023225 EMSG2(_("E795: Cannot delete variable %s"),
23226 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023227 return TRUE;
23228 }
23229 return FALSE;
23230}
23231
23232/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023233 * Check if a funcref is assigned to a valid variable name.
23234 * Return TRUE and give an error if not.
23235 */
23236 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023237var_check_func_name(
23238 char_u *name, /* points to start of variable name */
23239 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023240{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023241 /* Allow for w: b: s: and t:. */
23242 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023243 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23244 ? name[2] : name[0]))
23245 {
23246 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23247 name);
23248 return TRUE;
23249 }
23250 /* Don't allow hiding a function. When "v" is not NULL we might be
23251 * assigning another function to the same var, the type is checked
23252 * below. */
23253 if (new_var && function_exists(name))
23254 {
23255 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23256 name);
23257 return TRUE;
23258 }
23259 return FALSE;
23260}
23261
23262/*
23263 * Check if a variable name is valid.
23264 * Return FALSE and give an error if not.
23265 */
23266 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023267valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023268{
23269 char_u *p;
23270
23271 for (p = varname; *p != NUL; ++p)
23272 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23273 && *p != AUTOLOAD_CHAR)
23274 {
23275 EMSG2(_(e_illvar), varname);
23276 return FALSE;
23277 }
23278 return TRUE;
23279}
23280
23281/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023282 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023283 * Also give an error message, using "name" or _("name") when use_gettext is
23284 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023285 */
23286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023287tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023288{
23289 if (lock & VAR_LOCKED)
23290 {
23291 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023292 name == NULL ? (char_u *)_("Unknown")
23293 : use_gettext ? (char_u *)_(name)
23294 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023295 return TRUE;
23296 }
23297 if (lock & VAR_FIXED)
23298 {
23299 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023300 name == NULL ? (char_u *)_("Unknown")
23301 : use_gettext ? (char_u *)_(name)
23302 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023303 return TRUE;
23304 }
23305 return FALSE;
23306}
23307
23308/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023309 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023310 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023311 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023312 * It is OK for "from" and "to" to point to the same item. This is used to
23313 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023314 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023315 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023316copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023318 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023319 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023320 switch (from->v_type)
23321 {
23322 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023323 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023324 to->vval.v_number = from->vval.v_number;
23325 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023326 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023327#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023328 to->vval.v_float = from->vval.v_float;
23329 break;
23330#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023331 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023332#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023333 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023334 if (to->vval.v_job != NULL)
23335 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023336 break;
23337#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023338 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023339#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023340 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023341 if (to->vval.v_channel != NULL)
23342 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023343 break;
23344#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023345 case VAR_STRING:
23346 case VAR_FUNC:
23347 if (from->vval.v_string == NULL)
23348 to->vval.v_string = NULL;
23349 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023350 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023351 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023352 if (from->v_type == VAR_FUNC)
23353 func_ref(to->vval.v_string);
23354 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023355 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023356 case VAR_PARTIAL:
23357 if (from->vval.v_partial == NULL)
23358 to->vval.v_partial = NULL;
23359 else
23360 {
23361 to->vval.v_partial = from->vval.v_partial;
23362 ++to->vval.v_partial->pt_refcount;
23363 }
23364 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023365 case VAR_LIST:
23366 if (from->vval.v_list == NULL)
23367 to->vval.v_list = NULL;
23368 else
23369 {
23370 to->vval.v_list = from->vval.v_list;
23371 ++to->vval.v_list->lv_refcount;
23372 }
23373 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023374 case VAR_DICT:
23375 if (from->vval.v_dict == NULL)
23376 to->vval.v_dict = NULL;
23377 else
23378 {
23379 to->vval.v_dict = from->vval.v_dict;
23380 ++to->vval.v_dict->dv_refcount;
23381 }
23382 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023383 case VAR_UNKNOWN:
23384 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023385 break;
23386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387}
23388
23389/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023390 * Make a copy of an item.
23391 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023392 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23393 * reference to an already copied list/dict can be used.
23394 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023395 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023396 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023397item_copy(
23398 typval_T *from,
23399 typval_T *to,
23400 int deep,
23401 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023402{
23403 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023404 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023405
Bram Moolenaar33570922005-01-25 22:26:29 +000023406 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023407 {
23408 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023409 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023410 }
23411 ++recurse;
23412
23413 switch (from->v_type)
23414 {
23415 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023416 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023417 case VAR_STRING:
23418 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023419 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023420 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023421 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023422 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023423 copy_tv(from, to);
23424 break;
23425 case VAR_LIST:
23426 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023427 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023428 if (from->vval.v_list == NULL)
23429 to->vval.v_list = NULL;
23430 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23431 {
23432 /* use the copy made earlier */
23433 to->vval.v_list = from->vval.v_list->lv_copylist;
23434 ++to->vval.v_list->lv_refcount;
23435 }
23436 else
23437 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23438 if (to->vval.v_list == NULL)
23439 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023440 break;
23441 case VAR_DICT:
23442 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023443 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023444 if (from->vval.v_dict == NULL)
23445 to->vval.v_dict = NULL;
23446 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23447 {
23448 /* use the copy made earlier */
23449 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23450 ++to->vval.v_dict->dv_refcount;
23451 }
23452 else
23453 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23454 if (to->vval.v_dict == NULL)
23455 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023456 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023457 case VAR_UNKNOWN:
23458 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023459 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023460 }
23461 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023462 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023463}
23464
23465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023466 * ":echo expr1 ..." print each argument separated with a space, add a
23467 * newline at the end.
23468 * ":echon expr1 ..." print each argument plain.
23469 */
23470 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023471ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472{
23473 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023474 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023475 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 char_u *p;
23477 int needclr = TRUE;
23478 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023479 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480
23481 if (eap->skip)
23482 ++emsg_skip;
23483 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23484 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023485 /* If eval1() causes an error message the text from the command may
23486 * still need to be cleared. E.g., "echo 22,44". */
23487 need_clr_eos = needclr;
23488
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023490 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491 {
23492 /*
23493 * Report the invalid expression unless the expression evaluation
23494 * has been cancelled due to an aborting error, an interrupt, or an
23495 * exception.
23496 */
23497 if (!aborting())
23498 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023499 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 break;
23501 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023502 need_clr_eos = FALSE;
23503
Bram Moolenaar071d4272004-06-13 20:20:40 +000023504 if (!eap->skip)
23505 {
23506 if (atstart)
23507 {
23508 atstart = FALSE;
23509 /* Call msg_start() after eval1(), evaluating the expression
23510 * may cause a message to appear. */
23511 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023512 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023513 /* Mark the saved text as finishing the line, so that what
23514 * follows is displayed on a new line when scrolling back
23515 * at the more prompt. */
23516 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519 }
23520 else if (eap->cmdidx == CMD_echo)
23521 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023522 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023523 if (p != NULL)
23524 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023526 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023528 if (*p != TAB && needclr)
23529 {
23530 /* remove any text still there from the command */
23531 msg_clr_eos();
23532 needclr = FALSE;
23533 }
23534 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 }
23536 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023537 {
23538#ifdef FEAT_MBYTE
23539 if (has_mbyte)
23540 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023541 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023542
23543 (void)msg_outtrans_len_attr(p, i, echo_attr);
23544 p += i - 1;
23545 }
23546 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023548 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023551 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023553 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 arg = skipwhite(arg);
23555 }
23556 eap->nextcmd = check_nextcmd(arg);
23557
23558 if (eap->skip)
23559 --emsg_skip;
23560 else
23561 {
23562 /* remove text that may still be there from the command */
23563 if (needclr)
23564 msg_clr_eos();
23565 if (eap->cmdidx == CMD_echo)
23566 msg_end();
23567 }
23568}
23569
23570/*
23571 * ":echohl {name}".
23572 */
23573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023574ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575{
23576 int id;
23577
23578 id = syn_name2id(eap->arg);
23579 if (id == 0)
23580 echo_attr = 0;
23581 else
23582 echo_attr = syn_id2attr(id);
23583}
23584
23585/*
23586 * ":execute expr1 ..." execute the result of an expression.
23587 * ":echomsg expr1 ..." Print a message
23588 * ":echoerr expr1 ..." Print an error
23589 * Each gets spaces around each argument and a newline at the end for
23590 * echo commands
23591 */
23592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023593ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594{
23595 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023596 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 int ret = OK;
23598 char_u *p;
23599 garray_T ga;
23600 int len;
23601 int save_did_emsg;
23602
23603 ga_init2(&ga, 1, 80);
23604
23605 if (eap->skip)
23606 ++emsg_skip;
23607 while (*arg != NUL && *arg != '|' && *arg != '\n')
23608 {
23609 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023610 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611 {
23612 /*
23613 * Report the invalid expression unless the expression evaluation
23614 * has been cancelled due to an aborting error, an interrupt, or an
23615 * exception.
23616 */
23617 if (!aborting())
23618 EMSG2(_(e_invexpr2), p);
23619 ret = FAIL;
23620 break;
23621 }
23622
23623 if (!eap->skip)
23624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023625 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626 len = (int)STRLEN(p);
23627 if (ga_grow(&ga, len + 2) == FAIL)
23628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023629 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 ret = FAIL;
23631 break;
23632 }
23633 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 ga.ga_len += len;
23637 }
23638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023639 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 arg = skipwhite(arg);
23641 }
23642
23643 if (ret != FAIL && ga.ga_data != NULL)
23644 {
23645 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023648 out_flush();
23649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 else if (eap->cmdidx == CMD_echoerr)
23651 {
23652 /* We don't want to abort following commands, restore did_emsg. */
23653 save_did_emsg = did_emsg;
23654 EMSG((char_u *)ga.ga_data);
23655 if (!force_abort)
23656 did_emsg = save_did_emsg;
23657 }
23658 else if (eap->cmdidx == CMD_execute)
23659 do_cmdline((char_u *)ga.ga_data,
23660 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23661 }
23662
23663 ga_clear(&ga);
23664
23665 if (eap->skip)
23666 --emsg_skip;
23667
23668 eap->nextcmd = check_nextcmd(arg);
23669}
23670
23671/*
23672 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23673 * "arg" points to the "&" or '+' when called, to "option" when returning.
23674 * Returns NULL when no option name found. Otherwise pointer to the char
23675 * after the option name.
23676 */
23677 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023678find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679{
23680 char_u *p = *arg;
23681
23682 ++p;
23683 if (*p == 'g' && p[1] == ':')
23684 {
23685 *opt_flags = OPT_GLOBAL;
23686 p += 2;
23687 }
23688 else if (*p == 'l' && p[1] == ':')
23689 {
23690 *opt_flags = OPT_LOCAL;
23691 p += 2;
23692 }
23693 else
23694 *opt_flags = 0;
23695
23696 if (!ASCII_ISALPHA(*p))
23697 return NULL;
23698 *arg = p;
23699
23700 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23701 p += 4; /* termcap option */
23702 else
23703 while (ASCII_ISALPHA(*p))
23704 ++p;
23705 return p;
23706}
23707
23708/*
23709 * ":function"
23710 */
23711 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023712ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023713{
23714 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023715 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 int j;
23717 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023719 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 char_u *name = NULL;
23721 char_u *p;
23722 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023723 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023724 garray_T newargs;
23725 garray_T newlines;
23726 int varargs = FALSE;
23727 int mustend = FALSE;
23728 int flags = 0;
23729 ufunc_T *fp;
23730 int indent;
23731 int nesting;
23732 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023733 dictitem_T *v;
23734 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023735 static int func_nr = 0; /* number for nameless function */
23736 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023737 hashtab_T *ht;
23738 int todo;
23739 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023740 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741
23742 /*
23743 * ":function" without argument: list functions.
23744 */
23745 if (ends_excmd(*eap->arg))
23746 {
23747 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023748 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023749 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023750 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023751 {
23752 if (!HASHITEM_EMPTY(hi))
23753 {
23754 --todo;
23755 fp = HI2UF(hi);
23756 if (!isdigit(*fp->uf_name))
23757 list_func_head(fp, FALSE);
23758 }
23759 }
23760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023761 eap->nextcmd = check_nextcmd(eap->arg);
23762 return;
23763 }
23764
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023765 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023766 * ":function /pat": list functions matching pattern.
23767 */
23768 if (*eap->arg == '/')
23769 {
23770 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23771 if (!eap->skip)
23772 {
23773 regmatch_T regmatch;
23774
23775 c = *p;
23776 *p = NUL;
23777 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23778 *p = c;
23779 if (regmatch.regprog != NULL)
23780 {
23781 regmatch.rm_ic = p_ic;
23782
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023783 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023784 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23785 {
23786 if (!HASHITEM_EMPTY(hi))
23787 {
23788 --todo;
23789 fp = HI2UF(hi);
23790 if (!isdigit(*fp->uf_name)
23791 && vim_regexec(&regmatch, fp->uf_name, 0))
23792 list_func_head(fp, FALSE);
23793 }
23794 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023795 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023796 }
23797 }
23798 if (*p == '/')
23799 ++p;
23800 eap->nextcmd = check_nextcmd(p);
23801 return;
23802 }
23803
23804 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023805 * Get the function name. There are these situations:
23806 * func normal function name
23807 * "name" == func, "fudi.fd_dict" == NULL
23808 * dict.func new dictionary entry
23809 * "name" == NULL, "fudi.fd_dict" set,
23810 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23811 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023812 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023813 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23814 * dict.func existing dict entry that's not a Funcref
23815 * "name" == NULL, "fudi.fd_dict" set,
23816 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023817 * s:func script-local function name
23818 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023821 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023822 paren = (vim_strchr(p, '(') != NULL);
23823 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824 {
23825 /*
23826 * Return on an invalid expression in braces, unless the expression
23827 * evaluation has been cancelled due to an aborting error, an
23828 * interrupt, or an exception.
23829 */
23830 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023831 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023832 if (!eap->skip && fudi.fd_newkey != NULL)
23833 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023834 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 else
23838 eap->skip = TRUE;
23839 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023840
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841 /* An error in a function call during evaluation of an expression in magic
23842 * braces should not cause the function not to be defined. */
23843 saved_did_emsg = did_emsg;
23844 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845
23846 /*
23847 * ":function func" with only function name: list function.
23848 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023849 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850 {
23851 if (!ends_excmd(*skipwhite(p)))
23852 {
23853 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023854 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 }
23856 eap->nextcmd = check_nextcmd(p);
23857 if (eap->nextcmd != NULL)
23858 *p = NUL;
23859 if (!eap->skip && !got_int)
23860 {
23861 fp = find_func(name);
23862 if (fp != NULL)
23863 {
23864 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023865 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023867 if (FUNCLINE(fp, j) == NULL)
23868 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 msg_putchar('\n');
23870 msg_outnum((long)(j + 1));
23871 if (j < 9)
23872 msg_putchar(' ');
23873 if (j < 99)
23874 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023875 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876 out_flush(); /* show a line at a time */
23877 ui_breakcheck();
23878 }
23879 if (!got_int)
23880 {
23881 msg_putchar('\n');
23882 msg_puts((char_u *)" endfunction");
23883 }
23884 }
23885 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023886 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023888 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 }
23890
23891 /*
23892 * ":function name(arg1, arg2)" Define function.
23893 */
23894 p = skipwhite(p);
23895 if (*p != '(')
23896 {
23897 if (!eap->skip)
23898 {
23899 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023900 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 }
23902 /* attempt to continue by skipping some text */
23903 if (vim_strchr(p, '(') != NULL)
23904 p = vim_strchr(p, '(');
23905 }
23906 p = skipwhite(p + 1);
23907
23908 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23909 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23910
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023911 if (!eap->skip)
23912 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023913 /* Check the name of the function. Unless it's a dictionary function
23914 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023915 if (name != NULL)
23916 arg = name;
23917 else
23918 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023919 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023920 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23921 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023922 {
23923 if (*arg == K_SPECIAL)
23924 j = 3;
23925 else
23926 j = 0;
23927 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23928 : eval_isnamec(arg[j])))
23929 ++j;
23930 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023931 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023932 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023933 /* Disallow using the g: dict. */
23934 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23935 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023936 }
23937
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 /*
23939 * Isolate the arguments: "arg1, arg2, ...)"
23940 */
23941 while (*p != ')')
23942 {
23943 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23944 {
23945 varargs = TRUE;
23946 p += 3;
23947 mustend = TRUE;
23948 }
23949 else
23950 {
23951 arg = p;
23952 while (ASCII_ISALNUM(*p) || *p == '_')
23953 ++p;
23954 if (arg == p || isdigit(*arg)
23955 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23956 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23957 {
23958 if (!eap->skip)
23959 EMSG2(_("E125: Illegal argument: %s"), arg);
23960 break;
23961 }
23962 if (ga_grow(&newargs, 1) == FAIL)
23963 goto erret;
23964 c = *p;
23965 *p = NUL;
23966 arg = vim_strsave(arg);
23967 if (arg == NULL)
23968 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023969
23970 /* Check for duplicate argument name. */
23971 for (i = 0; i < newargs.ga_len; ++i)
23972 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23973 {
23974 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023975 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023976 goto erret;
23977 }
23978
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23980 *p = c;
23981 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023982 if (*p == ',')
23983 ++p;
23984 else
23985 mustend = TRUE;
23986 }
23987 p = skipwhite(p);
23988 if (mustend && *p != ')')
23989 {
23990 if (!eap->skip)
23991 EMSG2(_(e_invarg2), eap->arg);
23992 break;
23993 }
23994 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023995 if (*p != ')')
23996 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 ++p; /* skip the ')' */
23998
Bram Moolenaare9a41262005-01-15 22:18:47 +000023999 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000 for (;;)
24001 {
24002 p = skipwhite(p);
24003 if (STRNCMP(p, "range", 5) == 0)
24004 {
24005 flags |= FC_RANGE;
24006 p += 5;
24007 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024008 else if (STRNCMP(p, "dict", 4) == 0)
24009 {
24010 flags |= FC_DICT;
24011 p += 4;
24012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013 else if (STRNCMP(p, "abort", 5) == 0)
24014 {
24015 flags |= FC_ABORT;
24016 p += 5;
24017 }
24018 else
24019 break;
24020 }
24021
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024022 /* When there is a line break use what follows for the function body.
24023 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24024 if (*p == '\n')
24025 line_arg = p + 1;
24026 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 EMSG(_(e_trailing));
24028
24029 /*
24030 * Read the body of the function, until ":endfunction" is found.
24031 */
24032 if (KeyTyped)
24033 {
24034 /* Check if the function already exists, don't let the user type the
24035 * whole function before telling him it doesn't work! For a script we
24036 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024037 if (!eap->skip && !eap->forceit)
24038 {
24039 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24040 EMSG(_(e_funcdict));
24041 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024042 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024045 if (!eap->skip && did_emsg)
24046 goto erret;
24047
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 msg_putchar('\n'); /* don't overwrite the function name */
24049 cmdline_row = msg_row;
24050 }
24051
24052 indent = 2;
24053 nesting = 0;
24054 for (;;)
24055 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024056 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024057 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024058 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024059 saved_wait_return = FALSE;
24060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024062 sourcing_lnum_off = sourcing_lnum;
24063
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024064 if (line_arg != NULL)
24065 {
24066 /* Use eap->arg, split up in parts by line breaks. */
24067 theline = line_arg;
24068 p = vim_strchr(theline, '\n');
24069 if (p == NULL)
24070 line_arg += STRLEN(line_arg);
24071 else
24072 {
24073 *p = NUL;
24074 line_arg = p + 1;
24075 }
24076 }
24077 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 theline = getcmdline(':', 0L, indent);
24079 else
24080 theline = eap->getline(':', eap->cookie, indent);
24081 if (KeyTyped)
24082 lines_left = Rows - 1;
24083 if (theline == NULL)
24084 {
24085 EMSG(_("E126: Missing :endfunction"));
24086 goto erret;
24087 }
24088
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024089 /* Detect line continuation: sourcing_lnum increased more than one. */
24090 if (sourcing_lnum > sourcing_lnum_off + 1)
24091 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24092 else
24093 sourcing_lnum_off = 0;
24094
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095 if (skip_until != NULL)
24096 {
24097 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24098 * don't check for ":endfunc". */
24099 if (STRCMP(theline, skip_until) == 0)
24100 {
24101 vim_free(skip_until);
24102 skip_until = NULL;
24103 }
24104 }
24105 else
24106 {
24107 /* skip ':' and blanks*/
24108 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24109 ;
24110
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024111 /* Check for "endfunction". */
24112 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024114 if (line_arg == NULL)
24115 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 break;
24117 }
24118
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024119 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120 * at "end". */
24121 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24122 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024123 else if (STRNCMP(p, "if", 2) == 0
24124 || STRNCMP(p, "wh", 2) == 0
24125 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126 || STRNCMP(p, "try", 3) == 0)
24127 indent += 2;
24128
24129 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024130 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024131 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024132 if (*p == '!')
24133 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024135 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024136 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024138 ++nesting;
24139 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 }
24141 }
24142
24143 /* Check for ":append" or ":insert". */
24144 p = skip_range(p, NULL);
24145 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24146 || (p[0] == 'i'
24147 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24148 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24149 skip_until = vim_strsave((char_u *)".");
24150
24151 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24152 arg = skipwhite(skiptowhite(p));
24153 if (arg[0] == '<' && arg[1] =='<'
24154 && ((p[0] == 'p' && p[1] == 'y'
24155 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24156 || (p[0] == 'p' && p[1] == 'e'
24157 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24158 || (p[0] == 't' && p[1] == 'c'
24159 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024160 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24161 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24163 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024164 || (p[0] == 'm' && p[1] == 'z'
24165 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166 ))
24167 {
24168 /* ":python <<" continues until a dot, like ":append" */
24169 p = skipwhite(arg + 2);
24170 if (*p == NUL)
24171 skip_until = vim_strsave((char_u *)".");
24172 else
24173 skip_until = vim_strsave(p);
24174 }
24175 }
24176
24177 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024178 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024179 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024180 if (line_arg == NULL)
24181 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024183 }
24184
24185 /* Copy the line to newly allocated memory. get_one_sourceline()
24186 * allocates 250 bytes per line, this saves 80% on average. The cost
24187 * is an extra alloc/free. */
24188 p = vim_strsave(theline);
24189 if (p != NULL)
24190 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024191 if (line_arg == NULL)
24192 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024193 theline = p;
24194 }
24195
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024196 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24197
24198 /* Add NULL lines for continuation lines, so that the line count is
24199 * equal to the index in the growarray. */
24200 while (sourcing_lnum_off-- > 0)
24201 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024202
24203 /* Check for end of eap->arg. */
24204 if (line_arg != NULL && *line_arg == NUL)
24205 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206 }
24207
24208 /* Don't define the function when skipping commands or when an error was
24209 * detected. */
24210 if (eap->skip || did_emsg)
24211 goto erret;
24212
24213 /*
24214 * If there are no errors, add the function
24215 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024216 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024217 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024218 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024219 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024220 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024221 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024222 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024223 goto erret;
24224 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024225
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024226 fp = find_func(name);
24227 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024229 if (!eap->forceit)
24230 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024231 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024232 goto erret;
24233 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024234 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024235 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024236 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024237 name);
24238 goto erret;
24239 }
24240 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024241 ga_clear_strings(&(fp->uf_args));
24242 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024243 vim_free(name);
24244 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246 }
24247 else
24248 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024249 char numbuf[20];
24250
24251 fp = NULL;
24252 if (fudi.fd_newkey == NULL && !eap->forceit)
24253 {
24254 EMSG(_(e_funcdict));
24255 goto erret;
24256 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024257 if (fudi.fd_di == NULL)
24258 {
24259 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024260 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024261 goto erret;
24262 }
24263 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024264 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024265 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024266
24267 /* Give the function a sequential number. Can only be used with a
24268 * Funcref! */
24269 vim_free(name);
24270 sprintf(numbuf, "%d", ++func_nr);
24271 name = vim_strsave((char_u *)numbuf);
24272 if (name == NULL)
24273 goto erret;
24274 }
24275
24276 if (fp == NULL)
24277 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024278 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024279 {
24280 int slen, plen;
24281 char_u *scriptname;
24282
24283 /* Check that the autoload name matches the script name. */
24284 j = FAIL;
24285 if (sourcing_name != NULL)
24286 {
24287 scriptname = autoload_name(name);
24288 if (scriptname != NULL)
24289 {
24290 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024291 plen = (int)STRLEN(p);
24292 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024293 if (slen > plen && fnamecmp(p,
24294 sourcing_name + slen - plen) == 0)
24295 j = OK;
24296 vim_free(scriptname);
24297 }
24298 }
24299 if (j == FAIL)
24300 {
24301 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24302 goto erret;
24303 }
24304 }
24305
24306 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 if (fp == NULL)
24308 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024309
24310 if (fudi.fd_dict != NULL)
24311 {
24312 if (fudi.fd_di == NULL)
24313 {
24314 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024315 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024316 if (fudi.fd_di == NULL)
24317 {
24318 vim_free(fp);
24319 goto erret;
24320 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024321 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24322 {
24323 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024324 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024325 goto erret;
24326 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024327 }
24328 else
24329 /* overwrite existing dict entry */
24330 clear_tv(&fudi.fd_di->di_tv);
24331 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024332 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024333 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024334 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024335
24336 /* behave like "dict" was used */
24337 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024338 }
24339
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024341 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024342 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24343 {
24344 vim_free(fp);
24345 goto erret;
24346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024348 fp->uf_args = newargs;
24349 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024350#ifdef FEAT_PROFILE
24351 fp->uf_tml_count = NULL;
24352 fp->uf_tml_total = NULL;
24353 fp->uf_tml_self = NULL;
24354 fp->uf_profiling = FALSE;
24355 if (prof_def_func())
24356 func_do_profile(fp);
24357#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024358 fp->uf_varargs = varargs;
24359 fp->uf_flags = flags;
24360 fp->uf_calls = 0;
24361 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024362 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363
24364erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365 ga_clear_strings(&newargs);
24366 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024367ret_free:
24368 vim_free(skip_until);
24369 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024372 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373}
24374
24375/*
24376 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024377 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024379 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024380 * TFN_INT: internal function name OK
24381 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024382 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 * Advances "pp" to just after the function name (if no error).
24384 */
24385 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024386trans_function_name(
24387 char_u **pp,
24388 int skip, /* only find the end, don't evaluate */
24389 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024390 funcdict_T *fdp, /* return: info about dictionary used */
24391 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024393 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024394 char_u *start;
24395 char_u *end;
24396 int lead;
24397 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024398 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024399 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024400
24401 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024402 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024404
24405 /* Check for hard coded <SNR>: already translated function ID (from a user
24406 * command). */
24407 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24408 && (*pp)[2] == (int)KE_SNR)
24409 {
24410 *pp += 3;
24411 len = get_id_len(pp) + 3;
24412 return vim_strnsave(start, len);
24413 }
24414
24415 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24416 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024418 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024419 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024420
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024421 /* Note that TFN_ flags use the same values as GLV_ flags. */
24422 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024423 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024424 if (end == start)
24425 {
24426 if (!skip)
24427 EMSG(_("E129: Function name required"));
24428 goto theend;
24429 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024430 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024431 {
24432 /*
24433 * Report an invalid expression in braces, unless the expression
24434 * evaluation has been cancelled due to an aborting error, an
24435 * interrupt, or an exception.
24436 */
24437 if (!aborting())
24438 {
24439 if (end != NULL)
24440 EMSG2(_(e_invarg2), start);
24441 }
24442 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024443 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024444 goto theend;
24445 }
24446
24447 if (lv.ll_tv != NULL)
24448 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024449 if (fdp != NULL)
24450 {
24451 fdp->fd_dict = lv.ll_dict;
24452 fdp->fd_newkey = lv.ll_newkey;
24453 lv.ll_newkey = NULL;
24454 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024455 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024456 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24457 {
24458 name = vim_strsave(lv.ll_tv->vval.v_string);
24459 *pp = end;
24460 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024461 else if (lv.ll_tv->v_type == VAR_PARTIAL
24462 && lv.ll_tv->vval.v_partial != NULL)
24463 {
24464 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24465 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024466 if (partial != NULL)
24467 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024468 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024469 else
24470 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024471 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24472 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024473 EMSG(_(e_funcref));
24474 else
24475 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024476 name = NULL;
24477 }
24478 goto theend;
24479 }
24480
24481 if (lv.ll_name == NULL)
24482 {
24483 /* Error found, but continue after the function name. */
24484 *pp = end;
24485 goto theend;
24486 }
24487
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024488 /* Check if the name is a Funcref. If so, use the value. */
24489 if (lv.ll_exp_name != NULL)
24490 {
24491 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024492 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024493 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024494 if (name == lv.ll_exp_name)
24495 name = NULL;
24496 }
24497 else
24498 {
24499 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024500 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024501 if (name == *pp)
24502 name = NULL;
24503 }
24504 if (name != NULL)
24505 {
24506 name = vim_strsave(name);
24507 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024508 if (STRNCMP(name, "<SNR>", 5) == 0)
24509 {
24510 /* Change "<SNR>" to the byte sequence. */
24511 name[0] = K_SPECIAL;
24512 name[1] = KS_EXTRA;
24513 name[2] = (int)KE_SNR;
24514 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24515 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024516 goto theend;
24517 }
24518
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024519 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024520 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024521 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024522 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24523 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24524 {
24525 /* When there was "s:" already or the name expanded to get a
24526 * leading "s:" then remove it. */
24527 lv.ll_name += 2;
24528 len -= 2;
24529 lead = 2;
24530 }
24531 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024532 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024533 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024534 /* skip over "s:" and "g:" */
24535 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024536 lv.ll_name += 2;
24537 len = (int)(end - lv.ll_name);
24538 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024539
24540 /*
24541 * Copy the function name to allocated memory.
24542 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24543 * Accept <SNR>123_name() outside a script.
24544 */
24545 if (skip)
24546 lead = 0; /* do nothing */
24547 else if (lead > 0)
24548 {
24549 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024550 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24551 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024552 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024553 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024554 if (current_SID <= 0)
24555 {
24556 EMSG(_(e_usingsid));
24557 goto theend;
24558 }
24559 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24560 lead += (int)STRLEN(sid_buf);
24561 }
24562 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024563 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024564 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024565 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024566 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024567 goto theend;
24568 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024569 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024570 {
24571 char_u *cp = vim_strchr(lv.ll_name, ':');
24572
24573 if (cp != NULL && cp < end)
24574 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024575 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024576 goto theend;
24577 }
24578 }
24579
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024580 name = alloc((unsigned)(len + lead + 1));
24581 if (name != NULL)
24582 {
24583 if (lead > 0)
24584 {
24585 name[0] = K_SPECIAL;
24586 name[1] = KS_EXTRA;
24587 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024588 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024589 STRCPY(name + 3, sid_buf);
24590 }
24591 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024592 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024593 }
24594 *pp = end;
24595
24596theend:
24597 clear_lval(&lv);
24598 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599}
24600
24601/*
24602 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24603 * Return 2 if "p" starts with "s:".
24604 * Return 0 otherwise.
24605 */
24606 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024607eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024608{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024609 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24610 * the standard library function. */
24611 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24612 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613 return 5;
24614 if (p[0] == 's' && p[1] == ':')
24615 return 2;
24616 return 0;
24617}
24618
24619/*
24620 * Return TRUE if "p" starts with "<SID>" or "s:".
24621 * Only works if eval_fname_script() returned non-zero for "p"!
24622 */
24623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024624eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625{
24626 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24627}
24628
24629/*
24630 * List the head of the function: "name(arg1, arg2)".
24631 */
24632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024633list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634{
24635 int j;
24636
24637 msg_start();
24638 if (indent)
24639 MSG_PUTS(" ");
24640 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024641 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642 {
24643 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024644 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645 }
24646 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024647 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024648 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024649 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 {
24651 if (j)
24652 MSG_PUTS(", ");
24653 msg_puts(FUNCARG(fp, j));
24654 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024655 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024656 {
24657 if (j)
24658 MSG_PUTS(", ");
24659 MSG_PUTS("...");
24660 }
24661 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024662 if (fp->uf_flags & FC_ABORT)
24663 MSG_PUTS(" abort");
24664 if (fp->uf_flags & FC_RANGE)
24665 MSG_PUTS(" range");
24666 if (fp->uf_flags & FC_DICT)
24667 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024668 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024669 if (p_verbose > 0)
24670 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024671}
24672
24673/*
24674 * Find a function by name, return pointer to it in ufuncs.
24675 * Return NULL for unknown function.
24676 */
24677 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024678find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024680 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024681
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024682 hi = hash_find(&func_hashtab, name);
24683 if (!HASHITEM_EMPTY(hi))
24684 return HI2UF(hi);
24685 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686}
24687
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024688#if defined(EXITFREE) || defined(PROTO)
24689 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024690free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024691{
24692 hashitem_T *hi;
24693
24694 /* Need to start all over every time, because func_free() may change the
24695 * hash table. */
24696 while (func_hashtab.ht_used > 0)
24697 for (hi = func_hashtab.ht_array; ; ++hi)
24698 if (!HASHITEM_EMPTY(hi))
24699 {
24700 func_free(HI2UF(hi));
24701 break;
24702 }
24703}
24704#endif
24705
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024706 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024707translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024708{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024709 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024710 return find_internal_func(name) >= 0;
24711 return find_func(name) != NULL;
24712}
24713
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024714/*
24715 * Return TRUE if a function "name" exists.
24716 */
24717 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024718function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024719{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024720 char_u *nm = name;
24721 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024722 int n = FALSE;
24723
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024724 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024725 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024726 nm = skipwhite(nm);
24727
24728 /* Only accept "funcname", "funcname ", "funcname (..." and
24729 * "funcname(...", not "funcname!...". */
24730 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024731 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024732 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024733 return n;
24734}
24735
Bram Moolenaara1544c02013-05-30 12:35:52 +020024736 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024737get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024738{
24739 char_u *nm = name;
24740 char_u *p;
24741
Bram Moolenaar65639032016-03-16 21:40:30 +010024742 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024743
24744 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024745 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024746 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024747
Bram Moolenaara1544c02013-05-30 12:35:52 +020024748 vim_free(p);
24749 return NULL;
24750}
24751
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024752/*
24753 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024754 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24755 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024756 */
24757 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024758builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024759{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024760 char_u *p;
24761
24762 if (!ASCII_ISLOWER(name[0]))
24763 return FALSE;
24764 p = vim_strchr(name, AUTOLOAD_CHAR);
24765 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024766}
24767
Bram Moolenaar05159a02005-02-26 23:04:13 +000024768#if defined(FEAT_PROFILE) || defined(PROTO)
24769/*
24770 * Start profiling function "fp".
24771 */
24772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024773func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024774{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024775 int len = fp->uf_lines.ga_len;
24776
24777 if (len == 0)
24778 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024779 fp->uf_tm_count = 0;
24780 profile_zero(&fp->uf_tm_self);
24781 profile_zero(&fp->uf_tm_total);
24782 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024783 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024784 if (fp->uf_tml_total == NULL)
24785 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024786 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024787 if (fp->uf_tml_self == NULL)
24788 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024789 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024790 fp->uf_tml_idx = -1;
24791 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24792 || fp->uf_tml_self == NULL)
24793 return; /* out of memory */
24794
24795 fp->uf_profiling = TRUE;
24796}
24797
24798/*
24799 * Dump the profiling results for all functions in file "fd".
24800 */
24801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024802func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024803{
24804 hashitem_T *hi;
24805 int todo;
24806 ufunc_T *fp;
24807 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024808 ufunc_T **sorttab;
24809 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024810
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024811 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024812 if (todo == 0)
24813 return; /* nothing to dump */
24814
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024815 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024816
Bram Moolenaar05159a02005-02-26 23:04:13 +000024817 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24818 {
24819 if (!HASHITEM_EMPTY(hi))
24820 {
24821 --todo;
24822 fp = HI2UF(hi);
24823 if (fp->uf_profiling)
24824 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024825 if (sorttab != NULL)
24826 sorttab[st_len++] = fp;
24827
Bram Moolenaar05159a02005-02-26 23:04:13 +000024828 if (fp->uf_name[0] == K_SPECIAL)
24829 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24830 else
24831 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24832 if (fp->uf_tm_count == 1)
24833 fprintf(fd, "Called 1 time\n");
24834 else
24835 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24836 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24837 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24838 fprintf(fd, "\n");
24839 fprintf(fd, "count total (s) self (s)\n");
24840
24841 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24842 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024843 if (FUNCLINE(fp, i) == NULL)
24844 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024845 prof_func_line(fd, fp->uf_tml_count[i],
24846 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024847 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24848 }
24849 fprintf(fd, "\n");
24850 }
24851 }
24852 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024853
24854 if (sorttab != NULL && st_len > 0)
24855 {
24856 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24857 prof_total_cmp);
24858 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24859 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24860 prof_self_cmp);
24861 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24862 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024863
24864 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024865}
Bram Moolenaar73830342005-02-28 22:48:19 +000024866
24867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024868prof_sort_list(
24869 FILE *fd,
24870 ufunc_T **sorttab,
24871 int st_len,
24872 char *title,
24873 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024874{
24875 int i;
24876 ufunc_T *fp;
24877
24878 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24879 fprintf(fd, "count total (s) self (s) function\n");
24880 for (i = 0; i < 20 && i < st_len; ++i)
24881 {
24882 fp = sorttab[i];
24883 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24884 prefer_self);
24885 if (fp->uf_name[0] == K_SPECIAL)
24886 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24887 else
24888 fprintf(fd, " %s()\n", fp->uf_name);
24889 }
24890 fprintf(fd, "\n");
24891}
24892
24893/*
24894 * Print the count and times for one function or function line.
24895 */
24896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024897prof_func_line(
24898 FILE *fd,
24899 int count,
24900 proftime_T *total,
24901 proftime_T *self,
24902 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024903{
24904 if (count > 0)
24905 {
24906 fprintf(fd, "%5d ", count);
24907 if (prefer_self && profile_equal(total, self))
24908 fprintf(fd, " ");
24909 else
24910 fprintf(fd, "%s ", profile_msg(total));
24911 if (!prefer_self && profile_equal(total, self))
24912 fprintf(fd, " ");
24913 else
24914 fprintf(fd, "%s ", profile_msg(self));
24915 }
24916 else
24917 fprintf(fd, " ");
24918}
24919
24920/*
24921 * Compare function for total time sorting.
24922 */
24923 static int
24924#ifdef __BORLANDC__
24925_RTLENTRYF
24926#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024927prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024928{
24929 ufunc_T *p1, *p2;
24930
24931 p1 = *(ufunc_T **)s1;
24932 p2 = *(ufunc_T **)s2;
24933 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24934}
24935
24936/*
24937 * Compare function for self time sorting.
24938 */
24939 static int
24940#ifdef __BORLANDC__
24941_RTLENTRYF
24942#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024943prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024944{
24945 ufunc_T *p1, *p2;
24946
24947 p1 = *(ufunc_T **)s1;
24948 p2 = *(ufunc_T **)s2;
24949 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24950}
24951
Bram Moolenaar05159a02005-02-26 23:04:13 +000024952#endif
24953
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024954/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024955 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024956 * Return TRUE if a package was loaded.
24957 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024959script_autoload(
24960 char_u *name,
24961 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024962{
24963 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024964 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024965 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024966 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024967
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024968 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024969 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024970 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024971 return FALSE;
24972
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024973 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024974
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024975 /* Find the name in the list of previously loaded package names. Skip
24976 * "autoload/", it's always the same. */
24977 for (i = 0; i < ga_loaded.ga_len; ++i)
24978 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24979 break;
24980 if (!reload && i < ga_loaded.ga_len)
24981 ret = FALSE; /* was loaded already */
24982 else
24983 {
24984 /* Remember the name if it wasn't loaded already. */
24985 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24986 {
24987 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24988 tofree = NULL;
24989 }
24990
24991 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024992 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024993 ret = TRUE;
24994 }
24995
24996 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024997 return ret;
24998}
24999
25000/*
25001 * Return the autoload script name for a function or variable name.
25002 * Returns NULL when out of memory.
25003 */
25004 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025005autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025006{
25007 char_u *p;
25008 char_u *scriptname;
25009
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025010 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025011 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25012 if (scriptname == NULL)
25013 return FALSE;
25014 STRCPY(scriptname, "autoload/");
25015 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025016 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025017 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025018 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025019 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025020 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025021}
25022
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25024
25025/*
25026 * Function given to ExpandGeneric() to obtain the list of user defined
25027 * function names.
25028 */
25029 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025030get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025032 static long_u done;
25033 static hashitem_T *hi;
25034 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025035
25036 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025037 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025038 done = 0;
25039 hi = func_hashtab.ht_array;
25040 }
25041 if (done < func_hashtab.ht_used)
25042 {
25043 if (done++ > 0)
25044 ++hi;
25045 while (HASHITEM_EMPTY(hi))
25046 ++hi;
25047 fp = HI2UF(hi);
25048
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025049 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025050 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025051
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025052 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25053 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054
25055 cat_func_name(IObuff, fp);
25056 if (xp->xp_context != EXPAND_USER_FUNC)
25057 {
25058 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025059 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060 STRCAT(IObuff, ")");
25061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062 return IObuff;
25063 }
25064 return NULL;
25065}
25066
25067#endif /* FEAT_CMDL_COMPL */
25068
25069/*
25070 * Copy the function name of "fp" to buffer "buf".
25071 * "buf" must be able to hold the function name plus three bytes.
25072 * Takes care of script-local function names.
25073 */
25074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025075cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025077 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025078 {
25079 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025080 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025081 }
25082 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025083 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084}
25085
25086/*
25087 * ":delfunction {name}"
25088 */
25089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025090ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025091{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025092 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093 char_u *p;
25094 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025095 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025096
25097 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025098 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025099 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025100 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025101 {
25102 if (fudi.fd_dict != NULL && !eap->skip)
25103 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025106 if (!ends_excmd(*skipwhite(p)))
25107 {
25108 vim_free(name);
25109 EMSG(_(e_trailing));
25110 return;
25111 }
25112 eap->nextcmd = check_nextcmd(p);
25113 if (eap->nextcmd != NULL)
25114 *p = NUL;
25115
25116 if (!eap->skip)
25117 fp = find_func(name);
25118 vim_free(name);
25119
25120 if (!eap->skip)
25121 {
25122 if (fp == NULL)
25123 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025124 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025125 return;
25126 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025127 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128 {
25129 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25130 return;
25131 }
25132
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025133 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025134 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025135 /* Delete the dict item that refers to the function, it will
25136 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025137 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025139 else
25140 func_free(fp);
25141 }
25142}
25143
25144/*
25145 * Free a function and remove it from the list of functions.
25146 */
25147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025148func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025149{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025150 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025151
25152 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025153 ga_clear_strings(&(fp->uf_args));
25154 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025155#ifdef FEAT_PROFILE
25156 vim_free(fp->uf_tml_count);
25157 vim_free(fp->uf_tml_total);
25158 vim_free(fp->uf_tml_self);
25159#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025160
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025161 /* remove the function from the function hashtable */
25162 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25163 if (HASHITEM_EMPTY(hi))
25164 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025165 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025166 hash_remove(&func_hashtab, hi);
25167
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025168 vim_free(fp);
25169}
25170
25171/*
25172 * Unreference a Function: decrement the reference count and free it when it
25173 * becomes zero. Only for numbered functions.
25174 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025175 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025176func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025177{
25178 ufunc_T *fp;
25179
25180 if (name != NULL && isdigit(*name))
25181 {
25182 fp = find_func(name);
25183 if (fp == NULL)
25184 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025185 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025186 {
25187 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025188 * when "uf_calls" becomes zero. */
25189 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025190 func_free(fp);
25191 }
25192 }
25193}
25194
25195/*
25196 * Count a reference to a Function.
25197 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025199func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025200{
25201 ufunc_T *fp;
25202
25203 if (name != NULL && isdigit(*name))
25204 {
25205 fp = find_func(name);
25206 if (fp == NULL)
25207 EMSG2(_(e_intern2), "func_ref()");
25208 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025209 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210 }
25211}
25212
25213/*
25214 * Call a user function.
25215 */
25216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025217call_user_func(
25218 ufunc_T *fp, /* pointer to function */
25219 int argcount, /* nr of args */
25220 typval_T *argvars, /* arguments */
25221 typval_T *rettv, /* return value */
25222 linenr_T firstline, /* first line of range */
25223 linenr_T lastline, /* last line of range */
25224 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225{
Bram Moolenaar33570922005-01-25 22:26:29 +000025226 char_u *save_sourcing_name;
25227 linenr_T save_sourcing_lnum;
25228 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025229 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025230 int save_did_emsg;
25231 static int depth = 0;
25232 dictitem_T *v;
25233 int fixvar_idx = 0; /* index in fixvar[] */
25234 int i;
25235 int ai;
25236 char_u numbuf[NUMBUFLEN];
25237 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025238 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025239#ifdef FEAT_PROFILE
25240 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025241 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243
25244 /* If depth of calling is getting too high, don't execute the function */
25245 if (depth >= p_mfd)
25246 {
25247 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025248 rettv->v_type = VAR_NUMBER;
25249 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025250 return;
25251 }
25252 ++depth;
25253
25254 line_breakcheck(); /* check for CTRL-C hit */
25255
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025256 fc = (funccall_T *)alloc(sizeof(funccall_T));
25257 fc->caller = current_funccal;
25258 current_funccal = fc;
25259 fc->func = fp;
25260 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025261 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025262 fc->linenr = 0;
25263 fc->returned = FALSE;
25264 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025266 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25267 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268
Bram Moolenaar33570922005-01-25 22:26:29 +000025269 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025270 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025271 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25272 * each argument variable and saves a lot of time.
25273 */
25274 /*
25275 * Init l: variables.
25276 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025277 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025278 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025279 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025280 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25281 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025282 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025283 name = v->di_key;
25284 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025285 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025286 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025287 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025288 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025289 v->di_tv.vval.v_dict = selfdict;
25290 ++selfdict->dv_refcount;
25291 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025292
Bram Moolenaar33570922005-01-25 22:26:29 +000025293 /*
25294 * Init a: variables.
25295 * Set a:0 to "argcount".
25296 * Set a:000 to a list with room for the "..." arguments.
25297 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025298 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025299 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025300 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025301 /* Use "name" to avoid a warning from some compiler that checks the
25302 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025303 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025304 name = v->di_key;
25305 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025306 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025307 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025308 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025309 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025310 v->di_tv.vval.v_list = &fc->l_varlist;
25311 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25312 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25313 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025314
25315 /*
25316 * Set a:firstline to "firstline" and a:lastline to "lastline".
25317 * Set a:name to named arguments.
25318 * Set a:N to the "..." arguments.
25319 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025320 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025321 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025322 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025323 (varnumber_T)lastline);
25324 for (i = 0; i < argcount; ++i)
25325 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025326 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025327 if (ai < 0)
25328 /* named argument a:name */
25329 name = FUNCARG(fp, i);
25330 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025331 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025332 /* "..." argument a:1, a:2, etc. */
25333 sprintf((char *)numbuf, "%d", ai + 1);
25334 name = numbuf;
25335 }
25336 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25337 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025338 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025339 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25340 }
25341 else
25342 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025343 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25344 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025345 if (v == NULL)
25346 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025347 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025348 }
25349 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025350 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025351
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025352 /* Note: the values are copied directly to avoid alloc/free.
25353 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025354 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025355 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025356
25357 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25358 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025359 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25360 fc->l_listitems[ai].li_tv = argvars[i];
25361 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025362 }
25363 }
25364
Bram Moolenaar071d4272004-06-13 20:20:40 +000025365 /* Don't redraw while executing the function. */
25366 ++RedrawingDisabled;
25367 save_sourcing_name = sourcing_name;
25368 save_sourcing_lnum = sourcing_lnum;
25369 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025370 /* need space for function name + ("function " + 3) or "[number]" */
25371 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25372 + STRLEN(fp->uf_name) + 20;
25373 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374 if (sourcing_name != NULL)
25375 {
25376 if (save_sourcing_name != NULL
25377 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025378 sprintf((char *)sourcing_name, "%s[%d]..",
25379 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380 else
25381 STRCPY(sourcing_name, "function ");
25382 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25383
25384 if (p_verbose >= 12)
25385 {
25386 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025387 verbose_enter_scroll();
25388
Bram Moolenaar555b2802005-05-19 21:08:39 +000025389 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025390 if (p_verbose >= 14)
25391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025392 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025393 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025394 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025395 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025396
25397 msg_puts((char_u *)"(");
25398 for (i = 0; i < argcount; ++i)
25399 {
25400 if (i > 0)
25401 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025402 if (argvars[i].v_type == VAR_NUMBER)
25403 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404 else
25405 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025406 /* Do not want errors such as E724 here. */
25407 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025408 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025409 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025410 if (s != NULL)
25411 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025412 if (vim_strsize(s) > MSG_BUF_CLEN)
25413 {
25414 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25415 s = buf;
25416 }
25417 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025418 vim_free(tofree);
25419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025420 }
25421 }
25422 msg_puts((char_u *)")");
25423 }
25424 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025425
25426 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025427 --no_wait_return;
25428 }
25429 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025430#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025431 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025432 {
25433 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25434 func_do_profile(fp);
25435 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025436 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025437 {
25438 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025439 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025440 profile_zero(&fp->uf_tm_children);
25441 }
25442 script_prof_save(&wait_start);
25443 }
25444#endif
25445
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025447 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025448 save_did_emsg = did_emsg;
25449 did_emsg = FALSE;
25450
25451 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025452 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25454
25455 --RedrawingDisabled;
25456
25457 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025458 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025460 clear_tv(rettv);
25461 rettv->v_type = VAR_NUMBER;
25462 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463 }
25464
Bram Moolenaar05159a02005-02-26 23:04:13 +000025465#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025466 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025467 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025468 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025469 profile_end(&call_start);
25470 profile_sub_wait(&wait_start, &call_start);
25471 profile_add(&fp->uf_tm_total, &call_start);
25472 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025473 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025474 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025475 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25476 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025477 }
25478 }
25479#endif
25480
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 /* when being verbose, mention the return value */
25482 if (p_verbose >= 12)
25483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025485 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486
Bram Moolenaar071d4272004-06-13 20:20:40 +000025487 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025488 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025489 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025490 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025491 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025492 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025494 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025495 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025496 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025497 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025498
Bram Moolenaar555b2802005-05-19 21:08:39 +000025499 /* The value may be very long. Skip the middle part, so that we
25500 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025501 * truncate it at the end. Don't want errors such as E724 here. */
25502 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025503 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025504 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025505 if (s != NULL)
25506 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025507 if (vim_strsize(s) > MSG_BUF_CLEN)
25508 {
25509 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25510 s = buf;
25511 }
25512 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025513 vim_free(tofree);
25514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025515 }
25516 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025517
25518 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025519 --no_wait_return;
25520 }
25521
25522 vim_free(sourcing_name);
25523 sourcing_name = save_sourcing_name;
25524 sourcing_lnum = save_sourcing_lnum;
25525 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025526#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025527 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025528 script_prof_restore(&wait_start);
25529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530
25531 if (p_verbose >= 12 && sourcing_name != NULL)
25532 {
25533 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025534 verbose_enter_scroll();
25535
Bram Moolenaar555b2802005-05-19 21:08:39 +000025536 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025537 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025538
25539 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025540 --no_wait_return;
25541 }
25542
25543 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025544 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025545 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025546
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025547 /* If the a:000 list and the l: and a: dicts are not referenced we can
25548 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025549 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25550 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25551 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25552 {
25553 free_funccal(fc, FALSE);
25554 }
25555 else
25556 {
25557 hashitem_T *hi;
25558 listitem_T *li;
25559 int todo;
25560
25561 /* "fc" is still in use. This can happen when returning "a:000" or
25562 * assigning "l:" to a global variable.
25563 * Link "fc" in the list for garbage collection later. */
25564 fc->caller = previous_funccal;
25565 previous_funccal = fc;
25566
25567 /* Make a copy of the a: variables, since we didn't do that above. */
25568 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25569 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25570 {
25571 if (!HASHITEM_EMPTY(hi))
25572 {
25573 --todo;
25574 v = HI2DI(hi);
25575 copy_tv(&v->di_tv, &v->di_tv);
25576 }
25577 }
25578
25579 /* Make a copy of the a:000 items, since we didn't do that above. */
25580 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25581 copy_tv(&li->li_tv, &li->li_tv);
25582 }
25583}
25584
25585/*
25586 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025587 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025588 */
25589 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025590can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025591{
25592 return (fc->l_varlist.lv_copyID != copyID
25593 && fc->l_vars.dv_copyID != copyID
25594 && fc->l_avars.dv_copyID != copyID);
25595}
25596
25597/*
25598 * Free "fc" and what it contains.
25599 */
25600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025601free_funccal(
25602 funccall_T *fc,
25603 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025604{
25605 listitem_T *li;
25606
25607 /* The a: variables typevals may not have been allocated, only free the
25608 * allocated variables. */
25609 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25610
25611 /* free all l: variables */
25612 vars_clear(&fc->l_vars.dv_hashtab);
25613
25614 /* Free the a:000 variables if they were allocated. */
25615 if (free_val)
25616 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25617 clear_tv(&li->li_tv);
25618
25619 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620}
25621
25622/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025623 * Add a number variable "name" to dict "dp" with value "nr".
25624 */
25625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025626add_nr_var(
25627 dict_T *dp,
25628 dictitem_T *v,
25629 char *name,
25630 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025631{
25632 STRCPY(v->di_key, name);
25633 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25634 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25635 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025636 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025637 v->di_tv.vval.v_number = nr;
25638}
25639
25640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641 * ":return [expr]"
25642 */
25643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025644ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645{
25646 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025647 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 int returning = FALSE;
25649
25650 if (current_funccal == NULL)
25651 {
25652 EMSG(_("E133: :return not inside a function"));
25653 return;
25654 }
25655
25656 if (eap->skip)
25657 ++emsg_skip;
25658
25659 eap->nextcmd = NULL;
25660 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025661 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025662 {
25663 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025664 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025665 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025666 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025667 }
25668 /* It's safer to return also on error. */
25669 else if (!eap->skip)
25670 {
25671 /*
25672 * Return unless the expression evaluation has been cancelled due to an
25673 * aborting error, an interrupt, or an exception.
25674 */
25675 if (!aborting())
25676 returning = do_return(eap, FALSE, TRUE, NULL);
25677 }
25678
25679 /* When skipping or the return gets pending, advance to the next command
25680 * in this line (!returning). Otherwise, ignore the rest of the line.
25681 * Following lines will be ignored by get_func_line(). */
25682 if (returning)
25683 eap->nextcmd = NULL;
25684 else if (eap->nextcmd == NULL) /* no argument */
25685 eap->nextcmd = check_nextcmd(arg);
25686
25687 if (eap->skip)
25688 --emsg_skip;
25689}
25690
25691/*
25692 * Return from a function. Possibly makes the return pending. Also called
25693 * for a pending return at the ":endtry" or after returning from an extra
25694 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025695 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025696 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025697 * FALSE when the return gets pending.
25698 */
25699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025700do_return(
25701 exarg_T *eap,
25702 int reanimate,
25703 int is_cmd,
25704 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025705{
25706 int idx;
25707 struct condstack *cstack = eap->cstack;
25708
25709 if (reanimate)
25710 /* Undo the return. */
25711 current_funccal->returned = FALSE;
25712
25713 /*
25714 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25715 * not in its finally clause (which then is to be executed next) is found.
25716 * In this case, make the ":return" pending for execution at the ":endtry".
25717 * Otherwise, return normally.
25718 */
25719 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25720 if (idx >= 0)
25721 {
25722 cstack->cs_pending[idx] = CSTP_RETURN;
25723
25724 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025725 /* A pending return again gets pending. "rettv" points to an
25726 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025727 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025728 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025729 else
25730 {
25731 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025732 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025733 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025734 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025736 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025737 {
25738 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025739 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025740 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741 else
25742 EMSG(_(e_outofmem));
25743 }
25744 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025745 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746
25747 if (reanimate)
25748 {
25749 /* The pending return value could be overwritten by a ":return"
25750 * without argument in a finally clause; reset the default
25751 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025752 current_funccal->rettv->v_type = VAR_NUMBER;
25753 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754 }
25755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025756 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025757 }
25758 else
25759 {
25760 current_funccal->returned = TRUE;
25761
25762 /* If the return is carried out now, store the return value. For
25763 * a return immediately after reanimation, the value is already
25764 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025765 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025767 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025768 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025770 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025771 }
25772 }
25773
25774 return idx < 0;
25775}
25776
25777/*
25778 * Free the variable with a pending return value.
25779 */
25780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025781discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025782{
Bram Moolenaar33570922005-01-25 22:26:29 +000025783 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784}
25785
25786/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025787 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025788 * is an allocated string. Used by report_pending() for verbose messages.
25789 */
25790 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025791get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025792{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025793 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025794 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025795 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025797 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025798 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025799 if (s == NULL)
25800 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025801
25802 STRCPY(IObuff, ":return ");
25803 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25804 if (STRLEN(s) + 8 >= IOSIZE)
25805 STRCPY(IObuff + IOSIZE - 4, "...");
25806 vim_free(tofree);
25807 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808}
25809
25810/*
25811 * Get next function line.
25812 * Called by do_cmdline() to get the next line.
25813 * Returns allocated string, or NULL for end of function.
25814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025815 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025816get_func_line(
25817 int c UNUSED,
25818 void *cookie,
25819 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025820{
Bram Moolenaar33570922005-01-25 22:26:29 +000025821 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025822 ufunc_T *fp = fcp->func;
25823 char_u *retval;
25824 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825
25826 /* If breakpoints have been added/deleted need to check for it. */
25827 if (fcp->dbg_tick != debug_tick)
25828 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025829 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 sourcing_lnum);
25831 fcp->dbg_tick = debug_tick;
25832 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025833#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025834 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025835 func_line_end(cookie);
25836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025837
Bram Moolenaar05159a02005-02-26 23:04:13 +000025838 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025839 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25840 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025841 retval = NULL;
25842 else
25843 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025844 /* Skip NULL lines (continuation lines). */
25845 while (fcp->linenr < gap->ga_len
25846 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25847 ++fcp->linenr;
25848 if (fcp->linenr >= gap->ga_len)
25849 retval = NULL;
25850 else
25851 {
25852 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25853 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025854#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025855 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025856 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025857#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025859 }
25860
25861 /* Did we encounter a breakpoint? */
25862 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25863 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025864 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025865 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025866 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025867 sourcing_lnum);
25868 fcp->dbg_tick = debug_tick;
25869 }
25870
25871 return retval;
25872}
25873
Bram Moolenaar05159a02005-02-26 23:04:13 +000025874#if defined(FEAT_PROFILE) || defined(PROTO)
25875/*
25876 * Called when starting to read a function line.
25877 * "sourcing_lnum" must be correct!
25878 * When skipping lines it may not actually be executed, but we won't find out
25879 * until later and we need to store the time now.
25880 */
25881 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025882func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025883{
25884 funccall_T *fcp = (funccall_T *)cookie;
25885 ufunc_T *fp = fcp->func;
25886
25887 if (fp->uf_profiling && sourcing_lnum >= 1
25888 && sourcing_lnum <= fp->uf_lines.ga_len)
25889 {
25890 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025891 /* Skip continuation lines. */
25892 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25893 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025894 fp->uf_tml_execed = FALSE;
25895 profile_start(&fp->uf_tml_start);
25896 profile_zero(&fp->uf_tml_children);
25897 profile_get_wait(&fp->uf_tml_wait);
25898 }
25899}
25900
25901/*
25902 * Called when actually executing a function line.
25903 */
25904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025905func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025906{
25907 funccall_T *fcp = (funccall_T *)cookie;
25908 ufunc_T *fp = fcp->func;
25909
25910 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25911 fp->uf_tml_execed = TRUE;
25912}
25913
25914/*
25915 * Called when done with a function line.
25916 */
25917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025918func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025919{
25920 funccall_T *fcp = (funccall_T *)cookie;
25921 ufunc_T *fp = fcp->func;
25922
25923 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25924 {
25925 if (fp->uf_tml_execed)
25926 {
25927 ++fp->uf_tml_count[fp->uf_tml_idx];
25928 profile_end(&fp->uf_tml_start);
25929 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025930 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025931 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25932 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025933 }
25934 fp->uf_tml_idx = -1;
25935 }
25936}
25937#endif
25938
Bram Moolenaar071d4272004-06-13 20:20:40 +000025939/*
25940 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025941 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942 */
25943 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025944func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025945{
Bram Moolenaar33570922005-01-25 22:26:29 +000025946 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947
25948 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25949 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025950 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025951 || fcp->returned);
25952}
25953
25954/*
25955 * return TRUE if cookie indicates a function which "abort"s on errors.
25956 */
25957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025958func_has_abort(
25959 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025960{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025961 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025962}
25963
25964#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25965typedef enum
25966{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025967 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25968 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25969 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970} var_flavour_T;
25971
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025972static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025973
25974 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025975var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025976{
25977 char_u *p = varname;
25978
25979 if (ASCII_ISUPPER(*p))
25980 {
25981 while (*(++p))
25982 if (ASCII_ISLOWER(*p))
25983 return VAR_FLAVOUR_SESSION;
25984 return VAR_FLAVOUR_VIMINFO;
25985 }
25986 else
25987 return VAR_FLAVOUR_DEFAULT;
25988}
25989#endif
25990
25991#if defined(FEAT_VIMINFO) || defined(PROTO)
25992/*
25993 * Restore global vars that start with a capital from the viminfo file
25994 */
25995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025996read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997{
25998 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025999 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026000 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026001 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026002
26003 if (!writing && (find_viminfo_parameter('!') != NULL))
26004 {
26005 tab = vim_strchr(virp->vir_line + 1, '\t');
26006 if (tab != NULL)
26007 {
26008 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026009 switch (*tab)
26010 {
26011 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026012#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026013 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026014#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026015 case 'D': type = VAR_DICT; break;
26016 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026017 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026019
26020 tab = vim_strchr(tab, '\t');
26021 if (tab != NULL)
26022 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026023 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026024 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026025 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026027#ifdef FEAT_FLOAT
26028 else if (type == VAR_FLOAT)
26029 (void)string2float(tab + 1, &tv.vval.v_float);
26030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026031 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026032 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026033 if (type == VAR_DICT || type == VAR_LIST)
26034 {
26035 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26036
26037 if (etv == NULL)
26038 /* Failed to parse back the dict or list, use it as a
26039 * string. */
26040 tv.v_type = VAR_STRING;
26041 else
26042 {
26043 vim_free(tv.vval.v_string);
26044 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026045 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026046 }
26047 }
26048
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026049 /* when in a function use global variables */
26050 save_funccal = current_funccal;
26051 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026052 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026053 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026054
26055 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026056 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026057 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26058 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026059 }
26060 }
26061 }
26062
26063 return viminfo_readline(virp);
26064}
26065
26066/*
26067 * Write global vars that start with a capital to the viminfo file
26068 */
26069 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026070write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026071{
Bram Moolenaar33570922005-01-25 22:26:29 +000026072 hashitem_T *hi;
26073 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026074 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026075 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026076 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026077 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026078 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026079
26080 if (find_viminfo_parameter('!') == NULL)
26081 return;
26082
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026083 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026085 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026086 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026087 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026088 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026089 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026090 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026091 this_var = HI2DI(hi);
26092 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026093 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026094 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026095 {
26096 case VAR_STRING: s = "STR"; break;
26097 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026098 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026099 case VAR_DICT: s = "DIC"; break;
26100 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026101 case VAR_SPECIAL: s = "XPL"; break;
26102
26103 case VAR_UNKNOWN:
26104 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026105 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026106 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026107 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026108 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026109 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026110 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026111 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026112 if (p != NULL)
26113 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026114 vim_free(tofree);
26115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026116 }
26117 }
26118}
26119#endif
26120
26121#if defined(FEAT_SESSION) || defined(PROTO)
26122 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026123store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026124{
Bram Moolenaar33570922005-01-25 22:26:29 +000026125 hashitem_T *hi;
26126 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026127 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026128 char_u *p, *t;
26129
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026130 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026131 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026132 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026133 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026134 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026135 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026136 this_var = HI2DI(hi);
26137 if ((this_var->di_tv.v_type == VAR_NUMBER
26138 || this_var->di_tv.v_type == VAR_STRING)
26139 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026140 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026141 /* Escape special characters with a backslash. Turn a LF and
26142 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026143 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026144 (char_u *)"\\\"\n\r");
26145 if (p == NULL) /* out of memory */
26146 break;
26147 for (t = p; *t != NUL; ++t)
26148 if (*t == '\n')
26149 *t = 'n';
26150 else if (*t == '\r')
26151 *t = 'r';
26152 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026153 this_var->di_key,
26154 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26155 : ' ',
26156 p,
26157 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26158 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026159 || put_eol(fd) == FAIL)
26160 {
26161 vim_free(p);
26162 return FAIL;
26163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026164 vim_free(p);
26165 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026166#ifdef FEAT_FLOAT
26167 else if (this_var->di_tv.v_type == VAR_FLOAT
26168 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26169 {
26170 float_T f = this_var->di_tv.vval.v_float;
26171 int sign = ' ';
26172
26173 if (f < 0)
26174 {
26175 f = -f;
26176 sign = '-';
26177 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026178 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026179 this_var->di_key, sign, f) < 0)
26180 || put_eol(fd) == FAIL)
26181 return FAIL;
26182 }
26183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026184 }
26185 }
26186 return OK;
26187}
26188#endif
26189
Bram Moolenaar661b1822005-07-28 22:36:45 +000026190/*
26191 * Display script name where an item was last set.
26192 * Should only be invoked when 'verbose' is non-zero.
26193 */
26194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026195last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026196{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026197 char_u *p;
26198
Bram Moolenaar661b1822005-07-28 22:36:45 +000026199 if (scriptID != 0)
26200 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026201 p = home_replace_save(NULL, get_scriptname(scriptID));
26202 if (p != NULL)
26203 {
26204 verbose_enter();
26205 MSG_PUTS(_("\n\tLast set from "));
26206 MSG_PUTS(p);
26207 vim_free(p);
26208 verbose_leave();
26209 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026210 }
26211}
26212
Bram Moolenaard812df62008-11-09 12:46:09 +000026213/*
26214 * List v:oldfiles in a nice way.
26215 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026216 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026217ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026218{
26219 list_T *l = vimvars[VV_OLDFILES].vv_list;
26220 listitem_T *li;
26221 int nr = 0;
26222
26223 if (l == NULL)
26224 msg((char_u *)_("No old files"));
26225 else
26226 {
26227 msg_start();
26228 msg_scroll = TRUE;
26229 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26230 {
26231 msg_outnum((long)++nr);
26232 MSG_PUTS(": ");
26233 msg_outtrans(get_tv_string(&li->li_tv));
26234 msg_putchar('\n');
26235 out_flush(); /* output one line at a time */
26236 ui_breakcheck();
26237 }
26238 /* Assume "got_int" was set to truncate the listing. */
26239 got_int = FALSE;
26240
26241#ifdef FEAT_BROWSE_CMD
26242 if (cmdmod.browse)
26243 {
26244 quit_more = FALSE;
26245 nr = prompt_for_number(FALSE);
26246 msg_starthere();
26247 if (nr > 0)
26248 {
26249 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26250 (long)nr);
26251
26252 if (p != NULL)
26253 {
26254 p = expand_env_save(p);
26255 eap->arg = p;
26256 eap->cmdidx = CMD_edit;
26257 cmdmod.browse = FALSE;
26258 do_exedit(eap, NULL);
26259 vim_free(p);
26260 }
26261 }
26262 }
26263#endif
26264 }
26265}
26266
Bram Moolenaar53744302015-07-17 17:38:22 +020026267/* reset v:option_new, v:option_old and v:option_type */
26268 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026269reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026270{
26271 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26272 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26273 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26274}
26275
26276
Bram Moolenaar071d4272004-06-13 20:20:40 +000026277#endif /* FEAT_EVAL */
26278
Bram Moolenaar071d4272004-06-13 20:20:40 +000026279
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026280#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026281
26282#ifdef WIN3264
26283/*
26284 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26285 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026286static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26287static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26288static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026289
26290/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026291 * Get the short path (8.3) for the filename in "fnamep".
26292 * Only works for a valid file name.
26293 * When the path gets longer "fnamep" is changed and the allocated buffer
26294 * is put in "bufp".
26295 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26296 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026297 */
26298 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026299get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026300{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026301 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026302 char_u *newbuf;
26303
26304 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026305 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026306 if (l > len - 1)
26307 {
26308 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026309 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310 newbuf = vim_strnsave(*fnamep, l);
26311 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026312 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026313
26314 vim_free(*bufp);
26315 *fnamep = *bufp = newbuf;
26316
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026317 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026318 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026319 }
26320
26321 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026322 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026323}
26324
26325/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026326 * Get the short path (8.3) for the filename in "fname". The converted
26327 * path is returned in "bufp".
26328 *
26329 * Some of the directories specified in "fname" may not exist. This function
26330 * will shorten the existing directories at the beginning of the path and then
26331 * append the remaining non-existing path.
26332 *
26333 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026334 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026335 * bufp - Pointer to an allocated buffer for the filename.
26336 * fnamelen - Length of the filename pointed to by fname
26337 *
26338 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026339 */
26340 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026341shortpath_for_invalid_fname(
26342 char_u **fname,
26343 char_u **bufp,
26344 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026345{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026346 char_u *short_fname, *save_fname, *pbuf_unused;
26347 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026348 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026349 int old_len, len;
26350 int new_len, sfx_len;
26351 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026352
26353 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026354 old_len = *fnamelen;
26355 save_fname = vim_strnsave(*fname, old_len);
26356 pbuf_unused = NULL;
26357 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026358
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026359 endp = save_fname + old_len - 1; /* Find the end of the copy */
26360 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026361
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026362 /*
26363 * Try shortening the supplied path till it succeeds by removing one
26364 * directory at a time from the tail of the path.
26365 */
26366 len = 0;
26367 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026368 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026369 /* go back one path-separator */
26370 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26371 --endp;
26372 if (endp <= save_fname)
26373 break; /* processed the complete path */
26374
26375 /*
26376 * Replace the path separator with a NUL and try to shorten the
26377 * resulting path.
26378 */
26379 ch = *endp;
26380 *endp = 0;
26381 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026382 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026383 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26384 {
26385 retval = FAIL;
26386 goto theend;
26387 }
26388 *endp = ch; /* preserve the string */
26389
26390 if (len > 0)
26391 break; /* successfully shortened the path */
26392
26393 /* failed to shorten the path. Skip the path separator */
26394 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026395 }
26396
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026397 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026398 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026399 /*
26400 * Succeeded in shortening the path. Now concatenate the shortened
26401 * path with the remaining path at the tail.
26402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026403
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026404 /* Compute the length of the new path. */
26405 sfx_len = (int)(save_endp - endp) + 1;
26406 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026407
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026408 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026409 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026410 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026411 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026412 /* There is not enough space in the currently allocated string,
26413 * copy it to a buffer big enough. */
26414 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026415 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026416 {
26417 retval = FAIL;
26418 goto theend;
26419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026420 }
26421 else
26422 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026423 /* Transfer short_fname to the main buffer (it's big enough),
26424 * unless get_short_pathname() did its work in-place. */
26425 *fname = *bufp = save_fname;
26426 if (short_fname != save_fname)
26427 vim_strncpy(save_fname, short_fname, len);
26428 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026429 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026430
26431 /* concat the not-shortened part of the path */
26432 vim_strncpy(*fname + len, endp, sfx_len);
26433 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026434 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026435
26436theend:
26437 vim_free(pbuf_unused);
26438 vim_free(save_fname);
26439
26440 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026441}
26442
26443/*
26444 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026445 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026446 */
26447 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026448shortpath_for_partial(
26449 char_u **fnamep,
26450 char_u **bufp,
26451 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452{
26453 int sepcount, len, tflen;
26454 char_u *p;
26455 char_u *pbuf, *tfname;
26456 int hasTilde;
26457
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026458 /* Count up the path separators from the RHS.. so we know which part
26459 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026460 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026461 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026462 if (vim_ispathsep(*p))
26463 ++sepcount;
26464
26465 /* Need full path first (use expand_env() to remove a "~/") */
26466 hasTilde = (**fnamep == '~');
26467 if (hasTilde)
26468 pbuf = tfname = expand_env_save(*fnamep);
26469 else
26470 pbuf = tfname = FullName_save(*fnamep, FALSE);
26471
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026472 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026473
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026474 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26475 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026476
26477 if (len == 0)
26478 {
26479 /* Don't have a valid filename, so shorten the rest of the
26480 * path if we can. This CAN give us invalid 8.3 filenames, but
26481 * there's not a lot of point in guessing what it might be.
26482 */
26483 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026484 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26485 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026486 }
26487
26488 /* Count the paths backward to find the beginning of the desired string. */
26489 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026490 {
26491#ifdef FEAT_MBYTE
26492 if (has_mbyte)
26493 p -= mb_head_off(tfname, p);
26494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026495 if (vim_ispathsep(*p))
26496 {
26497 if (sepcount == 0 || (hasTilde && sepcount == 1))
26498 break;
26499 else
26500 sepcount --;
26501 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026503 if (hasTilde)
26504 {
26505 --p;
26506 if (p >= tfname)
26507 *p = '~';
26508 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026509 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026510 }
26511 else
26512 ++p;
26513
26514 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26515 vim_free(*bufp);
26516 *fnamelen = (int)STRLEN(p);
26517 *bufp = pbuf;
26518 *fnamep = p;
26519
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026520 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026521}
26522#endif /* WIN3264 */
26523
26524/*
26525 * Adjust a filename, according to a string of modifiers.
26526 * *fnamep must be NUL terminated when called. When returning, the length is
26527 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026528 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026529 * When there is an error, *fnamep is set to NULL.
26530 */
26531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026532modify_fname(
26533 char_u *src, /* string with modifiers */
26534 int *usedlen, /* characters after src that are used */
26535 char_u **fnamep, /* file name so far */
26536 char_u **bufp, /* buffer for allocated file name or NULL */
26537 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026538{
26539 int valid = 0;
26540 char_u *tail;
26541 char_u *s, *p, *pbuf;
26542 char_u dirname[MAXPATHL];
26543 int c;
26544 int has_fullname = 0;
26545#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026546 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026547 int has_shortname = 0;
26548#endif
26549
26550repeat:
26551 /* ":p" - full path/file_name */
26552 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26553 {
26554 has_fullname = 1;
26555
26556 valid |= VALID_PATH;
26557 *usedlen += 2;
26558
26559 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26560 if ((*fnamep)[0] == '~'
26561#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26562 && ((*fnamep)[1] == '/'
26563# ifdef BACKSLASH_IN_FILENAME
26564 || (*fnamep)[1] == '\\'
26565# endif
26566 || (*fnamep)[1] == NUL)
26567
26568#endif
26569 )
26570 {
26571 *fnamep = expand_env_save(*fnamep);
26572 vim_free(*bufp); /* free any allocated file name */
26573 *bufp = *fnamep;
26574 if (*fnamep == NULL)
26575 return -1;
26576 }
26577
26578 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026579 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026580 {
26581 if (vim_ispathsep(*p)
26582 && p[1] == '.'
26583 && (p[2] == NUL
26584 || vim_ispathsep(p[2])
26585 || (p[2] == '.'
26586 && (p[3] == NUL || vim_ispathsep(p[3])))))
26587 break;
26588 }
26589
26590 /* FullName_save() is slow, don't use it when not needed. */
26591 if (*p != NUL || !vim_isAbsName(*fnamep))
26592 {
26593 *fnamep = FullName_save(*fnamep, *p != NUL);
26594 vim_free(*bufp); /* free any allocated file name */
26595 *bufp = *fnamep;
26596 if (*fnamep == NULL)
26597 return -1;
26598 }
26599
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026600#ifdef WIN3264
26601# if _WIN32_WINNT >= 0x0500
26602 if (vim_strchr(*fnamep, '~') != NULL)
26603 {
26604 /* Expand 8.3 filename to full path. Needed to make sure the same
26605 * file does not have two different names.
26606 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26607 p = alloc(_MAX_PATH + 1);
26608 if (p != NULL)
26609 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026610 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026611 {
26612 vim_free(*bufp);
26613 *bufp = *fnamep = p;
26614 }
26615 else
26616 vim_free(p);
26617 }
26618 }
26619# endif
26620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026621 /* Append a path separator to a directory. */
26622 if (mch_isdir(*fnamep))
26623 {
26624 /* Make room for one or two extra characters. */
26625 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26626 vim_free(*bufp); /* free any allocated file name */
26627 *bufp = *fnamep;
26628 if (*fnamep == NULL)
26629 return -1;
26630 add_pathsep(*fnamep);
26631 }
26632 }
26633
26634 /* ":." - path relative to the current directory */
26635 /* ":~" - path relative to the home directory */
26636 /* ":8" - shortname path - postponed till after */
26637 while (src[*usedlen] == ':'
26638 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26639 {
26640 *usedlen += 2;
26641 if (c == '8')
26642 {
26643#ifdef WIN3264
26644 has_shortname = 1; /* Postpone this. */
26645#endif
26646 continue;
26647 }
26648 pbuf = NULL;
26649 /* Need full path first (use expand_env() to remove a "~/") */
26650 if (!has_fullname)
26651 {
26652 if (c == '.' && **fnamep == '~')
26653 p = pbuf = expand_env_save(*fnamep);
26654 else
26655 p = pbuf = FullName_save(*fnamep, FALSE);
26656 }
26657 else
26658 p = *fnamep;
26659
26660 has_fullname = 0;
26661
26662 if (p != NULL)
26663 {
26664 if (c == '.')
26665 {
26666 mch_dirname(dirname, MAXPATHL);
26667 s = shorten_fname(p, dirname);
26668 if (s != NULL)
26669 {
26670 *fnamep = s;
26671 if (pbuf != NULL)
26672 {
26673 vim_free(*bufp); /* free any allocated file name */
26674 *bufp = pbuf;
26675 pbuf = NULL;
26676 }
26677 }
26678 }
26679 else
26680 {
26681 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26682 /* Only replace it when it starts with '~' */
26683 if (*dirname == '~')
26684 {
26685 s = vim_strsave(dirname);
26686 if (s != NULL)
26687 {
26688 *fnamep = s;
26689 vim_free(*bufp);
26690 *bufp = s;
26691 }
26692 }
26693 }
26694 vim_free(pbuf);
26695 }
26696 }
26697
26698 tail = gettail(*fnamep);
26699 *fnamelen = (int)STRLEN(*fnamep);
26700
26701 /* ":h" - head, remove "/file_name", can be repeated */
26702 /* Don't remove the first "/" or "c:\" */
26703 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26704 {
26705 valid |= VALID_HEAD;
26706 *usedlen += 2;
26707 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026708 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026709 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026710 *fnamelen = (int)(tail - *fnamep);
26711#ifdef VMS
26712 if (*fnamelen > 0)
26713 *fnamelen += 1; /* the path separator is part of the path */
26714#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026715 if (*fnamelen == 0)
26716 {
26717 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26718 p = vim_strsave((char_u *)".");
26719 if (p == NULL)
26720 return -1;
26721 vim_free(*bufp);
26722 *bufp = *fnamep = tail = p;
26723 *fnamelen = 1;
26724 }
26725 else
26726 {
26727 while (tail > s && !after_pathsep(s, tail))
26728 mb_ptr_back(*fnamep, tail);
26729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026730 }
26731
26732 /* ":8" - shortname */
26733 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26734 {
26735 *usedlen += 2;
26736#ifdef WIN3264
26737 has_shortname = 1;
26738#endif
26739 }
26740
26741#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026742 /*
26743 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026744 */
26745 if (has_shortname)
26746 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026747 /* Copy the string if it is shortened by :h and when it wasn't copied
26748 * yet, because we are going to change it in place. Avoids changing
26749 * the buffer name for "%:8". */
26750 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026751 {
26752 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026753 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026754 return -1;
26755 vim_free(*bufp);
26756 *bufp = *fnamep = p;
26757 }
26758
26759 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026760 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026761 if (!has_fullname && !vim_isAbsName(*fnamep))
26762 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026763 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026764 return -1;
26765 }
26766 else
26767 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026768 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026769
Bram Moolenaardc935552011-08-17 15:23:23 +020026770 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026771 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026772 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026773 return -1;
26774
26775 if (l == 0)
26776 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026777 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026778 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026779 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026780 return -1;
26781 }
26782 *fnamelen = l;
26783 }
26784 }
26785#endif /* WIN3264 */
26786
26787 /* ":t" - tail, just the basename */
26788 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26789 {
26790 *usedlen += 2;
26791 *fnamelen -= (int)(tail - *fnamep);
26792 *fnamep = tail;
26793 }
26794
26795 /* ":e" - extension, can be repeated */
26796 /* ":r" - root, without extension, can be repeated */
26797 while (src[*usedlen] == ':'
26798 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26799 {
26800 /* find a '.' in the tail:
26801 * - for second :e: before the current fname
26802 * - otherwise: The last '.'
26803 */
26804 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26805 s = *fnamep - 2;
26806 else
26807 s = *fnamep + *fnamelen - 1;
26808 for ( ; s > tail; --s)
26809 if (s[0] == '.')
26810 break;
26811 if (src[*usedlen + 1] == 'e') /* :e */
26812 {
26813 if (s > tail)
26814 {
26815 *fnamelen += (int)(*fnamep - (s + 1));
26816 *fnamep = s + 1;
26817#ifdef VMS
26818 /* cut version from the extension */
26819 s = *fnamep + *fnamelen - 1;
26820 for ( ; s > *fnamep; --s)
26821 if (s[0] == ';')
26822 break;
26823 if (s > *fnamep)
26824 *fnamelen = s - *fnamep;
26825#endif
26826 }
26827 else if (*fnamep <= tail)
26828 *fnamelen = 0;
26829 }
26830 else /* :r */
26831 {
26832 if (s > tail) /* remove one extension */
26833 *fnamelen = (int)(s - *fnamep);
26834 }
26835 *usedlen += 2;
26836 }
26837
26838 /* ":s?pat?foo?" - substitute */
26839 /* ":gs?pat?foo?" - global substitute */
26840 if (src[*usedlen] == ':'
26841 && (src[*usedlen + 1] == 's'
26842 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26843 {
26844 char_u *str;
26845 char_u *pat;
26846 char_u *sub;
26847 int sep;
26848 char_u *flags;
26849 int didit = FALSE;
26850
26851 flags = (char_u *)"";
26852 s = src + *usedlen + 2;
26853 if (src[*usedlen + 1] == 'g')
26854 {
26855 flags = (char_u *)"g";
26856 ++s;
26857 }
26858
26859 sep = *s++;
26860 if (sep)
26861 {
26862 /* find end of pattern */
26863 p = vim_strchr(s, sep);
26864 if (p != NULL)
26865 {
26866 pat = vim_strnsave(s, (int)(p - s));
26867 if (pat != NULL)
26868 {
26869 s = p + 1;
26870 /* find end of substitution */
26871 p = vim_strchr(s, sep);
26872 if (p != NULL)
26873 {
26874 sub = vim_strnsave(s, (int)(p - s));
26875 str = vim_strnsave(*fnamep, *fnamelen);
26876 if (sub != NULL && str != NULL)
26877 {
26878 *usedlen = (int)(p + 1 - src);
26879 s = do_string_sub(str, pat, sub, flags);
26880 if (s != NULL)
26881 {
26882 *fnamep = s;
26883 *fnamelen = (int)STRLEN(s);
26884 vim_free(*bufp);
26885 *bufp = s;
26886 didit = TRUE;
26887 }
26888 }
26889 vim_free(sub);
26890 vim_free(str);
26891 }
26892 vim_free(pat);
26893 }
26894 }
26895 /* after using ":s", repeat all the modifiers */
26896 if (didit)
26897 goto repeat;
26898 }
26899 }
26900
Bram Moolenaar26df0922014-02-23 23:39:13 +010026901 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26902 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026903 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026904 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026905 if (c != NUL)
26906 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026907 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026908 if (c != NUL)
26909 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026910 if (p == NULL)
26911 return -1;
26912 vim_free(*bufp);
26913 *bufp = *fnamep = p;
26914 *fnamelen = (int)STRLEN(p);
26915 *usedlen += 2;
26916 }
26917
Bram Moolenaar071d4272004-06-13 20:20:40 +000026918 return valid;
26919}
26920
26921/*
26922 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26923 * "flags" can be "g" to do a global substitute.
26924 * Returns an allocated string, NULL for error.
26925 */
26926 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026927do_string_sub(
26928 char_u *str,
26929 char_u *pat,
26930 char_u *sub,
26931 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026932{
26933 int sublen;
26934 regmatch_T regmatch;
26935 int i;
26936 int do_all;
26937 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026938 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026939 garray_T ga;
26940 char_u *ret;
26941 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026942 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026943
26944 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26945 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026946 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026947
26948 ga_init2(&ga, 1, 200);
26949
26950 do_all = (flags[0] == 'g');
26951
26952 regmatch.rm_ic = p_ic;
26953 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26954 if (regmatch.regprog != NULL)
26955 {
26956 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026957 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026958 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26959 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026960 /* Skip empty match except for first match. */
26961 if (regmatch.startp[0] == regmatch.endp[0])
26962 {
26963 if (zero_width == regmatch.startp[0])
26964 {
26965 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026966 i = MB_PTR2LEN(tail);
26967 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26968 (size_t)i);
26969 ga.ga_len += i;
26970 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026971 continue;
26972 }
26973 zero_width = regmatch.startp[0];
26974 }
26975
Bram Moolenaar071d4272004-06-13 20:20:40 +000026976 /*
26977 * Get some space for a temporary buffer to do the substitution
26978 * into. It will contain:
26979 * - The text up to where the match is.
26980 * - The substituted text.
26981 * - The text after the match.
26982 */
26983 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026984 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026985 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26986 {
26987 ga_clear(&ga);
26988 break;
26989 }
26990
26991 /* copy the text up to where the match is */
26992 i = (int)(regmatch.startp[0] - tail);
26993 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26994 /* add the substituted text */
26995 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26996 + ga.ga_len + i, TRUE, TRUE, FALSE);
26997 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026998 tail = regmatch.endp[0];
26999 if (*tail == NUL)
27000 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027001 if (!do_all)
27002 break;
27003 }
27004
27005 if (ga.ga_data != NULL)
27006 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27007
Bram Moolenaar473de612013-06-08 18:19:48 +020027008 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027009 }
27010
27011 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27012 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027013 if (p_cpo == empty_option)
27014 p_cpo = save_cpo;
27015 else
27016 /* Darn, evaluating {sub} expression changed the value. */
27017 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027018
27019 return ret;
27020}
27021
27022#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */