blob: e74ccfa33eb1b5f719b22736237e0dc401b4a3dc [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 Moolenaar7480b5c2005-01-16 22:07:38 +000037 * Structure returned by get_lval() and used by set_var_lval().
38 * For a plain name:
39 * "name" points to the variable name.
40 * "exp_name" is NULL.
41 * "tv" is NULL
42 * For a magic braces name:
43 * "name" points to the expanded variable name.
44 * "exp_name" is non-NULL, to be freed later.
45 * "tv" is NULL
46 * For an index in a list:
47 * "name" points to the (expanded) variable name.
48 * "exp_name" NULL or non-NULL, to be freed later.
49 * "tv" points to the (first) list item value
50 * "li" points to the (first) list item
51 * "range", "n1", "n2" and "empty2" indicate what items are used.
52 * For an existing Dict item:
53 * "name" points to the (expanded) variable name.
54 * "exp_name" NULL or non-NULL, to be freed later.
55 * "tv" points to the dict item value
56 * "newkey" is NULL
57 * For a non-existing Dict item:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000060 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000061 * "newkey" is the key for the new item.
62 */
63typedef struct lval_S
64{
65 char_u *ll_name; /* start of variable name (can be NULL) */
66 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000067 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000068 isn't NULL it's the Dict to which to add
69 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000070 listitem_T *ll_li; /* The list item or NULL. */
71 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 int ll_range; /* TRUE when a [i:j] range was used */
73 long ll_n1; /* First index for list */
74 long ll_n2; /* Second index for list range */
75 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000076 dict_T *ll_dict; /* The Dictionary or NULL */
77 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000078 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000081static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000082static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000083static char *e_undefvar = N_("E121: Undefined variable: %s");
84static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000085static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000086static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000087static char *e_listreq = N_("E714: List required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +020088#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020089static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +020090#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +000091static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000092static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
93static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
94static char *e_funcdict = N_("E717: Dictionary entry already exists");
95static char *e_funcref = N_("E718: Funcref required");
96static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
97static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +000098static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +000099static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200100#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200101static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200102#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000103
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100104#define NAMESPACE_CHAR (char_u *)"abglstvw"
105
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200106static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000107#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108
109/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000110 * Old Vim variables such as "v:version" are also available without the "v:".
111 * Also in functions. We need a special hashtable for them.
112 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000113static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000114
115/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000116 * When recursively copying lists and dicts we need to remember which ones we
117 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000118 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000119 */
120static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +0200121
Bram Moolenaard9fba312005-06-26 22:34:35 +0000122/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000123 * Array to hold the hashtab with variables local to each sourced script.
124 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000126typedef struct
127{
128 dictitem_T sv_var;
129 dict_T sv_dict;
130} scriptvar_T;
131
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200132static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
133#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
134#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136static int echo_attr = 0; /* attributes used for ":echo" */
137
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000138/* Values for trans_function_name() argument: */
139#define TFN_INT 1 /* internal function name OK */
140#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100141#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
142
143/* Values for get_lval() flags argument: */
144#define GLV_QUIET TFN_QUIET /* no error messages */
145#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000146
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147/*
148 * Structure to hold info for a user function.
149 */
150typedef struct ufunc ufunc_T;
151
152struct ufunc
153{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000154 int uf_varargs; /* variable nr of arguments */
155 int uf_flags;
156 int uf_calls; /* nr of active calls */
157 garray_T uf_args; /* arguments */
158 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000159#ifdef FEAT_PROFILE
160 int uf_profiling; /* TRUE when func is being profiled */
161 /* profiling the function as a whole */
162 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000163 proftime_T uf_tm_total; /* time spent in function + children */
164 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000165 proftime_T uf_tm_children; /* time spent in children this call */
166 /* profiling the function per line */
167 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000168 proftime_T *uf_tml_total; /* time spent in a line + children */
169 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000170 proftime_T uf_tml_start; /* start time for current line */
171 proftime_T uf_tml_children; /* time spent in children for this line */
172 proftime_T uf_tml_wait; /* start wait time for current line */
173 int uf_tml_idx; /* index of line being timed; -1 if none */
174 int uf_tml_execed; /* line being timed was executed */
175#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000176 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000178 int uf_refcount; /* for numbered function: reference count */
179 char_u uf_name[1]; /* name of function (actually longer); can
180 start with <SNR>123_ (<SNR> is K_SPECIAL
181 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182};
183
184/* function flags */
185#define FC_ABORT 1 /* abort function on error */
186#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000187#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
189/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000190 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000192static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000194/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000195static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
196
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100197/* List heads for garbage collection. Although there can be a reference loop
198 * from partial to dict to partial, we don't need to keep track of the partial,
199 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000200static list_T *first_list = NULL; /* list of all lists */
201
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000202/* From user function to hashitem and back. */
203static ufunc_T dumuf;
204#define UF2HIKEY(fp) ((fp)->uf_name)
205#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
206#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
207
208#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
209#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaar33570922005-01-25 22:26:29 +0000211#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
212#define VAR_SHORT_LEN 20 /* short variable name length */
213#define FIXVAR_CNT 12 /* number of fixed variables */
214
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000216typedef struct funccall_S funccall_T;
217
218struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219{
220 ufunc_T *func; /* function being called */
221 int linenr; /* next line to be executed */
222 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000223 struct /* fixed variables for arguments */
224 {
225 dictitem_T var; /* variable (without room for name) */
226 char_u room[VAR_SHORT_LEN]; /* room for the name */
227 } fixvar[FIXVAR_CNT];
228 dict_T l_vars; /* l: local function variables */
229 dictitem_T l_vars_var; /* variable for l: scope */
230 dict_T l_avars; /* a: argument variables */
231 dictitem_T l_avars_var; /* variable for a: scope */
232 list_T l_varlist; /* list for a:000 */
233 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
234 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 linenr_T breakpoint; /* next line with breakpoint or zero */
236 int dbg_tick; /* debug_tick when breakpoint was set */
237 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000238#ifdef FEAT_PROFILE
239 proftime_T prof_child; /* time spent in a child */
240#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000241 funccall_T *caller; /* calling function or NULL */
242};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
244/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000245 * Info used by a ":for" loop.
246 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000247typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248{
249 int fi_semicolon; /* TRUE if ending in '; var]' */
250 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000251 listwatch_T fi_lw; /* keep an eye on the item used. */
252 list_T *fi_list; /* list being used */
253} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000254
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000255/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000256 * Struct used by trans_function_name()
257 */
258typedef struct
259{
Bram Moolenaar33570922005-01-25 22:26:29 +0000260 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000261 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000262 dictitem_T *fd_di; /* Dictionary item used */
263} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000264
Bram Moolenaara7043832005-01-21 11:56:39 +0000265
266/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 * Array to hold the value of v: variables.
268 * The value is in a dictitem, so that it can also be used in the v: scope.
269 * The reason to use this table anyway is for very quick access to the
270 * variables with the VV_ defines.
271 */
272#include "version.h"
273
274/* values for vv_flags: */
275#define VV_COMPAT 1 /* compatible, also used without "v:" */
276#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000277#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100279#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000280
281static struct vimvar
282{
283 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100284 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000285 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
286} vimvars[VV_LEN] =
287{
288 /*
289 * The order here must match the VV_ defines in vim.h!
290 * Initializing a union does not work, leave tv.vval empty to get zero's.
291 */
292 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
293 {VV_NAME("count1", VAR_NUMBER), VV_RO},
294 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
295 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
296 {VV_NAME("warningmsg", VAR_STRING), 0},
297 {VV_NAME("statusmsg", VAR_STRING), 0},
298 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
299 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
300 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
301 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
302 {VV_NAME("termresponse", VAR_STRING), VV_RO},
303 {VV_NAME("fname", VAR_STRING), VV_RO},
304 {VV_NAME("lang", VAR_STRING), VV_RO},
305 {VV_NAME("lc_time", VAR_STRING), VV_RO},
306 {VV_NAME("ctype", VAR_STRING), VV_RO},
307 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
308 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
309 {VV_NAME("fname_in", VAR_STRING), VV_RO},
310 {VV_NAME("fname_out", VAR_STRING), VV_RO},
311 {VV_NAME("fname_new", VAR_STRING), VV_RO},
312 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
313 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
314 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
315 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
316 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
317 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
318 {VV_NAME("progname", VAR_STRING), VV_RO},
319 {VV_NAME("servername", VAR_STRING), VV_RO},
320 {VV_NAME("dying", VAR_NUMBER), VV_RO},
321 {VV_NAME("exception", VAR_STRING), VV_RO},
322 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
323 {VV_NAME("register", VAR_STRING), VV_RO},
324 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
325 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000326 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
327 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000328 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000329 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
330 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000331 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
332 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200333 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000334 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
335 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000337 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000338 {VV_NAME("swapname", VAR_STRING), VV_RO},
339 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000340 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200341 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000342 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200343 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000344 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
345 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000346 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000347 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100348 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000349 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200350 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200351 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200352 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200353 {VV_NAME("option_new", VAR_STRING), VV_RO},
354 {VV_NAME("option_old", VAR_STRING), VV_RO},
355 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100356 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100357 {VV_NAME("false", VAR_SPECIAL), VV_RO},
358 {VV_NAME("true", VAR_SPECIAL), VV_RO},
359 {VV_NAME("null", VAR_SPECIAL), VV_RO},
360 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100361 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200362 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200371#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000372#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000373
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200374static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000375#define vimvarht vimvardict.dv_hashtab
376
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100377static void prepare_vimvar(int idx, typval_T *save_tv);
378static void restore_vimvar(int idx, typval_T *save_tv);
379static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
380static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
381static char_u *skip_var_one(char_u *arg);
382static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
383static void list_glob_vars(int *first);
384static void list_buf_vars(int *first);
385static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000386#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100387static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000388#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100389static void list_vim_vars(int *first);
390static void list_script_vars(int *first);
391static void list_func_vars(int *first);
392static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
393static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
394static int check_changedtick(char_u *arg);
395static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
396static void clear_lval(lval_T *lp);
397static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
398static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
399static void list_fix_watch(list_T *l, listitem_T *item);
400static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
401static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
402static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
403static void item_lock(typval_T *tv, int deep, int lock);
404static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000405
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100407static int eval2(char_u **arg, typval_T *rettv, int evaluate);
408static int eval3(char_u **arg, typval_T *rettv, int evaluate);
409static int eval4(char_u **arg, typval_T *rettv, int evaluate);
410static int eval5(char_u **arg, typval_T *rettv, int evaluate);
411static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
412static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000413
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100414static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
415static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
416static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
417static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
418static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200419static void list_free_contents(list_T *l);
420static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100421static long list_len(list_T *l);
422static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100423static long list_find_nr(list_T *l, long idx, int *errorp);
424static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100425static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
426static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
427static list_T *list_copy(list_T *orig, int deep, int copyID);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200428static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
429static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap);
430static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100431static int free_unref_items(int copyID);
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200432static int get_function_args(char_u **argp, char_u endchar, garray_T *newargs, int *varargs, int skip);
433static int get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100434static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100435static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100437static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
438static 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 +0100439static void emsg_funcname(char *ermsg, char_u *name);
440static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000441
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000442#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100443static void f_abs(typval_T *argvars, typval_T *rettv);
444static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000445#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100446static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100447static void f_and(typval_T *argvars, typval_T *rettv);
448static void f_append(typval_T *argvars, typval_T *rettv);
449static void f_argc(typval_T *argvars, typval_T *rettv);
450static void f_argidx(typval_T *argvars, typval_T *rettv);
451static void f_arglistid(typval_T *argvars, typval_T *rettv);
452static void f_argv(typval_T *argvars, typval_T *rettv);
453static void f_assert_equal(typval_T *argvars, typval_T *rettv);
454static void f_assert_exception(typval_T *argvars, typval_T *rettv);
455static void f_assert_fails(typval_T *argvars, typval_T *rettv);
456static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200457static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200458static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
459static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100460static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static void f_asin(typval_T *argvars, typval_T *rettv);
463static void f_atan(typval_T *argvars, typval_T *rettv);
464static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100466static void f_browse(typval_T *argvars, typval_T *rettv);
467static void f_browsedir(typval_T *argvars, typval_T *rettv);
468static void f_bufexists(typval_T *argvars, typval_T *rettv);
469static void f_buflisted(typval_T *argvars, typval_T *rettv);
470static void f_bufloaded(typval_T *argvars, typval_T *rettv);
471static void f_bufname(typval_T *argvars, typval_T *rettv);
472static void f_bufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb3619a92016-06-04 17:58:52 +0200473static void f_bufwinid(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
475static void f_byte2line(typval_T *argvars, typval_T *rettv);
476static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
477static void f_byteidx(typval_T *argvars, typval_T *rettv);
478static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
479static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000480#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100481static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100483#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100484static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100485static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
486static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100487static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100488static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100489static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100490static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100491static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
492static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100493static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100494static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100495static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
496static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100497static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100498static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100499#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100500static void f_changenr(typval_T *argvars, typval_T *rettv);
501static void f_char2nr(typval_T *argvars, typval_T *rettv);
502static void f_cindent(typval_T *argvars, typval_T *rettv);
503static void f_clearmatches(typval_T *argvars, typval_T *rettv);
504static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000505#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100506static void f_complete(typval_T *argvars, typval_T *rettv);
507static void f_complete_add(typval_T *argvars, typval_T *rettv);
508static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100510static void f_confirm(typval_T *argvars, typval_T *rettv);
511static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100513static void f_cos(typval_T *argvars, typval_T *rettv);
514static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100516static void f_count(typval_T *argvars, typval_T *rettv);
517static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
518static void f_cursor(typval_T *argsvars, typval_T *rettv);
519static void f_deepcopy(typval_T *argvars, typval_T *rettv);
520static void f_delete(typval_T *argvars, typval_T *rettv);
521static void f_did_filetype(typval_T *argvars, typval_T *rettv);
522static void f_diff_filler(typval_T *argvars, typval_T *rettv);
523static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
524static void f_empty(typval_T *argvars, typval_T *rettv);
525static void f_escape(typval_T *argvars, typval_T *rettv);
526static void f_eval(typval_T *argvars, typval_T *rettv);
527static void f_eventhandler(typval_T *argvars, typval_T *rettv);
528static void f_executable(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79815f12016-07-09 17:07:29 +0200529static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_exepath(typval_T *argvars, typval_T *rettv);
531static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200532#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100535static void f_expand(typval_T *argvars, typval_T *rettv);
536static void f_extend(typval_T *argvars, typval_T *rettv);
537static void f_feedkeys(typval_T *argvars, typval_T *rettv);
538static void f_filereadable(typval_T *argvars, typval_T *rettv);
539static void f_filewritable(typval_T *argvars, typval_T *rettv);
540static void f_filter(typval_T *argvars, typval_T *rettv);
541static void f_finddir(typval_T *argvars, typval_T *rettv);
542static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_float2nr(typval_T *argvars, typval_T *rettv);
545static void f_floor(typval_T *argvars, typval_T *rettv);
546static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100548static void f_fnameescape(typval_T *argvars, typval_T *rettv);
549static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
550static void f_foldclosed(typval_T *argvars, typval_T *rettv);
551static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
552static void f_foldlevel(typval_T *argvars, typval_T *rettv);
553static void f_foldtext(typval_T *argvars, typval_T *rettv);
554static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
555static void f_foreground(typval_T *argvars, typval_T *rettv);
556static void f_function(typval_T *argvars, typval_T *rettv);
557static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
558static void f_get(typval_T *argvars, typval_T *rettv);
559static void f_getbufline(typval_T *argvars, typval_T *rettv);
560static void f_getbufvar(typval_T *argvars, typval_T *rettv);
561static void f_getchar(typval_T *argvars, typval_T *rettv);
562static void f_getcharmod(typval_T *argvars, typval_T *rettv);
563static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
564static void f_getcmdline(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200565#if defined(FEAT_CMDL_COMPL)
566static void f_getcompletion(typval_T *argvars, typval_T *rettv);
567#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100568static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
569static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
570static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
571static void f_getcwd(typval_T *argvars, typval_T *rettv);
572static void f_getfontname(typval_T *argvars, typval_T *rettv);
573static void f_getfperm(typval_T *argvars, typval_T *rettv);
574static void f_getfsize(typval_T *argvars, typval_T *rettv);
575static void f_getftime(typval_T *argvars, typval_T *rettv);
576static void f_getftype(typval_T *argvars, typval_T *rettv);
577static void f_getline(typval_T *argvars, typval_T *rettv);
578static void f_getmatches(typval_T *argvars, typval_T *rettv);
579static void f_getpid(typval_T *argvars, typval_T *rettv);
580static void f_getcurpos(typval_T *argvars, typval_T *rettv);
581static void f_getpos(typval_T *argvars, typval_T *rettv);
582static void f_getqflist(typval_T *argvars, typval_T *rettv);
583static void f_getreg(typval_T *argvars, typval_T *rettv);
584static void f_getregtype(typval_T *argvars, typval_T *rettv);
585static void f_gettabvar(typval_T *argvars, typval_T *rettv);
586static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
587static void f_getwinposx(typval_T *argvars, typval_T *rettv);
588static void f_getwinposy(typval_T *argvars, typval_T *rettv);
589static void f_getwinvar(typval_T *argvars, typval_T *rettv);
590static void f_glob(typval_T *argvars, typval_T *rettv);
591static void f_globpath(typval_T *argvars, typval_T *rettv);
592static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
593static void f_has(typval_T *argvars, typval_T *rettv);
594static void f_has_key(typval_T *argvars, typval_T *rettv);
595static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
596static void f_hasmapto(typval_T *argvars, typval_T *rettv);
597static void f_histadd(typval_T *argvars, typval_T *rettv);
598static void f_histdel(typval_T *argvars, typval_T *rettv);
599static void f_histget(typval_T *argvars, typval_T *rettv);
600static void f_histnr(typval_T *argvars, typval_T *rettv);
601static void f_hlID(typval_T *argvars, typval_T *rettv);
602static void f_hlexists(typval_T *argvars, typval_T *rettv);
603static void f_hostname(typval_T *argvars, typval_T *rettv);
604static void f_iconv(typval_T *argvars, typval_T *rettv);
605static void f_indent(typval_T *argvars, typval_T *rettv);
606static void f_index(typval_T *argvars, typval_T *rettv);
607static void f_input(typval_T *argvars, typval_T *rettv);
608static void f_inputdialog(typval_T *argvars, typval_T *rettv);
609static void f_inputlist(typval_T *argvars, typval_T *rettv);
610static void f_inputrestore(typval_T *argvars, typval_T *rettv);
611static void f_inputsave(typval_T *argvars, typval_T *rettv);
612static void f_inputsecret(typval_T *argvars, typval_T *rettv);
613static void f_insert(typval_T *argvars, typval_T *rettv);
614static void f_invert(typval_T *argvars, typval_T *rettv);
615static void f_isdirectory(typval_T *argvars, typval_T *rettv);
616static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100617#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
618static void f_isnan(typval_T *argvars, typval_T *rettv);
619#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100620static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100621#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100622static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100623static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100624static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100625static void f_job_start(typval_T *argvars, typval_T *rettv);
626static void f_job_stop(typval_T *argvars, typval_T *rettv);
627static void f_job_status(typval_T *argvars, typval_T *rettv);
628#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100629static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100630static void f_js_decode(typval_T *argvars, typval_T *rettv);
631static void f_js_encode(typval_T *argvars, typval_T *rettv);
632static void f_json_decode(typval_T *argvars, typval_T *rettv);
633static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100634static void f_keys(typval_T *argvars, typval_T *rettv);
635static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
636static void f_len(typval_T *argvars, typval_T *rettv);
637static void f_libcall(typval_T *argvars, typval_T *rettv);
638static void f_libcallnr(typval_T *argvars, typval_T *rettv);
639static void f_line(typval_T *argvars, typval_T *rettv);
640static void f_line2byte(typval_T *argvars, typval_T *rettv);
641static void f_lispindent(typval_T *argvars, typval_T *rettv);
642static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000643#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_log(typval_T *argvars, typval_T *rettv);
645static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000646#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200647#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100648static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200649#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100650static void f_map(typval_T *argvars, typval_T *rettv);
651static void f_maparg(typval_T *argvars, typval_T *rettv);
652static void f_mapcheck(typval_T *argvars, typval_T *rettv);
653static void f_match(typval_T *argvars, typval_T *rettv);
654static void f_matchadd(typval_T *argvars, typval_T *rettv);
655static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
656static void f_matcharg(typval_T *argvars, typval_T *rettv);
657static void f_matchdelete(typval_T *argvars, typval_T *rettv);
658static void f_matchend(typval_T *argvars, typval_T *rettv);
659static void f_matchlist(typval_T *argvars, typval_T *rettv);
660static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200661static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_max(typval_T *argvars, typval_T *rettv);
663static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000664#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000666#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100667static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100668#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
672static void f_nr2char(typval_T *argvars, typval_T *rettv);
673static void f_or(typval_T *argvars, typval_T *rettv);
674static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100675#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100677#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000678#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
682static void f_printf(typval_T *argvars, typval_T *rettv);
683static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200684#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200686#endif
687#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200689#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_range(typval_T *argvars, typval_T *rettv);
691static void f_readfile(typval_T *argvars, typval_T *rettv);
692static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100693#ifdef FEAT_FLOAT
694static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
695#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_reltimestr(typval_T *argvars, typval_T *rettv);
697static void f_remote_expr(typval_T *argvars, typval_T *rettv);
698static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
699static void f_remote_peek(typval_T *argvars, typval_T *rettv);
700static void f_remote_read(typval_T *argvars, typval_T *rettv);
701static void f_remote_send(typval_T *argvars, typval_T *rettv);
702static void f_remove(typval_T *argvars, typval_T *rettv);
703static void f_rename(typval_T *argvars, typval_T *rettv);
704static void f_repeat(typval_T *argvars, typval_T *rettv);
705static void f_resolve(typval_T *argvars, typval_T *rettv);
706static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000707#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_screenattr(typval_T *argvars, typval_T *rettv);
711static void f_screenchar(typval_T *argvars, typval_T *rettv);
712static void f_screencol(typval_T *argvars, typval_T *rettv);
713static void f_screenrow(typval_T *argvars, typval_T *rettv);
714static void f_search(typval_T *argvars, typval_T *rettv);
715static void f_searchdecl(typval_T *argvars, typval_T *rettv);
716static void f_searchpair(typval_T *argvars, typval_T *rettv);
717static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
718static void f_searchpos(typval_T *argvars, typval_T *rettv);
719static void f_server2client(typval_T *argvars, typval_T *rettv);
720static void f_serverlist(typval_T *argvars, typval_T *rettv);
721static void f_setbufvar(typval_T *argvars, typval_T *rettv);
722static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
723static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100724static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100725static void f_setline(typval_T *argvars, typval_T *rettv);
726static void f_setloclist(typval_T *argvars, typval_T *rettv);
727static void f_setmatches(typval_T *argvars, typval_T *rettv);
728static void f_setpos(typval_T *argvars, typval_T *rettv);
729static void f_setqflist(typval_T *argvars, typval_T *rettv);
730static void f_setreg(typval_T *argvars, typval_T *rettv);
731static void f_settabvar(typval_T *argvars, typval_T *rettv);
732static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
733static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100734#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100736#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100737static void f_shellescape(typval_T *argvars, typval_T *rettv);
738static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
739static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000740#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100741static void f_sin(typval_T *argvars, typval_T *rettv);
742static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000743#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100744static void f_sort(typval_T *argvars, typval_T *rettv);
745static void f_soundfold(typval_T *argvars, typval_T *rettv);
746static void f_spellbadword(typval_T *argvars, typval_T *rettv);
747static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
748static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000749#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_sqrt(typval_T *argvars, typval_T *rettv);
751static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000752#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100753static void f_str2nr(typval_T *argvars, typval_T *rettv);
754static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000755#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000757#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200758static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_stridx(typval_T *argvars, typval_T *rettv);
760static void f_string(typval_T *argvars, typval_T *rettv);
761static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200762static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_strpart(typval_T *argvars, typval_T *rettv);
764static void f_strridx(typval_T *argvars, typval_T *rettv);
765static void f_strtrans(typval_T *argvars, typval_T *rettv);
766static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
767static void f_strwidth(typval_T *argvars, typval_T *rettv);
768static void f_submatch(typval_T *argvars, typval_T *rettv);
769static void f_substitute(typval_T *argvars, typval_T *rettv);
770static void f_synID(typval_T *argvars, typval_T *rettv);
771static void f_synIDattr(typval_T *argvars, typval_T *rettv);
772static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
773static void f_synstack(typval_T *argvars, typval_T *rettv);
774static void f_synconcealed(typval_T *argvars, typval_T *rettv);
775static void f_system(typval_T *argvars, typval_T *rettv);
776static void f_systemlist(typval_T *argvars, typval_T *rettv);
777static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
778static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
779static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
780static void f_taglist(typval_T *argvars, typval_T *rettv);
781static void f_tagfiles(typval_T *argvars, typval_T *rettv);
782static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200783static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5c719942016-07-09 23:40:45 +0200784static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200785static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200786static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
787#ifdef FEAT_JOB_CHANNEL
788static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
789#endif
790static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
791#ifdef FEAT_JOB_CHANNEL
792static void f_test_null_job(typval_T *argvars, typval_T *rettv);
793#endif
794static void f_test_null_list(typval_T *argvars, typval_T *rettv);
795static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
796static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200797static void f_test_settime(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200798#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100799static void f_tan(typval_T *argvars, typval_T *rettv);
800static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200801#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100802#ifdef FEAT_TIMERS
803static void f_timer_start(typval_T *argvars, typval_T *rettv);
804static void f_timer_stop(typval_T *argvars, typval_T *rettv);
805#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100806static void f_tolower(typval_T *argvars, typval_T *rettv);
807static void f_toupper(typval_T *argvars, typval_T *rettv);
808static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000809#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100810static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000811#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100812static void f_type(typval_T *argvars, typval_T *rettv);
813static void f_undofile(typval_T *argvars, typval_T *rettv);
814static void f_undotree(typval_T *argvars, typval_T *rettv);
815static void f_uniq(typval_T *argvars, typval_T *rettv);
816static void f_values(typval_T *argvars, typval_T *rettv);
817static void f_virtcol(typval_T *argvars, typval_T *rettv);
818static void f_visualmode(typval_T *argvars, typval_T *rettv);
819static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100820static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100821static void f_win_getid(typval_T *argvars, typval_T *rettv);
822static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
823static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
824static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100825static void f_winbufnr(typval_T *argvars, typval_T *rettv);
826static void f_wincol(typval_T *argvars, typval_T *rettv);
827static void f_winheight(typval_T *argvars, typval_T *rettv);
828static void f_winline(typval_T *argvars, typval_T *rettv);
829static void f_winnr(typval_T *argvars, typval_T *rettv);
830static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
831static void f_winrestview(typval_T *argvars, typval_T *rettv);
832static void f_winsaveview(typval_T *argvars, typval_T *rettv);
833static void f_winwidth(typval_T *argvars, typval_T *rettv);
834static void f_writefile(typval_T *argvars, typval_T *rettv);
835static void f_wordcount(typval_T *argvars, typval_T *rettv);
836static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000837
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100838static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
839static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
840static int get_env_len(char_u **arg);
841static int get_id_len(char_u **arg);
842static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
843static 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 +0000844#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
845#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
846 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
848static int eval_isnamec(int c);
849static int eval_isnamec1(int c);
850static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
851static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static typval_T *alloc_string_tv(char_u *string);
853static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100854#ifdef FEAT_FLOAT
855static float_T get_tv_float(typval_T *varp);
856#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100857static linenr_T get_tv_lnum(typval_T *argvars);
858static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
860static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
861static hashtab_T *find_var_ht(char_u *name, char_u **varname);
862static funccall_T *get_funccal(void);
863static void vars_clear_ext(hashtab_T *ht, int free_val);
864static void delete_var(hashtab_T *ht, hashitem_T *hi);
865static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
866static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
867static void set_var(char_u *name, typval_T *varp, int copy);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100868static int var_check_fixed(int flags, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100870static 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 +0100871static int eval_fname_script(char_u *p);
872static int eval_fname_sid(char_u *p);
873static void list_func_head(ufunc_T *fp, int indent);
874static ufunc_T *find_func(char_u *name);
875static int function_exists(char_u *name);
876static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000877#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static void func_do_profile(ufunc_T *fp);
879static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
880static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000881static int
882# ifdef __BORLANDC__
883 _RTLENTRYF
884# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000886static int
887# ifdef __BORLANDC__
888 _RTLENTRYF
889# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100890 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100892static int script_autoload(char_u *name, int reload);
893static char_u *autoload_name(char_u *name);
894static void cat_func_name(char_u *buf, ufunc_T *fp);
895static void func_free(ufunc_T *fp);
896static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
897static int can_free_funccal(funccall_T *fc, int copyID) ;
898static void free_funccal(funccall_T *fc, int free_val);
899static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
900static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
901static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
902static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
903static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
904static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
905static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
906static int write_list(FILE *fd, list_T *list, int binary);
907static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000908
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909
910#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100911static int compare_func_name(const void *s1, const void *s2);
912static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200913#endif
914
Bram Moolenaar33570922005-01-25 22:26:29 +0000915/*
916 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000917 */
918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100919eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000920{
Bram Moolenaar33570922005-01-25 22:26:29 +0000921 int i;
922 struct vimvar *p;
923
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200924 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
925 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200926 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000927 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000928 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000929
930 for (i = 0; i < VV_LEN; ++i)
931 {
932 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200933 if (STRLEN(p->vv_name) > 16)
934 {
935 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
936 getout(1);
937 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 STRCPY(p->vv_di.di_key, p->vv_name);
939 if (p->vv_flags & VV_RO)
940 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
941 else if (p->vv_flags & VV_RO_SBX)
942 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
943 else
944 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000945
946 /* add to v: scope dict, unless the value is not always available */
947 if (p->vv_type != VAR_UNKNOWN)
948 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000949 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000950 /* add to compat scope dict */
951 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100953 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
954
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000955 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100956 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200957 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100958 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100959
960 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
961 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
962 set_vim_var_nr(VV_NONE, VVAL_NONE);
963 set_vim_var_nr(VV_NULL, VVAL_NULL);
964
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200965 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200966
967#ifdef EBCDIC
968 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100969 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200970 */
971 sortFunctions();
972#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000973}
974
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000975#if defined(EXITFREE) || defined(PROTO)
976 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100977eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000978{
979 int i;
980 struct vimvar *p;
981
982 for (i = 0; i < VV_LEN; ++i)
983 {
984 p = &vimvars[i];
985 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000986 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000987 vim_free(p->vv_str);
988 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000989 }
990 else if (p->vv_di.di_tv.v_type == VAR_LIST)
991 {
992 list_unref(p->vv_list);
993 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000994 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995 }
996 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000997 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000998 hash_clear(&compat_hashtab);
999
Bram Moolenaard9fba312005-06-26 22:34:35 +00001000 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001001# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001002 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001003# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001004
1005 /* global variables */
1006 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001007
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001008 /* autoloaded script names */
1009 ga_clear_strings(&ga_loaded);
1010
Bram Moolenaarcca74132013-09-25 21:00:28 +02001011 /* Script-local variables. First clear all the variables and in a second
1012 * loop free the scriptvar_T, because a variable in one script might hold
1013 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001014 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001015 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001016 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001017 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001018 ga_clear(&ga_scripts);
1019
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001020 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001021 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001022
1023 /* functions */
1024 free_all_functions();
1025 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001026}
1027#endif
1028
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 * Return the name of the executed function.
1031 */
1032 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001033func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001035 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036}
1037
1038/*
1039 * Return the address holding the next breakpoint line for a funccall cookie.
1040 */
1041 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001042func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
Bram Moolenaar33570922005-01-25 22:26:29 +00001044 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045}
1046
1047/*
1048 * Return the address holding the debug tick for a funccall cookie.
1049 */
1050 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001051func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar33570922005-01-25 22:26:29 +00001053 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054}
1055
1056/*
1057 * Return the nesting level for a funccall cookie.
1058 */
1059 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001060func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
Bram Moolenaar33570922005-01-25 22:26:29 +00001062 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063}
1064
1065/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001066funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001068/* pointer to list of previously used funccal, still around because some
1069 * item in it is still being used. */
1070funccall_T *previous_funccal = NULL;
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072/*
1073 * Return TRUE when a function was ended by a ":return" command.
1074 */
1075 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001076current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
1078 return current_funccal->returned;
1079}
1080
1081
1082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 * Set an internal variable to a string value. Creates the variable if it does
1084 * not already exist.
1085 */
1086 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001087set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001089 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001090 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 val = vim_strsave(value);
1093 if (val != NULL)
1094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001095 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001096 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001098 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001099 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 }
1101 }
1102}
1103
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001105#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001106static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107static char_u *redir_endp = NULL;
1108static char_u *redir_varname = NULL;
1109
1110/*
1111 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001112 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 * Returns OK if successfully completed the setup. FAIL otherwise.
1114 */
1115 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001116var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117{
1118 int save_emsg;
1119 int err;
1120 typval_T tv;
1121
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001122 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001123 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 {
1125 EMSG(_(e_invarg));
1126 return FAIL;
1127 }
1128
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001129 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 redir_varname = vim_strsave(name);
1131 if (redir_varname == NULL)
1132 return FAIL;
1133
1134 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1135 if (redir_lval == NULL)
1136 {
1137 var_redir_stop();
1138 return FAIL;
1139 }
1140
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001141 /* The output is stored in growarray "redir_ga" until redirection ends. */
1142 ga_init2(&redir_ga, (int)sizeof(char), 500);
1143
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001145 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001146 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1148 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001149 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (redir_endp != NULL && *redir_endp != NUL)
1151 /* Trailing characters are present after the variable name */
1152 EMSG(_(e_trailing));
1153 else
1154 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 var_redir_stop();
1157 return FAIL;
1158 }
1159
1160 /* check if we can write to the variable: set it to or append an empty
1161 * string */
1162 save_emsg = did_emsg;
1163 did_emsg = FALSE;
1164 tv.v_type = VAR_STRING;
1165 tv.vval.v_string = (char_u *)"";
1166 if (append)
1167 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1168 else
1169 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001170 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001172 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 if (err)
1174 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001175 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 var_redir_stop();
1177 return FAIL;
1178 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179
1180 return OK;
1181}
1182
1183/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001184 * Append "value[value_len]" to the variable set by var_redir_start().
1185 * The actual appending is postponed until redirection ends, because the value
1186 * appended may in fact be the string we write to, changing it may cause freed
1187 * memory to be used:
1188 * :redir => foo
1189 * :let foo
1190 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191 */
1192 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001193var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001195 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196
1197 if (redir_lval == NULL)
1198 return;
1199
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001200 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001201 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001203 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001205 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001206 {
1207 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001208 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001209 }
1210 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212}
1213
1214/*
1215 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001216 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001217 */
1218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001219var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001221 typval_T tv;
1222
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001223 if (EVALCMD_BUSY)
1224 {
1225 redir_lval = NULL;
1226 return;
1227 }
1228
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229 if (redir_lval != NULL)
1230 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001231 /* If there was no error: assign the text to the variable. */
1232 if (redir_endp != NULL)
1233 {
1234 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1235 tv.v_type = VAR_STRING;
1236 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001237 /* Call get_lval() again, if it's inside a Dict or List it may
1238 * have changed. */
1239 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001240 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001241 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1242 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1243 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001244 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001245
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001246 /* free the collected output */
1247 vim_free(redir_ga.ga_data);
1248 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001249
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001250 vim_free(redir_lval);
1251 redir_lval = NULL;
1252 }
1253 vim_free(redir_varname);
1254 redir_varname = NULL;
1255}
1256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257# if defined(FEAT_MBYTE) || defined(PROTO)
1258 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001259eval_charconvert(
1260 char_u *enc_from,
1261 char_u *enc_to,
1262 char_u *fname_from,
1263 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 int err = FALSE;
1266
1267 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1268 set_vim_var_string(VV_CC_TO, enc_to, -1);
1269 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1270 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1271 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1272 err = TRUE;
1273 set_vim_var_string(VV_CC_FROM, NULL, -1);
1274 set_vim_var_string(VV_CC_TO, NULL, -1);
1275 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1276 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1277
1278 if (err)
1279 return FAIL;
1280 return OK;
1281}
1282# endif
1283
1284# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1285 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001286eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
1288 int err = FALSE;
1289
1290 set_vim_var_string(VV_FNAME_IN, fname, -1);
1291 set_vim_var_string(VV_CMDARG, args, -1);
1292 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1293 err = TRUE;
1294 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1295 set_vim_var_string(VV_CMDARG, NULL, -1);
1296
1297 if (err)
1298 {
1299 mch_remove(fname);
1300 return FAIL;
1301 }
1302 return OK;
1303}
1304# endif
1305
1306# if defined(FEAT_DIFF) || defined(PROTO)
1307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001308eval_diff(
1309 char_u *origfile,
1310 char_u *newfile,
1311 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
1313 int err = FALSE;
1314
1315 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1316 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1317 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1318 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1319 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1320 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1321 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1322}
1323
1324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001325eval_patch(
1326 char_u *origfile,
1327 char_u *difffile,
1328 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
1330 int err;
1331
1332 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1333 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1334 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1335 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1336 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1337 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1338 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1339}
1340# endif
1341
1342/*
1343 * Top level evaluation function, returning a boolean.
1344 * Sets "error" to TRUE if there was an error.
1345 * Return TRUE or FALSE.
1346 */
1347 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001348eval_to_bool(
1349 char_u *arg,
1350 int *error,
1351 char_u **nextcmd,
1352 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001355 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
1357 if (skip)
1358 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 else
1362 {
1363 *error = FALSE;
1364 if (!skip)
1365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001366 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369 }
1370 if (skip)
1371 --emsg_skip;
1372
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001373 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374}
1375
1376/*
1377 * Top level evaluation function, returning a string. If "skip" is TRUE,
1378 * only parsing to "nextcmd" is done, without reporting errors. Return
1379 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1380 */
1381 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001382eval_to_string_skip(
1383 char_u *arg,
1384 char_u **nextcmd,
1385 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
Bram Moolenaar33570922005-01-25 22:26:29 +00001387 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 char_u *retval;
1389
1390 if (skip)
1391 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 retval = NULL;
1394 else
1395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001396 retval = vim_strsave(get_tv_string(&tv));
1397 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399 if (skip)
1400 --emsg_skip;
1401
1402 return retval;
1403}
1404
1405/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001406 * Skip over an expression at "*pp".
1407 * Return FAIL for an error, OK otherwise.
1408 */
1409 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001410skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001411{
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001413
1414 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001416}
1417
1418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001420 * When "convert" is TRUE convert a List into a sequence of lines and convert
1421 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 * Return pointer to allocated memory, or NULL for failure.
1423 */
1424 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001425eval_to_string(
1426 char_u *arg,
1427 char_u **nextcmd,
1428 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429{
Bram Moolenaar33570922005-01-25 22:26:29 +00001430 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001432 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001433#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001434 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 retval = NULL;
1439 else
1440 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001441 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001442 {
1443 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001444 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001445 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001446 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001447 if (tv.vval.v_list->lv_len > 0)
1448 ga_append(&ga, NL);
1449 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001450 ga_append(&ga, NUL);
1451 retval = (char_u *)ga.ga_data;
1452 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001453#ifdef FEAT_FLOAT
1454 else if (convert && tv.v_type == VAR_FLOAT)
1455 {
1456 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1457 retval = vim_strsave(numbuf);
1458 }
1459#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001460 else
1461 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001462 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 }
1464
1465 return retval;
1466}
1467
1468/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001469 * Call eval_to_string() without using current local variables and using
1470 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 */
1472 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001473eval_to_string_safe(
1474 char_u *arg,
1475 char_u **nextcmd,
1476 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
1478 char_u *retval;
1479 void *save_funccalp;
1480
1481 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001482 if (use_sandbox)
1483 ++sandbox;
1484 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001485 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001486 if (use_sandbox)
1487 --sandbox;
1488 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 restore_funccal(save_funccalp);
1490 return retval;
1491}
1492
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493/*
1494 * Top level evaluation function, returning a number.
1495 * Evaluates "expr" silently.
1496 * Returns -1 for an error.
1497 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001498 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001499eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
Bram Moolenaar33570922005-01-25 22:26:29 +00001501 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001502 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001503 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505 ++emsg_off;
1506
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001507 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 retval = -1;
1509 else
1510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001511 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001512 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
1514 --emsg_off;
1515
1516 return retval;
1517}
1518
Bram Moolenaara40058a2005-07-11 22:42:07 +00001519/*
1520 * Prepare v: variable "idx" to be used.
1521 * Save the current typeval in "save_tv".
1522 * When not used yet add the variable to the v: hashtable.
1523 */
1524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001525prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001526{
1527 *save_tv = vimvars[idx].vv_tv;
1528 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1529 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1530}
1531
1532/*
1533 * Restore v: variable "idx" to typeval "save_tv".
1534 * When no longer defined, remove the variable from the v: hashtable.
1535 */
1536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001537restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001538{
1539 hashitem_T *hi;
1540
Bram Moolenaara40058a2005-07-11 22:42:07 +00001541 vimvars[idx].vv_tv = *save_tv;
1542 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1543 {
1544 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1545 if (HASHITEM_EMPTY(hi))
1546 EMSG2(_(e_intern2), "restore_vimvar()");
1547 else
1548 hash_remove(&vimvarht, hi);
1549 }
1550}
1551
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001552#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001553/*
1554 * Evaluate an expression to a list with suggestions.
1555 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001556 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001557 */
1558 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001559eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001560{
1561 typval_T save_val;
1562 typval_T rettv;
1563 list_T *list = NULL;
1564 char_u *p = skipwhite(expr);
1565
1566 /* Set "v:val" to the bad word. */
1567 prepare_vimvar(VV_VAL, &save_val);
1568 vimvars[VV_VAL].vv_type = VAR_STRING;
1569 vimvars[VV_VAL].vv_str = badword;
1570 if (p_verbose == 0)
1571 ++emsg_off;
1572
1573 if (eval1(&p, &rettv, TRUE) == OK)
1574 {
1575 if (rettv.v_type != VAR_LIST)
1576 clear_tv(&rettv);
1577 else
1578 list = rettv.vval.v_list;
1579 }
1580
1581 if (p_verbose == 0)
1582 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001583 restore_vimvar(VV_VAL, &save_val);
1584
1585 return list;
1586}
1587
1588/*
1589 * "list" is supposed to contain two items: a word and a number. Return the
1590 * word in "pp" and the number as the return value.
1591 * Return -1 if anything isn't right.
1592 * Used to get the good word and score from the eval_spell_expr() result.
1593 */
1594 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001595get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001596{
1597 listitem_T *li;
1598
1599 li = list->lv_first;
1600 if (li == NULL)
1601 return -1;
1602 *pp = get_tv_string(&li->li_tv);
1603
1604 li = li->li_next;
1605 if (li == NULL)
1606 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001607 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001608}
1609#endif
1610
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001611/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001612 * Top level evaluation function.
1613 * Returns an allocated typval_T with the result.
1614 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001615 */
1616 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001617eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001618{
1619 typval_T *tv;
1620
1621 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001622 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001623 {
1624 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001625 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001626 }
1627
1628 return tv;
1629}
1630
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001634 * Uses argv[argc] for the function arguments. Only Number and String
1635 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001638 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001639call_vim_function(
1640 char_u *func,
1641 int argc,
1642 char_u **argv,
1643 int safe, /* use the sandbox */
1644 int str_arg_only, /* all arguments are strings */
1645 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646{
Bram Moolenaar33570922005-01-25 22:26:29 +00001647 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001648 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 int len;
1650 int i;
1651 int doesrange;
1652 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001655 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
1659 for (i = 0; i < argc; i++)
1660 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001661 /* Pass a NULL or empty argument as an empty string */
1662 if (argv[i] == NULL || *argv[i] == NUL)
1663 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001664 argvars[i].v_type = VAR_STRING;
1665 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001666 continue;
1667 }
1668
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001669 if (str_arg_only)
1670 len = 0;
1671 else
1672 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001673 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if (len != 0 && len == (int)STRLEN(argv[i]))
1675 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001676 argvars[i].v_type = VAR_NUMBER;
1677 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
1679 else
1680 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001681 argvars[i].v_type = VAR_STRING;
1682 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684 }
1685
1686 if (safe)
1687 {
1688 save_funccalp = save_funccal();
1689 ++sandbox;
1690 }
1691
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1693 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001695 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 if (safe)
1697 {
1698 --sandbox;
1699 restore_funccal(save_funccalp);
1700 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 vim_free(argvars);
1702
1703 if (ret == FAIL)
1704 clear_tv(rettv);
1705
1706 return ret;
1707}
1708
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001709/*
1710 * Call vimL function "func" and return the result as a number.
1711 * Returns -1 when calling the function fails.
1712 * Uses argv[argc] for the function arguments.
1713 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001714 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001715call_func_retnr(
1716 char_u *func,
1717 int argc,
1718 char_u **argv,
1719 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001720{
1721 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001722 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001723
1724 /* All arguments are passed as strings, no conversion to number. */
1725 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1726 return -1;
1727
1728 retval = get_tv_number_chk(&rettv, NULL);
1729 clear_tv(&rettv);
1730 return retval;
1731}
1732
1733#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1734 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1735
Bram Moolenaar4f688582007-07-24 12:34:30 +00001736# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001737/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001738 * Call vimL function "func" and return the result as a string.
1739 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001740 * Uses argv[argc] for the function arguments.
1741 */
1742 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001743call_func_retstr(
1744 char_u *func,
1745 int argc,
1746 char_u **argv,
1747 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748{
1749 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001750 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001752 /* All arguments are passed as strings, no conversion to number. */
1753 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001754 return NULL;
1755
1756 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 return retval;
1759}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001760# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001762/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001763 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001764 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001765 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001766 */
1767 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001768call_func_retlist(
1769 char_u *func,
1770 int argc,
1771 char_u **argv,
1772 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001773{
1774 typval_T rettv;
1775
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001776 /* All arguments are passed as strings, no conversion to number. */
1777 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001778 return NULL;
1779
1780 if (rettv.v_type != VAR_LIST)
1781 {
1782 clear_tv(&rettv);
1783 return NULL;
1784 }
1785
1786 return rettv.vval.v_list;
1787}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
1789
1790/*
1791 * Save the current function call pointer, and set it to NULL.
1792 * Used when executing autocommands and for ":source".
1793 */
1794 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001795save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001797 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 current_funccal = NULL;
1800 return (void *)fc;
1801}
1802
1803 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001804restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001806 funccall_T *fc = (funccall_T *)vfc;
1807
1808 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809}
1810
Bram Moolenaar05159a02005-02-26 23:04:13 +00001811#if defined(FEAT_PROFILE) || defined(PROTO)
1812/*
1813 * Prepare profiling for entering a child or something else that is not
1814 * counted for the script/function itself.
1815 * Should always be called in pair with prof_child_exit().
1816 */
1817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818prof_child_enter(
1819 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001820{
1821 funccall_T *fc = current_funccal;
1822
1823 if (fc != NULL && fc->func->uf_profiling)
1824 profile_start(&fc->prof_child);
1825 script_prof_save(tm);
1826}
1827
1828/*
1829 * Take care of time spent in a child.
1830 * Should always be called after prof_child_enter().
1831 */
1832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001833prof_child_exit(
1834 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001835{
1836 funccall_T *fc = current_funccal;
1837
1838 if (fc != NULL && fc->func->uf_profiling)
1839 {
1840 profile_end(&fc->prof_child);
1841 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1842 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1843 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1844 }
1845 script_prof_restore(tm);
1846}
1847#endif
1848
1849
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850#ifdef FEAT_FOLDING
1851/*
1852 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1853 * it in "*cp". Doesn't give error messages.
1854 */
1855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001856eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001859 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001861 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1862 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
1864 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001865 if (use_sandbox)
1866 ++sandbox;
1867 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 retval = 0;
1871 else
1872 {
1873 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 if (tv.v_type == VAR_NUMBER)
1875 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001876 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 retval = 0;
1878 else
1879 {
1880 /* If the result is a string, check if there is a non-digit before
1881 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (!VIM_ISDIGIT(*s) && *s != '-')
1884 *cp = *s++;
1885 retval = atol((char *)s);
1886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 }
1889 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001890 if (use_sandbox)
1891 --sandbox;
1892 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001894 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895}
1896#endif
1897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 * ":let" list all variable values
1900 * ":let var1 var2" list variable values
1901 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001902 * ":let var += expr" assignment command.
1903 * ":let var -= expr" assignment command.
1904 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 */
1907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001908ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909{
1910 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 int var_count = 0;
1915 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001917 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001918 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
Bram Moolenaardb552d602006-03-23 22:59:57 +00001920 argend = skip_var_list(arg, &var_count, &semicolon);
1921 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001923 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1924 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001925 expr = skipwhite(argend);
1926 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1927 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001929 /*
1930 * ":let" without "=": list variables
1931 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 if (*arg == '[')
1933 EMSG(_(e_invarg));
1934 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001936 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001940 list_glob_vars(&first);
1941 list_buf_vars(&first);
1942 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001943#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001944 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001945#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001946 list_script_vars(&first);
1947 list_func_vars(&first);
1948 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 eap->nextcmd = check_nextcmd(arg);
1951 }
1952 else
1953 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001954 op[0] = '=';
1955 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001956 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001958 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1959 op[0] = *expr; /* +=, -= or .= */
1960 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001962 else
1963 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 if (eap->skip)
1966 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (eap->skip)
1969 {
1970 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 --emsg_skip;
1973 }
1974 else if (i != FAIL)
1975 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001978 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 }
1980 }
1981}
1982
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983/*
1984 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1985 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 * When "nextchars" is not NULL it points to a string with characters that
1987 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1988 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 * Returns OK or FAIL;
1990 */
1991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001992ex_let_vars(
1993 char_u *arg_start,
1994 typval_T *tv,
1995 int copy, /* copy values from "tv", don't move */
1996 int semicolon, /* from skip_var_list() */
1997 int var_count, /* from skip_var_list() */
1998 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999{
2000 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002001 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002003 listitem_T *item;
2004 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005
2006 if (*arg != '[')
2007 {
2008 /*
2009 * ":let var = expr" or ":for var in list"
2010 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 return FAIL;
2013 return OK;
2014 }
2015
2016 /*
2017 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2018 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002019 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 {
2021 EMSG(_(e_listreq));
2022 return FAIL;
2023 }
2024
2025 i = list_len(l);
2026 if (semicolon == 0 && var_count < i)
2027 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002028 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 return FAIL;
2030 }
2031 if (var_count - semicolon > i)
2032 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002033 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 return FAIL;
2035 }
2036
2037 item = l->lv_first;
2038 while (*arg != ']')
2039 {
2040 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002041 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002042 item = item->li_next;
2043 if (arg == NULL)
2044 return FAIL;
2045
2046 arg = skipwhite(arg);
2047 if (*arg == ';')
2048 {
2049 /* Put the rest of the list (may be empty) in the var after ';'.
2050 * Create a new list for this. */
2051 l = list_alloc();
2052 if (l == NULL)
2053 return FAIL;
2054 while (item != NULL)
2055 {
2056 list_append_tv(l, &item->li_tv);
2057 item = item->li_next;
2058 }
2059
2060 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002061 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 ltv.vval.v_list = l;
2063 l->lv_refcount = 1;
2064
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002065 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2066 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002067 clear_tv(&ltv);
2068 if (arg == NULL)
2069 return FAIL;
2070 break;
2071 }
2072 else if (*arg != ',' && *arg != ']')
2073 {
2074 EMSG2(_(e_intern2), "ex_let_vars()");
2075 return FAIL;
2076 }
2077 }
2078
2079 return OK;
2080}
2081
2082/*
2083 * Skip over assignable variable "var" or list of variables "[var, var]".
2084 * Used for ":let varvar = expr" and ":for varvar in expr".
2085 * For "[var, var]" increment "*var_count" for each variable.
2086 * for "[var, var; var]" set "semicolon".
2087 * Return NULL for an error.
2088 */
2089 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002090skip_var_list(
2091 char_u *arg,
2092 int *var_count,
2093 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094{
2095 char_u *p, *s;
2096
2097 if (*arg == '[')
2098 {
2099 /* "[var, var]": find the matching ']'. */
2100 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002101 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102 {
2103 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2104 s = skip_var_one(p);
2105 if (s == p)
2106 {
2107 EMSG2(_(e_invarg2), p);
2108 return NULL;
2109 }
2110 ++*var_count;
2111
2112 p = skipwhite(s);
2113 if (*p == ']')
2114 break;
2115 else if (*p == ';')
2116 {
2117 if (*semicolon == 1)
2118 {
2119 EMSG(_("Double ; in list of variables"));
2120 return NULL;
2121 }
2122 *semicolon = 1;
2123 }
2124 else if (*p != ',')
2125 {
2126 EMSG2(_(e_invarg2), p);
2127 return NULL;
2128 }
2129 }
2130 return p + 1;
2131 }
2132 else
2133 return skip_var_one(arg);
2134}
2135
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002137 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002138 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002139 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002140 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002141skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002142{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002143 if (*arg == '@' && arg[1] != NUL)
2144 return arg + 2;
2145 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2146 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002147}
2148
Bram Moolenaara7043832005-01-21 11:56:39 +00002149/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002150 * List variables for hashtab "ht" with prefix "prefix".
2151 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002152 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002154list_hashtable_vars(
2155 hashtab_T *ht,
2156 char_u *prefix,
2157 int empty,
2158 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002159{
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 hashitem_T *hi;
2161 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002162 int todo;
2163
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002164 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002165 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2166 {
2167 if (!HASHITEM_EMPTY(hi))
2168 {
2169 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002170 di = HI2DI(hi);
2171 if (empty || di->di_tv.v_type != VAR_STRING
2172 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002174 }
2175 }
2176}
2177
2178/*
2179 * List global variables.
2180 */
2181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002182list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002183{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002185}
2186
2187/*
2188 * List buffer variables.
2189 */
2190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002191list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002192{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193 char_u numbuf[NUMBUFLEN];
2194
Bram Moolenaar429fa852013-04-15 12:27:36 +02002195 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197
2198 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002199 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2200 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002201}
2202
2203/*
2204 * List window variables.
2205 */
2206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002207list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002208{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002209 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002211}
2212
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002213#ifdef FEAT_WINDOWS
2214/*
2215 * List tab page variables.
2216 */
2217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002219{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002220 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002221 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002222}
2223#endif
2224
Bram Moolenaara7043832005-01-21 11:56:39 +00002225/*
2226 * List Vim variables.
2227 */
2228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002229list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232}
2233
2234/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002235 * List script-local variables, if there is a script.
2236 */
2237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002238list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002239{
2240 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2242 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002243}
2244
2245/*
2246 * List function variables, if there is a function.
2247 */
2248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002249list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002250{
2251 if (current_funccal != NULL)
2252 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002253 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002254}
2255
2256/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 * List variables in "arg".
2258 */
2259 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002260list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261{
2262 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 char_u *name_start;
2266 char_u *arg_subsc;
2267 char_u *tofree;
2268 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269
2270 while (!ends_excmd(*arg) && !got_int)
2271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002274 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2276 {
2277 emsg_severe = TRUE;
2278 EMSG(_(e_trailing));
2279 break;
2280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 /* get_name_len() takes care of expanding curly braces */
2285 name_start = name = arg;
2286 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2287 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 /* This is mainly to keep test 49 working: when expanding
2290 * curly braces fails overrule the exception error message. */
2291 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293 emsg_severe = TRUE;
2294 EMSG2(_(e_invarg2), arg);
2295 break;
2296 }
2297 error = TRUE;
2298 }
2299 else
2300 {
2301 if (tofree != NULL)
2302 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002303 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 else
2306 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 /* handle d.key, l[idx], f(expr) */
2308 arg_subsc = arg;
2309 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002310 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002312 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002314 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002316 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002317 case 'g': list_glob_vars(first); break;
2318 case 'b': list_buf_vars(first); break;
2319 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002320#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002321 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002322#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002323 case 'v': list_vim_vars(first); break;
2324 case 's': list_script_vars(first); break;
2325 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 default:
2327 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002328 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002329 }
2330 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002331 {
2332 char_u numbuf[NUMBUFLEN];
2333 char_u *tf;
2334 int c;
2335 char_u *s;
2336
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002337 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002338 c = *arg;
2339 *arg = NUL;
2340 list_one_var_a((char_u *)"",
2341 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002342 tv.v_type,
2343 s == NULL ? (char_u *)"" : s,
2344 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345 *arg = c;
2346 vim_free(tf);
2347 }
2348 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002349 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 }
2351 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002352
2353 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002355
2356 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 }
2358
2359 return arg;
2360}
2361
2362/*
2363 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2364 * Returns a pointer to the char just after the var name.
2365 * Returns NULL if there is an error.
2366 */
2367 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002368ex_let_one(
2369 char_u *arg, /* points to variable name */
2370 typval_T *tv, /* value to assign to variable */
2371 int copy, /* copy value from "tv" */
2372 char_u *endchars, /* valid chars after variable name or NULL */
2373 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374{
2375 int c1;
2376 char_u *name;
2377 char_u *p;
2378 char_u *arg_end = NULL;
2379 int len;
2380 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382
2383 /*
2384 * ":let $VAR = expr": Set environment variable.
2385 */
2386 if (*arg == '$')
2387 {
2388 /* Find the end of the name. */
2389 ++arg;
2390 name = arg;
2391 len = get_env_len(&arg);
2392 if (len == 0)
2393 EMSG2(_(e_invarg2), name - 1);
2394 else
2395 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 if (op != NULL && (*op == '+' || *op == '-'))
2397 EMSG2(_(e_letwrong), op);
2398 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002399 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002401 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 {
2403 c1 = name[len];
2404 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002405 p = get_tv_string_chk(tv);
2406 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 {
2408 int mustfree = FALSE;
2409 char_u *s = vim_getenv(name, &mustfree);
2410
2411 if (s != NULL)
2412 {
2413 p = tofree = concat_str(s, p);
2414 if (mustfree)
2415 vim_free(s);
2416 }
2417 }
2418 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002421 if (STRICMP(name, "HOME") == 0)
2422 init_homedir();
2423 else if (didset_vim && STRICMP(name, "VIM") == 0)
2424 didset_vim = FALSE;
2425 else if (didset_vimruntime
2426 && STRICMP(name, "VIMRUNTIME") == 0)
2427 didset_vimruntime = FALSE;
2428 arg_end = arg;
2429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 }
2433 }
2434 }
2435
2436 /*
2437 * ":let &option = expr": Set option value.
2438 * ":let &l:option = expr": Set local option value.
2439 * ":let &g:option = expr": Set global option value.
2440 */
2441 else if (*arg == '&')
2442 {
2443 /* Find the end of the name. */
2444 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002445 if (p == NULL || (endchars != NULL
2446 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 EMSG(_(e_letunexp));
2448 else
2449 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 long n;
2451 int opt_type;
2452 long numval;
2453 char_u *stringval = NULL;
2454 char_u *s;
2455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 c1 = *p;
2457 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002459 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002460 s = get_tv_string_chk(tv); /* != NULL if number or string */
2461 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 {
2463 opt_type = get_option_value(arg, &numval,
2464 &stringval, opt_flags);
2465 if ((opt_type == 1 && *op == '.')
2466 || (opt_type == 0 && *op != '.'))
2467 EMSG2(_(e_letwrong), op);
2468 else
2469 {
2470 if (opt_type == 1) /* number */
2471 {
2472 if (*op == '+')
2473 n = numval + n;
2474 else
2475 n = numval - n;
2476 }
2477 else if (opt_type == 0 && stringval != NULL) /* string */
2478 {
2479 s = concat_str(stringval, s);
2480 vim_free(stringval);
2481 stringval = s;
2482 }
2483 }
2484 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002485 if (s != NULL)
2486 {
2487 set_option_value(arg, n, s, opt_flags);
2488 arg_end = p;
2489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 }
2493 }
2494
2495 /*
2496 * ":let @r = expr": Set register contents.
2497 */
2498 else if (*arg == '@')
2499 {
2500 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 if (op != NULL && (*op == '+' || *op == '-'))
2502 EMSG2(_(e_letwrong), op);
2503 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 EMSG(_(e_letunexp));
2506 else
2507 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002508 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509 char_u *s;
2510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002511 p = get_tv_string_chk(tv);
2512 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002514 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 if (s != NULL)
2516 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002517 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 vim_free(s);
2519 }
2520 }
2521 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002522 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002524 arg_end = arg + 1;
2525 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002526 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 }
2528 }
2529
2530 /*
2531 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002534 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002538 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2542 EMSG(_(e_letunexp));
2543 else
2544 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 arg_end = p;
2547 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 }
2551
2552 else
2553 EMSG2(_(e_invarg2), arg);
2554
2555 return arg_end;
2556}
2557
2558/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2560 */
2561 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002562check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563{
2564 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2565 {
2566 EMSG2(_(e_readonlyvar), arg);
2567 return TRUE;
2568 }
2569 return FALSE;
2570}
2571
2572/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 * Get an lval: variable, Dict item or List item that can be assigned a value
2574 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2575 * "name.key", "name.key[expr]" etc.
2576 * Indexing only works if "name" is an existing List or Dictionary.
2577 * "name" points to the start of the name.
2578 * If "rettv" is not NULL it points to the value to be assigned.
2579 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2580 * wrong; must end in space or cmd separator.
2581 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002582 * flags:
2583 * GLV_QUIET: do not give error messages
2584 * GLV_NO_AUTOLOAD: do not use script autoloading
2585 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002587 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589 */
2590 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002591get_lval(
2592 char_u *name,
2593 typval_T *rettv,
2594 lval_T *lp,
2595 int unlet,
2596 int skip,
2597 int flags, /* GLV_ values */
2598 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 char_u *expr_start, *expr_end;
2602 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002603 dictitem_T *v;
2604 typval_T var1;
2605 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002607 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002609 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002610 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002611 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002614 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615
2616 if (skip)
2617 {
2618 /* When skipping just find the end of the name. */
2619 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002620 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 }
2622
2623 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002624 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (expr_start != NULL)
2626 {
2627 /* Don't expand the name when we already know there is an error. */
2628 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2629 && *p != '[' && *p != '.')
2630 {
2631 EMSG(_(e_trailing));
2632 return NULL;
2633 }
2634
2635 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2636 if (lp->ll_exp_name == NULL)
2637 {
2638 /* Report an invalid expression in braces, unless the
2639 * expression evaluation has been cancelled due to an
2640 * aborting error, an interrupt, or an exception. */
2641 if (!aborting() && !quiet)
2642 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002643 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 EMSG2(_(e_invarg2), name);
2645 return NULL;
2646 }
2647 }
2648 lp->ll_name = lp->ll_exp_name;
2649 }
2650 else
2651 lp->ll_name = name;
2652
2653 /* Without [idx] or .key we are done. */
2654 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2655 return p;
2656
2657 cc = *p;
2658 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002659 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (v == NULL && !quiet)
2661 EMSG2(_(e_undefvar), lp->ll_name);
2662 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 if (v == NULL)
2664 return NULL;
2665
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 /*
2667 * Loop until no more [idx] or .key is following.
2668 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002669 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2673 && !(lp->ll_tv->v_type == VAR_DICT
2674 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
2677 EMSG(_("E689: Can only index a List or Dictionary"));
2678 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002679 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
2683 EMSG(_("E708: [:] must come last"));
2684 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002685 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 len = -1;
2688 if (*p == '.')
2689 {
2690 key = p + 1;
2691 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2692 ;
2693 if (len == 0)
2694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
2696 EMSG(_(e_emptykey));
2697 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 }
2699 p = key + len;
2700 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 else
2702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (*p == ':')
2706 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707 else
2708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 empty1 = FALSE;
2710 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002712 if (get_tv_string_chk(&var1) == NULL)
2713 {
2714 /* not a number or string */
2715 clear_tv(&var1);
2716 return NULL;
2717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719
2720 /* Optionally get the second index [ :expr]. */
2721 if (*p == ':')
2722 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002726 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002730 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (rettv != NULL && (rettv->v_type != VAR_LIST
2732 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 if (!quiet)
2735 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (!empty1)
2737 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 }
2740 p = skipwhite(p + 1);
2741 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 else
2744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2747 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 if (!empty1)
2749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002752 if (get_tv_string_chk(&var2) == NULL)
2753 {
2754 /* not a number or string */
2755 if (!empty1)
2756 clear_tv(&var1);
2757 clear_tv(&var2);
2758 return NULL;
2759 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002765
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (!quiet)
2769 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 if (!empty1)
2771 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776
2777 /* Skip to past ']'. */
2778 ++p;
2779 }
2780
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 {
2783 if (len == -1)
2784 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002786 key = get_tv_string_chk(&var1); /* is number or string */
2787 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 }
2792 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002793 lp->ll_list = NULL;
2794 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002795 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002796
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002797 /* When assigning to a scope dictionary check that a function and
2798 * variable name is valid (only variable name unless it is l: or
2799 * g: dictionary). Disallow overwriting a builtin function. */
2800 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002801 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002802 int prevval;
2803 int wrong;
2804
2805 if (len != -1)
2806 {
2807 prevval = key[len];
2808 key[len] = NUL;
2809 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002810 else
2811 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002812 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2813 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002814 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002815 || !valid_varname(key);
2816 if (len != -1)
2817 key[len] = prevval;
2818 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002819 return NULL;
2820 }
2821
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002824 /* Can't add "v:" variable. */
2825 if (lp->ll_dict == &vimvardict)
2826 {
2827 EMSG2(_(e_illvar), name);
2828 return NULL;
2829 }
2830
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002831 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002835 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 if (len == -1)
2837 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 }
2840 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 if (len == -1)
2845 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 p = NULL;
2848 break;
2849 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002850 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002851 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002852 return NULL;
2853
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 if (len == -1)
2855 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 }
2858 else
2859 {
2860 /*
2861 * Get the number and item for the only or first index of the List.
2862 */
2863 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 else
2866 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002867 lp->ll_n1 = (long)get_tv_number(&var1);
2868 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 clear_tv(&var1);
2870 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002871 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 lp->ll_list = lp->ll_tv->vval.v_list;
2873 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2874 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002876 if (lp->ll_n1 < 0)
2877 {
2878 lp->ll_n1 = 0;
2879 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2880 }
2881 }
2882 if (lp->ll_li == NULL)
2883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002886 if (!quiet)
2887 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 }
2890
2891 /*
2892 * May need to find the item or absolute index for the second
2893 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 * When no index given: "lp->ll_empty2" is TRUE.
2895 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002898 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002899 lp->ll_n2 = (long)get_tv_number(&var2);
2900 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002906 {
2907 if (!quiet)
2908 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002910 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002912 }
2913
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2915 if (lp->ll_n1 < 0)
2916 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2917 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002918 {
2919 if (!quiet)
2920 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002922 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 }
2924
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002926 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 }
2928
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 return p;
2930}
2931
2932/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002933 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 */
2935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002936clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937{
2938 vim_free(lp->ll_exp_name);
2939 vim_free(lp->ll_newkey);
2940}
2941
2942/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002943 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 */
2947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002948set_var_lval(
2949 lval_T *lp,
2950 char_u *endp,
2951 typval_T *rettv,
2952 int copy,
2953 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954{
2955 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002956 listitem_T *ri;
2957 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958
2959 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002962 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 cc = *endp;
2964 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002965 if (op != NULL && *op != '=')
2966 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002967 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002968
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002969 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002970 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002971 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002972 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002974 if ((di == NULL
2975 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2976 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2977 FALSE)))
2978 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002979 set_var(lp->ll_name, &tv, FALSE);
2980 clear_tv(&tv);
2981 }
2982 }
2983 else
2984 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002988 else if (tv_check_lock(lp->ll_newkey == NULL
2989 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002990 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002991 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 else if (lp->ll_range)
2993 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002994 listitem_T *ll_li = lp->ll_li;
2995 int ll_n1 = lp->ll_n1;
2996
2997 /*
2998 * Check whether any of the list items is locked
2999 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003000 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003001 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003002 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003003 return;
3004 ri = ri->li_next;
3005 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3006 break;
3007 ll_li = ll_li->li_next;
3008 ++ll_n1;
3009 }
3010
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 /*
3012 * Assign the List values to the list items.
3013 */
3014 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003015 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003016 if (op != NULL && *op != '=')
3017 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3018 else
3019 {
3020 clear_tv(&lp->ll_li->li_tv);
3021 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3022 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 ri = ri->li_next;
3024 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3025 break;
3026 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003029 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003030 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 ri = NULL;
3032 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003033 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003034 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 lp->ll_li = lp->ll_li->li_next;
3036 ++lp->ll_n1;
3037 }
3038 if (ri != NULL)
3039 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003040 else if (lp->ll_empty2
3041 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 : lp->ll_n1 != lp->ll_n2)
3043 EMSG(_("E711: List value has not enough items"));
3044 }
3045 else
3046 {
3047 /*
3048 * Assign to a List or Dictionary item.
3049 */
3050 if (lp->ll_newkey != NULL)
3051 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 if (op != NULL && *op != '=')
3053 {
3054 EMSG2(_(e_letwrong), op);
3055 return;
3056 }
3057
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003059 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 if (di == NULL)
3061 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003062 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3063 {
3064 vim_free(di);
3065 return;
3066 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003068 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 else if (op != NULL && *op != '=')
3070 {
3071 tv_op(lp->ll_tv, rettv, op);
3072 return;
3073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003074 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003075 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003076
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077 /*
3078 * Assign the value to the variable or list item.
3079 */
3080 if (copy)
3081 copy_tv(rettv, lp->ll_tv);
3082 else
3083 {
3084 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003085 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003086 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003087 }
3088 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003089}
3090
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003091/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003092 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3093 * Returns OK or FAIL.
3094 */
3095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003096tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003098 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 char_u numbuf[NUMBUFLEN];
3100 char_u *s;
3101
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003102 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3103 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3104 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 {
3106 switch (tv1->v_type)
3107 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003108 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 case VAR_DICT:
3110 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003111 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003112 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003113 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003114 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003115 break;
3116
3117 case VAR_LIST:
3118 if (*op != '+' || tv2->v_type != VAR_LIST)
3119 break;
3120 /* List += List */
3121 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3122 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3123 return OK;
3124
3125 case VAR_NUMBER:
3126 case VAR_STRING:
3127 if (tv2->v_type == VAR_LIST)
3128 break;
3129 if (*op == '+' || *op == '-')
3130 {
3131 /* nr += nr or nr -= nr*/
3132 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003133#ifdef FEAT_FLOAT
3134 if (tv2->v_type == VAR_FLOAT)
3135 {
3136 float_T f = n;
3137
3138 if (*op == '+')
3139 f += tv2->vval.v_float;
3140 else
3141 f -= tv2->vval.v_float;
3142 clear_tv(tv1);
3143 tv1->v_type = VAR_FLOAT;
3144 tv1->vval.v_float = f;
3145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147#endif
3148 {
3149 if (*op == '+')
3150 n += get_tv_number(tv2);
3151 else
3152 n -= get_tv_number(tv2);
3153 clear_tv(tv1);
3154 tv1->v_type = VAR_NUMBER;
3155 tv1->vval.v_number = n;
3156 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003157 }
3158 else
3159 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003160 if (tv2->v_type == VAR_FLOAT)
3161 break;
3162
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003163 /* str .= str */
3164 s = get_tv_string(tv1);
3165 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3166 clear_tv(tv1);
3167 tv1->v_type = VAR_STRING;
3168 tv1->vval.v_string = s;
3169 }
3170 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003172 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003173#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003174 {
3175 float_T f;
3176
3177 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3178 && tv2->v_type != VAR_NUMBER
3179 && tv2->v_type != VAR_STRING))
3180 break;
3181 if (tv2->v_type == VAR_FLOAT)
3182 f = tv2->vval.v_float;
3183 else
3184 f = get_tv_number(tv2);
3185 if (*op == '+')
3186 tv1->vval.v_float += f;
3187 else
3188 tv1->vval.v_float -= f;
3189 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003190#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003191 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003192 }
3193 }
3194
3195 EMSG2(_(e_letwrong), op);
3196 return FAIL;
3197}
3198
3199/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 * Add a watcher to a list.
3201 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003203list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204{
3205 lw->lw_next = l->lv_watch;
3206 l->lv_watch = lw;
3207}
3208
3209/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003210 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211 * No warning when it isn't found...
3212 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003214list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215{
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217
3218 lwp = &l->lv_watch;
3219 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3220 {
3221 if (lw == lwrem)
3222 {
3223 *lwp = lw->lw_next;
3224 break;
3225 }
3226 lwp = &lw->lw_next;
3227 }
3228}
3229
3230/*
3231 * Just before removing an item from a list: advance watchers to the next
3232 * item.
3233 */
3234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236{
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238
3239 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3240 if (lw->lw_item == item)
3241 lw->lw_item = item->li_next;
3242}
3243
3244/*
3245 * Evaluate the expression used in a ":for var in expr" command.
3246 * "arg" points to "var".
3247 * Set "*errp" to TRUE for an error, FALSE otherwise;
3248 * Return a pointer that holds the info. Null when there is an error.
3249 */
3250 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003251eval_for_line(
3252 char_u *arg,
3253 int *errp,
3254 char_u **nextcmdp,
3255 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256{
Bram Moolenaar33570922005-01-25 22:26:29 +00003257 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 typval_T tv;
3260 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261
3262 *errp = TRUE; /* default: there is an error */
3263
Bram Moolenaar33570922005-01-25 22:26:29 +00003264 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 if (fi == NULL)
3266 return NULL;
3267
3268 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3269 if (expr == NULL)
3270 return fi;
3271
3272 expr = skipwhite(expr);
3273 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3274 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003275 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 return fi;
3277 }
3278
3279 if (skip)
3280 ++emsg_skip;
3281 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3282 {
3283 *errp = FALSE;
3284 if (!skip)
3285 {
3286 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003287 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003288 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003289 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003290 clear_tv(&tv);
3291 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003292 else if (l == NULL)
3293 {
3294 /* a null list is like an empty list: do nothing */
3295 clear_tv(&tv);
3296 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 else
3298 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003299 /* No need to increment the refcount, it's already set for the
3300 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 fi->fi_list = l;
3302 list_add_watch(l, &fi->fi_lw);
3303 fi->fi_lw.lw_item = l->lv_first;
3304 }
3305 }
3306 }
3307 if (skip)
3308 --emsg_skip;
3309
3310 return fi;
3311}
3312
3313/*
3314 * Use the first item in a ":for" list. Advance to the next.
3315 * Assign the values to the variable (list). "arg" points to the first one.
3316 * Return TRUE when a valid item was found, FALSE when at end of list or
3317 * something wrong.
3318 */
3319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003320next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003322 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003324 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325
3326 item = fi->fi_lw.lw_item;
3327 if (item == NULL)
3328 result = FALSE;
3329 else
3330 {
3331 fi->fi_lw.lw_item = item->li_next;
3332 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3333 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3334 }
3335 return result;
3336}
3337
3338/*
3339 * Free the structure used to store info used by ":for".
3340 */
3341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343{
Bram Moolenaar33570922005-01-25 22:26:29 +00003344 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003346 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003347 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003349 list_unref(fi->fi_list);
3350 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 vim_free(fi);
3352}
3353
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3355
3356 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003357set_context_for_expression(
3358 expand_T *xp,
3359 char_u *arg,
3360 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 int got_eq = FALSE;
3363 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003364 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003366 if (cmdidx == CMD_let)
3367 {
3368 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003369 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003370 {
3371 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003372 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003373 {
3374 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003375 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003376 if (vim_iswhite(*p))
3377 break;
3378 }
3379 return;
3380 }
3381 }
3382 else
3383 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3384 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 while ((xp->xp_pattern = vim_strpbrk(arg,
3386 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3387 {
3388 c = *xp->xp_pattern;
3389 if (c == '&')
3390 {
3391 c = xp->xp_pattern[1];
3392 if (c == '&')
3393 {
3394 ++xp->xp_pattern;
3395 xp->xp_context = cmdidx != CMD_let || got_eq
3396 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3397 }
3398 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003401 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3402 xp->xp_pattern += 2;
3403
3404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406 else if (c == '$')
3407 {
3408 /* environment variable */
3409 xp->xp_context = EXPAND_ENV_VARS;
3410 }
3411 else if (c == '=')
3412 {
3413 got_eq = TRUE;
3414 xp->xp_context = EXPAND_EXPRESSION;
3415 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003416 else if (c == '#'
3417 && xp->xp_context == EXPAND_EXPRESSION)
3418 {
3419 /* Autoload function/variable contains '#'. */
3420 break;
3421 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003422 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 && xp->xp_context == EXPAND_FUNCTIONS
3424 && vim_strchr(xp->xp_pattern, '(') == NULL)
3425 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003426 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 break;
3428 }
3429 else if (cmdidx != CMD_let || got_eq)
3430 {
3431 if (c == '"') /* string */
3432 {
3433 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3434 if (c == '\\' && xp->xp_pattern[1] != NUL)
3435 ++xp->xp_pattern;
3436 xp->xp_context = EXPAND_NOTHING;
3437 }
3438 else if (c == '\'') /* literal string */
3439 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003440 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3442 /* skip */ ;
3443 xp->xp_context = EXPAND_NOTHING;
3444 }
3445 else if (c == '|')
3446 {
3447 if (xp->xp_pattern[1] == '|')
3448 {
3449 ++xp->xp_pattern;
3450 xp->xp_context = EXPAND_EXPRESSION;
3451 }
3452 else
3453 xp->xp_context = EXPAND_COMMANDS;
3454 }
3455 else
3456 xp->xp_context = EXPAND_EXPRESSION;
3457 }
3458 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003459 /* Doesn't look like something valid, expand as an expression
3460 * anyway. */
3461 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 arg = xp->xp_pattern;
3463 if (*arg != NUL)
3464 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3465 /* skip */ ;
3466 }
3467 xp->xp_pattern = arg;
3468}
3469
3470#endif /* FEAT_CMDL_COMPL */
3471
3472/*
3473 * ":1,25call func(arg1, arg2)" function call.
3474 */
3475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003476ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478 char_u *arg = eap->arg;
3479 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003481 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003483 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 linenr_T lnum;
3485 int doesrange;
3486 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003487 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003488 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003490 if (eap->skip)
3491 {
3492 /* trans_function_name() doesn't work well when skipping, use eval0()
3493 * instead to skip to any following command, e.g. for:
3494 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003495 ++emsg_skip;
3496 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3497 clear_tv(&rettv);
3498 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003499 return;
3500 }
3501
Bram Moolenaar65639032016-03-16 21:40:30 +01003502 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003503 if (fudi.fd_newkey != NULL)
3504 {
3505 /* Still need to give an error message for missing key. */
3506 EMSG2(_(e_dictkey), fudi.fd_newkey);
3507 vim_free(fudi.fd_newkey);
3508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003509 if (tofree == NULL)
3510 return;
3511
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003512 /* Increase refcount on dictionary, it could get deleted when evaluating
3513 * the arguments. */
3514 if (fudi.fd_dict != NULL)
3515 ++fudi.fd_dict->dv_refcount;
3516
Bram Moolenaar65639032016-03-16 21:40:30 +01003517 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3518 * contents. For VAR_PARTIAL get its partial, unless we already have one
3519 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003520 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003521 name = deref_func_name(tofree, &len,
3522 partial != NULL ? NULL : &partial, FALSE);
3523
Bram Moolenaar532c7802005-01-27 14:44:31 +00003524 /* Skip white space to allow ":call func ()". Not good, but required for
3525 * backward compatibility. */
3526 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003527 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
3529 if (*startarg != '(')
3530 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003531 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 goto end;
3533 }
3534
3535 /*
3536 * When skipping, evaluate the function once, to find the end of the
3537 * arguments.
3538 * When the function takes a range, this is discovered after the first
3539 * call, and the loop is broken.
3540 */
3541 if (eap->skip)
3542 {
3543 ++emsg_skip;
3544 lnum = eap->line2; /* do it once, also with an invalid range */
3545 }
3546 else
3547 lnum = eap->line1;
3548 for ( ; lnum <= eap->line2; ++lnum)
3549 {
3550 if (!eap->skip && eap->addr_count > 0)
3551 {
3552 curwin->w_cursor.lnum = lnum;
3553 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003554#ifdef FEAT_VIRTUALEDIT
3555 curwin->w_cursor.coladd = 0;
3556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003559 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003560 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003561 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
3563 failed = TRUE;
3564 break;
3565 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003566
3567 /* Handle a function returning a Funcref, Dictionary or List. */
3568 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3569 {
3570 failed = TRUE;
3571 break;
3572 }
3573
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003574 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 if (doesrange || eap->skip)
3576 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003577
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003579 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003580 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003581 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 if (aborting())
3583 break;
3584 }
3585 if (eap->skip)
3586 --emsg_skip;
3587
3588 if (!failed)
3589 {
3590 /* Check for trailing illegal characters and a following command. */
3591 if (!ends_excmd(*arg))
3592 {
3593 emsg_severe = TRUE;
3594 EMSG(_(e_trailing));
3595 }
3596 else
3597 eap->nextcmd = check_nextcmd(arg);
3598 }
3599
3600end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003601 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003602 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603}
3604
3605/*
3606 * ":unlet[!] var1 ... " command.
3607 */
3608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003609ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611 ex_unletlock(eap, eap->arg, 0);
3612}
3613
3614/*
3615 * ":lockvar" and ":unlockvar" commands
3616 */
3617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003618ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003619{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003621 int deep = 2;
3622
3623 if (eap->forceit)
3624 deep = -1;
3625 else if (vim_isdigit(*arg))
3626 {
3627 deep = getdigits(&arg);
3628 arg = skipwhite(arg);
3629 }
3630
3631 ex_unletlock(eap, arg, deep);
3632}
3633
3634/*
3635 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3636 */
3637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003638ex_unletlock(
3639 exarg_T *eap,
3640 char_u *argstart,
3641 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003642{
3643 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 do
3649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003651 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003652 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 if (lv.ll_name == NULL)
3654 error = TRUE; /* error but continue parsing */
3655 if (name_end == NULL || (!vim_iswhite(*name_end)
3656 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 if (name_end != NULL)
3659 {
3660 emsg_severe = TRUE;
3661 EMSG(_(e_trailing));
3662 }
3663 if (!(eap->skip || error))
3664 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 break;
3666 }
3667
3668 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003669 {
3670 if (eap->cmdidx == CMD_unlet)
3671 {
3672 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3673 error = TRUE;
3674 }
3675 else
3676 {
3677 if (do_lock_var(&lv, name_end, deep,
3678 eap->cmdidx == CMD_lockvar) == FAIL)
3679 error = TRUE;
3680 }
3681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 if (!eap->skip)
3684 clear_lval(&lv);
3685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 arg = skipwhite(name_end);
3687 } while (!ends_excmd(*arg));
3688
3689 eap->nextcmd = check_nextcmd(arg);
3690}
3691
Bram Moolenaar8c711452005-01-14 21:53:12 +00003692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003693do_unlet_var(
3694 lval_T *lp,
3695 char_u *name_end,
3696 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003697{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 int ret = OK;
3699 int cc;
3700
3701 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 cc = *name_end;
3704 *name_end = NUL;
3705
3706 /* Normal name or expanded name. */
3707 if (check_changedtick(lp->ll_name))
3708 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003709 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003712 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003713 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003714 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003715 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003716 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003717 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 else if (lp->ll_range)
3719 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003720 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003721 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003722 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003723
3724 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3725 {
3726 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003727 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003728 return FAIL;
3729 ll_li = li;
3730 ++ll_n1;
3731 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003732
3733 /* Delete a range of List items. */
3734 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3735 {
3736 li = lp->ll_li->li_next;
3737 listitem_remove(lp->ll_list, lp->ll_li);
3738 lp->ll_li = li;
3739 ++lp->ll_n1;
3740 }
3741 }
3742 else
3743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003744 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003745 /* unlet a List item. */
3746 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003747 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003748 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003749 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003750 }
3751
3752 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003753}
3754
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755/*
3756 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003757 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 */
3759 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003760do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761{
Bram Moolenaar33570922005-01-25 22:26:29 +00003762 hashtab_T *ht;
3763 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003764 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003765 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003766 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
Bram Moolenaar33570922005-01-25 22:26:29 +00003768 ht = find_var_ht(name, &varname);
3769 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003771 if (ht == &globvarht)
3772 d = &globvardict;
3773 else if (current_funccal != NULL
3774 && ht == &current_funccal->l_vars.dv_hashtab)
3775 d = &current_funccal->l_vars;
3776 else if (ht == &compat_hashtab)
3777 d = &vimvardict;
3778 else
3779 {
3780 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3781 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3782 }
3783 if (d == NULL)
3784 {
3785 EMSG2(_(e_intern2), "do_unlet()");
3786 return FAIL;
3787 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003788 hi = hash_find(ht, varname);
3789 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003790 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003791 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003792 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003793 || var_check_ro(di->di_flags, name, FALSE)
3794 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003795 return FAIL;
3796
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003797 delete_var(ht, hi);
3798 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003801 if (forceit)
3802 return OK;
3803 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return FAIL;
3805}
3806
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003807/*
3808 * Lock or unlock variable indicated by "lp".
3809 * "deep" is the levels to go (-1 for unlimited);
3810 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3811 */
3812 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003813do_lock_var(
3814 lval_T *lp,
3815 char_u *name_end,
3816 int deep,
3817 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818{
3819 int ret = OK;
3820 int cc;
3821 dictitem_T *di;
3822
3823 if (deep == 0) /* nothing to do */
3824 return OK;
3825
3826 if (lp->ll_tv == NULL)
3827 {
3828 cc = *name_end;
3829 *name_end = NUL;
3830
3831 /* Normal name or expanded name. */
3832 if (check_changedtick(lp->ll_name))
3833 ret = FAIL;
3834 else
3835 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003836 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003837 if (di == NULL)
3838 ret = FAIL;
3839 else
3840 {
3841 if (lock)
3842 di->di_flags |= DI_FLAGS_LOCK;
3843 else
3844 di->di_flags &= ~DI_FLAGS_LOCK;
3845 item_lock(&di->di_tv, deep, lock);
3846 }
3847 }
3848 *name_end = cc;
3849 }
3850 else if (lp->ll_range)
3851 {
3852 listitem_T *li = lp->ll_li;
3853
3854 /* (un)lock a range of List items. */
3855 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3856 {
3857 item_lock(&li->li_tv, deep, lock);
3858 li = li->li_next;
3859 ++lp->ll_n1;
3860 }
3861 }
3862 else if (lp->ll_list != NULL)
3863 /* (un)lock a List item. */
3864 item_lock(&lp->ll_li->li_tv, deep, lock);
3865 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003866 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003867 item_lock(&lp->ll_di->di_tv, deep, lock);
3868
3869 return ret;
3870}
3871
3872/*
3873 * Lock or unlock an item. "deep" is nr of levels to go.
3874 */
3875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003876item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003877{
3878 static int recurse = 0;
3879 list_T *l;
3880 listitem_T *li;
3881 dict_T *d;
3882 hashitem_T *hi;
3883 int todo;
3884
3885 if (recurse >= DICT_MAXNEST)
3886 {
3887 EMSG(_("E743: variable nested too deep for (un)lock"));
3888 return;
3889 }
3890 if (deep == 0)
3891 return;
3892 ++recurse;
3893
3894 /* lock/unlock the item itself */
3895 if (lock)
3896 tv->v_lock |= VAR_LOCKED;
3897 else
3898 tv->v_lock &= ~VAR_LOCKED;
3899
3900 switch (tv->v_type)
3901 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003902 case VAR_UNKNOWN:
3903 case VAR_NUMBER:
3904 case VAR_STRING:
3905 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003906 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003907 case VAR_FLOAT:
3908 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003909 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003910 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003911 break;
3912
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003913 case VAR_LIST:
3914 if ((l = tv->vval.v_list) != NULL)
3915 {
3916 if (lock)
3917 l->lv_lock |= VAR_LOCKED;
3918 else
3919 l->lv_lock &= ~VAR_LOCKED;
3920 if (deep < 0 || deep > 1)
3921 /* recursive: lock/unlock the items the List contains */
3922 for (li = l->lv_first; li != NULL; li = li->li_next)
3923 item_lock(&li->li_tv, deep - 1, lock);
3924 }
3925 break;
3926 case VAR_DICT:
3927 if ((d = tv->vval.v_dict) != NULL)
3928 {
3929 if (lock)
3930 d->dv_lock |= VAR_LOCKED;
3931 else
3932 d->dv_lock &= ~VAR_LOCKED;
3933 if (deep < 0 || deep > 1)
3934 {
3935 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003936 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003937 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3938 {
3939 if (!HASHITEM_EMPTY(hi))
3940 {
3941 --todo;
3942 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3943 }
3944 }
3945 }
3946 }
3947 }
3948 --recurse;
3949}
3950
Bram Moolenaara40058a2005-07-11 22:42:07 +00003951/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003952 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3953 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003954 */
3955 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003956tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003957{
3958 return (tv->v_lock & VAR_LOCKED)
3959 || (tv->v_type == VAR_LIST
3960 && tv->vval.v_list != NULL
3961 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3962 || (tv->v_type == VAR_DICT
3963 && tv->vval.v_dict != NULL
3964 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3965}
3966
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3968/*
3969 * Delete all "menutrans_" variables.
3970 */
3971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003972del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003975 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
Bram Moolenaar33570922005-01-25 22:26:29 +00003977 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003978 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003979 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003980 {
3981 if (!HASHITEM_EMPTY(hi))
3982 {
3983 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003984 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3985 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003986 }
3987 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003988 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989}
3990#endif
3991
3992#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3993
3994/*
3995 * Local string buffer for the next two functions to store a variable name
3996 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3997 * get_user_var_name().
3998 */
3999
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004000static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001
4002static char_u *varnamebuf = NULL;
4003static int varnamebuflen = 0;
4004
4005/*
4006 * Function to concatenate a prefix and a variable name.
4007 */
4008 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004009cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010{
4011 int len;
4012
4013 len = (int)STRLEN(name) + 3;
4014 if (len > varnamebuflen)
4015 {
4016 vim_free(varnamebuf);
4017 len += 10; /* some additional space */
4018 varnamebuf = alloc(len);
4019 if (varnamebuf == NULL)
4020 {
4021 varnamebuflen = 0;
4022 return NULL;
4023 }
4024 varnamebuflen = len;
4025 }
4026 *varnamebuf = prefix;
4027 varnamebuf[1] = ':';
4028 STRCPY(varnamebuf + 2, name);
4029 return varnamebuf;
4030}
4031
4032/*
4033 * Function given to ExpandGeneric() to obtain the list of user defined
4034 * (global/buffer/window/built-in) variable names.
4035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004037get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004039 static long_u gdone;
4040 static long_u bdone;
4041 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004042#ifdef FEAT_WINDOWS
4043 static long_u tdone;
4044#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 static int vidx;
4046 static hashitem_T *hi;
4047 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
4049 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004050 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004051 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004052#ifdef FEAT_WINDOWS
4053 tdone = 0;
4054#endif
4055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056
4057 /* Global variables */
4058 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004060 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 else
4063 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004064 while (HASHITEM_EMPTY(hi))
4065 ++hi;
4066 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4067 return cat_prefix_varname('g', hi->hi_key);
4068 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004070
4071 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004072 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004073 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004075 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004077 else
4078 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004079 while (HASHITEM_EMPTY(hi))
4080 ++hi;
4081 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004085 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 return (char_u *)"b:changedtick";
4087 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004088
4089 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004090 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004091 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004093 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004094 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004095 else
4096 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004097 while (HASHITEM_EMPTY(hi))
4098 ++hi;
4099 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004101
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004102#ifdef FEAT_WINDOWS
4103 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004104 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004105 if (tdone < ht->ht_used)
4106 {
4107 if (tdone++ == 0)
4108 hi = ht->ht_array;
4109 else
4110 ++hi;
4111 while (HASHITEM_EMPTY(hi))
4112 ++hi;
4113 return cat_prefix_varname('t', hi->hi_key);
4114 }
4115#endif
4116
Bram Moolenaar33570922005-01-25 22:26:29 +00004117 /* v: variables */
4118 if (vidx < VV_LEN)
4119 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121 vim_free(varnamebuf);
4122 varnamebuf = NULL;
4123 varnamebuflen = 0;
4124 return NULL;
4125}
4126
4127#endif /* FEAT_CMDL_COMPL */
4128
4129/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004130 * Return TRUE if "pat" matches "text".
4131 * Does not use 'cpo' and always uses 'magic'.
4132 */
4133 static int
4134pattern_match(char_u *pat, char_u *text, int ic)
4135{
4136 int matches = FALSE;
4137 char_u *save_cpo;
4138 regmatch_T regmatch;
4139
4140 /* avoid 'l' flag in 'cpoptions' */
4141 save_cpo = p_cpo;
4142 p_cpo = (char_u *)"";
4143 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4144 if (regmatch.regprog != NULL)
4145 {
4146 regmatch.rm_ic = ic;
4147 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4148 vim_regfree(regmatch.regprog);
4149 }
4150 p_cpo = save_cpo;
4151 return matches;
4152}
4153
4154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * types for expressions.
4156 */
4157typedef enum
4158{
4159 TYPE_UNKNOWN = 0
4160 , TYPE_EQUAL /* == */
4161 , TYPE_NEQUAL /* != */
4162 , TYPE_GREATER /* > */
4163 , TYPE_GEQUAL /* >= */
4164 , TYPE_SMALLER /* < */
4165 , TYPE_SEQUAL /* <= */
4166 , TYPE_MATCH /* =~ */
4167 , TYPE_NOMATCH /* !~ */
4168} exptype_T;
4169
4170/*
4171 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4174 */
4175
4176/*
4177 * Handle zero level expression.
4178 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004180 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 * Return OK or FAIL.
4182 */
4183 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004184eval0(
4185 char_u *arg,
4186 typval_T *rettv,
4187 char_u **nextcmd,
4188 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189{
4190 int ret;
4191 char_u *p;
4192
4193 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if (ret == FAIL || !ends_excmd(*p))
4196 {
4197 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 /*
4200 * Report the invalid expression unless the expression evaluation has
4201 * been cancelled due to an aborting error, an interrupt, or an
4202 * exception.
4203 */
4204 if (!aborting())
4205 EMSG2(_(e_invexpr2), arg);
4206 ret = FAIL;
4207 }
4208 if (nextcmd != NULL)
4209 *nextcmd = check_nextcmd(p);
4210
4211 return ret;
4212}
4213
4214/*
4215 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004216 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 *
4218 * "arg" must point to the first non-white of the expression.
4219 * "arg" is advanced to the next non-white after the recognized expression.
4220 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004221 * Note: "rettv.v_lock" is not set.
4222 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 * Return OK or FAIL.
4224 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004225 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004226eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
4228 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004229 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 /*
4232 * Get the first variable.
4233 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 return FAIL;
4236
4237 if ((*arg)[0] == '?')
4238 {
4239 result = FALSE;
4240 if (evaluate)
4241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 int error = FALSE;
4243
4244 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 if (error)
4248 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250
4251 /*
4252 * Get the second variable.
4253 */
4254 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return FAIL;
4257
4258 /*
4259 * Check for the ":".
4260 */
4261 if ((*arg)[0] != ':')
4262 {
4263 EMSG(_("E109: Missing ':' after '?'"));
4264 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 return FAIL;
4267 }
4268
4269 /*
4270 * Get the third variable.
4271 */
4272 *arg = skipwhite(*arg + 1);
4273 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4274 {
4275 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 return FAIL;
4278 }
4279 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282
4283 return OK;
4284}
4285
4286/*
4287 * Handle first level expression:
4288 * expr2 || expr2 || expr2 logical OR
4289 *
4290 * "arg" must point to the first non-white of the expression.
4291 * "arg" is advanced to the next non-white after the recognized expression.
4292 *
4293 * Return OK or FAIL.
4294 */
4295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004296eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 long result;
4300 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004301 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
4303 /*
4304 * Get the first variable.
4305 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004306 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 return FAIL;
4308
4309 /*
4310 * Repeat until there is no following "||".
4311 */
4312 first = TRUE;
4313 result = FALSE;
4314 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4315 {
4316 if (evaluate && first)
4317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 if (error)
4322 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 first = FALSE;
4324 }
4325
4326 /*
4327 * Get the second variable.
4328 */
4329 *arg = skipwhite(*arg + 2);
4330 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4331 return FAIL;
4332
4333 /*
4334 * Compute the result.
4335 */
4336 if (evaluate && !result)
4337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (error)
4342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 if (evaluate)
4345 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004346 rettv->v_type = VAR_NUMBER;
4347 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 }
4350
4351 return OK;
4352}
4353
4354/*
4355 * Handle second level expression:
4356 * expr3 && expr3 && expr3 logical AND
4357 *
4358 * "arg" must point to the first non-white of the expression.
4359 * "arg" is advanced to the next non-white after the recognized expression.
4360 *
4361 * Return OK or FAIL.
4362 */
4363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004364eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365{
Bram Moolenaar33570922005-01-25 22:26:29 +00004366 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 long result;
4368 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004369 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
4371 /*
4372 * Get the first variable.
4373 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004374 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 return FAIL;
4376
4377 /*
4378 * Repeat until there is no following "&&".
4379 */
4380 first = TRUE;
4381 result = TRUE;
4382 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4383 {
4384 if (evaluate && first)
4385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004388 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004389 if (error)
4390 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 first = FALSE;
4392 }
4393
4394 /*
4395 * Get the second variable.
4396 */
4397 *arg = skipwhite(*arg + 2);
4398 if (eval4(arg, &var2, evaluate && result) == FAIL)
4399 return FAIL;
4400
4401 /*
4402 * Compute the result.
4403 */
4404 if (evaluate && result)
4405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 if (error)
4410 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
4412 if (evaluate)
4413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414 rettv->v_type = VAR_NUMBER;
4415 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
4417 }
4418
4419 return OK;
4420}
4421
4422/*
4423 * Handle third level expression:
4424 * var1 == var2
4425 * var1 =~ var2
4426 * var1 != var2
4427 * var1 !~ var2
4428 * var1 > var2
4429 * var1 >= var2
4430 * var1 < var2
4431 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 * var1 is var2
4433 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 *
4435 * "arg" must point to the first non-white of the expression.
4436 * "arg" is advanced to the next non-white after the recognized expression.
4437 *
4438 * Return OK or FAIL.
4439 */
4440 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004441eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442{
Bram Moolenaar33570922005-01-25 22:26:29 +00004443 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 char_u *p;
4445 int i;
4446 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004447 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004449 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 char_u *s1, *s2;
4451 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 /*
4455 * Get the first variable.
4456 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 return FAIL;
4459
4460 p = *arg;
4461 switch (p[0])
4462 {
4463 case '=': if (p[1] == '=')
4464 type = TYPE_EQUAL;
4465 else if (p[1] == '~')
4466 type = TYPE_MATCH;
4467 break;
4468 case '!': if (p[1] == '=')
4469 type = TYPE_NEQUAL;
4470 else if (p[1] == '~')
4471 type = TYPE_NOMATCH;
4472 break;
4473 case '>': if (p[1] != '=')
4474 {
4475 type = TYPE_GREATER;
4476 len = 1;
4477 }
4478 else
4479 type = TYPE_GEQUAL;
4480 break;
4481 case '<': if (p[1] != '=')
4482 {
4483 type = TYPE_SMALLER;
4484 len = 1;
4485 }
4486 else
4487 type = TYPE_SEQUAL;
4488 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004489 case 'i': if (p[1] == 's')
4490 {
4491 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4492 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004493 i = p[len];
4494 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 {
4496 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4497 type_is = TRUE;
4498 }
4499 }
4500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502
4503 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004504 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 */
4506 if (type != TYPE_UNKNOWN)
4507 {
4508 /* extra question mark appended: ignore case */
4509 if (p[len] == '?')
4510 {
4511 ic = TRUE;
4512 ++len;
4513 }
4514 /* extra '#' appended: match case */
4515 else if (p[len] == '#')
4516 {
4517 ic = FALSE;
4518 ++len;
4519 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004520 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 else
4522 ic = p_ic;
4523
4524 /*
4525 * Get the second variable.
4526 */
4527 *arg = skipwhite(p + len);
4528 if (eval5(arg, &var2, evaluate) == FAIL)
4529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004530 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 return FAIL;
4532 }
4533
4534 if (evaluate)
4535 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004536 if (type_is && rettv->v_type != var2.v_type)
4537 {
4538 /* For "is" a different type always means FALSE, for "notis"
4539 * it means TRUE. */
4540 n1 = (type == TYPE_NEQUAL);
4541 }
4542 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4543 {
4544 if (type_is)
4545 {
4546 n1 = (rettv->v_type == var2.v_type
4547 && rettv->vval.v_list == var2.vval.v_list);
4548 if (type == TYPE_NEQUAL)
4549 n1 = !n1;
4550 }
4551 else if (rettv->v_type != var2.v_type
4552 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4553 {
4554 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004555 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004556 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004557 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004558 clear_tv(rettv);
4559 clear_tv(&var2);
4560 return FAIL;
4561 }
4562 else
4563 {
4564 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004565 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4566 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004567 if (type == TYPE_NEQUAL)
4568 n1 = !n1;
4569 }
4570 }
4571
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004572 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4573 {
4574 if (type_is)
4575 {
4576 n1 = (rettv->v_type == var2.v_type
4577 && rettv->vval.v_dict == var2.vval.v_dict);
4578 if (type == TYPE_NEQUAL)
4579 n1 = !n1;
4580 }
4581 else if (rettv->v_type != var2.v_type
4582 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4583 {
4584 if (rettv->v_type != var2.v_type)
4585 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4586 else
4587 EMSG(_("E736: Invalid operation for Dictionary"));
4588 clear_tv(rettv);
4589 clear_tv(&var2);
4590 return FAIL;
4591 }
4592 else
4593 {
4594 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004595 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4596 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004597 if (type == TYPE_NEQUAL)
4598 n1 = !n1;
4599 }
4600 }
4601
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004602 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4603 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004604 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004605 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004606 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004607 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004608 clear_tv(rettv);
4609 clear_tv(&var2);
4610 return FAIL;
4611 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004612 if ((rettv->v_type == VAR_PARTIAL
4613 && rettv->vval.v_partial == NULL)
4614 || (var2.v_type == VAR_PARTIAL
4615 && var2.vval.v_partial == NULL))
4616 /* when a partial is NULL assume not equal */
4617 n1 = FALSE;
4618 else if (type_is)
4619 {
4620 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4621 /* strings are considered the same if their value is
4622 * the same */
4623 n1 = tv_equal(rettv, &var2, ic, FALSE);
4624 else if (rettv->v_type == VAR_PARTIAL
4625 && var2.v_type == VAR_PARTIAL)
4626 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4627 else
4628 n1 = FALSE;
4629 }
4630 else
4631 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004632 if (type == TYPE_NEQUAL)
4633 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004634 }
4635
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004636#ifdef FEAT_FLOAT
4637 /*
4638 * If one of the two variables is a float, compare as a float.
4639 * When using "=~" or "!~", always compare as string.
4640 */
4641 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4642 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4643 {
4644 float_T f1, f2;
4645
4646 if (rettv->v_type == VAR_FLOAT)
4647 f1 = rettv->vval.v_float;
4648 else
4649 f1 = get_tv_number(rettv);
4650 if (var2.v_type == VAR_FLOAT)
4651 f2 = var2.vval.v_float;
4652 else
4653 f2 = get_tv_number(&var2);
4654 n1 = FALSE;
4655 switch (type)
4656 {
4657 case TYPE_EQUAL: n1 = (f1 == f2); break;
4658 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4659 case TYPE_GREATER: n1 = (f1 > f2); break;
4660 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4661 case TYPE_SMALLER: n1 = (f1 < f2); break;
4662 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4663 case TYPE_UNKNOWN:
4664 case TYPE_MATCH:
4665 case TYPE_NOMATCH: break; /* avoid gcc warning */
4666 }
4667 }
4668#endif
4669
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 /*
4671 * If one of the two variables is a number, compare as a number.
4672 * When using "=~" or "!~", always compare as string.
4673 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004674 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 n1 = get_tv_number(rettv);
4678 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 switch (type)
4680 {
4681 case TYPE_EQUAL: n1 = (n1 == n2); break;
4682 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4683 case TYPE_GREATER: n1 = (n1 > n2); break;
4684 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4685 case TYPE_SMALLER: n1 = (n1 < n2); break;
4686 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4687 case TYPE_UNKNOWN:
4688 case TYPE_MATCH:
4689 case TYPE_NOMATCH: break; /* avoid gcc warning */
4690 }
4691 }
4692 else
4693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004694 s1 = get_tv_string_buf(rettv, buf1);
4695 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4697 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4698 else
4699 i = 0;
4700 n1 = FALSE;
4701 switch (type)
4702 {
4703 case TYPE_EQUAL: n1 = (i == 0); break;
4704 case TYPE_NEQUAL: n1 = (i != 0); break;
4705 case TYPE_GREATER: n1 = (i > 0); break;
4706 case TYPE_GEQUAL: n1 = (i >= 0); break;
4707 case TYPE_SMALLER: n1 = (i < 0); break;
4708 case TYPE_SEQUAL: n1 = (i <= 0); break;
4709
4710 case TYPE_MATCH:
4711 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004712 n1 = pattern_match(s2, s1, ic);
4713 if (type == TYPE_NOMATCH)
4714 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 break;
4716
4717 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4718 }
4719 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004720 clear_tv(rettv);
4721 clear_tv(&var2);
4722 rettv->v_type = VAR_NUMBER;
4723 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
4725 }
4726
4727 return OK;
4728}
4729
4730/*
4731 * Handle fourth level expression:
4732 * + number addition
4733 * - number subtraction
4734 * . string concatenation
4735 *
4736 * "arg" must point to the first non-white of the expression.
4737 * "arg" is advanced to the next non-white after the recognized expression.
4738 *
4739 * Return OK or FAIL.
4740 */
4741 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004742eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
Bram Moolenaar33570922005-01-25 22:26:29 +00004744 typval_T var2;
4745 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004747 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004748#ifdef FEAT_FLOAT
4749 float_T f1 = 0, f2 = 0;
4750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 char_u *s1, *s2;
4752 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4753 char_u *p;
4754
4755 /*
4756 * Get the first variable.
4757 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004758 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 return FAIL;
4760
4761 /*
4762 * Repeat computing, until no '+', '-' or '.' is following.
4763 */
4764 for (;;)
4765 {
4766 op = **arg;
4767 if (op != '+' && op != '-' && op != '.')
4768 break;
4769
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770 if ((op != '+' || rettv->v_type != VAR_LIST)
4771#ifdef FEAT_FLOAT
4772 && (op == '.' || rettv->v_type != VAR_FLOAT)
4773#endif
4774 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 {
4776 /* For "list + ...", an illegal use of the first operand as
4777 * a number cannot be determined before evaluating the 2nd
4778 * operand: if this is also a list, all is ok.
4779 * For "something . ...", "something - ..." or "non-list + ...",
4780 * we know that the first operand needs to be a string or number
4781 * without evaluating the 2nd operand. So check before to avoid
4782 * side effects after an error. */
4783 if (evaluate && get_tv_string_chk(rettv) == NULL)
4784 {
4785 clear_tv(rettv);
4786 return FAIL;
4787 }
4788 }
4789
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 /*
4791 * Get the second variable.
4792 */
4793 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004794 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004796 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 return FAIL;
4798 }
4799
4800 if (evaluate)
4801 {
4802 /*
4803 * Compute the result.
4804 */
4805 if (op == '.')
4806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4808 s2 = get_tv_string_buf_chk(&var2, buf2);
4809 if (s2 == NULL) /* type error ? */
4810 {
4811 clear_tv(rettv);
4812 clear_tv(&var2);
4813 return FAIL;
4814 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004815 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004816 clear_tv(rettv);
4817 rettv->v_type = VAR_STRING;
4818 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004820 else if (op == '+' && rettv->v_type == VAR_LIST
4821 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004822 {
4823 /* concatenate Lists */
4824 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4825 &var3) == FAIL)
4826 {
4827 clear_tv(rettv);
4828 clear_tv(&var2);
4829 return FAIL;
4830 }
4831 clear_tv(rettv);
4832 *rettv = var3;
4833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 else
4835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004836 int error = FALSE;
4837
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004838#ifdef FEAT_FLOAT
4839 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004840 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841 f1 = rettv->vval.v_float;
4842 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004843 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844 else
4845#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004846 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004847 n1 = get_tv_number_chk(rettv, &error);
4848 if (error)
4849 {
4850 /* This can only happen for "list + non-list". For
4851 * "non-list + ..." or "something - ...", we returned
4852 * before evaluating the 2nd operand. */
4853 clear_tv(rettv);
4854 return FAIL;
4855 }
4856#ifdef FEAT_FLOAT
4857 if (var2.v_type == VAR_FLOAT)
4858 f1 = n1;
4859#endif
4860 }
4861#ifdef FEAT_FLOAT
4862 if (var2.v_type == VAR_FLOAT)
4863 {
4864 f2 = var2.vval.v_float;
4865 n2 = 0;
4866 }
4867 else
4868#endif
4869 {
4870 n2 = get_tv_number_chk(&var2, &error);
4871 if (error)
4872 {
4873 clear_tv(rettv);
4874 clear_tv(&var2);
4875 return FAIL;
4876 }
4877#ifdef FEAT_FLOAT
4878 if (rettv->v_type == VAR_FLOAT)
4879 f2 = n2;
4880#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004881 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883
4884#ifdef FEAT_FLOAT
4885 /* If there is a float on either side the result is a float. */
4886 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4887 {
4888 if (op == '+')
4889 f1 = f1 + f2;
4890 else
4891 f1 = f1 - f2;
4892 rettv->v_type = VAR_FLOAT;
4893 rettv->vval.v_float = f1;
4894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004896#endif
4897 {
4898 if (op == '+')
4899 n1 = n1 + n2;
4900 else
4901 n1 = n1 - n2;
4902 rettv->v_type = VAR_NUMBER;
4903 rettv->vval.v_number = n1;
4904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004906 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
4908 }
4909 return OK;
4910}
4911
4912/*
4913 * Handle fifth level expression:
4914 * * number multiplication
4915 * / number division
4916 * % number modulo
4917 *
4918 * "arg" must point to the first non-white of the expression.
4919 * "arg" is advanced to the next non-white after the recognized expression.
4920 *
4921 * Return OK or FAIL.
4922 */
4923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004924eval6(
4925 char_u **arg,
4926 typval_T *rettv,
4927 int evaluate,
4928 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929{
Bram Moolenaar33570922005-01-25 22:26:29 +00004930 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004932 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004933#ifdef FEAT_FLOAT
4934 int use_float = FALSE;
4935 float_T f1 = 0, f2;
4936#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004937 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939 /*
4940 * Get the first variable.
4941 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004942 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 return FAIL;
4944
4945 /*
4946 * Repeat computing, until no '*', '/' or '%' is following.
4947 */
4948 for (;;)
4949 {
4950 op = **arg;
4951 if (op != '*' && op != '/' && op != '%')
4952 break;
4953
4954 if (evaluate)
4955 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956#ifdef FEAT_FLOAT
4957 if (rettv->v_type == VAR_FLOAT)
4958 {
4959 f1 = rettv->vval.v_float;
4960 use_float = TRUE;
4961 n1 = 0;
4962 }
4963 else
4964#endif
4965 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004966 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004967 if (error)
4968 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 }
4970 else
4971 n1 = 0;
4972
4973 /*
4974 * Get the second variable.
4975 */
4976 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004977 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 return FAIL;
4979
4980 if (evaluate)
4981 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982#ifdef FEAT_FLOAT
4983 if (var2.v_type == VAR_FLOAT)
4984 {
4985 if (!use_float)
4986 {
4987 f1 = n1;
4988 use_float = TRUE;
4989 }
4990 f2 = var2.vval.v_float;
4991 n2 = 0;
4992 }
4993 else
4994#endif
4995 {
4996 n2 = get_tv_number_chk(&var2, &error);
4997 clear_tv(&var2);
4998 if (error)
4999 return FAIL;
5000#ifdef FEAT_FLOAT
5001 if (use_float)
5002 f2 = n2;
5003#endif
5004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
5006 /*
5007 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010#ifdef FEAT_FLOAT
5011 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005013 if (op == '*')
5014 f1 = f1 * f2;
5015 else if (op == '/')
5016 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005017# ifdef VMS
5018 /* VMS crashes on divide by zero, work around it */
5019 if (f2 == 0.0)
5020 {
5021 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005022 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005023 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005024 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005025 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005026 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005027 }
5028 else
5029 f1 = f1 / f2;
5030# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005031 /* We rely on the floating point library to handle divide
5032 * by zero to result in "inf" and not a crash. */
5033 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005034# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005037 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005038 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005039 return FAIL;
5040 }
5041 rettv->v_type = VAR_FLOAT;
5042 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005047 if (op == '*')
5048 n1 = n1 * n2;
5049 else if (op == '/')
5050 {
5051 if (n2 == 0) /* give an error message? */
5052 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005053#ifdef FEAT_NUM64
5054 if (n1 == 0)
5055 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
5056 else if (n1 < 0)
5057 n1 = -0x7fffffffffffffff;
5058 else
5059 n1 = 0x7fffffffffffffff;
5060#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005061 if (n1 == 0)
5062 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5063 else if (n1 < 0)
5064 n1 = -0x7fffffffL;
5065 else
5066 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005067#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005068 }
5069 else
5070 n1 = n1 / n2;
5071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 {
5074 if (n2 == 0) /* give an error message? */
5075 n1 = 0;
5076 else
5077 n1 = n1 % n2;
5078 }
5079 rettv->v_type = VAR_NUMBER;
5080 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
5083 }
5084
5085 return OK;
5086}
5087
5088/*
5089 * Handle sixth level expression:
5090 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005091 * "string" string constant
5092 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 * &option-name option value
5094 * @r register contents
5095 * identifier variable value
5096 * function() function call
5097 * $VAR environment variable
5098 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005099 * [expr, expr] List
5100 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 *
5102 * Also handle:
5103 * ! in front logical NOT
5104 * - in front unary minus
5105 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 * trailing [] subscript in String or List
5107 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 *
5109 * "arg" must point to the first non-white of the expression.
5110 * "arg" is advanced to the next non-white after the recognized expression.
5111 *
5112 * Return OK or FAIL.
5113 */
5114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005115eval7(
5116 char_u **arg,
5117 typval_T *rettv,
5118 int evaluate,
5119 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005121 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 int len;
5123 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 char_u *start_leader, *end_leader;
5125 int ret = OK;
5126 char_u *alias;
5127
5128 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005129 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005130 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005132 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 /*
5135 * Skip '!' and '-' characters. They are handled later.
5136 */
5137 start_leader = *arg;
5138 while (**arg == '!' || **arg == '-' || **arg == '+')
5139 *arg = skipwhite(*arg + 1);
5140 end_leader = *arg;
5141
5142 switch (**arg)
5143 {
5144 /*
5145 * Number constant.
5146 */
5147 case '0':
5148 case '1':
5149 case '2':
5150 case '3':
5151 case '4':
5152 case '5':
5153 case '6':
5154 case '7':
5155 case '8':
5156 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005157 {
5158#ifdef FEAT_FLOAT
5159 char_u *p = skipdigits(*arg + 1);
5160 int get_float = FALSE;
5161
5162 /* We accept a float when the format matches
5163 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005164 * strict to avoid backwards compatibility problems.
5165 * Don't look for a float after the "." operator, so that
5166 * ":let vers = 1.2.3" doesn't fail. */
5167 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005169 get_float = TRUE;
5170 p = skipdigits(p + 2);
5171 if (*p == 'e' || *p == 'E')
5172 {
5173 ++p;
5174 if (*p == '-' || *p == '+')
5175 ++p;
5176 if (!vim_isdigit(*p))
5177 get_float = FALSE;
5178 else
5179 p = skipdigits(p + 1);
5180 }
5181 if (ASCII_ISALPHA(*p) || *p == '.')
5182 get_float = FALSE;
5183 }
5184 if (get_float)
5185 {
5186 float_T f;
5187
5188 *arg += string2float(*arg, &f);
5189 if (evaluate)
5190 {
5191 rettv->v_type = VAR_FLOAT;
5192 rettv->vval.v_float = f;
5193 }
5194 }
5195 else
5196#endif
5197 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005198 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005199 *arg += len;
5200 if (evaluate)
5201 {
5202 rettv->v_type = VAR_NUMBER;
5203 rettv->vval.v_number = n;
5204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
5206 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
5209 /*
5210 * String constant: "string".
5211 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005212 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 break;
5214
5215 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005218 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219 break;
5220
5221 /*
5222 * List: [expr, expr]
5223 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 break;
5226
5227 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005228 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 * Dictionary: {key: val, key: val}
5230 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005231 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
5232 if (ret == NOTDONE)
5233 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 break;
5235
5236 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005237 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005239 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 break;
5241
5242 /*
5243 * Environment variable: $VAR.
5244 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005245 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 break;
5247
5248 /*
5249 * Register contents: @r.
5250 */
5251 case '@': ++*arg;
5252 if (evaluate)
5253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005254 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005255 rettv->vval.v_string = get_reg_contents(**arg,
5256 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
5258 if (**arg != NUL)
5259 ++*arg;
5260 break;
5261
5262 /*
5263 * nested expression: (expression).
5264 */
5265 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005266 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 if (**arg == ')')
5268 ++*arg;
5269 else if (ret == OK)
5270 {
5271 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005272 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 ret = FAIL;
5274 }
5275 break;
5276
Bram Moolenaar8c711452005-01-14 21:53:12 +00005277 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 break;
5279 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280
5281 if (ret == NOTDONE)
5282 {
5283 /*
5284 * Must be a variable or function name.
5285 * Can also be a curly-braces kind of name: {expr}.
5286 */
5287 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 if (alias != NULL)
5290 s = alias;
5291
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005292 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005293 ret = FAIL;
5294 else
5295 {
5296 if (**arg == '(') /* recursive! */
5297 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005298 partial_T *partial;
5299
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 /* If "s" is the name of a variable of type VAR_FUNC
5301 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005302 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303
5304 /* Invoke the function. */
5305 ret = get_func_tv(s, len, rettv, arg,
5306 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005307 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005308
5309 /* If evaluate is FALSE rettv->v_type was not set in
5310 * get_func_tv, but it's needed in handle_subscript() to parse
5311 * what follows. So set it here. */
5312 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5313 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005314 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005315 rettv->v_type = VAR_FUNC;
5316 }
5317
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 /* Stop the expression evaluation when immediately
5319 * aborting on error, or when an interrupt occurred or
5320 * an exception was thrown but not caught. */
5321 if (aborting())
5322 {
5323 if (ret == OK)
5324 clear_tv(rettv);
5325 ret = FAIL;
5326 }
5327 }
5328 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005329 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005330 else
5331 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005333 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005334 }
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 *arg = skipwhite(*arg);
5337
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005338 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5339 * expr(expr). */
5340 if (ret == OK)
5341 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
5343 /*
5344 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5345 */
5346 if (ret == OK && evaluate && end_leader > start_leader)
5347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005348 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005349 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005350#ifdef FEAT_FLOAT
5351 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005352
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005353 if (rettv->v_type == VAR_FLOAT)
5354 f = rettv->vval.v_float;
5355 else
5356#endif
5357 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005358 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005360 clear_tv(rettv);
5361 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 else
5364 {
5365 while (end_leader > start_leader)
5366 {
5367 --end_leader;
5368 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005369 {
5370#ifdef FEAT_FLOAT
5371 if (rettv->v_type == VAR_FLOAT)
5372 f = !f;
5373 else
5374#endif
5375 val = !val;
5376 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005377 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005378 {
5379#ifdef FEAT_FLOAT
5380 if (rettv->v_type == VAR_FLOAT)
5381 f = -f;
5382 else
5383#endif
5384 val = -val;
5385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005386 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005387#ifdef FEAT_FLOAT
5388 if (rettv->v_type == VAR_FLOAT)
5389 {
5390 clear_tv(rettv);
5391 rettv->vval.v_float = f;
5392 }
5393 else
5394#endif
5395 {
5396 clear_tv(rettv);
5397 rettv->v_type = VAR_NUMBER;
5398 rettv->vval.v_number = val;
5399 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402
5403 return ret;
5404}
5405
5406/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005407 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5408 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5410 */
5411 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005412eval_index(
5413 char_u **arg,
5414 typval_T *rettv,
5415 int evaluate,
5416 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417{
5418 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005419 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005421 long len = -1;
5422 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425
Bram Moolenaara03f2332016-02-06 18:09:59 +01005426 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005428 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005429 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005430 if (verbose)
5431 EMSG(_("E695: Cannot index a Funcref"));
5432 return FAIL;
5433 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005434#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005435 if (verbose)
5436 EMSG(_(e_float_as_string));
5437 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005438#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005439 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005440 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005441 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005442 if (verbose)
5443 EMSG(_("E909: Cannot index a special variable"));
5444 return FAIL;
5445 case VAR_UNKNOWN:
5446 if (evaluate)
5447 return FAIL;
5448 /* FALLTHROUGH */
5449
5450 case VAR_STRING:
5451 case VAR_NUMBER:
5452 case VAR_LIST:
5453 case VAR_DICT:
5454 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005455 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005457 init_tv(&var1);
5458 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461 /*
5462 * dict.name
5463 */
5464 key = *arg + 1;
5465 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5466 ;
5467 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 }
5471 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005473 /*
5474 * something[idx]
5475 *
5476 * Get the (first) variable from inside the [].
5477 */
5478 *arg = skipwhite(*arg + 1);
5479 if (**arg == ':')
5480 empty1 = TRUE;
5481 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5482 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005483 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5484 {
5485 /* not a number or string */
5486 clear_tv(&var1);
5487 return FAIL;
5488 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489
5490 /*
5491 * Get the second variable from inside the [:].
5492 */
5493 if (**arg == ':')
5494 {
5495 range = TRUE;
5496 *arg = skipwhite(*arg + 1);
5497 if (**arg == ']')
5498 empty2 = TRUE;
5499 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005501 if (!empty1)
5502 clear_tv(&var1);
5503 return FAIL;
5504 }
5505 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5506 {
5507 /* not a number or string */
5508 if (!empty1)
5509 clear_tv(&var1);
5510 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511 return FAIL;
5512 }
5513 }
5514
5515 /* Check for the ']'. */
5516 if (**arg != ']')
5517 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005518 if (verbose)
5519 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005520 clear_tv(&var1);
5521 if (range)
5522 clear_tv(&var2);
5523 return FAIL;
5524 }
5525 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 }
5527
5528 if (evaluate)
5529 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005530 n1 = 0;
5531 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 n1 = get_tv_number(&var1);
5534 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 }
5536 if (range)
5537 {
5538 if (empty2)
5539 n2 = -1;
5540 else
5541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 n2 = get_tv_number(&var2);
5543 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 }
5545 }
5546
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005547 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005549 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005550 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005551 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005552 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005553 case VAR_SPECIAL:
5554 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005555 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005556 break; /* not evaluating, skipping over subscript */
5557
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 case VAR_NUMBER:
5559 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 len = (long)STRLEN(s);
5562 if (range)
5563 {
5564 /* The resulting variable is a substring. If the indexes
5565 * are out of range the result is empty. */
5566 if (n1 < 0)
5567 {
5568 n1 = len + n1;
5569 if (n1 < 0)
5570 n1 = 0;
5571 }
5572 if (n2 < 0)
5573 n2 = len + n2;
5574 else if (n2 >= len)
5575 n2 = len;
5576 if (n1 >= len || n2 < 0 || n1 > n2)
5577 s = NULL;
5578 else
5579 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5580 }
5581 else
5582 {
5583 /* The resulting variable is a string of a single
5584 * character. If the index is too big or negative the
5585 * result is empty. */
5586 if (n1 >= len || n1 < 0)
5587 s = NULL;
5588 else
5589 s = vim_strnsave(s + n1, 1);
5590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005591 clear_tv(rettv);
5592 rettv->v_type = VAR_STRING;
5593 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 break;
5595
5596 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005597 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 if (n1 < 0)
5599 n1 = len + n1;
5600 if (!empty1 && (n1 < 0 || n1 >= len))
5601 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005602 /* For a range we allow invalid values and return an empty
5603 * list. A list index out of range is an error. */
5604 if (!range)
5605 {
5606 if (verbose)
5607 EMSGN(_(e_listidx), n1);
5608 return FAIL;
5609 }
5610 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005611 }
5612 if (range)
5613 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 list_T *l;
5615 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616
5617 if (n2 < 0)
5618 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005619 else if (n2 >= len)
5620 n2 = len - 1;
5621 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005622 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 l = list_alloc();
5624 if (l == NULL)
5625 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005626 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 n1 <= n2; ++n1)
5628 {
5629 if (list_append_tv(l, &item->li_tv) == FAIL)
5630 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005631 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 return FAIL;
5633 }
5634 item = item->li_next;
5635 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005636 clear_tv(rettv);
5637 rettv->v_type = VAR_LIST;
5638 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005639 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 }
5641 else
5642 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005643 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 clear_tv(rettv);
5645 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 }
5647 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648
5649 case VAR_DICT:
5650 if (range)
5651 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005652 if (verbose)
5653 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 if (len == -1)
5655 clear_tv(&var1);
5656 return FAIL;
5657 }
5658 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005659 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660
5661 if (len == -1)
5662 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005663 key = get_tv_string_chk(&var1);
5664 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 clear_tv(&var1);
5667 return FAIL;
5668 }
5669 }
5670
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005671 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005673 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005674 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 if (len == -1)
5676 clear_tv(&var1);
5677 if (item == NULL)
5678 return FAIL;
5679
5680 copy_tv(&item->di_tv, &var1);
5681 clear_tv(rettv);
5682 *rettv = var1;
5683 }
5684 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005685 }
5686 }
5687
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 return OK;
5689}
5690
5691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 * Get an option value.
5693 * "arg" points to the '&' or '+' before the option name.
5694 * "arg" is advanced to character after the option name.
5695 * Return OK or FAIL.
5696 */
5697 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005698get_option_tv(
5699 char_u **arg,
5700 typval_T *rettv, /* when NULL, only check if option exists */
5701 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
5703 char_u *option_end;
5704 long numval;
5705 char_u *stringval;
5706 int opt_type;
5707 int c;
5708 int working = (**arg == '+'); /* has("+option") */
5709 int ret = OK;
5710 int opt_flags;
5711
5712 /*
5713 * Isolate the option name and find its value.
5714 */
5715 option_end = find_option_end(arg, &opt_flags);
5716 if (option_end == NULL)
5717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005718 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 EMSG2(_("E112: Option name missing: %s"), *arg);
5720 return FAIL;
5721 }
5722
5723 if (!evaluate)
5724 {
5725 *arg = option_end;
5726 return OK;
5727 }
5728
5729 c = *option_end;
5730 *option_end = NUL;
5731 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005732 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
5734 if (opt_type == -3) /* invalid name */
5735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005736 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 EMSG2(_("E113: Unknown option: %s"), *arg);
5738 ret = FAIL;
5739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005740 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
5742 if (opt_type == -2) /* hidden string option */
5743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005744 rettv->v_type = VAR_STRING;
5745 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
5747 else if (opt_type == -1) /* hidden number option */
5748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005749 rettv->v_type = VAR_NUMBER;
5750 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
5752 else if (opt_type == 1) /* number option */
5753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005754 rettv->v_type = VAR_NUMBER;
5755 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
5757 else /* string option */
5758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005759 rettv->v_type = VAR_STRING;
5760 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
5762 }
5763 else if (working && (opt_type == -2 || opt_type == -1))
5764 ret = FAIL;
5765
5766 *option_end = c; /* put back for error messages */
5767 *arg = option_end;
5768
5769 return ret;
5770}
5771
5772/*
5773 * Allocate a variable for a string constant.
5774 * Return OK or FAIL.
5775 */
5776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005777get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
5779 char_u *p;
5780 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 int extra = 0;
5782
5783 /*
5784 * Find the end of the string, skipping backslashed characters.
5785 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
5788 if (*p == '\\' && p[1] != NUL)
5789 {
5790 ++p;
5791 /* A "\<x>" form occupies at least 4 characters, and produces up
5792 * to 6 characters: reserve space for 2 extra */
5793 if (*p == '<')
5794 extra += 2;
5795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 }
5797
5798 if (*p != '"')
5799 {
5800 EMSG2(_("E114: Missing quote: %s"), *arg);
5801 return FAIL;
5802 }
5803
5804 /* If only parsing, set *arg and return here */
5805 if (!evaluate)
5806 {
5807 *arg = p + 1;
5808 return OK;
5809 }
5810
5811 /*
5812 * Copy the string into allocated memory, handling backslashed
5813 * characters.
5814 */
5815 name = alloc((unsigned)(p - *arg + extra));
5816 if (name == NULL)
5817 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 rettv->v_type = VAR_STRING;
5819 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 {
5823 if (*p == '\\')
5824 {
5825 switch (*++p)
5826 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 case 'b': *name++ = BS; ++p; break;
5828 case 'e': *name++ = ESC; ++p; break;
5829 case 'f': *name++ = FF; ++p; break;
5830 case 'n': *name++ = NL; ++p; break;
5831 case 'r': *name++ = CAR; ++p; break;
5832 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
5834 case 'X': /* hex: "\x1", "\x12" */
5835 case 'x':
5836 case 'u': /* Unicode: "\u0023" */
5837 case 'U':
5838 if (vim_isxdigit(p[1]))
5839 {
5840 int n, nr;
5841 int c = toupper(*p);
5842
5843 if (c == 'X')
5844 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005845 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005847 else
5848 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 nr = 0;
5850 while (--n >= 0 && vim_isxdigit(p[1]))
5851 {
5852 ++p;
5853 nr = (nr << 4) + hex2nr(*p);
5854 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856#ifdef FEAT_MBYTE
5857 /* For "\u" store the number according to
5858 * 'encoding'. */
5859 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 else
5862#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 break;
5866
5867 /* octal: "\1", "\12", "\123" */
5868 case '0':
5869 case '1':
5870 case '2':
5871 case '3':
5872 case '4':
5873 case '5':
5874 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 case '7': *name = *p++ - '0';
5876 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 *name = (*name << 3) + *p++ - '0';
5879 if (*p >= '0' && *p <= '7')
5880 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 break;
5884
5885 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 if (extra != 0)
5888 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 break;
5891 }
5892 /* FALLTHROUGH */
5893
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 break;
5896 }
5897 }
5898 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005902 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 *arg = p + 1;
5904
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 return OK;
5906}
5907
5908/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005909 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 * Return OK or FAIL.
5911 */
5912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005913get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914{
5915 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005916 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917 int reduce = 0;
5918
5919 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005921 */
5922 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5923 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 break;
5928 ++reduce;
5929 ++p;
5930 }
5931 }
5932
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005935 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936 return FAIL;
5937 }
5938
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940 if (!evaluate)
5941 {
5942 *arg = p + 1;
5943 return OK;
5944 }
5945
5946 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005947 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005948 */
5949 str = alloc((unsigned)((p - *arg) - reduce));
5950 if (str == NULL)
5951 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005952 rettv->v_type = VAR_STRING;
5953 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005957 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005959 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005960 break;
5961 ++p;
5962 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005963 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005964 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005965 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005966 *arg = p + 1;
5967
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005968 return OK;
5969}
5970
Bram Moolenaarddecc252016-04-06 22:59:37 +02005971 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005972partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005973{
5974 int i;
5975
5976 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005977 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005978 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005979 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005980 func_unref(pt->pt_name);
5981 vim_free(pt->pt_name);
5982 vim_free(pt);
5983}
5984
5985/*
5986 * Unreference a closure: decrement the reference count and free it when it
5987 * becomes zero.
5988 */
5989 void
5990partial_unref(partial_T *pt)
5991{
5992 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005993 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005994}
5995
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005996/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 * Allocate a variable for a List and fill it from "*arg".
5998 * Return OK or FAIL.
5999 */
6000 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006001get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002{
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 list_T *l = NULL;
6004 typval_T tv;
6005 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006
6007 if (evaluate)
6008 {
6009 l = list_alloc();
6010 if (l == NULL)
6011 return FAIL;
6012 }
6013
6014 *arg = skipwhite(*arg + 1);
6015 while (**arg != ']' && **arg != NUL)
6016 {
6017 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6018 goto failret;
6019 if (evaluate)
6020 {
6021 item = listitem_alloc();
6022 if (item != NULL)
6023 {
6024 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006025 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 list_append(l, item);
6027 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006028 else
6029 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 }
6031
6032 if (**arg == ']')
6033 break;
6034 if (**arg != ',')
6035 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006036 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 goto failret;
6038 }
6039 *arg = skipwhite(*arg + 1);
6040 }
6041
6042 if (**arg != ']')
6043 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006044 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045failret:
6046 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006047 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 return FAIL;
6049 }
6050
6051 *arg = skipwhite(*arg + 1);
6052 if (evaluate)
6053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006054 rettv->v_type = VAR_LIST;
6055 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 ++l->lv_refcount;
6057 }
6058
6059 return OK;
6060}
6061
6062/*
6063 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006064 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006066 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006067list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006069 list_T *l;
6070
6071 l = (list_T *)alloc_clear(sizeof(list_T));
6072 if (l != NULL)
6073 {
6074 /* Prepend the list to the list of lists for garbage collection. */
6075 if (first_list != NULL)
6076 first_list->lv_used_prev = l;
6077 l->lv_used_prev = NULL;
6078 l->lv_used_next = first_list;
6079 first_list = l;
6080 }
6081 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082}
6083
6084/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006085 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006086 * Returns OK or FAIL.
6087 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006088 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006089rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006090{
6091 list_T *l = list_alloc();
6092
6093 if (l == NULL)
6094 return FAIL;
6095
6096 rettv->vval.v_list = l;
6097 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006098 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006099 ++l->lv_refcount;
6100 return OK;
6101}
6102
6103/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006104 * Unreference a list: decrement the reference count and free it when it
6105 * becomes zero.
6106 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006108list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006109{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006110 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006111 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006112}
6113
6114/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006115 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116 * Ignores the reference count.
6117 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006118 static void
6119list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120{
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006123 for (item = l->lv_first; item != NULL; item = l->lv_first)
6124 {
6125 /* Remove the item before deleting it. */
6126 l->lv_first = item->li_next;
6127 clear_tv(&item->li_tv);
6128 vim_free(item);
6129 }
6130}
6131
6132 static void
6133list_free_list(list_T *l)
6134{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006135 /* Remove the list from the list of lists for garbage collection. */
6136 if (l->lv_used_prev == NULL)
6137 first_list = l->lv_used_next;
6138 else
6139 l->lv_used_prev->lv_used_next = l->lv_used_next;
6140 if (l->lv_used_next != NULL)
6141 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6142
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143 vim_free(l);
6144}
6145
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006146 void
6147list_free(list_T *l)
6148{
6149 if (!in_free_unref_items)
6150 {
6151 list_free_contents(l);
6152 list_free_list(l);
6153 }
6154}
6155
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006156/*
6157 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006158 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006160 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006161listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162{
Bram Moolenaar33570922005-01-25 22:26:29 +00006163 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164}
6165
6166/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006167 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006170listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006172 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 vim_free(item);
6174}
6175
6176/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006177 * Remove a list item from a List and free it. Also clears the value.
6178 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006179 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006180listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006181{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006182 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006183 listitem_free(item);
6184}
6185
6186/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006187 * Get the number of items in a list.
6188 */
6189 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006190list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192 if (l == NULL)
6193 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006194 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195}
6196
6197/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006198 * Return TRUE when two lists have exactly the same values.
6199 */
6200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006201list_equal(
6202 list_T *l1,
6203 list_T *l2,
6204 int ic, /* ignore case for strings */
6205 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206{
Bram Moolenaar33570922005-01-25 22:26:29 +00006207 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006208
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006209 if (l1 == NULL || l2 == NULL)
6210 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006211 if (l1 == l2)
6212 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006213 if (list_len(l1) != list_len(l2))
6214 return FALSE;
6215
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006216 for (item1 = l1->lv_first, item2 = l2->lv_first;
6217 item1 != NULL && item2 != NULL;
6218 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006219 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006220 return FALSE;
6221 return item1 == NULL && item2 == NULL;
6222}
6223
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006224static int tv_equal_recurse_limit;
6225
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006226 static int
6227func_equal(
6228 typval_T *tv1,
6229 typval_T *tv2,
6230 int ic) /* ignore case */
6231{
6232 char_u *s1, *s2;
6233 dict_T *d1, *d2;
6234 int a1, a2;
6235 int i;
6236
6237 /* empty and NULL function name considered the same */
6238 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6239 : tv1->vval.v_partial->pt_name;
6240 if (s1 != NULL && *s1 == NUL)
6241 s1 = NULL;
6242 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6243 : tv2->vval.v_partial->pt_name;
6244 if (s2 != NULL && *s2 == NUL)
6245 s2 = NULL;
6246 if (s1 == NULL || s2 == NULL)
6247 {
6248 if (s1 != s2)
6249 return FALSE;
6250 }
6251 else if (STRCMP(s1, s2) != 0)
6252 return FALSE;
6253
6254 /* empty dict and NULL dict is different */
6255 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6256 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6257 if (d1 == NULL || d2 == NULL)
6258 {
6259 if (d1 != d2)
6260 return FALSE;
6261 }
6262 else if (!dict_equal(d1, d2, ic, TRUE))
6263 return FALSE;
6264
6265 /* empty list and no list considered the same */
6266 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6267 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6268 if (a1 != a2)
6269 return FALSE;
6270 for (i = 0; i < a1; ++i)
6271 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6272 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6273 return FALSE;
6274
6275 return TRUE;
6276}
6277
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006278/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006279 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006280 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006281 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006282 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006283 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006284tv_equal(
6285 typval_T *tv1,
6286 typval_T *tv2,
6287 int ic, /* ignore case */
6288 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006289{
6290 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006291 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006292 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006293 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006294
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006295 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006296 * recursiveness to a limit. We guess they are equal then.
6297 * A fixed limit has the problem of still taking an awful long time.
6298 * Reduce the limit every time running into it. That should work fine for
6299 * deeply linked structures that are not recursively linked and catch
6300 * recursiveness quickly. */
6301 if (!recursive)
6302 tv_equal_recurse_limit = 1000;
6303 if (recursive_cnt >= tv_equal_recurse_limit)
6304 {
6305 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006306 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006307 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006308
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02006309 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
6310 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006311 if ((tv1->v_type == VAR_FUNC
6312 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6313 && (tv2->v_type == VAR_FUNC
6314 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6315 {
6316 ++recursive_cnt;
6317 r = func_equal(tv1, tv2, ic);
6318 --recursive_cnt;
6319 return r;
6320 }
6321
6322 if (tv1->v_type != tv2->v_type)
6323 return FALSE;
6324
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006325 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006326 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006327 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006328 ++recursive_cnt;
6329 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6330 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006331 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006332
6333 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006334 ++recursive_cnt;
6335 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6336 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006337 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006338
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006339 case VAR_NUMBER:
6340 return tv1->vval.v_number == tv2->vval.v_number;
6341
6342 case VAR_STRING:
6343 s1 = get_tv_string_buf(tv1, buf1);
6344 s2 = get_tv_string_buf(tv2, buf2);
6345 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006346
6347 case VAR_SPECIAL:
6348 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006349
6350 case VAR_FLOAT:
6351#ifdef FEAT_FLOAT
6352 return tv1->vval.v_float == tv2->vval.v_float;
6353#endif
6354 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006355#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006356 return tv1->vval.v_job == tv2->vval.v_job;
6357#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006358 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006359#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006360 return tv1->vval.v_channel == tv2->vval.v_channel;
6361#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006362 case VAR_FUNC:
6363 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006364 case VAR_UNKNOWN:
6365 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006366 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006367
Bram Moolenaara03f2332016-02-06 18:09:59 +01006368 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6369 * does not equal anything, not even itself. */
6370 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006371}
6372
6373/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006374 * Locate item with index "n" in list "l" and return it.
6375 * A negative index is counted from the end; -1 is the last item.
6376 * Returns NULL when "n" is out of range.
6377 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006378 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006379list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380{
Bram Moolenaar33570922005-01-25 22:26:29 +00006381 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382 long idx;
6383
6384 if (l == NULL)
6385 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006386
6387 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006389 n = l->lv_len + n;
6390
6391 /* Check for index out of range. */
6392 if (n < 0 || n >= l->lv_len)
6393 return NULL;
6394
6395 /* When there is a cached index may start search from there. */
6396 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006398 if (n < l->lv_idx / 2)
6399 {
6400 /* closest to the start of the list */
6401 item = l->lv_first;
6402 idx = 0;
6403 }
6404 else if (n > (l->lv_idx + l->lv_len) / 2)
6405 {
6406 /* closest to the end of the list */
6407 item = l->lv_last;
6408 idx = l->lv_len - 1;
6409 }
6410 else
6411 {
6412 /* closest to the cached index */
6413 item = l->lv_idx_item;
6414 idx = l->lv_idx;
6415 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 }
6417 else
6418 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006419 if (n < l->lv_len / 2)
6420 {
6421 /* closest to the start of the list */
6422 item = l->lv_first;
6423 idx = 0;
6424 }
6425 else
6426 {
6427 /* closest to the end of the list */
6428 item = l->lv_last;
6429 idx = l->lv_len - 1;
6430 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432
6433 while (n > idx)
6434 {
6435 /* search forward */
6436 item = item->li_next;
6437 ++idx;
6438 }
6439 while (n < idx)
6440 {
6441 /* search backward */
6442 item = item->li_prev;
6443 --idx;
6444 }
6445
6446 /* cache the used index */
6447 l->lv_idx = idx;
6448 l->lv_idx_item = item;
6449
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 return item;
6451}
6452
6453/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006454 * Get list item "l[idx]" as a number.
6455 */
6456 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006457list_find_nr(
6458 list_T *l,
6459 long idx,
6460 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006461{
6462 listitem_T *li;
6463
6464 li = list_find(l, idx);
6465 if (li == NULL)
6466 {
6467 if (errorp != NULL)
6468 *errorp = TRUE;
6469 return -1L;
6470 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006471 return (long)get_tv_number_chk(&li->li_tv, errorp);
Bram Moolenaara5525202006-03-02 22:52:09 +00006472}
6473
6474/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006475 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6476 */
6477 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006479{
6480 listitem_T *li;
6481
6482 li = list_find(l, idx - 1);
6483 if (li == NULL)
6484 {
6485 EMSGN(_(e_listidx), idx);
6486 return NULL;
6487 }
6488 return get_tv_string(&li->li_tv);
6489}
6490
6491/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006492 * Locate "item" list "l" and return its index.
6493 * Returns -1 when "item" is not in the list.
6494 */
6495 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006496list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006497{
6498 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006500
6501 if (l == NULL)
6502 return -1;
6503 idx = 0;
6504 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6505 ++idx;
6506 if (li == NULL)
6507 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006508 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006509}
6510
6511/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 * Append item "item" to the end of list "l".
6513 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006514 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006515list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006516{
6517 if (l->lv_last == NULL)
6518 {
6519 /* empty list */
6520 l->lv_first = item;
6521 l->lv_last = item;
6522 item->li_prev = NULL;
6523 }
6524 else
6525 {
6526 l->lv_last->li_next = item;
6527 item->li_prev = l->lv_last;
6528 l->lv_last = item;
6529 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006530 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 item->li_next = NULL;
6532}
6533
6534/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006535 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006539list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006541 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542
Bram Moolenaar05159a02005-02-26 23:04:13 +00006543 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006544 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006545 copy_tv(tv, &li->li_tv);
6546 list_append(l, li);
6547 return OK;
6548}
6549
6550/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006551 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006552 * Return FAIL when out of memory.
6553 */
6554 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006555list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006556{
6557 listitem_T *li = listitem_alloc();
6558
6559 if (li == NULL)
6560 return FAIL;
6561 li->li_tv.v_type = VAR_DICT;
6562 li->li_tv.v_lock = 0;
6563 li->li_tv.vval.v_dict = dict;
6564 list_append(list, li);
6565 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006566 return OK;
6567}
6568
6569/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006570 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006571 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006572 * Returns FAIL when out of memory.
6573 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006574 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006575list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006576{
6577 listitem_T *li = listitem_alloc();
6578
6579 if (li == NULL)
6580 return FAIL;
6581 list_append(l, li);
6582 li->li_tv.v_type = VAR_STRING;
6583 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006584 if (str == NULL)
6585 li->li_tv.vval.v_string = NULL;
6586 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006587 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006588 return FAIL;
6589 return OK;
6590}
6591
6592/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006593 * Append "n" to list "l".
6594 * Returns FAIL when out of memory.
6595 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006596 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006597list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006598{
6599 listitem_T *li;
6600
6601 li = listitem_alloc();
6602 if (li == NULL)
6603 return FAIL;
6604 li->li_tv.v_type = VAR_NUMBER;
6605 li->li_tv.v_lock = 0;
6606 li->li_tv.vval.v_number = n;
6607 list_append(l, li);
6608 return OK;
6609}
6610
6611/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006613 * If "item" is NULL append at the end.
6614 * Return FAIL when out of memory.
6615 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006616 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006617list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006618{
Bram Moolenaar33570922005-01-25 22:26:29 +00006619 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006620
6621 if (ni == NULL)
6622 return FAIL;
6623 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006624 list_insert(l, ni, item);
6625 return OK;
6626}
6627
6628 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006629list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006630{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 if (item == NULL)
6632 /* Append new item at end of list. */
6633 list_append(l, ni);
6634 else
6635 {
6636 /* Insert new item before existing item. */
6637 ni->li_prev = item->li_prev;
6638 ni->li_next = item;
6639 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006640 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006642 ++l->lv_idx;
6643 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006645 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006647 l->lv_idx_item = NULL;
6648 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006649 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006650 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652}
6653
6654/*
6655 * Extend "l1" with "l2".
6656 * If "bef" is NULL append at the end, otherwise insert before this item.
6657 * Returns FAIL when out of memory.
6658 */
6659 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006660list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661{
Bram Moolenaar33570922005-01-25 22:26:29 +00006662 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006663 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006665 /* We also quit the loop when we have inserted the original item count of
6666 * the list, avoid a hang when we extend a list with itself. */
6667 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006668 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6669 return FAIL;
6670 return OK;
6671}
6672
6673/*
6674 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6675 * Return FAIL when out of memory.
6676 */
6677 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006678list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006679{
Bram Moolenaar33570922005-01-25 22:26:29 +00006680 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006681
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006682 if (l1 == NULL || l2 == NULL)
6683 return FAIL;
6684
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006685 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006686 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006687 if (l == NULL)
6688 return FAIL;
6689 tv->v_type = VAR_LIST;
6690 tv->vval.v_list = l;
6691
6692 /* append all items from the second list */
6693 return list_extend(l, l2, NULL);
6694}
6695
6696/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006697 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006698 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006699 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700 * Returns NULL when out of memory.
6701 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006703list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704{
Bram Moolenaar33570922005-01-25 22:26:29 +00006705 list_T *copy;
6706 listitem_T *item;
6707 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006708
6709 if (orig == NULL)
6710 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006711
6712 copy = list_alloc();
6713 if (copy != NULL)
6714 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006715 if (copyID != 0)
6716 {
6717 /* Do this before adding the items, because one of the items may
6718 * refer back to this list. */
6719 orig->lv_copyID = copyID;
6720 orig->lv_copylist = copy;
6721 }
6722 for (item = orig->lv_first; item != NULL && !got_int;
6723 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006724 {
6725 ni = listitem_alloc();
6726 if (ni == NULL)
6727 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006728 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006729 {
6730 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6731 {
6732 vim_free(ni);
6733 break;
6734 }
6735 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006736 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006737 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006738 list_append(copy, ni);
6739 }
6740 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006741 if (item != NULL)
6742 {
6743 list_unref(copy);
6744 copy = NULL;
6745 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006746 }
6747
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006748 return copy;
6749}
6750
6751/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006752 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006753 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006754 * This used to be called list_remove, but that conflicts with a Sun header
6755 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006756 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006758vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006759{
Bram Moolenaar33570922005-01-25 22:26:29 +00006760 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006761
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006762 /* notify watchers */
6763 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006764 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006765 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006766 list_fix_watch(l, ip);
6767 if (ip == item2)
6768 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006769 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006770
6771 if (item2->li_next == NULL)
6772 l->lv_last = item->li_prev;
6773 else
6774 item2->li_next->li_prev = item->li_prev;
6775 if (item->li_prev == NULL)
6776 l->lv_first = item2->li_next;
6777 else
6778 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006779 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780}
6781
6782/*
6783 * Return an allocated string with the string representation of a list.
6784 * May return NULL.
6785 */
6786 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006787list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006788{
6789 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006790
6791 if (tv->vval.v_list == NULL)
6792 return NULL;
6793 ga_init2(&ga, (int)sizeof(char), 80);
6794 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006795 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6796 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006797 {
6798 vim_free(ga.ga_data);
6799 return NULL;
6800 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006801 ga_append(&ga, ']');
6802 ga_append(&ga, NUL);
6803 return (char_u *)ga.ga_data;
6804}
6805
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006806typedef struct join_S {
6807 char_u *s;
6808 char_u *tofree;
6809} join_T;
6810
6811 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006812list_join_inner(
6813 garray_T *gap, /* to store the result in */
6814 list_T *l,
6815 char_u *sep,
6816 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006817 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006818 int copyID,
6819 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006820{
6821 int i;
6822 join_T *p;
6823 int len;
6824 int sumlen = 0;
6825 int first = TRUE;
6826 char_u *tofree;
6827 char_u numbuf[NUMBUFLEN];
6828 listitem_T *item;
6829 char_u *s;
6830
6831 /* Stringify each item in the list. */
6832 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6833 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006834 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6835 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006836 if (s == NULL)
6837 return FAIL;
6838
6839 len = (int)STRLEN(s);
6840 sumlen += len;
6841
Bram Moolenaarcde88542015-08-11 19:14:00 +02006842 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006843 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6844 if (tofree != NULL || s != numbuf)
6845 {
6846 p->s = s;
6847 p->tofree = tofree;
6848 }
6849 else
6850 {
6851 p->s = vim_strnsave(s, len);
6852 p->tofree = p->s;
6853 }
6854
6855 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006856 if (did_echo_string_emsg) /* recursion error, bail out */
6857 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006858 }
6859
6860 /* Allocate result buffer with its total size, avoid re-allocation and
6861 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6862 if (join_gap->ga_len >= 2)
6863 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6864 if (ga_grow(gap, sumlen + 2) == FAIL)
6865 return FAIL;
6866
6867 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6868 {
6869 if (first)
6870 first = FALSE;
6871 else
6872 ga_concat(gap, sep);
6873 p = ((join_T *)join_gap->ga_data) + i;
6874
6875 if (p->s != NULL)
6876 ga_concat(gap, p->s);
6877 line_breakcheck();
6878 }
6879
6880 return OK;
6881}
6882
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006883/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006884 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006885 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006886 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006887 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006889list_join(
6890 garray_T *gap,
6891 list_T *l,
6892 char_u *sep,
6893 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006894 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006895 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006896{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006897 garray_T join_ga;
6898 int retval;
6899 join_T *p;
6900 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901
Bram Moolenaard39a7512015-04-16 22:51:22 +02006902 if (l->lv_len < 1)
6903 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006904 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006905 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6906 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006907
6908 /* Dispose each item in join_ga. */
6909 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006910 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006911 p = (join_T *)join_ga.ga_data;
6912 for (i = 0; i < join_ga.ga_len; ++i)
6913 {
6914 vim_free(p->tofree);
6915 ++p;
6916 }
6917 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006918 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006919
6920 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006921}
6922
6923/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006924 * Return the next (unique) copy ID.
6925 * Used for serializing nested structures.
6926 */
6927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006928get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006929{
6930 current_copyID += COPYID_INC;
6931 return current_copyID;
6932}
6933
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006934/* Used by get_func_tv() */
6935static garray_T funcargs = GA_EMPTY;
6936
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006937/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938 * Garbage collection for lists and dictionaries.
6939 *
6940 * We use reference counts to be able to free most items right away when they
6941 * are no longer used. But for composite items it's possible that it becomes
6942 * unused while the reference count is > 0: When there is a recursive
6943 * reference. Example:
6944 * :let l = [1, 2, 3]
6945 * :let d = {9: l}
6946 * :let l[1] = d
6947 *
6948 * Since this is quite unusual we handle this with garbage collection: every
6949 * once in a while find out which lists and dicts are not referenced from any
6950 * variable.
6951 *
6952 * Here is a good reference text about garbage collection (refers to Python
6953 * but it applies to all reference-counting mechanisms):
6954 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006955 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006956
6957/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02006959 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006961 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006963garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006964{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006966 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 buf_T *buf;
6968 win_T *wp;
6969 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006970 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006971 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006973#ifdef FEAT_WINDOWS
6974 tabpage_T *tp;
6975#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006977 if (!testing)
6978 {
6979 /* Only do this once. */
6980 want_garbage_collect = FALSE;
6981 may_garbage_collect = FALSE;
6982 garbage_collect_at_exit = FALSE;
6983 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006984
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 /* We advance by two because we add one for items referenced through
6986 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006987 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006988
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 /*
6990 * 1. Go through all accessible variables and mark all lists and dicts
6991 * with copyID.
6992 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006993
6994 /* Don't free variables in the previous_funccal list unless they are only
6995 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006996 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 for (fc = previous_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 + 1,
7000 NULL);
7001 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7002 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007003 }
7004
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 /* script-local variables */
7006 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007007 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008
7009 /* buffer-local variables */
7010 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007011 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7012 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013
7014 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007015 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007016 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7017 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007018#ifdef FEAT_AUTOCMD
7019 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7021 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007022#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007024#ifdef FEAT_WINDOWS
7025 /* tabpage-local variables */
7026 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7028 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007029#endif
7030
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033
7034 /* function-local variables */
7035 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7036 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7038 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 }
7040
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007041 /* function call arguments, if v:testing is set. */
7042 for (i = 0; i < funcargs.ga_len; ++i)
7043 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7044 copyID, NULL, NULL);
7045
Bram Moolenaard812df62008-11-09 12:46:09 +00007046 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007048
Bram Moolenaar1dced572012-04-05 16:54:08 +02007049#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007051#endif
7052
Bram Moolenaardb913952012-06-29 12:54:53 +02007053#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007055#endif
7056
7057#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007059#endif
7060
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007061#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007062 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007063 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007064#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007065#ifdef FEAT_NETBEANS_INTG
7066 abort = abort || set_ref_in_nb_channel(copyID);
7067#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007068
Bram Moolenaare3188e22016-05-31 21:13:04 +02007069#ifdef FEAT_TIMERS
7070 abort = abort || set_ref_in_timer(copyID);
7071#endif
7072
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007074 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 /*
7076 * 2. Free lists and dictionaries that are not referenced.
7077 */
7078 did_free = free_unref_items(copyID);
7079
7080 /*
7081 * 3. Check if any funccal can be freed now.
7082 */
7083 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007084 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 if (can_free_funccal(*pfc, copyID))
7086 {
7087 fc = *pfc;
7088 *pfc = fc->caller;
7089 free_funccal(fc, TRUE);
7090 did_free = TRUE;
7091 did_free_funccal = TRUE;
7092 }
7093 else
7094 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007095 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007096 if (did_free_funccal)
7097 /* When a funccal was freed some more items might be garbage
7098 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007099 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007100 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 else if (p_verbose > 0)
7102 {
7103 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7104 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007105
7106 return did_free;
7107}
7108
7109/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007110 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007111 */
7112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007113free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007114{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007115 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007116 int did_free = FALSE;
7117
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007118 /* Let all "free" functions know that we are here. This means no
7119 * dictionaries, lists, channels or jobs are to be freed, because we will
7120 * do that here. */
7121 in_free_unref_items = TRUE;
7122
7123 /*
7124 * PASS 1: free the contents of the items. We don't free the items
7125 * themselves yet, so that it is possible to decrement refcount counters
7126 */
7127
Bram Moolenaarcd524592016-07-17 14:57:05 +02007128 /* Go through the list of dicts and free items without the copyID. */
7129 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007130
7131 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007132 * Go through the list of lists and free items without the copyID.
7133 * But don't free a list that has a watcher (used in a for loop), these
7134 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007135 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007136 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7137 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7138 && ll->lv_watch == NULL)
7139 {
7140 /* Free the List and ordinary items it contains, but don't recurse
7141 * into Lists and Dictionaries, they will be in the list of dicts
7142 * or list of lists. */
7143 list_free_contents(ll);
7144 did_free = TRUE;
7145 }
7146
7147#ifdef FEAT_JOB_CHANNEL
7148 /* Go through the list of jobs and free items without the copyID. This
7149 * must happen before doing channels, because jobs refer to channels, but
7150 * the reference from the channel to the job isn't tracked. */
7151 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7152
7153 /* Go through the list of channels and free items without the copyID. */
7154 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7155#endif
7156
7157 /*
7158 * PASS 2: free the items themselves.
7159 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007160 dict_free_items(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007161
7162 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007163 {
7164 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007165 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7166 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007167 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007168 /* Free the List and ordinary items it contains, but don't recurse
7169 * into Lists and Dictionaries, they will be in the list of dicts
7170 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007171 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007172 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007173 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007174
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007175#ifdef FEAT_JOB_CHANNEL
7176 /* Go through the list of jobs and free items without the copyID. This
7177 * must happen before doing channels, because jobs refer to channels, but
7178 * the reference from the channel to the job isn't tracked. */
7179 free_unused_jobs(copyID, COPYID_MASK);
7180
7181 /* Go through the list of channels and free items without the copyID. */
7182 free_unused_channels(copyID, COPYID_MASK);
7183#endif
7184
7185 in_free_unref_items = FALSE;
7186
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187 return did_free;
7188}
7189
7190/*
7191 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007192 * "list_stack" is used to add lists to be marked. Can be NULL.
7193 *
7194 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007195 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007196 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007197set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007198{
7199 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007200 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007201 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007202 hashtab_T *cur_ht;
7203 ht_stack_T *ht_stack = NULL;
7204 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007205
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007206 cur_ht = ht;
7207 for (;;)
7208 {
7209 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007210 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007211 /* Mark each item in the hashtab. If the item contains a hashtab
7212 * it is added to ht_stack, if it contains a list it is added to
7213 * list_stack. */
7214 todo = (int)cur_ht->ht_used;
7215 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7216 if (!HASHITEM_EMPTY(hi))
7217 {
7218 --todo;
7219 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7220 &ht_stack, list_stack);
7221 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007222 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007223
7224 if (ht_stack == NULL)
7225 break;
7226
7227 /* take an item from the stack */
7228 cur_ht = ht_stack->ht;
7229 tempitem = ht_stack;
7230 ht_stack = ht_stack->prev;
7231 free(tempitem);
7232 }
7233
7234 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007235}
7236
7237/*
7238 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007239 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7240 *
7241 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007242 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007243 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007244set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007245{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007246 listitem_T *li;
7247 int abort = FALSE;
7248 list_T *cur_l;
7249 list_stack_T *list_stack = NULL;
7250 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007251
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007252 cur_l = l;
7253 for (;;)
7254 {
7255 if (!abort)
7256 /* Mark each item in the list. If the item contains a hashtab
7257 * it is added to ht_stack, if it contains a list it is added to
7258 * list_stack. */
7259 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7260 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7261 ht_stack, &list_stack);
7262 if (list_stack == NULL)
7263 break;
7264
7265 /* take an item from the stack */
7266 cur_l = list_stack->list;
7267 tempitem = list_stack;
7268 list_stack = list_stack->prev;
7269 free(tempitem);
7270 }
7271
7272 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007273}
7274
7275/*
7276 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007277 * "list_stack" is used to add lists to be marked. Can be NULL.
7278 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7279 *
7280 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007281 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007283set_ref_in_item(
7284 typval_T *tv,
7285 int copyID,
7286 ht_stack_T **ht_stack,
7287 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007288{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007289 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007290
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007291 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007292 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007293 dict_T *dd = tv->vval.v_dict;
7294
Bram Moolenaara03f2332016-02-06 18:09:59 +01007295 if (dd != NULL && dd->dv_copyID != copyID)
7296 {
7297 /* Didn't see this dict yet. */
7298 dd->dv_copyID = copyID;
7299 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007300 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007301 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7302 }
7303 else
7304 {
7305 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7306 if (newitem == NULL)
7307 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007308 else
7309 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007310 newitem->ht = &dd->dv_hashtab;
7311 newitem->prev = *ht_stack;
7312 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007313 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007314 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007315 }
7316 }
7317 else if (tv->v_type == VAR_LIST)
7318 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007319 list_T *ll = tv->vval.v_list;
7320
Bram Moolenaara03f2332016-02-06 18:09:59 +01007321 if (ll != NULL && ll->lv_copyID != copyID)
7322 {
7323 /* Didn't see this list yet. */
7324 ll->lv_copyID = copyID;
7325 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007326 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007327 abort = set_ref_in_list(ll, copyID, ht_stack);
7328 }
7329 else
7330 {
7331 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007332 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007333 if (newitem == NULL)
7334 abort = TRUE;
7335 else
7336 {
7337 newitem->list = ll;
7338 newitem->prev = *list_stack;
7339 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007340 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007341 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007342 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007343 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007344 else if (tv->v_type == VAR_PARTIAL)
7345 {
7346 partial_T *pt = tv->vval.v_partial;
7347 int i;
7348
7349 /* A partial does not have a copyID, because it cannot contain itself.
7350 */
7351 if (pt != NULL)
7352 {
7353 if (pt->pt_dict != NULL)
7354 {
7355 typval_T dtv;
7356
7357 dtv.v_type = VAR_DICT;
7358 dtv.vval.v_dict = pt->pt_dict;
7359 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7360 }
7361
7362 for (i = 0; i < pt->pt_argc; ++i)
7363 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7364 ht_stack, list_stack);
7365 }
7366 }
7367#ifdef FEAT_JOB_CHANNEL
7368 else if (tv->v_type == VAR_JOB)
7369 {
7370 job_T *job = tv->vval.v_job;
7371 typval_T dtv;
7372
7373 if (job != NULL && job->jv_copyID != copyID)
7374 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007375 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007376 if (job->jv_channel != NULL)
7377 {
7378 dtv.v_type = VAR_CHANNEL;
7379 dtv.vval.v_channel = job->jv_channel;
7380 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7381 }
7382 if (job->jv_exit_partial != NULL)
7383 {
7384 dtv.v_type = VAR_PARTIAL;
7385 dtv.vval.v_partial = job->jv_exit_partial;
7386 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7387 }
7388 }
7389 }
7390 else if (tv->v_type == VAR_CHANNEL)
7391 {
7392 channel_T *ch =tv->vval.v_channel;
7393 int part;
7394 typval_T dtv;
7395 jsonq_T *jq;
7396 cbq_T *cq;
7397
7398 if (ch != NULL && ch->ch_copyID != copyID)
7399 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007400 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007401 for (part = PART_SOCK; part <= PART_IN; ++part)
7402 {
7403 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7404 jq = jq->jq_next)
7405 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7406 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7407 cq = cq->cq_next)
7408 if (cq->cq_partial != NULL)
7409 {
7410 dtv.v_type = VAR_PARTIAL;
7411 dtv.vval.v_partial = cq->cq_partial;
7412 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7413 }
7414 if (ch->ch_part[part].ch_partial != NULL)
7415 {
7416 dtv.v_type = VAR_PARTIAL;
7417 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7418 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7419 }
7420 }
7421 if (ch->ch_partial != NULL)
7422 {
7423 dtv.v_type = VAR_PARTIAL;
7424 dtv.vval.v_partial = ch->ch_partial;
7425 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7426 }
7427 if (ch->ch_close_partial != NULL)
7428 {
7429 dtv.v_type = VAR_PARTIAL;
7430 dtv.vval.v_partial = ch->ch_close_partial;
7431 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7432 }
7433 }
7434 }
7435#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007436 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007437}
7438
Bram Moolenaar069c1e72016-07-15 21:25:08 +02007439/* Get function arguments. */
7440 static int
7441get_function_args(
7442 char_u **argp,
7443 char_u endchar,
7444 garray_T *newargs,
7445 int *varargs,
7446 int skip)
7447{
7448 int mustend = FALSE;
7449 char_u *arg = *argp;
7450 char_u *p = arg;
7451 int c;
7452 int i;
7453
7454 if (newargs != NULL)
7455 ga_init2(newargs, (int)sizeof(char_u *), 3);
7456
7457 if (varargs != NULL)
7458 *varargs = FALSE;
7459
7460 /*
7461 * Isolate the arguments: "arg1, arg2, ...)"
7462 */
7463 while (*p != endchar)
7464 {
7465 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
7466 {
7467 if (varargs != NULL)
7468 *varargs = TRUE;
7469 p += 3;
7470 mustend = TRUE;
7471 }
7472 else
7473 {
7474 arg = p;
7475 while (ASCII_ISALNUM(*p) || *p == '_')
7476 ++p;
7477 if (arg == p || isdigit(*arg)
7478 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
7479 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
7480 {
7481 if (!skip)
7482 EMSG2(_("E125: Illegal argument: %s"), arg);
7483 break;
7484 }
7485 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
7486 return FAIL;
7487 if (newargs != NULL)
7488 {
7489 c = *p;
7490 *p = NUL;
7491 arg = vim_strsave(arg);
7492 if (arg == NULL)
7493 goto err_ret;
7494
7495 /* Check for duplicate argument name. */
7496 for (i = 0; i < newargs->ga_len; ++i)
7497 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg) == 0)
7498 {
7499 EMSG2(_("E853: Duplicate argument name: %s"), arg);
7500 vim_free(arg);
7501 goto err_ret;
7502 }
7503 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg;
7504 newargs->ga_len++;
7505
7506 *p = c;
7507 }
7508 if (*p == ',')
7509 ++p;
7510 else
7511 mustend = TRUE;
7512 }
7513 p = skipwhite(p);
7514 if (mustend && *p != endchar)
7515 {
7516 if (!skip)
7517 EMSG2(_(e_invarg2), *argp);
7518 break;
7519 }
7520 }
7521 ++p; /* skip the ')' */
7522
7523 *argp = p;
7524 return OK;
7525
7526err_ret:
7527 if (newargs != NULL)
7528 ga_clear_strings(newargs);
7529 return FAIL;
7530}
7531
7532/*
7533 * Parse a lambda expression and get a Funcref from "*arg".
7534 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
7535 */
7536 static int
7537get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
7538{
7539 garray_T newargs;
7540 garray_T newlines;
7541 ufunc_T *fp = NULL;
7542 int varargs;
7543 int ret;
7544 char_u name[20];
7545 char_u *start = skipwhite(*arg + 1);
7546 char_u *s, *e;
7547 static int lambda_no = 0;
7548
7549 ga_init(&newargs);
7550 ga_init(&newlines);
7551
Bram Moolenaarcd524592016-07-17 14:57:05 +02007552 /* First, check if this is a lambda expression. "->" must exist. */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02007553 ret = get_function_args(&start, '-', NULL, NULL, TRUE);
7554 if (ret == FAIL || *start != '>')
7555 return NOTDONE;
7556
7557 /* Parse the arguments again. */
7558 *arg = skipwhite(*arg + 1);
7559 ret = get_function_args(arg, '-', &newargs, &varargs, FALSE);
7560 if (ret == FAIL || **arg != '>')
7561 goto errret;
7562
7563 /* Get the start and the end of the expression. */
7564 *arg = skipwhite(*arg + 1);
7565 s = *arg;
7566 ret = skip_expr(arg);
7567 if (ret == FAIL)
7568 goto errret;
7569 e = *arg;
7570 *arg = skipwhite(*arg);
7571 if (**arg != '}')
7572 goto errret;
7573 ++*arg;
7574
7575 if (evaluate)
7576 {
7577 int len;
7578 char_u *p;
7579
7580 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + 20));
7581 if (fp == NULL)
7582 goto errret;
7583
7584 sprintf((char*)name, "<lambda>%d", ++lambda_no);
7585
7586 ga_init2(&newlines, (int)sizeof(char_u *), 1);
7587 if (ga_grow(&newlines, 1) == FAIL)
7588 goto errret;
7589
7590 /* Add "return " before the expression.
7591 * TODO: Support multiple expressions. */
7592 len = 7 + e - s + 1;
7593 p = (char_u *)alloc(len);
7594 if (p == NULL)
7595 goto errret;
7596 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
7597 STRCPY(p, "return ");
7598 STRNCPY(p + 7, s, e - s);
7599 p[7 + e - s] = NUL;
7600
7601 fp->uf_refcount = 1;
7602 STRCPY(fp->uf_name, name);
7603 hash_add(&func_hashtab, UF2HIKEY(fp));
7604 fp->uf_args = newargs;
7605 fp->uf_lines = newlines;
7606
7607#ifdef FEAT_PROFILE
7608 fp->uf_tml_count = NULL;
7609 fp->uf_tml_total = NULL;
7610 fp->uf_tml_self = NULL;
7611 fp->uf_profiling = FALSE;
7612 if (prof_def_func())
7613 func_do_profile(fp);
7614#endif
7615 fp->uf_varargs = TRUE;
7616 fp->uf_flags = 0;
7617 fp->uf_calls = 0;
7618 fp->uf_script_ID = current_SID;
7619
7620 rettv->vval.v_string = vim_strsave(name);
7621 rettv->v_type = VAR_FUNC;
7622 }
7623 else
7624 ga_clear_strings(&newargs);
7625
7626 return OK;
7627
7628errret:
7629 ga_clear_strings(&newargs);
7630 ga_clear_strings(&newlines);
7631 vim_free(fp);
7632 return FAIL;
7633}
7634
Bram Moolenaar17a13432016-01-24 14:22:10 +01007635 static char *
7636get_var_special_name(int nr)
7637{
7638 switch (nr)
7639 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007640 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007641 case VVAL_TRUE: return "v:true";
7642 case VVAL_NONE: return "v:none";
7643 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007644 }
7645 EMSG2(_(e_intern2), "get_var_special_name()");
7646 return "42";
7647}
7648
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650 * Return a string with the string representation of a variable.
7651 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007652 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007653 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007654 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
7655 * "string()", otherwise does not put quotes around strings, as ":echo"
7656 * displays values.
7657 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
7658 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007659 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007660 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007661 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007662echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01007663 typval_T *tv,
7664 char_u **tofree,
7665 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007666 int copyID,
7667 int echo_style,
7668 int restore_copyID,
7669 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007670{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 static int recurse = 0;
7672 char_u *r = NULL;
7673
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007675 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007676 if (!did_echo_string_emsg)
7677 {
7678 /* Only give this message once for a recursive call to avoid
7679 * flooding the user with errors. And stop iterating over lists
7680 * and dicts. */
7681 did_echo_string_emsg = TRUE;
7682 EMSG(_("E724: variable nested too deep for displaying"));
7683 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007684 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007685 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007686 }
7687 ++recurse;
7688
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007689 switch (tv->v_type)
7690 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007691 case VAR_STRING:
7692 if (echo_style && !dict_val)
7693 {
7694 *tofree = NULL;
7695 r = get_tv_string_buf(tv, numbuf);
7696 }
7697 else
7698 {
7699 *tofree = string_quote(tv->vval.v_string, FALSE);
7700 r = *tofree;
7701 }
7702 break;
7703
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007704 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007705 if (echo_style)
7706 {
7707 *tofree = NULL;
7708 r = tv->vval.v_string;
7709 }
7710 else
7711 {
7712 *tofree = string_quote(tv->vval.v_string, TRUE);
7713 r = *tofree;
7714 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007715 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007716
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007717 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01007718 {
7719 partial_T *pt = tv->vval.v_partial;
7720 char_u *fname = string_quote(pt == NULL ? NULL
7721 : pt->pt_name, FALSE);
7722 garray_T ga;
7723 int i;
7724 char_u *tf;
7725
7726 ga_init2(&ga, 1, 100);
7727 ga_concat(&ga, (char_u *)"function(");
7728 if (fname != NULL)
7729 {
7730 ga_concat(&ga, fname);
7731 vim_free(fname);
7732 }
7733 if (pt != NULL && pt->pt_argc > 0)
7734 {
7735 ga_concat(&ga, (char_u *)", [");
7736 for (i = 0; i < pt->pt_argc; ++i)
7737 {
7738 if (i > 0)
7739 ga_concat(&ga, (char_u *)", ");
7740 ga_concat(&ga,
7741 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7742 vim_free(tf);
7743 }
7744 ga_concat(&ga, (char_u *)"]");
7745 }
7746 if (pt != NULL && pt->pt_dict != NULL)
7747 {
7748 typval_T dtv;
7749
7750 ga_concat(&ga, (char_u *)", ");
7751 dtv.v_type = VAR_DICT;
7752 dtv.vval.v_dict = pt->pt_dict;
7753 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7754 vim_free(tf);
7755 }
7756 ga_concat(&ga, (char_u *)")");
7757
7758 *tofree = ga.ga_data;
7759 r = *tofree;
7760 break;
7761 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007762
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007763 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007764 if (tv->vval.v_list == NULL)
7765 {
7766 *tofree = NULL;
7767 r = NULL;
7768 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007769 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
7770 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007771 {
7772 *tofree = NULL;
7773 r = (char_u *)"[...]";
7774 }
7775 else
7776 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007777 int old_copyID = tv->vval.v_list->lv_copyID;
7778
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007779 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007780 *tofree = list2string(tv, copyID, restore_copyID);
7781 if (restore_copyID)
7782 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007783 r = *tofree;
7784 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007785 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007786
Bram Moolenaar8c711452005-01-14 21:53:12 +00007787 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007788 if (tv->vval.v_dict == NULL)
7789 {
7790 *tofree = NULL;
7791 r = NULL;
7792 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007793 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
7794 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007795 {
7796 *tofree = NULL;
7797 r = (char_u *)"{...}";
7798 }
7799 else
7800 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007801 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007802 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007803 *tofree = dict2string(tv, copyID, restore_copyID);
7804 if (restore_copyID)
7805 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007806 r = *tofree;
7807 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007808 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007809
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007810 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007811 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007812 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007813 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007814 *tofree = NULL;
7815 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007816 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007817
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007818 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007819#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007820 *tofree = NULL;
7821 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7822 r = numbuf;
7823 break;
7824#endif
7825
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007826 case VAR_SPECIAL:
7827 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007828 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007829 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007831
Bram Moolenaar8502c702014-06-17 12:51:16 +02007832 if (--recurse == 0)
7833 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007834 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007835}
7836
7837/*
7838 * Return a string with the string representation of a variable.
7839 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7840 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007841 * Does not put quotes around strings, as ":echo" displays values.
7842 * When "copyID" is not NULL replace recursive lists and dicts with "...".
7843 * May return NULL.
7844 */
7845 static char_u *
7846echo_string(
7847 typval_T *tv,
7848 char_u **tofree,
7849 char_u *numbuf,
7850 int copyID)
7851{
7852 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
7853}
7854
7855/*
7856 * Return a string with the string representation of a variable.
7857 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7858 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007859 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007860 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007861 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02007862 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007863tv2string(
7864 typval_T *tv,
7865 char_u **tofree,
7866 char_u *numbuf,
7867 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007868{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007869 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007870}
7871
7872/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007873 * Return string "str" in ' quotes, doubling ' characters.
7874 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007875 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007876 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007877 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007878string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007879{
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007881 char_u *p, *r, *s;
7882
Bram Moolenaar33570922005-01-25 22:26:29 +00007883 len = (function ? 13 : 3);
7884 if (str != NULL)
7885 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007886 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007887 for (p = str; *p != NUL; mb_ptr_adv(p))
7888 if (*p == '\'')
7889 ++len;
7890 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891 s = r = alloc(len);
7892 if (r != NULL)
7893 {
7894 if (function)
7895 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007896 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897 r += 10;
7898 }
7899 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007900 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007901 if (str != NULL)
7902 for (p = str; *p != NUL; )
7903 {
7904 if (*p == '\'')
7905 *r++ = '\'';
7906 MB_COPY_CHAR(p, r);
7907 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007908 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909 if (function)
7910 *r++ = ')';
7911 *r++ = NUL;
7912 }
7913 return s;
7914}
7915
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007916#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007917/*
7918 * Convert the string "text" to a floating point number.
7919 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7920 * this always uses a decimal point.
7921 * Returns the length of the text that was consumed.
7922 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007923 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007924string2float(
7925 char_u *text,
7926 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007927{
7928 char *s = (char *)text;
7929 float_T f;
7930
7931 f = strtod(s, &s);
7932 *value = f;
7933 return (int)((char_u *)s - text);
7934}
7935#endif
7936
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 * Get the value of an environment variable.
7939 * "arg" is pointing to the '$'. It is advanced to after the name.
7940 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007941 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 */
7943 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007944get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
7946 char_u *string = NULL;
7947 int len;
7948 int cc;
7949 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007950 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951
7952 ++*arg;
7953 name = *arg;
7954 len = get_env_len(arg);
7955 if (evaluate)
7956 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007957 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007958 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007959
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007960 cc = name[len];
7961 name[len] = NUL;
7962 /* first try vim_getenv(), fast for normal environment vars */
7963 string = vim_getenv(name, &mustfree);
7964 if (string != NULL && *string != NUL)
7965 {
7966 if (!mustfree)
7967 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007969 else
7970 {
7971 if (mustfree)
7972 vim_free(string);
7973
7974 /* next try expanding things like $VIM and ${HOME} */
7975 string = expand_env_save(name - 1);
7976 if (string != NULL && *string == '$')
7977 {
7978 vim_free(string);
7979 string = NULL;
7980 }
7981 }
7982 name[len] = cc;
7983
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984 rettv->v_type = VAR_STRING;
7985 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 }
7987
7988 return OK;
7989}
7990
7991/*
7992 * Array with names and number of arguments of all internal functions
7993 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7994 */
7995static struct fst
7996{
7997 char *f_name; /* function name */
7998 char f_min_argc; /* minimal number of arguments */
7999 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008000 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008001 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002} functions[] =
8003{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008004#ifdef FEAT_FLOAT
8005 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008006 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008007#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008008 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008009 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {"append", 2, 2, f_append},
8011 {"argc", 0, 0, f_argc},
8012 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008013 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008014 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008015#ifdef FEAT_FLOAT
8016 {"asin", 1, 1, f_asin}, /* WJMc */
8017#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008018 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008019 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008020 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008021 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008022 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008023 {"assert_notequal", 2, 3, f_assert_notequal},
8024 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008025 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008026#ifdef FEAT_FLOAT
8027 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008028 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008031 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 {"bufexists", 1, 1, f_bufexists},
8033 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8034 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8035 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8036 {"buflisted", 1, 1, f_buflisted},
8037 {"bufloaded", 1, 1, f_bufloaded},
8038 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008039 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008040 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {"bufwinnr", 1, 1, f_bufwinnr},
8042 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008043 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008044 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008045 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008046#ifdef FEAT_FLOAT
8047 {"ceil", 1, 1, f_ceil},
8048#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008049#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008050 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008051 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8052 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008053 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008054 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008055 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008056 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008057 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008058 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008059 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008060 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008061 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8062 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008063 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008064 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008065#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008066 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008067 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008069 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008071#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008072 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008073 {"complete_add", 1, 1, f_complete_add},
8074 {"complete_check", 0, 0, f_complete_check},
8075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008077 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#ifdef FEAT_FLOAT
8079 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008080 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008081#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008082 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008084 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008085 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008086 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008088 {"diff_filler", 1, 1, f_diff_filler},
8089 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008090 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008092 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"eventhandler", 0, 0, f_eventhandler},
8094 {"executable", 1, 1, f_executable},
Bram Moolenaar79815f12016-07-09 17:07:29 +02008095 {"execute", 1, 2, f_execute},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008096 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008098#ifdef FEAT_FLOAT
8099 {"exp", 1, 1, f_exp},
8100#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008101 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008102 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008103 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8105 {"filereadable", 1, 1, f_filereadable},
8106 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008107 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008108 {"finddir", 1, 3, f_finddir},
8109 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008110#ifdef FEAT_FLOAT
8111 {"float2nr", 1, 1, f_float2nr},
8112 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008113 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008114#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008115 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {"fnamemodify", 2, 2, f_fnamemodify},
8117 {"foldclosed", 1, 1, f_foldclosed},
8118 {"foldclosedend", 1, 1, f_foldclosedend},
8119 {"foldlevel", 1, 1, f_foldlevel},
8120 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008121 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008123 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008124 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008125 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008126 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008127 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"getchar", 0, 1, f_getchar},
8129 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008130 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"getcmdline", 0, 0, f_getcmdline},
8132 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008133 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008134 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02008135#if defined(FEAT_CMDL_COMPL)
8136 {"getcompletion", 2, 2, f_getcompletion},
8137#endif
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008138 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008139 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008140 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008141 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"getfsize", 1, 1, f_getfsize},
8143 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008144 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008145 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008146 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008147 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008148 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008149 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008150 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008151 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008153 {"gettabvar", 2, 3, f_gettabvar},
8154 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"getwinposx", 0, 0, f_getwinposx},
8156 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008157 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008158 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008159 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008160 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008162 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008163 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008164 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8166 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8167 {"histadd", 2, 2, f_histadd},
8168 {"histdel", 1, 2, f_histdel},
8169 {"histget", 1, 2, f_histget},
8170 {"histnr", 1, 1, f_histnr},
8171 {"hlID", 1, 1, f_hlID},
8172 {"hlexists", 1, 1, f_hlexists},
8173 {"hostname", 0, 0, f_hostname},
8174 {"iconv", 3, 3, f_iconv},
8175 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008176 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008177 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008179 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"inputrestore", 0, 0, f_inputrestore},
8181 {"inputsave", 0, 0, f_inputsave},
8182 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008183 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008184 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008186 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008187#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8188 {"isnan", 1, 1, f_isnan},
8189#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008190 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008191#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008192 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008193 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008194 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008195 {"job_start", 1, 2, f_job_start},
8196 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008197 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008198#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008199 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008200 {"js_decode", 1, 1, f_js_decode},
8201 {"js_encode", 1, 1, f_js_encode},
8202 {"json_decode", 1, 1, f_json_decode},
8203 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008204 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008206 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"libcall", 3, 3, f_libcall},
8208 {"libcallnr", 3, 3, f_libcallnr},
8209 {"line", 1, 1, f_line},
8210 {"line2byte", 1, 1, f_line2byte},
8211 {"lispindent", 1, 1, f_lispindent},
8212 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008213#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008214 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008215 {"log10", 1, 1, f_log10},
8216#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008217#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008218 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008219#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008220 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008221 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008222 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008223 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008224 {"matchadd", 2, 5, f_matchadd},
8225 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008226 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008227 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008228 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008229 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008230 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008231 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008232 {"max", 1, 1, f_max},
8233 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008234#ifdef vim_mkdir
8235 {"mkdir", 1, 3, f_mkdir},
8236#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008238#ifdef FEAT_MZSCHEME
8239 {"mzeval", 1, 1, f_mzeval},
8240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008242 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008243 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008244 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008245#ifdef FEAT_PERL
8246 {"perleval", 1, 1, f_perleval},
8247#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008248#ifdef FEAT_FLOAT
8249 {"pow", 2, 2, f_pow},
8250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008252 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008253 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008254#ifdef FEAT_PYTHON3
8255 {"py3eval", 1, 1, f_py3eval},
8256#endif
8257#ifdef FEAT_PYTHON
8258 {"pyeval", 1, 1, f_pyeval},
8259#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008260 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008261 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008262 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008263#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008264 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008265#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008266 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"remote_expr", 2, 3, f_remote_expr},
8268 {"remote_foreground", 1, 1, f_remote_foreground},
8269 {"remote_peek", 1, 2, f_remote_peek},
8270 {"remote_read", 1, 1, f_remote_read},
8271 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008272 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008274 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008276 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008277#ifdef FEAT_FLOAT
8278 {"round", 1, 1, f_round},
8279#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008280 {"screenattr", 2, 2, f_screenattr},
8281 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008282 {"screencol", 0, 0, f_screencol},
8283 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008284 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008285 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008286 {"searchpair", 3, 7, f_searchpair},
8287 {"searchpairpos", 3, 7, f_searchpairpos},
8288 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {"server2client", 2, 2, f_server2client},
8290 {"serverlist", 0, 0, f_serverlist},
8291 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008292 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008294 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008296 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008297 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008298 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008299 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008301 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008302 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008304#ifdef FEAT_CRYPT
8305 {"sha256", 1, 1, f_sha256},
8306#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008307 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008308 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008310#ifdef FEAT_FLOAT
8311 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008312 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008313#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008314 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008315 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008316 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008317 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008318 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008319#ifdef FEAT_FLOAT
8320 {"sqrt", 1, 1, f_sqrt},
8321 {"str2float", 1, 1, f_str2float},
8322#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008323 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008324 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008325 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008326 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327#ifdef HAVE_STRFTIME
8328 {"strftime", 1, 2, f_strftime},
8329#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008330 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008331 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008332 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"strlen", 1, 1, f_strlen},
8334 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008335 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008337 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008338 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {"substitute", 4, 4, f_substitute},
8340 {"synID", 3, 3, f_synID},
8341 {"synIDattr", 2, 3, f_synIDattr},
8342 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008343 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008344 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008345 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008346 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008347 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008348 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008349 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008350 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008351 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008352#ifdef FEAT_FLOAT
8353 {"tan", 1, 1, f_tan},
8354 {"tanh", 1, 1, f_tanh},
8355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008357 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
Bram Moolenaar5c719942016-07-09 23:40:45 +02008358 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008359 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008360 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8361#ifdef FEAT_JOB_CHANNEL
8362 {"test_null_channel", 0, 0, f_test_null_channel},
8363#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008364 {"test_null_dict", 0, 0, f_test_null_dict},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008365#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008366 {"test_null_job", 0, 0, f_test_null_job},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008367#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008368 {"test_null_list", 0, 0, f_test_null_list},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008369 {"test_null_partial", 0, 0, f_test_null_partial},
8370 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008371 {"test_settime", 1, 1, f_test_settime},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008372#ifdef FEAT_TIMERS
8373 {"timer_start", 2, 3, f_timer_start},
8374 {"timer_stop", 1, 1, f_timer_stop},
8375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {"tolower", 1, 1, f_tolower},
8377 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008378 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008379#ifdef FEAT_FLOAT
8380 {"trunc", 1, 1, f_trunc},
8381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008383 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008384 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008385 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008386 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {"virtcol", 1, 1, f_virtcol},
8388 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008389 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008390 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008391 {"win_getid", 0, 2, f_win_getid},
8392 {"win_gotoid", 1, 1, f_win_gotoid},
8393 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8394 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {"winbufnr", 1, 1, f_winbufnr},
8396 {"wincol", 0, 0, f_wincol},
8397 {"winheight", 1, 1, f_winheight},
8398 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008399 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008401 {"winrestview", 1, 1, f_winrestview},
8402 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008404 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008405 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008406 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407};
8408
8409#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8410
8411/*
8412 * Function given to ExpandGeneric() to obtain the list of internal
8413 * or user defined function names.
8414 */
8415 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008416get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
8418 static int intidx = -1;
8419 char_u *name;
8420
8421 if (idx == 0)
8422 intidx = -1;
8423 if (intidx < 0)
8424 {
8425 name = get_user_func_name(xp, idx);
8426 if (name != NULL)
8427 return name;
8428 }
8429 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8430 {
8431 STRCPY(IObuff, functions[intidx].f_name);
8432 STRCAT(IObuff, "(");
8433 if (functions[intidx].f_max_argc == 0)
8434 STRCAT(IObuff, ")");
8435 return IObuff;
8436 }
8437
8438 return NULL;
8439}
8440
8441/*
8442 * Function given to ExpandGeneric() to obtain the list of internal or
8443 * user defined variable or function names.
8444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008446get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 static int intidx = -1;
8449 char_u *name;
8450
8451 if (idx == 0)
8452 intidx = -1;
8453 if (intidx < 0)
8454 {
8455 name = get_function_name(xp, idx);
8456 if (name != NULL)
8457 return name;
8458 }
8459 return get_user_var_name(xp, ++intidx);
8460}
8461
8462#endif /* FEAT_CMDL_COMPL */
8463
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008464#if defined(EBCDIC) || defined(PROTO)
8465/*
8466 * Compare struct fst by function name.
8467 */
8468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008469compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008470{
8471 struct fst *p1 = (struct fst *)s1;
8472 struct fst *p2 = (struct fst *)s2;
8473
8474 return STRCMP(p1->f_name, p2->f_name);
8475}
8476
8477/*
8478 * Sort the function table by function name.
8479 * The sorting of the table above is ASCII dependant.
8480 * On machines using EBCDIC we have to sort it.
8481 */
8482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008483sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008484{
8485 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8486
8487 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8488}
8489#endif
8490
8491
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492/*
8493 * Find internal function in table above.
8494 * Return index, or -1 if not found
8495 */
8496 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008497find_internal_func(
8498 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499{
8500 int first = 0;
8501 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8502 int cmp;
8503 int x;
8504
8505 /*
8506 * Find the function name in the table. Binary search.
8507 */
8508 while (first <= last)
8509 {
8510 x = first + ((unsigned)(last - first) >> 1);
8511 cmp = STRCMP(name, functions[x].f_name);
8512 if (cmp < 0)
8513 last = x - 1;
8514 else if (cmp > 0)
8515 first = x + 1;
8516 else
8517 return x;
8518 }
8519 return -1;
8520}
8521
8522/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008523 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8524 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008525 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8526 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527 */
8528 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008529deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530{
Bram Moolenaar33570922005-01-25 22:26:29 +00008531 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008532 int cc;
8533
Bram Moolenaar65639032016-03-16 21:40:30 +01008534 if (partialp != NULL)
8535 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008536
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008537 cc = name[*lenp];
8538 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008539 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008540 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008542 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008544 {
8545 *lenp = 0;
8546 return (char_u *)""; /* just in case */
8547 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008548 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008549 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008550 }
8551
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008552 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8553 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008554 partial_T *pt = v->di_tv.vval.v_partial;
8555
8556 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008557 {
8558 *lenp = 0;
8559 return (char_u *)""; /* just in case */
8560 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008561 if (partialp != NULL)
8562 *partialp = pt;
8563 *lenp = (int)STRLEN(pt->pt_name);
8564 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008565 }
8566
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008567 return name;
8568}
8569
8570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 * Allocate a variable for the result of a function.
8572 * Return OK or FAIL.
8573 */
8574 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008575get_func_tv(
8576 char_u *name, /* name of the function */
8577 int len, /* length of "name" */
8578 typval_T *rettv,
8579 char_u **arg, /* argument, pointing to the '(' */
8580 linenr_T firstline, /* first line of range */
8581 linenr_T lastline, /* last line of range */
8582 int *doesrange, /* return: function handled range */
8583 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008584 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008585 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586{
8587 char_u *argp;
8588 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008589 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 int argcount = 0; /* number of arguments found */
8591
8592 /*
8593 * Get the arguments.
8594 */
8595 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008596 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 {
8598 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8599 if (*argp == ')' || *argp == ',' || *argp == NUL)
8600 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8602 {
8603 ret = FAIL;
8604 break;
8605 }
8606 ++argcount;
8607 if (*argp != ',')
8608 break;
8609 }
8610 if (*argp == ')')
8611 ++argp;
8612 else
8613 ret = FAIL;
8614
8615 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008616 {
8617 int i = 0;
8618
8619 if (get_vim_var_nr(VV_TESTING))
8620 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008621 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008622 * what variables are used on the call stack. */
8623 if (funcargs.ga_itemsize == 0)
8624 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8625 for (i = 0; i < argcount; ++i)
8626 if (ga_grow(&funcargs, 1) == OK)
8627 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8628 &argvars[i];
8629 }
8630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008632 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008633
8634 funcargs.ga_len -= i;
8635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008637 {
8638 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008639 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008641 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643
8644 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008645 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646
8647 *arg = skipwhite(argp);
8648 return ret;
8649}
8650
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008651#define ERROR_UNKNOWN 0
8652#define ERROR_TOOMANY 1
8653#define ERROR_TOOFEW 2
8654#define ERROR_SCRIPT 3
8655#define ERROR_DICT 4
8656#define ERROR_NONE 5
8657#define ERROR_OTHER 6
8658#define FLEN_FIXED 40
8659
8660/*
8661 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8662 * Change <SNR>123_name() to K_SNR 123_name().
8663 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8664 * (slow).
8665 */
8666 static char_u *
8667fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8668{
8669 int llen;
8670 char_u *fname;
8671 int i;
8672
8673 llen = eval_fname_script(name);
8674 if (llen > 0)
8675 {
8676 fname_buf[0] = K_SPECIAL;
8677 fname_buf[1] = KS_EXTRA;
8678 fname_buf[2] = (int)KE_SNR;
8679 i = 3;
8680 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8681 {
8682 if (current_SID <= 0)
8683 *error = ERROR_SCRIPT;
8684 else
8685 {
8686 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8687 i = (int)STRLEN(fname_buf);
8688 }
8689 }
8690 if (i + STRLEN(name + llen) < FLEN_FIXED)
8691 {
8692 STRCPY(fname_buf + i, name + llen);
8693 fname = fname_buf;
8694 }
8695 else
8696 {
8697 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8698 if (fname == NULL)
8699 *error = ERROR_OTHER;
8700 else
8701 {
8702 *tofree = fname;
8703 mch_memmove(fname, fname_buf, (size_t)i);
8704 STRCPY(fname + i, name + llen);
8705 }
8706 }
8707 }
8708 else
8709 fname = name;
8710 return fname;
8711}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712
8713/*
8714 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008715 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008716 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008718 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008719call_func(
8720 char_u *funcname, /* name of the function */
8721 int len, /* length of "name" */
8722 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008723 int argcount_in, /* number of "argvars" */
8724 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008725 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008726 linenr_T firstline, /* first line of range */
8727 linenr_T lastline, /* last line of range */
8728 int *doesrange, /* return: function handled range */
8729 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008730 partial_T *partial, /* optional, can be NULL */
8731 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
8733 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 int error = ERROR_NONE;
8735 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008738 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008740 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008741 int argcount = argcount_in;
8742 typval_T *argvars = argvars_in;
8743 dict_T *selfdict = selfdict_in;
8744 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8745 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008746
8747 /* Make a copy of the name, if it comes from a funcref variable it could
8748 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008749 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008750 if (name == NULL)
8751 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008753 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754
8755 *doesrange = FALSE;
8756
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008757 if (partial != NULL)
8758 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02008759 /* When the function has a partial with a dict and there is a dict
8760 * argument, use the dict argument. That is backwards compatible.
8761 * When the dict was bound explicitly use the one from the partial. */
8762 if (partial->pt_dict != NULL
8763 && (selfdict_in == NULL || !partial->pt_auto))
8764 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008765 if (error == ERROR_NONE && partial->pt_argc > 0)
8766 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008767 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8768 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8769 for (i = 0; i < argcount_in; ++i)
8770 argv[i + argv_clear] = argvars_in[i];
8771 argvars = argv;
8772 argcount = partial->pt_argc + argcount_in;
8773 }
8774 }
8775
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776
8777 /* execute the function if no errors detected and executing */
8778 if (evaluate && error == ERROR_NONE)
8779 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008780 char_u *rfname = fname;
8781
8782 /* Ignore "g:" before a function name. */
8783 if (fname[0] == 'g' && fname[1] == ':')
8784 rfname = fname + 2;
8785
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008786 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8787 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 error = ERROR_UNKNOWN;
8789
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008790 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 {
8792 /*
8793 * User defined function.
8794 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008795 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008796
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008798 /* Trigger FuncUndefined event, may load the function. */
8799 if (fp == NULL
8800 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008801 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008802 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008804 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008805 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 }
8807#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008808 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008809 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008810 {
8811 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008812 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008813 }
8814
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 if (fp != NULL)
8816 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008817 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008819 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008821 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008823 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008824 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 else
8826 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008827 int did_save_redo = FALSE;
8828
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 /*
8830 * Call the user function.
8831 * Save and restore search patterns, script variables and
8832 * redo buffer.
8833 */
8834 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008835#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008836 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008837#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008838 {
8839 saveRedobuff();
8840 did_save_redo = TRUE;
8841 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008842 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008844 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008845 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02008846 if (--fp->uf_calls <= 0 && (isdigit(*fp->uf_name)
8847 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008848 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008849 /* Function was unreferenced while being used, free it
8850 * now. */
8851 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008852 if (did_save_redo)
8853 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 restore_search_patterns();
8855 error = ERROR_NONE;
8856 }
8857 }
8858 }
8859 else
8860 {
8861 /*
8862 * Find the function name in the table, call its implementation.
8863 */
8864 i = find_internal_func(fname);
8865 if (i >= 0)
8866 {
8867 if (argcount < functions[i].f_min_argc)
8868 error = ERROR_TOOFEW;
8869 else if (argcount > functions[i].f_max_argc)
8870 error = ERROR_TOOMANY;
8871 else
8872 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008873 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 error = ERROR_NONE;
8876 }
8877 }
8878 }
8879 /*
8880 * The function call (or "FuncUndefined" autocommand sequence) might
8881 * have been aborted by an error, an interrupt, or an explicitly thrown
8882 * exception that has not been caught so far. This situation can be
8883 * tested for by calling aborting(). For an error in an internal
8884 * function or for the "E132" error in call_user_func(), however, the
8885 * throw point at which the "force_abort" flag (temporarily reset by
8886 * emsg()) is normally updated has not been reached yet. We need to
8887 * update that flag first to make aborting() reliable.
8888 */
8889 update_force_abort();
8890 }
8891 if (error == ERROR_NONE)
8892 ret = OK;
8893
8894 /*
8895 * Report an error unless the argument evaluation or function call has been
8896 * cancelled due to an aborting error, an interrupt, or an exception.
8897 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008898 if (!aborting())
8899 {
8900 switch (error)
8901 {
8902 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008903 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008904 break;
8905 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008906 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008907 break;
8908 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008909 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008910 name);
8911 break;
8912 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008913 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008914 name);
8915 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008916 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008917 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008918 name);
8919 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008920 }
8921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008923 while (argv_clear > 0)
8924 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008925 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008926 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927
8928 return ret;
8929}
8930
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008931/*
8932 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008933 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008934 */
8935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008936emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008937{
8938 char_u *p;
8939
8940 if (*name == K_SPECIAL)
8941 p = concat_str((char_u *)"<SNR>", name + 3);
8942 else
8943 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008944 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008945 if (p != name)
8946 vim_free(p);
8947}
8948
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008949/*
8950 * Return TRUE for a non-zero Number and a non-empty String.
8951 */
8952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008953non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008954{
8955 return ((argvars[0].v_type == VAR_NUMBER
8956 && argvars[0].vval.v_number != 0)
Bram Moolenaare381d3d2016-07-07 14:50:41 +02008957 || (argvars[0].v_type == VAR_SPECIAL
8958 && argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008959 || (argvars[0].v_type == VAR_STRING
8960 && argvars[0].vval.v_string != NULL
8961 && *argvars[0].vval.v_string != NUL));
8962}
8963
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964/*********************************************
8965 * Implementation of the built-in functions
8966 */
8967
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008968#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008969static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008970
8971/*
8972 * Get the float value of "argvars[0]" into "f".
8973 * Returns FAIL when the argument is not a Number or Float.
8974 */
8975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008976get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008977{
8978 if (argvars[0].v_type == VAR_FLOAT)
8979 {
8980 *f = argvars[0].vval.v_float;
8981 return OK;
8982 }
8983 if (argvars[0].v_type == VAR_NUMBER)
8984 {
8985 *f = (float_T)argvars[0].vval.v_number;
8986 return OK;
8987 }
8988 EMSG(_("E808: Number or Float required"));
8989 return FAIL;
8990}
8991
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008992/*
8993 * "abs(expr)" function
8994 */
8995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008996f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008997{
8998 if (argvars[0].v_type == VAR_FLOAT)
8999 {
9000 rettv->v_type = VAR_FLOAT;
9001 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9002 }
9003 else
9004 {
9005 varnumber_T n;
9006 int error = FALSE;
9007
9008 n = get_tv_number_chk(&argvars[0], &error);
9009 if (error)
9010 rettv->vval.v_number = -1;
9011 else if (n > 0)
9012 rettv->vval.v_number = n;
9013 else
9014 rettv->vval.v_number = -n;
9015 }
9016}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009017
9018/*
9019 * "acos()" function
9020 */
9021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009022f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009023{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009024 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009025
9026 rettv->v_type = VAR_FLOAT;
9027 if (get_float_arg(argvars, &f) == OK)
9028 rettv->vval.v_float = acos(f);
9029 else
9030 rettv->vval.v_float = 0.0;
9031}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009032#endif
9033
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009035 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 */
9037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009038f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009043 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009045 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009046 && !tv_check_lock(l->lv_lock,
9047 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009048 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009050 }
9051 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009052 EMSG(_(e_listreq));
9053}
9054
9055/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009056 * "and(expr, expr)" function
9057 */
9058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009059f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009060{
9061 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9062 & get_tv_number_chk(&argvars[1], NULL);
9063}
9064
9065/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009066 * "append(lnum, string/list)" function
9067 */
9068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009069f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009070{
9071 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009072 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009073 list_T *l = NULL;
9074 listitem_T *li = NULL;
9075 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009076 long added = 0;
9077
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009078 /* When coming here from Insert mode, sync undo, so that this can be
9079 * undone separately from what was previously inserted. */
9080 if (u_sync_once == 2)
9081 {
9082 u_sync_once = 1; /* notify that u_sync() was called */
9083 u_sync(TRUE);
9084 }
9085
Bram Moolenaar0d660222005-01-07 21:51:51 +00009086 lnum = get_tv_lnum(argvars);
9087 if (lnum >= 0
9088 && lnum <= curbuf->b_ml.ml_line_count
9089 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009090 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009091 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009092 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009093 l = argvars[1].vval.v_list;
9094 if (l == NULL)
9095 return;
9096 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009097 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009098 for (;;)
9099 {
9100 if (l == NULL)
9101 tv = &argvars[1]; /* append a string */
9102 else if (li == NULL)
9103 break; /* end of list */
9104 else
9105 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106 line = get_tv_string_chk(tv);
9107 if (line == NULL) /* type error */
9108 {
9109 rettv->vval.v_number = 1; /* Failed */
9110 break;
9111 }
9112 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009113 ++added;
9114 if (l == NULL)
9115 break;
9116 li = li->li_next;
9117 }
9118
9119 appended_lines_mark(lnum, added);
9120 if (curwin->w_cursor.lnum > lnum)
9121 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009123 else
9124 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
9127/*
9128 * "argc()" function
9129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009131f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134}
9135
9136/*
9137 * "argidx()" function
9138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009140f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143}
9144
9145/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009146 * "arglistid()" function
9147 */
9148 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009149f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009150{
9151 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009152
9153 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009154 wp = find_tabwin(&argvars[0], &argvars[1]);
9155 if (wp != NULL)
9156 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009157}
9158
9159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 * "argv(nr)" function
9161 */
9162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009163f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165 int idx;
9166
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009167 if (argvars[0].v_type != VAR_UNKNOWN)
9168 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009169 idx = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009170 if (idx >= 0 && idx < ARGCOUNT)
9171 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9172 else
9173 rettv->vval.v_string = NULL;
9174 rettv->v_type = VAR_STRING;
9175 }
9176 else if (rettv_list_alloc(rettv) == OK)
9177 for (idx = 0; idx < ARGCOUNT; ++idx)
9178 list_append_string(rettv->vval.v_list,
9179 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180}
9181
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009182typedef enum
9183{
9184 ASSERT_EQUAL,
9185 ASSERT_NOTEQUAL,
9186 ASSERT_MATCH,
9187 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009188 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009189} assert_type_T;
9190
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009191static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009192static 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 +01009193static void assert_error(garray_T *gap);
9194static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009195
9196/*
9197 * Prepare "gap" for an assert error and add the sourcing position.
9198 */
9199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009200prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009201{
9202 char buf[NUMBUFLEN];
9203
9204 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009205 if (sourcing_name != NULL)
9206 {
9207 ga_concat(gap, sourcing_name);
9208 if (sourcing_lnum > 0)
9209 ga_concat(gap, (char_u *)" ");
9210 }
9211 if (sourcing_lnum > 0)
9212 {
9213 sprintf(buf, "line %ld", (long)sourcing_lnum);
9214 ga_concat(gap, (char_u *)buf);
9215 }
9216 if (sourcing_name != NULL || sourcing_lnum > 0)
9217 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009218}
9219
9220/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009221 * Append "str" to "gap", escaping unprintable characters.
9222 * Changes NL to \n, CR to \r, etc.
9223 */
9224 static void
9225ga_concat_esc(garray_T *gap, char_u *str)
9226{
9227 char_u *p;
9228 char_u buf[NUMBUFLEN];
9229
Bram Moolenaarf1551962016-03-15 12:55:58 +01009230 if (str == NULL)
9231 {
9232 ga_concat(gap, (char_u *)"NULL");
9233 return;
9234 }
9235
Bram Moolenaar23689172016-02-15 22:37:37 +01009236 for (p = str; *p != NUL; ++p)
9237 switch (*p)
9238 {
9239 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9240 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9241 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9242 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9243 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9244 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9245 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9246 default:
9247 if (*p < ' ')
9248 {
9249 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9250 ga_concat(gap, buf);
9251 }
9252 else
9253 ga_append(gap, *p);
9254 break;
9255 }
9256}
9257
9258/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009259 * Fill "gap" with information about an assert error.
9260 */
9261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009262fill_assert_error(
9263 garray_T *gap,
9264 typval_T *opt_msg_tv,
9265 char_u *exp_str,
9266 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009267 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009268 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009269{
9270 char_u numbuf[NUMBUFLEN];
9271 char_u *tofree;
9272
9273 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9274 {
9275 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9276 vim_free(tofree);
9277 }
9278 else
9279 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009280 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009281 ga_concat(gap, (char_u *)"Pattern ");
9282 else
9283 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009284 if (exp_str == NULL)
9285 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009286 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009287 vim_free(tofree);
9288 }
9289 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009290 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009291 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009292 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009293 else if (atype == ASSERT_NOTMATCH)
9294 ga_concat(gap, (char_u *)" does match ");
9295 else if (atype == ASSERT_NOTEQUAL)
9296 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009297 else
9298 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009299 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009300 vim_free(tofree);
9301 }
9302}
Bram Moolenaar43345542015-11-29 17:35:35 +01009303
9304/*
9305 * Add an assert error to v:errors.
9306 */
9307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009308assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009309{
9310 struct vimvar *vp = &vimvars[VV_ERRORS];
9311
9312 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9313 /* Make sure v:errors is a list. */
9314 set_vim_var_list(VV_ERRORS, list_alloc());
9315 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9316}
9317
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009318 static void
9319assert_equal_common(typval_T *argvars, assert_type_T atype)
9320{
9321 garray_T ga;
9322
9323 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9324 != (atype == ASSERT_EQUAL))
9325 {
9326 prepare_assert_error(&ga);
9327 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9328 atype);
9329 assert_error(&ga);
9330 ga_clear(&ga);
9331 }
9332}
9333
Bram Moolenaar43345542015-11-29 17:35:35 +01009334/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009335 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009336 */
9337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009338f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009339{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009340 assert_equal_common(argvars, ASSERT_EQUAL);
9341}
Bram Moolenaar43345542015-11-29 17:35:35 +01009342
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009343/*
9344 * "assert_notequal(expected, actual[, msg])" function
9345 */
9346 static void
9347f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9348{
9349 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009350}
9351
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009352/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009353 * "assert_exception(string[, msg])" function
9354 */
9355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009356f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009357{
9358 garray_T ga;
9359 char *error;
9360
9361 error = (char *)get_tv_string_chk(&argvars[0]);
9362 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9363 {
9364 prepare_assert_error(&ga);
9365 ga_concat(&ga, (char_u *)"v:exception is not set");
9366 assert_error(&ga);
9367 ga_clear(&ga);
9368 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009369 else if (error != NULL
9370 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009371 {
9372 prepare_assert_error(&ga);
9373 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009374 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009375 assert_error(&ga);
9376 ga_clear(&ga);
9377 }
9378}
9379
9380/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009381 * "assert_fails(cmd [, error])" function
9382 */
9383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009384f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009385{
9386 char_u *cmd = get_tv_string_chk(&argvars[0]);
9387 garray_T ga;
9388
9389 called_emsg = FALSE;
9390 suppress_errthrow = TRUE;
9391 emsg_silent = TRUE;
9392 do_cmdline_cmd(cmd);
9393 if (!called_emsg)
9394 {
9395 prepare_assert_error(&ga);
9396 ga_concat(&ga, (char_u *)"command did not fail: ");
9397 ga_concat(&ga, cmd);
9398 assert_error(&ga);
9399 ga_clear(&ga);
9400 }
9401 else if (argvars[1].v_type != VAR_UNKNOWN)
9402 {
9403 char_u buf[NUMBUFLEN];
9404 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9405
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009406 if (error == NULL
9407 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009408 {
9409 prepare_assert_error(&ga);
9410 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009411 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009412 assert_error(&ga);
9413 ga_clear(&ga);
9414 }
9415 }
9416
9417 called_emsg = FALSE;
9418 suppress_errthrow = FALSE;
9419 emsg_silent = FALSE;
9420 emsg_on_display = FALSE;
9421 set_vim_var_string(VV_ERRMSG, NULL, 0);
9422}
9423
9424/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009425 * Common for assert_true() and assert_false().
9426 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009428assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009429{
9430 int error = FALSE;
9431 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009432
Bram Moolenaar37127922016-02-06 20:29:28 +01009433 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009434 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009435 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009436 if (argvars[0].v_type != VAR_NUMBER
9437 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9438 || error)
9439 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009440 prepare_assert_error(&ga);
9441 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009442 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009443 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009444 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009445 ga_clear(&ga);
9446 }
9447}
9448
9449/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009450 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009451 */
9452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009453f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009454{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009455 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009456}
9457
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009458 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009459assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009460{
9461 garray_T ga;
9462 char_u buf1[NUMBUFLEN];
9463 char_u buf2[NUMBUFLEN];
9464 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9465 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9466
Bram Moolenaar72188e92016-03-28 22:48:29 +02009467 if (pat == NULL || text == NULL)
9468 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009469 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009470 {
9471 prepare_assert_error(&ga);
9472 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009473 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009474 assert_error(&ga);
9475 ga_clear(&ga);
9476 }
9477}
9478
9479/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009480 * "assert_match(pattern, actual[, msg])" function
9481 */
9482 static void
9483f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9484{
9485 assert_match_common(argvars, ASSERT_MATCH);
9486}
9487
9488/*
9489 * "assert_notmatch(pattern, actual[, msg])" function
9490 */
9491 static void
9492f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9493{
9494 assert_match_common(argvars, ASSERT_NOTMATCH);
9495}
9496
9497/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009498 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009499 */
9500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009502{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009503 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009504}
9505
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009506#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009507/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009508 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009509 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009511f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009512{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009513 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009514
9515 rettv->v_type = VAR_FLOAT;
9516 if (get_float_arg(argvars, &f) == OK)
9517 rettv->vval.v_float = asin(f);
9518 else
9519 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009520}
9521
9522/*
9523 * "atan()" function
9524 */
9525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009526f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009527{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009528 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009529
9530 rettv->v_type = VAR_FLOAT;
9531 if (get_float_arg(argvars, &f) == OK)
9532 rettv->vval.v_float = atan(f);
9533 else
9534 rettv->vval.v_float = 0.0;
9535}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009536
9537/*
9538 * "atan2()" function
9539 */
9540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009541f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009542{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009543 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009544
9545 rettv->v_type = VAR_FLOAT;
9546 if (get_float_arg(argvars, &fx) == OK
9547 && get_float_arg(&argvars[1], &fy) == OK)
9548 rettv->vval.v_float = atan2(fx, fy);
9549 else
9550 rettv->vval.v_float = 0.0;
9551}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009552#endif
9553
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554/*
9555 * "browse(save, title, initdir, default)" function
9556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009558f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559{
9560#ifdef FEAT_BROWSE
9561 int save;
9562 char_u *title;
9563 char_u *initdir;
9564 char_u *defname;
9565 char_u buf[NUMBUFLEN];
9566 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009569 save = (int)get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009570 title = get_tv_string_chk(&argvars[1]);
9571 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9572 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574 if (error || title == NULL || initdir == NULL || defname == NULL)
9575 rettv->vval.v_string = NULL;
9576 else
9577 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009578 do_browse(save ? BROWSE_SAVE : 0,
9579 title, defname, NULL, initdir, NULL, curbuf);
9580#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009582#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009583 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009584}
9585
9586/*
9587 * "browsedir(title, initdir)" function
9588 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009590f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009591{
9592#ifdef FEAT_BROWSE
9593 char_u *title;
9594 char_u *initdir;
9595 char_u buf[NUMBUFLEN];
9596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009597 title = get_tv_string_chk(&argvars[0]);
9598 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009600 if (title == NULL || initdir == NULL)
9601 rettv->vval.v_string = NULL;
9602 else
9603 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009604 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609}
9610
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009611static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009612
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613/*
9614 * Find a buffer by number or exact name.
9615 */
9616 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009617find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
9619 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009621 if (avar->v_type == VAR_NUMBER)
9622 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009623 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009625 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009626 if (buf == NULL)
9627 {
9628 /* No full path name match, try a match with a URL or a "nofile"
9629 * buffer, these don't use the full path. */
9630 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9631 if (buf->b_fname != NULL
9632 && (path_with_url(buf->b_fname)
9633#ifdef FEAT_QUICKFIX
9634 || bt_nofile(buf)
9635#endif
9636 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009637 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009638 break;
9639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 }
9641 return buf;
9642}
9643
9644/*
9645 * "bufexists(expr)" function
9646 */
9647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009648f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651}
9652
9653/*
9654 * "buflisted(expr)" function
9655 */
9656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009657f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658{
9659 buf_T *buf;
9660
9661 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009662 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663}
9664
9665/*
9666 * "bufloaded(expr)" function
9667 */
9668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009669f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670{
9671 buf_T *buf;
9672
9673 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675}
9676
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009677 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009678buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 int save_magic;
9681 char_u *save_cpo;
9682 buf_T *buf;
9683
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9685 save_magic = p_magic;
9686 p_magic = TRUE;
9687 save_cpo = p_cpo;
9688 p_cpo = (char_u *)"";
9689
9690 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009691 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
9693 p_magic = save_magic;
9694 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009695 return buf;
9696}
9697
9698/*
9699 * Get buffer by number or pattern.
9700 */
9701 static buf_T *
9702get_buf_tv(typval_T *tv, int curtab_only)
9703{
9704 char_u *name = tv->vval.v_string;
9705 buf_T *buf;
9706
9707 if (tv->v_type == VAR_NUMBER)
9708 return buflist_findnr((int)tv->vval.v_number);
9709 if (tv->v_type != VAR_STRING)
9710 return NULL;
9711 if (name == NULL || *name == NUL)
9712 return curbuf;
9713 if (name[0] == '$' && name[1] == NUL)
9714 return lastbuf;
9715
9716 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717
9718 /* If not found, try expanding the name, like done for bufexists(). */
9719 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721
9722 return buf;
9723}
9724
9725/*
9726 * "bufname(expr)" function
9727 */
9728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009729f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731 buf_T *buf;
9732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009733 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009735 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 --emsg_off;
9742}
9743
9744/*
9745 * "bufnr(expr)" function
9746 */
9747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009748f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
9750 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009751 int error = FALSE;
9752 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009754 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009756 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009757 --emsg_off;
9758
9759 /* If the buffer isn't found and the second argument is not zero create a
9760 * new buffer. */
9761 if (buf == NULL
9762 && argvars[1].v_type != VAR_UNKNOWN
9763 && get_tv_number_chk(&argvars[1], &error) != 0
9764 && !error
9765 && (name = get_tv_string_chk(&argvars[0])) != NULL
9766 && !error)
9767 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9768
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009770 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773}
9774
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +02009776buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
9778#ifdef FEAT_WINDOWS
9779 win_T *wp;
9780 int winnr = 0;
9781#endif
9782 buf_T *buf;
9783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009786 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787#ifdef FEAT_WINDOWS
9788 for (wp = firstwin; wp; wp = wp->w_next)
9789 {
9790 ++winnr;
9791 if (wp->w_buffer == buf)
9792 break;
9793 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +02009794 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +02009796 rettv->vval.v_number = (curwin->w_buffer == buf
9797 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798#endif
9799 --emsg_off;
9800}
9801
9802/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +02009803 * "bufwinid(nr)" function
9804 */
9805 static void
9806f_bufwinid(typval_T *argvars, typval_T *rettv)
9807{
9808 buf_win_common(argvars, rettv, FALSE);
9809}
9810
9811/*
9812 * "bufwinnr(nr)" function
9813 */
9814 static void
9815f_bufwinnr(typval_T *argvars, typval_T *rettv)
9816{
9817 buf_win_common(argvars, rettv, TRUE);
9818}
9819
9820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 * "byte2line(byte)" function
9822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009824f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825{
9826#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828#else
9829 long boff = 0;
9830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009831 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 (linenr_T)0, &boff);
9837#endif
9838}
9839
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009841byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009842{
9843#ifdef FEAT_MBYTE
9844 char_u *t;
9845#endif
9846 char_u *str;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009847 varnumber_T idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009849 str = get_tv_string_chk(&argvars[0]);
9850 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009851 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009852 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009853 return;
9854
9855#ifdef FEAT_MBYTE
9856 t = str;
9857 for ( ; idx > 0; idx--)
9858 {
9859 if (*t == NUL) /* EOL reached */
9860 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009861 if (enc_utf8 && comp)
9862 t += utf_ptr2len(t);
9863 else
9864 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009865 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009866 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009867#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009868 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009870#endif
9871}
9872
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009873/*
9874 * "byteidx()" function
9875 */
9876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009877f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009878{
9879 byteidx(argvars, rettv, FALSE);
9880}
9881
9882/*
9883 * "byteidxcomp()" function
9884 */
9885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009886f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009887{
9888 byteidx(argvars, rettv, TRUE);
9889}
9890
Bram Moolenaardb913952012-06-29 12:54:53 +02009891 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009892func_call(
9893 char_u *name,
9894 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009895 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009896 dict_T *selfdict,
9897 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009898{
9899 listitem_T *item;
9900 typval_T argv[MAX_FUNC_ARGS + 1];
9901 int argc = 0;
9902 int dummy;
9903 int r = 0;
9904
9905 for (item = args->vval.v_list->lv_first; item != NULL;
9906 item = item->li_next)
9907 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009908 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009909 {
9910 EMSG(_("E699: Too many arguments"));
9911 break;
9912 }
9913 /* Make a copy of each argument. This is needed to be able to set
9914 * v_lock to VAR_FIXED in the copy without changing the original list.
9915 */
9916 copy_tv(&item->li_tv, &argv[argc++]);
9917 }
9918
9919 if (item == NULL)
9920 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9921 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009922 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009923
9924 /* Free the arguments. */
9925 while (argc > 0)
9926 clear_tv(&argv[--argc]);
9927
9928 return r;
9929}
9930
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009931/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009932 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009933 */
9934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009935f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009936{
9937 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009938 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009939 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009940
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009941 if (argvars[1].v_type != VAR_LIST)
9942 {
9943 EMSG(_(e_listreq));
9944 return;
9945 }
9946 if (argvars[1].vval.v_list == NULL)
9947 return;
9948
9949 if (argvars[0].v_type == VAR_FUNC)
9950 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009951 else if (argvars[0].v_type == VAR_PARTIAL)
9952 {
9953 partial = argvars[0].vval.v_partial;
9954 func = partial->pt_name;
9955 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009956 else
9957 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009958 if (*func == NUL)
9959 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009960
Bram Moolenaare9a41262005-01-15 22:18:47 +00009961 if (argvars[2].v_type != VAR_UNKNOWN)
9962 {
9963 if (argvars[2].v_type != VAR_DICT)
9964 {
9965 EMSG(_(e_dictreq));
9966 return;
9967 }
9968 selfdict = argvars[2].vval.v_dict;
9969 }
9970
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009971 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009972}
9973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009974#ifdef FEAT_FLOAT
9975/*
9976 * "ceil({float})" function
9977 */
9978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009979f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009980{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009981 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009982
9983 rettv->v_type = VAR_FLOAT;
9984 if (get_float_arg(argvars, &f) == OK)
9985 rettv->vval.v_float = ceil(f);
9986 else
9987 rettv->vval.v_float = 0.0;
9988}
9989#endif
9990
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009991#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009992/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009993 * "ch_close()" function
9994 */
9995 static void
9996f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9997{
Bram Moolenaar437905c2016-04-26 19:01:05 +02009998 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009999
10000 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010001 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010002 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010003 channel_clear(channel);
10004 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010005}
10006
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010007/*
10008 * "ch_getbufnr()" function
10009 */
10010 static void
10011f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10012{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010013 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010014
10015 rettv->vval.v_number = -1;
10016 if (channel != NULL)
10017 {
10018 char_u *what = get_tv_string(&argvars[1]);
10019 int part;
10020
10021 if (STRCMP(what, "err") == 0)
10022 part = PART_ERR;
10023 else if (STRCMP(what, "out") == 0)
10024 part = PART_OUT;
10025 else if (STRCMP(what, "in") == 0)
10026 part = PART_IN;
10027 else
10028 part = PART_SOCK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020010029 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
10030 rettv->vval.v_number =
10031 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010032 }
10033}
10034
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010035/*
10036 * "ch_getjob()" function
10037 */
10038 static void
10039f_ch_getjob(typval_T *argvars, typval_T *rettv)
10040{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010041 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010042
10043 if (channel != NULL)
10044 {
10045 rettv->v_type = VAR_JOB;
10046 rettv->vval.v_job = channel->ch_job;
10047 if (channel->ch_job != NULL)
10048 ++channel->ch_job->jv_refcount;
10049 }
10050}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010051
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010052/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010053 * "ch_info()" function
10054 */
10055 static void
10056f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10057{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010058 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010059
10060 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10061 channel_info(channel, rettv->vval.v_dict);
10062}
10063
10064/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010065 * "ch_log()" function
10066 */
10067 static void
10068f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10069{
10070 char_u *msg = get_tv_string(&argvars[0]);
10071 channel_T *channel = NULL;
10072
10073 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010074 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010075
10076 ch_log(channel, (char *)msg);
10077}
10078
10079/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010080 * "ch_logfile()" function
10081 */
10082 static void
10083f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10084{
10085 char_u *fname;
10086 char_u *opt = (char_u *)"";
10087 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010088
10089 fname = get_tv_string(&argvars[0]);
10090 if (argvars[1].v_type == VAR_STRING)
10091 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010092 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010093}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010094
10095/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010096 * "ch_open()" function
10097 */
10098 static void
10099f_ch_open(typval_T *argvars, typval_T *rettv)
10100{
Bram Moolenaar77073442016-02-13 23:23:53 +010010101 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010102 if (check_restricted() || check_secure())
10103 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010104 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010105}
10106
10107/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010108 * "ch_read()" function
10109 */
10110 static void
10111f_ch_read(typval_T *argvars, typval_T *rettv)
10112{
10113 common_channel_read(argvars, rettv, FALSE);
10114}
10115
10116/*
10117 * "ch_readraw()" function
10118 */
10119 static void
10120f_ch_readraw(typval_T *argvars, typval_T *rettv)
10121{
10122 common_channel_read(argvars, rettv, TRUE);
10123}
10124
10125/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010126 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010127 */
10128 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010129f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10130{
10131 ch_expr_common(argvars, rettv, TRUE);
10132}
10133
10134/*
10135 * "ch_sendexpr()" function
10136 */
10137 static void
10138f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10139{
10140 ch_expr_common(argvars, rettv, FALSE);
10141}
10142
10143/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010144 * "ch_evalraw()" function
10145 */
10146 static void
10147f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10148{
10149 ch_raw_common(argvars, rettv, TRUE);
10150}
10151
10152/*
10153 * "ch_sendraw()" function
10154 */
10155 static void
10156f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10157{
10158 ch_raw_common(argvars, rettv, FALSE);
10159}
10160
10161/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010162 * "ch_setoptions()" function
10163 */
10164 static void
10165f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10166{
10167 channel_T *channel;
10168 jobopt_T opt;
10169
Bram Moolenaar437905c2016-04-26 19:01:05 +020010170 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010171 if (channel == NULL)
10172 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010173 clear_job_options(&opt);
10174 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010175 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10176 channel_set_options(channel, &opt);
10177 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010178}
10179
10180/*
10181 * "ch_status()" function
10182 */
10183 static void
10184f_ch_status(typval_T *argvars, typval_T *rettv)
10185{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010186 channel_T *channel;
10187
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010188 /* return an empty string by default */
10189 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010190 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010191
Bram Moolenaar437905c2016-04-26 19:01:05 +020010192 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010193 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010194}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010195#endif
10196
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010197/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010198 * "changenr()" function
10199 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010201f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010202{
10203 rettv->vval.v_number = curbuf->b_u_seq_cur;
10204}
10205
10206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 * "char2nr(string)" function
10208 */
10209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010210f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211{
10212#ifdef FEAT_MBYTE
10213 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010214 {
10215 int utf8 = 0;
10216
10217 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010218 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010010219
10220 if (utf8)
10221 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10222 else
10223 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 else
10226#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010227 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228}
10229
10230/*
10231 * "cindent(lnum)" function
10232 */
10233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010234f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235{
10236#ifdef FEAT_CINDENT
10237 pos_T pos;
10238 linenr_T lnum;
10239
10240 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10243 {
10244 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010245 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 curwin->w_cursor = pos;
10247 }
10248 else
10249#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010250 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251}
10252
10253/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010254 * "clearmatches()" function
10255 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010257f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010258{
10259#ifdef FEAT_SEARCH_EXTRA
10260 clear_matches(curwin);
10261#endif
10262}
10263
10264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 * "col(string)" function
10266 */
10267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010268f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269{
10270 colnr_T col = 0;
10271 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010272 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010274 fp = var2fpos(&argvars[0], FALSE, &fnum);
10275 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 {
10277 if (fp->col == MAXCOL)
10278 {
10279 /* '> can be MAXCOL, get the length of the line then */
10280 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010281 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 else
10283 col = MAXCOL;
10284 }
10285 else
10286 {
10287 col = fp->col + 1;
10288#ifdef FEAT_VIRTUALEDIT
10289 /* col(".") when the cursor is on the NUL at the end of the line
10290 * because of "coladd" can be seen as an extra column. */
10291 if (virtual_active() && fp == &curwin->w_cursor)
10292 {
10293 char_u *p = ml_get_cursor();
10294
10295 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10296 curwin->w_virtcol - curwin->w_cursor.coladd))
10297 {
10298# ifdef FEAT_MBYTE
10299 int l;
10300
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010301 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 col += l;
10303# else
10304 if (*p != NUL && p[1] == NUL)
10305 ++col;
10306# endif
10307 }
10308 }
10309#endif
10310 }
10311 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313}
10314
Bram Moolenaar572cb562005-08-05 21:35:02 +000010315#if defined(FEAT_INS_EXPAND)
10316/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010317 * "complete()" function
10318 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010320f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010321{
10322 int startcol;
10323
10324 if ((State & INSERT) == 0)
10325 {
10326 EMSG(_("E785: complete() can only be used in Insert mode"));
10327 return;
10328 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010329
10330 /* Check for undo allowed here, because if something was already inserted
10331 * the line was already saved for undo and this check isn't done. */
10332 if (!undo_allowed())
10333 return;
10334
Bram Moolenaarade00832006-03-10 21:46:58 +000010335 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10336 {
10337 EMSG(_(e_invarg));
10338 return;
10339 }
10340
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010341 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaarade00832006-03-10 21:46:58 +000010342 if (startcol <= 0)
10343 return;
10344
10345 set_completion(startcol - 1, argvars[1].vval.v_list);
10346}
10347
10348/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010349 * "complete_add()" function
10350 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010352f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010353{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010354 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010355}
10356
10357/*
10358 * "complete_check()" function
10359 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010361f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010362{
10363 int saved = RedrawingDisabled;
10364
10365 RedrawingDisabled = 0;
10366 ins_compl_check_keys(0);
10367 rettv->vval.v_number = compl_interrupted;
10368 RedrawingDisabled = saved;
10369}
10370#endif
10371
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372/*
10373 * "confirm(message, buttons[, default [, type]])" function
10374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010376f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377{
10378#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10379 char_u *message;
10380 char_u *buttons = NULL;
10381 char_u buf[NUMBUFLEN];
10382 char_u buf2[NUMBUFLEN];
10383 int def = 1;
10384 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010385 char_u *typestr;
10386 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010388 message = get_tv_string_chk(&argvars[0]);
10389 if (message == NULL)
10390 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010391 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010393 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10394 if (buttons == NULL)
10395 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010396 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010398 def = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010399 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010401 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10402 if (typestr == NULL)
10403 error = TRUE;
10404 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010406 switch (TOUPPER_ASC(*typestr))
10407 {
10408 case 'E': type = VIM_ERROR; break;
10409 case 'Q': type = VIM_QUESTION; break;
10410 case 'I': type = VIM_INFO; break;
10411 case 'W': type = VIM_WARNING; break;
10412 case 'G': type = VIM_GENERIC; break;
10413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 }
10415 }
10416 }
10417 }
10418
10419 if (buttons == NULL || *buttons == NUL)
10420 buttons = (char_u *)_("&Ok");
10421
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010422 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010423 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010424 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425#endif
10426}
10427
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010428/*
10429 * "copy()" function
10430 */
10431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010432f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010433{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010434 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010435}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010437#ifdef FEAT_FLOAT
10438/*
10439 * "cos()" function
10440 */
10441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010442f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010443{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010444 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010445
10446 rettv->v_type = VAR_FLOAT;
10447 if (get_float_arg(argvars, &f) == OK)
10448 rettv->vval.v_float = cos(f);
10449 else
10450 rettv->vval.v_float = 0.0;
10451}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010452
10453/*
10454 * "cosh()" function
10455 */
10456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010457f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010458{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010459 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010460
10461 rettv->v_type = VAR_FLOAT;
10462 if (get_float_arg(argvars, &f) == OK)
10463 rettv->vval.v_float = cosh(f);
10464 else
10465 rettv->vval.v_float = 0.0;
10466}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010467#endif
10468
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010470 * "count()" function
10471 */
10472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010473f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010474{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010475 long n = 0;
10476 int ic = FALSE;
10477
Bram Moolenaare9a41262005-01-15 22:18:47 +000010478 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010479 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010480 listitem_T *li;
10481 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010482 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010483
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 if ((l = argvars[0].vval.v_list) != NULL)
10485 {
10486 li = l->lv_first;
10487 if (argvars[2].v_type != VAR_UNKNOWN)
10488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010489 int error = FALSE;
10490
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010491 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010492 if (argvars[3].v_type != VAR_UNKNOWN)
10493 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010494 idx = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 if (!error)
10496 {
10497 li = list_find(l, idx);
10498 if (li == NULL)
10499 EMSGN(_(e_listidx), idx);
10500 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010501 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010502 if (error)
10503 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010504 }
10505
10506 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010507 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010508 ++n;
10509 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010510 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010511 else if (argvars[0].v_type == VAR_DICT)
10512 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010513 int todo;
10514 dict_T *d;
10515 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010516
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010517 if ((d = argvars[0].vval.v_dict) != NULL)
10518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 int error = FALSE;
10520
Bram Moolenaare9a41262005-01-15 22:18:47 +000010521 if (argvars[2].v_type != VAR_UNKNOWN)
10522 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010523 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010524 if (argvars[3].v_type != VAR_UNKNOWN)
10525 EMSG(_(e_invarg));
10526 }
10527
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010528 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010529 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010530 {
10531 if (!HASHITEM_EMPTY(hi))
10532 {
10533 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010534 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010535 ++n;
10536 }
10537 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010538 }
10539 }
10540 else
10541 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010542 rettv->vval.v_number = n;
10543}
10544
10545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10547 *
10548 * Checks the existence of a cscope connection.
10549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010551f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552{
10553#ifdef FEAT_CSCOPE
10554 int num = 0;
10555 char_u *dbpath = NULL;
10556 char_u *prepend = NULL;
10557 char_u buf[NUMBUFLEN];
10558
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010559 if (argvars[0].v_type != VAR_UNKNOWN
10560 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010562 num = (int)get_tv_number(&argvars[0]);
10563 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010564 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 }
10567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010568 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569#endif
10570}
10571
10572/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010573 * "cursor(lnum, col)" function, or
10574 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010576 * Moves the cursor to the specified line and column.
10577 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010580f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581{
10582 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010583#ifdef FEAT_VIRTUALEDIT
10584 long coladd = 0;
10585#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010586 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010588 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010589 if (argvars[1].v_type == VAR_UNKNOWN)
10590 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010591 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010592 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010593
Bram Moolenaar493c1782014-05-28 14:34:46 +020010594 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010595 {
10596 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010597 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010598 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010599 line = pos.lnum;
10600 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010601#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010602 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010603#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010604 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010605 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010606 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010607 set_curswant = FALSE;
10608 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010609 }
10610 else
10611 {
10612 line = get_tv_lnum(argvars);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010613 col = (long)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000010614#ifdef FEAT_VIRTUALEDIT
10615 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010616 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000010617#endif
10618 }
10619 if (line < 0 || col < 0
10620#ifdef FEAT_VIRTUALEDIT
10621 || coladd < 0
10622#endif
10623 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010624 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 if (line > 0)
10626 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 if (col > 0)
10628 curwin->w_cursor.col = col - 1;
10629#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010630 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631#endif
10632
10633 /* Make sure the cursor is in a valid position. */
10634 check_cursor();
10635#ifdef FEAT_MBYTE
10636 /* Correct cursor for multi-byte character. */
10637 if (has_mbyte)
10638 mb_adjust_cursor();
10639#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010640
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010641 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010642 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643}
10644
10645/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010646 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 */
10648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010649f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010651 int noref = 0;
10652
10653 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010654 noref = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010655 if (noref < 0 || noref > 1)
10656 EMSG(_(e_invarg));
10657 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010658 {
10659 current_copyID += COPYID_INC;
10660 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662}
10663
10664/*
10665 * "delete()" function
10666 */
10667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010668f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010670 char_u nbuf[NUMBUFLEN];
10671 char_u *name;
10672 char_u *flags;
10673
10674 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010676 return;
10677
10678 name = get_tv_string(&argvars[0]);
10679 if (name == NULL || *name == NUL)
10680 {
10681 EMSG(_(e_invarg));
10682 return;
10683 }
10684
10685 if (argvars[1].v_type != VAR_UNKNOWN)
10686 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010688 flags = (char_u *)"";
10689
10690 if (*flags == NUL)
10691 /* delete a file */
10692 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10693 else if (STRCMP(flags, "d") == 0)
10694 /* delete an empty directory */
10695 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10696 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010697 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010698 rettv->vval.v_number = delete_recursive(name);
10699 else
10700 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701}
10702
10703/*
10704 * "did_filetype()" function
10705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010707f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708{
10709#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711#endif
10712}
10713
10714/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010715 * "diff_filler()" function
10716 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010718f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010719{
10720#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010722#endif
10723}
10724
10725/*
10726 * "diff_hlID()" function
10727 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010729f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010730{
10731#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010732 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010733 static linenr_T prev_lnum = 0;
10734 static int changedtick = 0;
10735 static int fnum = 0;
10736 static int change_start = 0;
10737 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010738 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010739 int filler_lines;
10740 int col;
10741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010742 if (lnum < 0) /* ignore type error in {lnum} arg */
10743 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010744 if (lnum != prev_lnum
10745 || changedtick != curbuf->b_changedtick
10746 || fnum != curbuf->b_fnum)
10747 {
10748 /* New line, buffer, change: need to get the values. */
10749 filler_lines = diff_check(curwin, lnum);
10750 if (filler_lines < 0)
10751 {
10752 if (filler_lines == -1)
10753 {
10754 change_start = MAXCOL;
10755 change_end = -1;
10756 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10757 hlID = HLF_ADD; /* added line */
10758 else
10759 hlID = HLF_CHD; /* changed line */
10760 }
10761 else
10762 hlID = HLF_ADD; /* added line */
10763 }
10764 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010765 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010766 prev_lnum = lnum;
10767 changedtick = curbuf->b_changedtick;
10768 fnum = curbuf->b_fnum;
10769 }
10770
10771 if (hlID == HLF_CHD || hlID == HLF_TXD)
10772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010774 if (col >= change_start && col <= change_end)
10775 hlID = HLF_TXD; /* changed text */
10776 else
10777 hlID = HLF_CHD; /* changed line */
10778 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010779 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010780#endif
10781}
10782
10783/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010784 * "empty({expr})" function
10785 */
10786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010787f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010788{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010789 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010790
10791 switch (argvars[0].v_type)
10792 {
10793 case VAR_STRING:
10794 case VAR_FUNC:
10795 n = argvars[0].vval.v_string == NULL
10796 || *argvars[0].vval.v_string == NUL;
10797 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010798 case VAR_PARTIAL:
10799 n = FALSE;
10800 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010801 case VAR_NUMBER:
10802 n = argvars[0].vval.v_number == 0;
10803 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010804 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010805#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010806 n = argvars[0].vval.v_float == 0.0;
10807 break;
10808#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010809 case VAR_LIST:
10810 n = argvars[0].vval.v_list == NULL
10811 || argvars[0].vval.v_list->lv_first == NULL;
10812 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010813 case VAR_DICT:
10814 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010815 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010817 case VAR_SPECIAL:
10818 n = argvars[0].vval.v_number != VVAL_TRUE;
10819 break;
10820
Bram Moolenaar835dc632016-02-07 14:27:38 +010010821 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010822#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010823 n = argvars[0].vval.v_job == NULL
10824 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10825 break;
10826#endif
10827 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010828#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010829 n = argvars[0].vval.v_channel == NULL
10830 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010831 break;
10832#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010833 case VAR_UNKNOWN:
10834 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10835 n = TRUE;
10836 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010837 }
10838
10839 rettv->vval.v_number = n;
10840}
10841
10842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 * "escape({string}, {chars})" function
10844 */
10845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010846f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847{
10848 char_u buf[NUMBUFLEN];
10849
Bram Moolenaar758711c2005-02-02 23:11:38 +000010850 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10851 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853}
10854
10855/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010856 * "eval()" function
10857 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010859f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010860{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010861 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010863 s = get_tv_string_chk(&argvars[0]);
10864 if (s != NULL)
10865 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010866
Bram Moolenaar615b9972015-01-14 17:15:05 +010010867 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10869 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010870 if (p != NULL && !aborting())
10871 EMSG2(_(e_invexpr2), p);
10872 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010873 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010874 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010876 else if (*s != NUL)
10877 EMSG(_(e_trailing));
10878}
10879
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020010880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 * "eventhandler()" function
10882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010884f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010886 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887}
10888
10889/*
10890 * "executable()" function
10891 */
10892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010893f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010895 char_u *name = get_tv_string(&argvars[0]);
10896
10897 /* Check in $PATH and also check directly if there is a directory name. */
10898 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10899 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010900}
10901
Bram Moolenaar79815f12016-07-09 17:07:29 +020010902static garray_T redir_execute_ga;
10903
10904/*
10905 * Append "value[value_len]" to the execute() output.
10906 */
10907 void
10908execute_redir_str(char_u *value, int value_len)
10909{
10910 int len;
10911
10912 if (value_len == -1)
10913 len = (int)STRLEN(value); /* Append the entire string */
10914 else
10915 len = value_len; /* Append only "value_len" characters */
10916 if (ga_grow(&redir_execute_ga, len) == OK)
10917 {
10918 mch_memmove((char *)redir_execute_ga.ga_data
10919 + redir_execute_ga.ga_len, value, len);
10920 redir_execute_ga.ga_len += len;
10921 }
10922}
10923
10924/*
10925 * Get next line from a list.
10926 * Called by do_cmdline() to get the next line.
10927 * Returns allocated string, or NULL for end of function.
10928 */
10929
10930 static char_u *
10931get_list_line(
10932 int c UNUSED,
10933 void *cookie,
10934 int indent UNUSED)
10935{
10936 listitem_T **p = (listitem_T **)cookie;
10937 listitem_T *item = *p;
10938 char_u buf[NUMBUFLEN];
10939 char_u *s;
10940
10941 if (item == NULL)
10942 return NULL;
10943 s = get_tv_string_buf_chk(&item->li_tv, buf);
10944 *p = item->li_next;
10945 return s == NULL ? NULL : vim_strsave(s);
10946}
10947
10948/*
10949 * "execute()" function
10950 */
10951 static void
10952f_execute(typval_T *argvars, typval_T *rettv)
10953{
10954 char_u *cmd = NULL;
10955 list_T *list = NULL;
10956 int save_msg_silent = msg_silent;
10957 int save_emsg_silent = emsg_silent;
10958 int save_emsg_noredir = emsg_noredir;
10959 int save_redir_execute = redir_execute;
10960 garray_T save_ga;
10961
10962 rettv->vval.v_string = NULL;
10963 rettv->v_type = VAR_STRING;
10964
10965 if (argvars[0].v_type == VAR_LIST)
10966 {
10967 list = argvars[0].vval.v_list;
10968 if (list == NULL || list->lv_first == NULL)
10969 /* empty list, no commands, empty output */
10970 return;
10971 ++list->lv_refcount;
10972 }
10973 else
10974 {
10975 cmd = get_tv_string_chk(&argvars[0]);
10976 if (cmd == NULL)
10977 return;
10978 }
10979
Bram Moolenaar79815f12016-07-09 17:07:29 +020010980 if (argvars[1].v_type != VAR_UNKNOWN)
10981 {
10982 char_u buf[NUMBUFLEN];
10983 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
10984
10985 if (s == NULL)
10986 return;
10987 if (STRNCMP(s, "silent", 6) == 0)
10988 ++msg_silent;
10989 if (STRCMP(s, "silent!") == 0)
10990 {
10991 emsg_silent = TRUE;
10992 emsg_noredir = TRUE;
10993 }
10994 }
10995 else
10996 ++msg_silent;
10997
Bram Moolenaared59aa62016-07-09 17:41:12 +020010998 if (redir_execute)
10999 save_ga = redir_execute_ga;
11000 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
11001 redir_execute = TRUE;
11002
Bram Moolenaar79815f12016-07-09 17:07:29 +020011003 if (cmd != NULL)
11004 do_cmdline_cmd(cmd);
11005 else
11006 {
11007 listitem_T *item = list->lv_first;
11008
11009 do_cmdline(NULL, get_list_line, (void *)&item,
11010 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
11011 --list->lv_refcount;
11012 }
11013
11014 rettv->vval.v_string = redir_execute_ga.ga_data;
11015 msg_silent = save_msg_silent;
11016 emsg_silent = save_emsg_silent;
11017 emsg_noredir = save_emsg_noredir;
11018
11019 redir_execute = save_redir_execute;
11020 if (redir_execute)
11021 redir_execute_ga = save_ga;
11022
11023 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
11024 * line. Put it back in the first column. */
11025 msg_col = 0;
11026}
11027
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011028/*
11029 * "exepath()" function
11030 */
11031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011032f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011033{
11034 char_u *p = NULL;
11035
Bram Moolenaarb5971142015-03-21 17:32:19 +010011036 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011037 rettv->v_type = VAR_STRING;
11038 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039}
11040
11041/*
11042 * "exists()" function
11043 */
11044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011045f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046{
11047 char_u *p;
11048 char_u *name;
11049 int n = FALSE;
11050 int len = 0;
11051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 if (*p == '$') /* environment variable */
11054 {
11055 /* first try "normal" environment variables (fast) */
11056 if (mch_getenv(p + 1) != NULL)
11057 n = TRUE;
11058 else
11059 {
11060 /* try expanding things like $VIM and ${HOME} */
11061 p = expand_env_save(p);
11062 if (p != NULL && *p != '$')
11063 n = TRUE;
11064 vim_free(p);
11065 }
11066 }
11067 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011070 if (*skipwhite(p) != NUL)
11071 n = FALSE; /* trailing garbage */
11072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 else if (*p == '*') /* internal or user defined function */
11074 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011075 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 }
11077 else if (*p == ':')
11078 {
11079 n = cmd_exists(p + 1);
11080 }
11081 else if (*p == '#')
11082 {
11083#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011084 if (p[1] == '#')
11085 n = autocmd_supported(p + 2);
11086 else
11087 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088#endif
11089 }
11090 else /* internal variable */
11091 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011092 char_u *tofree;
11093 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011095 /* get_name_len() takes care of expanding curly braces */
11096 name = p;
11097 len = get_name_len(&p, &tofree, TRUE, FALSE);
11098 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011100 if (tofree != NULL)
11101 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011102 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011103 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011105 /* handle d.key, l[idx], f(expr) */
11106 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11107 if (n)
11108 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 }
11110 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011111 if (*p != NUL)
11112 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011114 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 }
11116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118}
11119
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011120#ifdef FEAT_FLOAT
11121/*
11122 * "exp()" function
11123 */
11124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011125f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011126{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011127 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011128
11129 rettv->v_type = VAR_FLOAT;
11130 if (get_float_arg(argvars, &f) == OK)
11131 rettv->vval.v_float = exp(f);
11132 else
11133 rettv->vval.v_float = 0.0;
11134}
11135#endif
11136
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137/*
11138 * "expand()" function
11139 */
11140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011141f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142{
11143 char_u *s;
11144 int len;
11145 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011146 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011149 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011152 if (argvars[1].v_type != VAR_UNKNOWN
11153 && argvars[2].v_type != VAR_UNKNOWN
11154 && get_tv_number_chk(&argvars[2], &error)
11155 && !error)
11156 {
11157 rettv->v_type = VAR_LIST;
11158 rettv->vval.v_list = NULL;
11159 }
11160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 if (*s == '%' || *s == '#' || *s == '<')
11163 {
11164 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011165 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011167 if (rettv->v_type == VAR_LIST)
11168 {
11169 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11170 list_append_string(rettv->vval.v_list, result, -1);
11171 else
11172 vim_free(result);
11173 }
11174 else
11175 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 }
11177 else
11178 {
11179 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011180 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011181 if (argvars[1].v_type != VAR_UNKNOWN
11182 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011183 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 if (!error)
11185 {
11186 ExpandInit(&xpc);
11187 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011188 if (p_wic)
11189 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011190 if (rettv->v_type == VAR_STRING)
11191 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11192 options, WILD_ALL);
11193 else if (rettv_list_alloc(rettv) != FAIL)
11194 {
11195 int i;
11196
11197 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11198 for (i = 0; i < xpc.xp_numfiles; i++)
11199 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11200 ExpandCleanup(&xpc);
11201 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011202 }
11203 else
11204 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 }
11206}
11207
11208/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011209 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011210 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011211 */
11212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011213f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011214{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011215 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011216
Bram Moolenaare9a41262005-01-15 22:18:47 +000011217 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011218 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011219 list_T *l1, *l2;
11220 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011221 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011223
Bram Moolenaare9a41262005-01-15 22:18:47 +000011224 l1 = argvars[0].vval.v_list;
11225 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011226 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011227 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011228 {
11229 if (argvars[2].v_type != VAR_UNKNOWN)
11230 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011231 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011232 if (error)
11233 return; /* type error; errmsg already given */
11234
Bram Moolenaar758711c2005-02-02 23:11:38 +000011235 if (before == l1->lv_len)
11236 item = NULL;
11237 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011238 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011239 item = list_find(l1, before);
11240 if (item == NULL)
11241 {
11242 EMSGN(_(e_listidx), before);
11243 return;
11244 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011245 }
11246 }
11247 else
11248 item = NULL;
11249 list_extend(l1, l2, item);
11250
Bram Moolenaare9a41262005-01-15 22:18:47 +000011251 copy_tv(&argvars[0], rettv);
11252 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011253 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011254 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11255 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011256 dict_T *d1, *d2;
11257 char_u *action;
11258 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011259
11260 d1 = argvars[0].vval.v_dict;
11261 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011262 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011263 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011264 {
11265 /* Check the third argument. */
11266 if (argvars[2].v_type != VAR_UNKNOWN)
11267 {
11268 static char *(av[]) = {"keep", "force", "error"};
11269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270 action = get_tv_string_chk(&argvars[2]);
11271 if (action == NULL)
11272 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011273 for (i = 0; i < 3; ++i)
11274 if (STRCMP(action, av[i]) == 0)
11275 break;
11276 if (i == 3)
11277 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011278 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011279 return;
11280 }
11281 }
11282 else
11283 action = (char_u *)"force";
11284
Bram Moolenaara9922d62013-05-30 13:01:18 +020011285 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011286
Bram Moolenaare9a41262005-01-15 22:18:47 +000011287 copy_tv(&argvars[0], rettv);
11288 }
11289 }
11290 else
11291 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011292}
11293
11294/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011295 * "feedkeys()" function
11296 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011298f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011299{
11300 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011301 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011302 char_u *keys, *flags;
11303 char_u nbuf[NUMBUFLEN];
11304 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011305 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011306 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011307 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011308
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011309 /* This is not allowed in the sandbox. If the commands would still be
11310 * executed in the sandbox it would be OK, but it probably happens later,
11311 * when "sandbox" is no longer set. */
11312 if (check_secure())
11313 return;
11314
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011315 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011316
11317 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011318 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011319 flags = get_tv_string_buf(&argvars[1], nbuf);
11320 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011321 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011322 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011323 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011324 case 'n': remap = FALSE; break;
11325 case 'm': remap = TRUE; break;
11326 case 't': typed = TRUE; break;
11327 case 'i': insert = TRUE; break;
11328 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011329 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011330 }
11331 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011332 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011333
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011334 if (*keys != NUL || execute)
11335 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011336 /* Need to escape K_SPECIAL and CSI before putting the string in the
11337 * typeahead buffer. */
11338 keys_esc = vim_strsave_escape_csi(keys);
11339 if (keys_esc != NULL)
11340 {
11341 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011342 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011343 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011344 if (vgetc_busy)
11345 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011346 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011347 {
11348 int save_msg_scroll = msg_scroll;
11349
11350 /* Avoid a 1 second delay when the keys start Insert mode. */
11351 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011352
Bram Moolenaar245c4102016-04-20 17:37:41 +020011353 if (!dangerous)
11354 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011355 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011356 if (!dangerous)
11357 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011358 msg_scroll |= save_msg_scroll;
11359 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011360 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011361 }
11362}
11363
11364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 * "filereadable()" function
11366 */
11367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011368f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011370 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 char_u *p;
11372 int n;
11373
Bram Moolenaarc236c162008-07-13 17:41:49 +000011374#ifndef O_NONBLOCK
11375# define O_NONBLOCK 0
11376#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011378 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11379 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 {
11381 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011382 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 }
11384 else
11385 n = FALSE;
11386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388}
11389
11390/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011391 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 * rights to write into.
11393 */
11394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011395f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011397 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398}
11399
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011400 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011401findfilendir(
11402 typval_T *argvars UNUSED,
11403 typval_T *rettv,
11404 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011405{
11406#ifdef FEAT_SEARCHPATH
11407 char_u *fname;
11408 char_u *fresult = NULL;
11409 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11410 char_u *p;
11411 char_u pathbuf[NUMBUFLEN];
11412 int count = 1;
11413 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011414 int error = FALSE;
11415#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011416
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011417 rettv->vval.v_string = NULL;
11418 rettv->v_type = VAR_STRING;
11419
11420#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011422
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011423 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011425 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11426 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011427 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011428 else
11429 {
11430 if (*p != NUL)
11431 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011433 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011434 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011436 }
11437
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011438 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11439 error = TRUE;
11440
11441 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 do
11444 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011445 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011446 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 fresult = find_file_in_path_option(first ? fname : NULL,
11448 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011449 0, first, path,
11450 find_what,
11451 curbuf->b_ffname,
11452 find_what == FINDFILE_DIR
11453 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011454 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011455
11456 if (fresult != NULL && rettv->v_type == VAR_LIST)
11457 list_append_string(rettv->vval.v_list, fresult, -1);
11458
11459 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011460 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011461
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011462 if (rettv->v_type == VAR_STRING)
11463 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011464#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011465}
11466
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011467static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011468static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011469
11470/*
11471 * Implementation of map() and filter().
11472 */
11473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011474filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011475{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011476 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011477 listitem_T *li, *nli;
11478 list_T *l = NULL;
11479 dictitem_T *di;
11480 hashtab_T *ht;
11481 hashitem_T *hi;
11482 dict_T *d = NULL;
11483 typval_T save_val;
11484 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011485 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011486 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011487 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011488 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011489 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011490 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011491 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011492
Bram Moolenaare9a41262005-01-15 22:18:47 +000011493 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011494 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011495 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011496 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011497 return;
11498 }
11499 else if (argvars[0].v_type == VAR_DICT)
11500 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011501 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011502 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011503 return;
11504 }
11505 else
11506 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011507 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011508 return;
11509 }
11510
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011511 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011512 /* On type errors, the preceding call has already displayed an error
11513 * message. Avoid a misleading error message for an empty string that
11514 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011515 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011518
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011519 /* We reset "did_emsg" to be able to detect whether an error
11520 * occurred during evaluation of the expression. */
11521 save_did_emsg = did_emsg;
11522 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011523
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011524 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011525 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011527 vimvars[VV_KEY].vv_type = VAR_STRING;
11528
11529 ht = &d->dv_hashtab;
11530 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011531 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011532 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011534 if (!HASHITEM_EMPTY(hi))
11535 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011536 int r;
11537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011538 --todo;
11539 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011540 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011541 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11542 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011543 break;
11544 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011545 r = filter_map_one(&di->di_tv, expr, map, &rem);
11546 clear_tv(&vimvars[VV_KEY].vv_tv);
11547 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011548 break;
11549 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011550 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011551 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11552 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011553 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011555 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011556 }
11557 }
11558 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 }
11560 else
11561 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011562 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011564 for (li = l->lv_first; li != NULL; li = nli)
11565 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011566 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011567 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011568 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011569 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011570 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011571 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011572 break;
11573 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011574 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011575 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011576 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011577 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011578
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011579 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011580 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011581
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011582 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011583 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011584
11585 copy_tv(&argvars[0], rettv);
11586}
11587
11588 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011589filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011590{
Bram Moolenaar33570922005-01-25 22:26:29 +000011591 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011592 typval_T argv[3];
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020011593 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011594 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011595 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011596 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011597
Bram Moolenaar33570922005-01-25 22:26:29 +000011598 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011599 argv[0] = vimvars[VV_KEY].vv_tv;
11600 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011601 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011602 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020011603 s = expr->vval.v_string;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011604 if (call_func(s, (int)STRLEN(s),
11605 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
11606 goto theend;
11607 }
11608 else if (expr->v_type == VAR_PARTIAL)
11609 {
11610 partial_T *partial = expr->vval.v_partial;
11611
11612 s = partial->pt_name;
11613 if (call_func(s, (int)STRLEN(s),
11614 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
11615 == FAIL)
11616 goto theend;
11617 }
11618 else
11619 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020011620 s = get_tv_string_buf_chk(expr, buf);
11621 if (s == NULL)
11622 goto theend;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011623 s = skipwhite(s);
11624 if (eval1(&s, &rettv, TRUE) == FAIL)
11625 goto theend;
11626 if (*s != NUL) /* check for trailing chars after expr */
11627 {
11628 EMSG2(_(e_invexpr2), s);
11629 goto theend;
11630 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011631 }
11632 if (map)
11633 {
11634 /* map(): replace the list item value */
11635 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011636 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011637 *tv = rettv;
11638 }
11639 else
11640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011641 int error = FALSE;
11642
Bram Moolenaare9a41262005-01-15 22:18:47 +000011643 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011644 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011645 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011646 /* On type error, nothing has been removed; return FAIL to stop the
11647 * loop. The error message was given by get_tv_number_chk(). */
11648 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011649 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011650 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011651 retval = OK;
11652theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011653 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011654 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011655}
11656
11657/*
11658 * "filter()" function
11659 */
11660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011661f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011662{
11663 filter_map(argvars, rettv, FALSE);
11664}
11665
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011666/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667 * "finddir({fname}[, {path}[, {count}]])" function
11668 */
11669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011670f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011671{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011672 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011673}
11674
11675/*
11676 * "findfile({fname}[, {path}[, {count}]])" function
11677 */
11678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011679f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011680{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011681 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011682}
11683
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011684#ifdef FEAT_FLOAT
11685/*
11686 * "float2nr({float})" function
11687 */
11688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011689f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011690{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011691 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011692
11693 if (get_float_arg(argvars, &f) == OK)
11694 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011695# ifdef FEAT_NUM64
11696 if (f < -0x7fffffffffffffff)
11697 rettv->vval.v_number = -0x7fffffffffffffff;
11698 else if (f > 0x7fffffffffffffff)
11699 rettv->vval.v_number = 0x7fffffffffffffff;
11700 else
11701 rettv->vval.v_number = (varnumber_T)f;
11702# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011703 if (f < -0x7fffffff)
11704 rettv->vval.v_number = -0x7fffffff;
11705 else if (f > 0x7fffffff)
11706 rettv->vval.v_number = 0x7fffffff;
11707 else
11708 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011709# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011710 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011711}
11712
11713/*
11714 * "floor({float})" function
11715 */
11716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011717f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011718{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011719 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011720
11721 rettv->v_type = VAR_FLOAT;
11722 if (get_float_arg(argvars, &f) == OK)
11723 rettv->vval.v_float = floor(f);
11724 else
11725 rettv->vval.v_float = 0.0;
11726}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011727
11728/*
11729 * "fmod()" function
11730 */
11731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011732f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011733{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011734 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011735
11736 rettv->v_type = VAR_FLOAT;
11737 if (get_float_arg(argvars, &fx) == OK
11738 && get_float_arg(&argvars[1], &fy) == OK)
11739 rettv->vval.v_float = fmod(fx, fy);
11740 else
11741 rettv->vval.v_float = 0.0;
11742}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011743#endif
11744
Bram Moolenaar0d660222005-01-07 21:51:51 +000011745/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011746 * "fnameescape({string})" function
11747 */
11748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011749f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011750{
11751 rettv->vval.v_string = vim_strsave_fnameescape(
11752 get_tv_string(&argvars[0]), FALSE);
11753 rettv->v_type = VAR_STRING;
11754}
11755
11756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 * "fnamemodify({fname}, {mods})" function
11758 */
11759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011760f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761{
11762 char_u *fname;
11763 char_u *mods;
11764 int usedlen = 0;
11765 int len;
11766 char_u *fbuf = NULL;
11767 char_u buf[NUMBUFLEN];
11768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011769 fname = get_tv_string_chk(&argvars[0]);
11770 mods = get_tv_string_buf_chk(&argvars[1], buf);
11771 if (fname == NULL || mods == NULL)
11772 fname = NULL;
11773 else
11774 {
11775 len = (int)STRLEN(fname);
11776 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 vim_free(fbuf);
11785}
11786
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011787static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788
11789/*
11790 * "foldclosed()" function
11791 */
11792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011793foldclosed_both(
11794 typval_T *argvars UNUSED,
11795 typval_T *rettv,
11796 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797{
11798#ifdef FEAT_FOLDING
11799 linenr_T lnum;
11800 linenr_T first, last;
11801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11804 {
11805 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11806 {
11807 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 return;
11812 }
11813 }
11814#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816}
11817
11818/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011819 * "foldclosed()" function
11820 */
11821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011822f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011823{
11824 foldclosed_both(argvars, rettv, FALSE);
11825}
11826
11827/*
11828 * "foldclosedend()" function
11829 */
11830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011831f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011832{
11833 foldclosed_both(argvars, rettv, TRUE);
11834}
11835
11836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 * "foldlevel()" function
11838 */
11839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011840f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841{
11842#ifdef FEAT_FOLDING
11843 linenr_T lnum;
11844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849}
11850
11851/*
11852 * "foldtext()" function
11853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011855f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856{
11857#ifdef FEAT_FOLDING
11858 linenr_T lnum;
11859 char_u *s;
11860 char_u *r;
11861 int len;
11862 char *txt;
11863#endif
11864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->v_type = VAR_STRING;
11866 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011868 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11869 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11870 <= curbuf->b_ml.ml_line_count
11871 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 {
11873 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011874 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11875 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876 {
11877 if (!linewhite(lnum))
11878 break;
11879 ++lnum;
11880 }
11881
11882 /* Find interesting text in this line. */
11883 s = skipwhite(ml_get(lnum));
11884 /* skip C comment-start */
11885 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011888 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011889 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011890 {
11891 s = skipwhite(ml_get(lnum + 1));
11892 if (*s == '*')
11893 s = skipwhite(s + 1);
11894 }
11895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 txt = _("+-%s%3ld lines: ");
11897 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011898 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 + 20 /* for %3ld */
11900 + STRLEN(s))); /* concatenated */
11901 if (r != NULL)
11902 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011903 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11904 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11905 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906 len = (int)STRLEN(r);
11907 STRCAT(r, s);
11908 /* remove 'foldmarker' and 'commentstring' */
11909 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011910 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 }
11912 }
11913#endif
11914}
11915
11916/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011917 * "foldtextresult(lnum)" function
11918 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011920f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011921{
11922#ifdef FEAT_FOLDING
11923 linenr_T lnum;
11924 char_u *text;
11925 char_u buf[51];
11926 foldinfo_T foldinfo;
11927 int fold_count;
11928#endif
11929
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930 rettv->v_type = VAR_STRING;
11931 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011932#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011934 /* treat illegal types and illegal string values for {lnum} the same */
11935 if (lnum < 0)
11936 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011937 fold_count = foldedCount(curwin, lnum, &foldinfo);
11938 if (fold_count > 0)
11939 {
11940 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11941 &foldinfo, buf);
11942 if (text == buf)
11943 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011944 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011945 }
11946#endif
11947}
11948
11949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 * "foreground()" function
11951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011953f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955#ifdef FEAT_GUI
11956 if (gui.in_use)
11957 gui_mch_set_foreground();
11958#else
11959# ifdef WIN32
11960 win32_set_foreground();
11961# endif
11962#endif
11963}
11964
11965/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011966 * "function()" function
11967 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011969f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011970{
11971 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011972 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011973 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011974 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011975
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011976 if (argvars[0].v_type == VAR_FUNC)
11977 {
11978 /* function(MyFunc, [arg], dict) */
11979 s = argvars[0].vval.v_string;
11980 }
11981 else if (argvars[0].v_type == VAR_PARTIAL
11982 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011983 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011984 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011985 arg_pt = argvars[0].vval.v_partial;
11986 s = arg_pt->pt_name;
11987 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011988 else
11989 {
11990 /* function('MyFunc', [arg], dict) */
11991 s = get_tv_string(&argvars[0]);
11992 use_string = TRUE;
11993 }
11994
11995 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011996 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011997 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011998 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11999 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012000 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012001 else
12002 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012003 int dict_idx = 0;
12004 int arg_idx = 0;
12005 list_T *list = NULL;
12006
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012007 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012008 {
12009 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012010 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012011
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012012 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12013 * also be called from another script. Using trans_function_name()
12014 * would also work, but some plugins depend on the name being
12015 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012016 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012017 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12018 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012019 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012020 STRCPY(name, sid_buf);
12021 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012022 }
12023 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012024 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012025 name = vim_strsave(s);
12026
12027 if (argvars[1].v_type != VAR_UNKNOWN)
12028 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012029 if (argvars[2].v_type != VAR_UNKNOWN)
12030 {
12031 /* function(name, [args], dict) */
12032 arg_idx = 1;
12033 dict_idx = 2;
12034 }
12035 else if (argvars[1].v_type == VAR_DICT)
12036 /* function(name, dict) */
12037 dict_idx = 1;
12038 else
12039 /* function(name, [args]) */
12040 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012041 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012042 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012043 if (argvars[dict_idx].v_type != VAR_DICT)
12044 {
12045 EMSG(_("E922: expected a dict"));
12046 vim_free(name);
12047 return;
12048 }
12049 if (argvars[dict_idx].vval.v_dict == NULL)
12050 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012051 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012052 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012053 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012054 if (argvars[arg_idx].v_type != VAR_LIST)
12055 {
12056 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12057 vim_free(name);
12058 return;
12059 }
12060 list = argvars[arg_idx].vval.v_list;
12061 if (list == NULL || list->lv_len == 0)
12062 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012063 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012064 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012065 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012066 {
12067 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012068
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012069 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012070 if (pt == NULL)
12071 vim_free(name);
12072 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012073 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012074 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012075 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012076 listitem_T *li;
12077 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012078 int arg_len = 0;
12079 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012080
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012081 if (arg_pt != NULL)
12082 arg_len = arg_pt->pt_argc;
12083 if (list != NULL)
12084 lv_len = list->lv_len;
12085 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012086 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012087 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012088 if (pt->pt_argv == NULL)
12089 {
12090 vim_free(pt);
12091 vim_free(name);
12092 return;
12093 }
12094 else
12095 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012096 for (i = 0; i < arg_len; i++)
12097 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12098 if (lv_len > 0)
12099 for (li = list->lv_first; li != NULL;
12100 li = li->li_next)
12101 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012102 }
12103 }
12104
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012105 /* For "function(dict.func, [], dict)" and "func" is a partial
12106 * use "dict". That is backwards compatible. */
12107 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012108 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012109 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012110 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12111 ++pt->pt_dict->dv_refcount;
12112 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012113 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012114 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012115 /* If the dict was bound automatically the result is also
12116 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012117 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012118 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012119 if (pt->pt_dict != NULL)
12120 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012121 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012122
12123 pt->pt_refcount = 1;
12124 pt->pt_name = name;
12125 func_ref(pt->pt_name);
12126 }
12127 rettv->v_type = VAR_PARTIAL;
12128 rettv->vval.v_partial = pt;
12129 }
12130 else
12131 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012132 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012133 rettv->v_type = VAR_FUNC;
12134 rettv->vval.v_string = name;
12135 func_ref(name);
12136 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012137 }
12138}
12139
12140/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012141 * "garbagecollect()" function
12142 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012144f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012145{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012146 /* This is postponed until we are back at the toplevel, because we may be
12147 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12148 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012149
12150 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12151 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012152}
12153
12154/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012155 * "get()" function
12156 */
12157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012158f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012159{
Bram Moolenaar33570922005-01-25 22:26:29 +000012160 listitem_T *li;
12161 list_T *l;
12162 dictitem_T *di;
12163 dict_T *d;
12164 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012165
Bram Moolenaare9a41262005-01-15 22:18:47 +000012166 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012167 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012168 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012170 int error = FALSE;
12171
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012172 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012173 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012174 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012175 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012176 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012177 else if (argvars[0].v_type == VAR_DICT)
12178 {
12179 if ((d = argvars[0].vval.v_dict) != NULL)
12180 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012181 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012182 if (di != NULL)
12183 tv = &di->di_tv;
12184 }
12185 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012186 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012187 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012188 partial_T *pt;
12189 partial_T fref_pt;
12190
12191 if (argvars[0].v_type == VAR_PARTIAL)
12192 pt = argvars[0].vval.v_partial;
12193 else
12194 {
12195 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12196 fref_pt.pt_name = argvars[0].vval.v_string;
12197 pt = &fref_pt;
12198 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012199
12200 if (pt != NULL)
12201 {
12202 char_u *what = get_tv_string(&argvars[1]);
12203
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012204 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012205 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012206 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012207 if (pt->pt_name == NULL)
12208 rettv->vval.v_string = NULL;
12209 else
12210 rettv->vval.v_string = vim_strsave(pt->pt_name);
12211 }
12212 else if (STRCMP(what, "dict") == 0)
12213 {
12214 rettv->v_type = VAR_DICT;
12215 rettv->vval.v_dict = pt->pt_dict;
12216 if (pt->pt_dict != NULL)
12217 ++pt->pt_dict->dv_refcount;
12218 }
12219 else if (STRCMP(what, "args") == 0)
12220 {
12221 rettv->v_type = VAR_LIST;
12222 if (rettv_list_alloc(rettv) == OK)
12223 {
12224 int i;
12225
12226 for (i = 0; i < pt->pt_argc; ++i)
12227 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12228 }
12229 }
12230 else
12231 EMSG2(_(e_invarg2), what);
12232 return;
12233 }
12234 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012235 else
12236 EMSG2(_(e_listdictarg), "get()");
12237
12238 if (tv == NULL)
12239 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012240 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012241 copy_tv(&argvars[2], rettv);
12242 }
12243 else
12244 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012245}
12246
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012247static 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 +000012248
12249/*
12250 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012251 * Return a range (from start to end) of lines in rettv from the specified
12252 * buffer.
12253 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012254 */
12255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012256get_buffer_lines(
12257 buf_T *buf,
12258 linenr_T start,
12259 linenr_T end,
12260 int retlist,
12261 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012262{
12263 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012264
Bram Moolenaar959a1432013-12-14 12:17:38 +010012265 rettv->v_type = VAR_STRING;
12266 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012267 if (retlist && rettv_list_alloc(rettv) == FAIL)
12268 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012269
12270 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12271 return;
12272
12273 if (!retlist)
12274 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012275 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12276 p = ml_get_buf(buf, start, FALSE);
12277 else
12278 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012279 rettv->vval.v_string = vim_strsave(p);
12280 }
12281 else
12282 {
12283 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012284 return;
12285
12286 if (start < 1)
12287 start = 1;
12288 if (end > buf->b_ml.ml_line_count)
12289 end = buf->b_ml.ml_line_count;
12290 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012291 if (list_append_string(rettv->vval.v_list,
12292 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012293 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012294 }
12295}
12296
12297/*
12298 * "getbufline()" function
12299 */
12300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012301f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012302{
12303 linenr_T lnum;
12304 linenr_T end;
12305 buf_T *buf;
12306
12307 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12308 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012309 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012310 --emsg_off;
12311
Bram Moolenaar661b1822005-07-28 22:36:45 +000012312 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012313 if (argvars[2].v_type == VAR_UNKNOWN)
12314 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012315 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012316 end = get_tv_lnum_buf(&argvars[2], buf);
12317
Bram Moolenaar342337a2005-07-21 21:11:17 +000012318 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012319}
12320
Bram Moolenaar0d660222005-01-07 21:51:51 +000012321/*
12322 * "getbufvar()" function
12323 */
12324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012325f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012326{
12327 buf_T *buf;
12328 buf_T *save_curbuf;
12329 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012330 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012331 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012333 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12334 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012335 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012336 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012337
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012338 rettv->v_type = VAR_STRING;
12339 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012340
12341 if (buf != NULL && varname != NULL)
12342 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012343 /* set curbuf to be our buf, temporarily */
12344 save_curbuf = curbuf;
12345 curbuf = buf;
12346
Bram Moolenaar0d660222005-01-07 21:51:51 +000012347 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012348 {
12349 if (get_option_tv(&varname, rettv, TRUE) == OK)
12350 done = TRUE;
12351 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012352 else if (STRCMP(varname, "changedtick") == 0)
12353 {
12354 rettv->v_type = VAR_NUMBER;
12355 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012356 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012357 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012358 else
12359 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012360 /* Look up the variable. */
12361 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12362 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12363 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012364 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012365 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012366 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012367 done = TRUE;
12368 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012369 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012370
12371 /* restore previous notion of curbuf */
12372 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012373 }
12374
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012375 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12376 /* use the default value */
12377 copy_tv(&argvars[2], rettv);
12378
Bram Moolenaar0d660222005-01-07 21:51:51 +000012379 --emsg_off;
12380}
12381
12382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 * "getchar()" function
12384 */
12385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012386f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387{
12388 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012389 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012391 /* Position the cursor. Needed after a message that ends in a space. */
12392 windgoto(msg_row, msg_col);
12393
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394 ++no_mapping;
12395 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012396 for (;;)
12397 {
12398 if (argvars[0].v_type == VAR_UNKNOWN)
12399 /* getchar(): blocking wait. */
12400 n = safe_vgetc();
12401 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12402 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012403 n = vpeekc_any();
12404 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012405 /* illegal argument or getchar(0) and no char avail: return zero */
12406 n = 0;
12407 else
12408 /* getchar(0) and char avail: return char */
12409 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012410
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012411 if (n == K_IGNORE)
12412 continue;
12413 break;
12414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 --no_mapping;
12416 --allow_keys;
12417
Bram Moolenaar219b8702006-11-01 14:32:36 +000012418 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012419 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012420 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12421 vimvars[VV_MOUSE_COL].vv_nr = 0;
12422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 if (IS_SPECIAL(n) || mod_mask != 0)
12425 {
12426 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12427 int i = 0;
12428
12429 /* Turn a special key into three bytes, plus modifier. */
12430 if (mod_mask != 0)
12431 {
12432 temp[i++] = K_SPECIAL;
12433 temp[i++] = KS_MODIFIER;
12434 temp[i++] = mod_mask;
12435 }
12436 if (IS_SPECIAL(n))
12437 {
12438 temp[i++] = K_SPECIAL;
12439 temp[i++] = K_SECOND(n);
12440 temp[i++] = K_THIRD(n);
12441 }
12442#ifdef FEAT_MBYTE
12443 else if (has_mbyte)
12444 i += (*mb_char2bytes)(n, temp + i);
12445#endif
12446 else
12447 temp[i++] = n;
12448 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 rettv->v_type = VAR_STRING;
12450 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012451
12452#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012453 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012454 {
12455 int row = mouse_row;
12456 int col = mouse_col;
12457 win_T *win;
12458 linenr_T lnum;
12459# ifdef FEAT_WINDOWS
12460 win_T *wp;
12461# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012462 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012463
12464 if (row >= 0 && col >= 0)
12465 {
12466 /* Find the window at the mouse coordinates and compute the
12467 * text position. */
12468 win = mouse_find_win(&row, &col);
12469 (void)mouse_comp_pos(win, &row, &col, &lnum);
12470# ifdef FEAT_WINDOWS
12471 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012472 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012473# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012474 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012475 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012476 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12477 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12478 }
12479 }
12480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 }
12482}
12483
12484/*
12485 * "getcharmod()" function
12486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012488f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491}
12492
12493/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012494 * "getcharsearch()" function
12495 */
12496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012497f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012498{
12499 if (rettv_dict_alloc(rettv) != FAIL)
12500 {
12501 dict_T *dict = rettv->vval.v_dict;
12502
12503 dict_add_nr_str(dict, "char", 0L, last_csearch());
12504 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12505 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12506 }
12507}
12508
12509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 * "getcmdline()" function
12511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012513f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012515 rettv->v_type = VAR_STRING;
12516 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517}
12518
12519/*
12520 * "getcmdpos()" function
12521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012523f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012525 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526}
12527
12528/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012529 * "getcmdtype()" function
12530 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012532f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012533{
12534 rettv->v_type = VAR_STRING;
12535 rettv->vval.v_string = alloc(2);
12536 if (rettv->vval.v_string != NULL)
12537 {
12538 rettv->vval.v_string[0] = get_cmdline_type();
12539 rettv->vval.v_string[1] = NUL;
12540 }
12541}
12542
12543/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012544 * "getcmdwintype()" function
12545 */
12546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012547f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012548{
12549 rettv->v_type = VAR_STRING;
12550 rettv->vval.v_string = NULL;
12551#ifdef FEAT_CMDWIN
12552 rettv->vval.v_string = alloc(2);
12553 if (rettv->vval.v_string != NULL)
12554 {
12555 rettv->vval.v_string[0] = cmdwin_type;
12556 rettv->vval.v_string[1] = NUL;
12557 }
12558#endif
12559}
12560
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020012561#if defined(FEAT_CMDL_COMPL)
12562/*
12563 * "getcompletion()" function
12564 */
12565 static void
12566f_getcompletion(typval_T *argvars, typval_T *rettv)
12567{
12568 char_u *pat;
12569 expand_T xpc;
12570 int options = WILD_KEEP_ALL | WILD_SILENT | WILD_USE_NL
12571 | WILD_LIST_NOTFOUND | WILD_NO_BEEP;
12572
12573 if (p_wic)
12574 options |= WILD_ICASE;
12575
12576 ExpandInit(&xpc);
12577 xpc.xp_pattern = get_tv_string(&argvars[0]);
Bram Moolenaar25065ec2016-07-10 19:22:53 +020012578 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020012579 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
12580 if (xpc.xp_context == EXPAND_NOTHING)
12581 {
12582 if (argvars[1].v_type == VAR_STRING)
12583 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
12584 else
12585 EMSG(_(e_invarg));
12586 return;
12587 }
12588
Bram Moolenaar4c068152016-07-11 23:15:25 +020012589# if defined(FEAT_MENU)
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020012590 if (xpc.xp_context == EXPAND_MENUS)
12591 {
12592 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
Bram Moolenaar25065ec2016-07-10 19:22:53 +020012593 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020012594 }
Bram Moolenaar4c068152016-07-11 23:15:25 +020012595# endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020012596
12597 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
12598 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
12599 {
12600 int i;
12601
12602 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
12603
12604 for (i = 0; i < xpc.xp_numfiles; i++)
12605 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12606 }
12607 vim_free(pat);
12608 ExpandCleanup(&xpc);
12609}
12610#endif
12611
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 * "getcwd()" function
12614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012616f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012618 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012619 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012621 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012622 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012623
12624 wp = find_tabwin(&argvars[0], &argvars[1]);
12625 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012627 if (wp->w_localdir != NULL)
12628 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar5c719942016-07-09 23:40:45 +020012629 else if (globaldir != NULL)
Bram Moolenaarc9703302016-01-17 21:49:33 +010012630 rettv->vval.v_string = vim_strsave(globaldir);
12631 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012632 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012633 cwd = alloc(MAXPATHL);
12634 if (cwd != NULL)
12635 {
12636 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12637 rettv->vval.v_string = vim_strsave(cwd);
12638 vim_free(cwd);
12639 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012640 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012641#ifdef BACKSLASH_IN_FILENAME
12642 if (rettv->vval.v_string != NULL)
12643 slash_adjust(rettv->vval.v_string);
12644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645 }
12646}
12647
12648/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012649 * "getfontname()" function
12650 */
12651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012652f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012653{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012654 rettv->v_type = VAR_STRING;
12655 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012656#ifdef FEAT_GUI
12657 if (gui.in_use)
12658 {
12659 GuiFont font;
12660 char_u *name = NULL;
12661
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012662 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012663 {
12664 /* Get the "Normal" font. Either the name saved by
12665 * hl_set_font_name() or from the font ID. */
12666 font = gui.norm_font;
12667 name = hl_get_font_name();
12668 }
12669 else
12670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012672 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12673 return;
12674 font = gui_mch_get_font(name, FALSE);
12675 if (font == NOFONT)
12676 return; /* Invalid font name, return empty string. */
12677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012678 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012679 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012680 gui_mch_free_font(font);
12681 }
12682#endif
12683}
12684
12685/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012686 * "getfperm({fname})" function
12687 */
12688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012689f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012690{
12691 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020012692 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012693 char_u *perm = NULL;
12694 char_u flags[] = "rwx";
12695 int i;
12696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012699 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012700 if (mch_stat((char *)fname, &st) >= 0)
12701 {
12702 perm = vim_strsave((char_u *)"---------");
12703 if (perm != NULL)
12704 {
12705 for (i = 0; i < 9; i++)
12706 {
12707 if (st.st_mode & (1 << (8 - i)))
12708 perm[i] = flags[i % 3];
12709 }
12710 }
12711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012713}
12714
12715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 * "getfsize({fname})" function
12717 */
12718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012719f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720{
12721 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020012722 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012724 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012726 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727
12728 if (mch_stat((char *)fname, &st) >= 0)
12729 {
12730 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012731 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012734 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012735
12736 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020012737 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000012738 rettv->vval.v_number = -2;
12739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 }
12741 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743}
12744
12745/*
12746 * "getftime({fname})" function
12747 */
12748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012749f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750{
12751 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020012752 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755
12756 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760}
12761
12762/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012763 * "getftype({fname})" function
12764 */
12765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012766f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012767{
12768 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020012769 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012770 char_u *type = NULL;
12771 char *t;
12772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012775 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012776 if (mch_lstat((char *)fname, &st) >= 0)
12777 {
12778#ifdef S_ISREG
12779 if (S_ISREG(st.st_mode))
12780 t = "file";
12781 else if (S_ISDIR(st.st_mode))
12782 t = "dir";
12783# ifdef S_ISLNK
12784 else if (S_ISLNK(st.st_mode))
12785 t = "link";
12786# endif
12787# ifdef S_ISBLK
12788 else if (S_ISBLK(st.st_mode))
12789 t = "bdev";
12790# endif
12791# ifdef S_ISCHR
12792 else if (S_ISCHR(st.st_mode))
12793 t = "cdev";
12794# endif
12795# ifdef S_ISFIFO
12796 else if (S_ISFIFO(st.st_mode))
12797 t = "fifo";
12798# endif
12799# ifdef S_ISSOCK
12800 else if (S_ISSOCK(st.st_mode))
12801 t = "fifo";
12802# endif
12803 else
12804 t = "other";
12805#else
12806# ifdef S_IFMT
12807 switch (st.st_mode & S_IFMT)
12808 {
12809 case S_IFREG: t = "file"; break;
12810 case S_IFDIR: t = "dir"; break;
12811# ifdef S_IFLNK
12812 case S_IFLNK: t = "link"; break;
12813# endif
12814# ifdef S_IFBLK
12815 case S_IFBLK: t = "bdev"; break;
12816# endif
12817# ifdef S_IFCHR
12818 case S_IFCHR: t = "cdev"; break;
12819# endif
12820# ifdef S_IFIFO
12821 case S_IFIFO: t = "fifo"; break;
12822# endif
12823# ifdef S_IFSOCK
12824 case S_IFSOCK: t = "socket"; break;
12825# endif
12826 default: t = "other";
12827 }
12828# else
12829 if (mch_isdir(fname))
12830 t = "dir";
12831 else
12832 t = "file";
12833# endif
12834#endif
12835 type = vim_strsave((char_u *)t);
12836 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012838}
12839
12840/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012841 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012842 */
12843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012844f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012845{
12846 linenr_T lnum;
12847 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012848 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012849
12850 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012851 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012852 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012853 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012854 retlist = FALSE;
12855 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012856 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012857 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012858 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012859 retlist = TRUE;
12860 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012861
Bram Moolenaar342337a2005-07-21 21:11:17 +000012862 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012863}
12864
12865/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012866 * "getmatches()" function
12867 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012869f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012870{
12871#ifdef FEAT_SEARCH_EXTRA
12872 dict_T *dict;
12873 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012874 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012875
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012876 if (rettv_list_alloc(rettv) == OK)
12877 {
12878 while (cur != NULL)
12879 {
12880 dict = dict_alloc();
12881 if (dict == NULL)
12882 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012883 if (cur->match.regprog == NULL)
12884 {
12885 /* match added with matchaddpos() */
12886 for (i = 0; i < MAXPOSMATCH; ++i)
12887 {
12888 llpos_T *llpos;
12889 char buf[6];
12890 list_T *l;
12891
12892 llpos = &cur->pos.pos[i];
12893 if (llpos->lnum == 0)
12894 break;
12895 l = list_alloc();
12896 if (l == NULL)
12897 break;
12898 list_append_number(l, (varnumber_T)llpos->lnum);
12899 if (llpos->col > 0)
12900 {
12901 list_append_number(l, (varnumber_T)llpos->col);
12902 list_append_number(l, (varnumber_T)llpos->len);
12903 }
12904 sprintf(buf, "pos%d", i + 1);
12905 dict_add_list(dict, buf, l);
12906 }
12907 }
12908 else
12909 {
12910 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12911 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012912 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012913 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12914 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020012915# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020012916 if (cur->conceal_char)
12917 {
12918 char_u buf[MB_MAXBYTES + 1];
12919
12920 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12921 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12922 }
12923# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012924 list_append_dict(rettv->vval.v_list, dict);
12925 cur = cur->next;
12926 }
12927 }
12928#endif
12929}
12930
12931/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012932 * "getpid()" function
12933 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012935f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012936{
12937 rettv->vval.v_number = mch_get_pid();
12938}
12939
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012941getpos_both(
12942 typval_T *argvars,
12943 typval_T *rettv,
12944 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012945{
Bram Moolenaara5525202006-03-02 22:52:09 +000012946 pos_T *fp;
12947 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012948 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012949
12950 if (rettv_list_alloc(rettv) == OK)
12951 {
12952 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012953 if (getcurpos)
12954 fp = &curwin->w_cursor;
12955 else
12956 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012957 if (fnum != -1)
12958 list_append_number(l, (varnumber_T)fnum);
12959 else
12960 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012961 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12962 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012963 list_append_number(l, (fp != NULL)
12964 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012965 : (varnumber_T)0);
12966 list_append_number(l,
12967#ifdef FEAT_VIRTUALEDIT
12968 (fp != NULL) ? (varnumber_T)fp->coladd :
12969#endif
12970 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012971 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012972 {
12973 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012974 list_append_number(l, curwin->w_curswant == MAXCOL ?
12975 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012976 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012977 }
12978 else
12979 rettv->vval.v_number = FALSE;
12980}
12981
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020012982
12983/*
12984 * "getcurpos()" function
12985 */
12986 static void
12987f_getcurpos(typval_T *argvars, typval_T *rettv)
12988{
12989 getpos_both(argvars, rettv, TRUE);
12990}
12991
12992/*
12993 * "getpos(string)" function
12994 */
12995 static void
12996f_getpos(typval_T *argvars, typval_T *rettv)
12997{
12998 getpos_both(argvars, rettv, FALSE);
12999}
13000
Bram Moolenaara5525202006-03-02 22:52:09 +000013001/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013002 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013003 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013005f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013006{
13007#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013008 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013009#endif
13010
Bram Moolenaar2641f772005-03-25 21:58:17 +000013011#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013012 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013013 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013014 wp = NULL;
13015 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13016 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013017 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013018 if (wp == NULL)
13019 return;
13020 }
13021
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013022 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013023 }
13024#endif
13025}
13026
13027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028 * "getreg()" function
13029 */
13030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013031f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032{
13033 char_u *strregname;
13034 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013035 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013036 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013037 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013039 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013041 strregname = get_tv_string_chk(&argvars[0]);
13042 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013043 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013044 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013045 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013046 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013047 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013048 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013051 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013052
13053 if (error)
13054 return;
13055
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056 regname = (strregname == NULL ? '"' : *strregname);
13057 if (regname == 0)
13058 regname = '"';
13059
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013060 if (return_list)
13061 {
13062 rettv->v_type = VAR_LIST;
13063 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13064 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013065 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013066 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013067 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013068 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013069 }
13070 else
13071 {
13072 rettv->v_type = VAR_STRING;
13073 rettv->vval.v_string = get_reg_contents(regname,
13074 arg2 ? GREG_EXPR_SRC : 0);
13075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076}
13077
13078/*
13079 * "getregtype()" function
13080 */
13081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013082f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083{
13084 char_u *strregname;
13085 int regname;
13086 char_u buf[NUMBUFLEN + 2];
13087 long reglen = 0;
13088
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013089 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013090 {
13091 strregname = get_tv_string_chk(&argvars[0]);
13092 if (strregname == NULL) /* type error; errmsg already given */
13093 {
13094 rettv->v_type = VAR_STRING;
13095 rettv->vval.v_string = NULL;
13096 return;
13097 }
13098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 else
13100 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013101 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102
13103 regname = (strregname == NULL ? '"' : *strregname);
13104 if (regname == 0)
13105 regname = '"';
13106
13107 buf[0] = NUL;
13108 buf[1] = NUL;
13109 switch (get_reg_type(regname, &reglen))
13110 {
13111 case MLINE: buf[0] = 'V'; break;
13112 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 case MBLOCK:
13114 buf[0] = Ctrl_V;
13115 sprintf((char *)buf + 1, "%ld", reglen + 1);
13116 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013118 rettv->v_type = VAR_STRING;
13119 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120}
13121
13122/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013123 * "gettabvar()" function
13124 */
13125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013126f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013127{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013128 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013129 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013130 dictitem_T *v;
13131 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013132 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013133
13134 rettv->v_type = VAR_STRING;
13135 rettv->vval.v_string = NULL;
13136
13137 varname = get_tv_string_chk(&argvars[1]);
13138 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13139 if (tp != NULL && varname != NULL)
13140 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013141 /* Set tp to be our tabpage, temporarily. Also set the window to the
13142 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013143 if (switch_win(&oldcurwin, &oldtabpage,
13144 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013145 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013146 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013147 /* look up the variable */
13148 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13149 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13150 if (v != NULL)
13151 {
13152 copy_tv(&v->di_tv, rettv);
13153 done = TRUE;
13154 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013155 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013156
13157 /* restore previous notion of curwin */
13158 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013159 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013160
13161 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013162 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013163}
13164
13165/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013166 * "gettabwinvar()" function
13167 */
13168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013169f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013170{
13171 getwinvar(argvars, rettv, 1);
13172}
13173
13174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175 * "getwinposx()" function
13176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013178f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013180 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181#ifdef FEAT_GUI
13182 if (gui.in_use)
13183 {
13184 int x, y;
13185
13186 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 }
13189#endif
13190}
13191
13192/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013193 * "win_findbuf()" function
13194 */
13195 static void
13196f_win_findbuf(typval_T *argvars, typval_T *rettv)
13197{
13198 if (rettv_list_alloc(rettv) != FAIL)
13199 win_findbuf(argvars, rettv->vval.v_list);
13200}
13201
13202/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013203 * "win_getid()" function
13204 */
13205 static void
13206f_win_getid(typval_T *argvars, typval_T *rettv)
13207{
13208 rettv->vval.v_number = win_getid(argvars);
13209}
13210
13211/*
13212 * "win_gotoid()" function
13213 */
13214 static void
13215f_win_gotoid(typval_T *argvars, typval_T *rettv)
13216{
13217 rettv->vval.v_number = win_gotoid(argvars);
13218}
13219
13220/*
13221 * "win_id2tabwin()" function
13222 */
13223 static void
13224f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13225{
13226 if (rettv_list_alloc(rettv) != FAIL)
13227 win_id2tabwin(argvars, rettv->vval.v_list);
13228}
13229
13230/*
13231 * "win_id2win()" function
13232 */
13233 static void
13234f_win_id2win(typval_T *argvars, typval_T *rettv)
13235{
13236 rettv->vval.v_number = win_id2win(argvars);
13237}
13238
13239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 * "getwinposy()" function
13241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013243f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246#ifdef FEAT_GUI
13247 if (gui.in_use)
13248 {
13249 int x, y;
13250
13251 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 }
13254#endif
13255}
13256
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013257/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013258 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013259 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013260 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013261find_win_by_nr(
13262 typval_T *vp,
13263 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013264{
13265#ifdef FEAT_WINDOWS
13266 win_T *wp;
13267#endif
13268 int nr;
13269
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013270 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000013271
13272#ifdef FEAT_WINDOWS
13273 if (nr < 0)
13274 return NULL;
13275 if (nr == 0)
13276 return curwin;
13277
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013278 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13279 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013280 if (nr >= LOWEST_WIN_ID)
13281 {
13282 if (wp->w_id == nr)
13283 return wp;
13284 }
13285 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013286 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013287 if (nr >= LOWEST_WIN_ID)
13288 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013289 return wp;
13290#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013291 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013292 return curwin;
13293 return NULL;
13294#endif
13295}
13296
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013298 * Find window specified by "wvp" in tabpage "tvp".
13299 */
13300 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013301find_tabwin(
13302 typval_T *wvp, /* VAR_UNKNOWN for current window */
13303 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013304{
13305 win_T *wp = NULL;
13306 tabpage_T *tp = NULL;
13307 long n;
13308
13309 if (wvp->v_type != VAR_UNKNOWN)
13310 {
13311 if (tvp->v_type != VAR_UNKNOWN)
13312 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013313 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010013314 if (n >= 0)
13315 tp = find_tabpage(n);
13316 }
13317 else
13318 tp = curtab;
13319
13320 if (tp != NULL)
13321 wp = find_win_by_nr(wvp, tp);
13322 }
13323 else
13324 wp = curwin;
13325
13326 return wp;
13327}
13328
13329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 * "getwinvar()" function
13331 */
13332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013333f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013335 getwinvar(argvars, rettv, 0);
13336}
13337
13338/*
13339 * getwinvar() and gettabwinvar()
13340 */
13341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013342getwinvar(
13343 typval_T *argvars,
13344 typval_T *rettv,
13345 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013346{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013347 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013349 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013350 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013351 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013352#ifdef FEAT_WINDOWS
13353 win_T *oldcurwin;
13354 tabpage_T *oldtabpage;
13355 int need_switch_win;
13356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013358#ifdef FEAT_WINDOWS
13359 if (off == 1)
13360 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13361 else
13362 tp = curtab;
13363#endif
13364 win = find_win_by_nr(&argvars[off], tp);
13365 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013366 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013368 rettv->v_type = VAR_STRING;
13369 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370
13371 if (win != NULL && varname != NULL)
13372 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013373#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013374 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013375 * otherwise the window is not valid. Only do this when needed,
13376 * autocommands get blocked. */
13377 need_switch_win = !(tp == curtab && win == curwin);
13378 if (!need_switch_win
13379 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13380#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013381 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013382 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013383 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013384 if (get_option_tv(&varname, rettv, 1) == OK)
13385 done = TRUE;
13386 }
13387 else
13388 {
13389 /* Look up the variable. */
13390 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13391 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13392 varname, FALSE);
13393 if (v != NULL)
13394 {
13395 copy_tv(&v->di_tv, rettv);
13396 done = TRUE;
13397 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013400
Bram Moolenaarba117c22015-09-29 16:53:22 +020013401#ifdef FEAT_WINDOWS
13402 if (need_switch_win)
13403 /* restore previous notion of curwin */
13404 restore_win(oldcurwin, oldtabpage, TRUE);
13405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406 }
13407
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013408 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13409 /* use the default return value */
13410 copy_tv(&argvars[off + 2], rettv);
13411
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 --emsg_off;
13413}
13414
13415/*
13416 * "glob()" function
13417 */
13418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013419f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013421 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013423 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013425 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013426 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013427 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013428 if (argvars[1].v_type != VAR_UNKNOWN)
13429 {
13430 if (get_tv_number_chk(&argvars[1], &error))
13431 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013432 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013433 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013434 if (get_tv_number_chk(&argvars[2], &error))
13435 {
13436 rettv->v_type = VAR_LIST;
13437 rettv->vval.v_list = NULL;
13438 }
13439 if (argvars[3].v_type != VAR_UNKNOWN
13440 && get_tv_number_chk(&argvars[3], &error))
13441 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013442 }
13443 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013444 if (!error)
13445 {
13446 ExpandInit(&xpc);
13447 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013448 if (p_wic)
13449 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013450 if (rettv->v_type == VAR_STRING)
13451 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013452 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013453 else if (rettv_list_alloc(rettv) != FAIL)
13454 {
13455 int i;
13456
13457 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13458 NULL, options, WILD_ALL_KEEP);
13459 for (i = 0; i < xpc.xp_numfiles; i++)
13460 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13461
13462 ExpandCleanup(&xpc);
13463 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013464 }
13465 else
13466 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467}
13468
13469/*
13470 * "globpath()" function
13471 */
13472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013473f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013475 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013478 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013479 garray_T ga;
13480 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013482 /* When the optional second argument is non-zero, don't remove matches
13483 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013484 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013485 if (argvars[2].v_type != VAR_UNKNOWN)
13486 {
13487 if (get_tv_number_chk(&argvars[2], &error))
13488 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013489 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013490 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013491 if (get_tv_number_chk(&argvars[3], &error))
13492 {
13493 rettv->v_type = VAR_LIST;
13494 rettv->vval.v_list = NULL;
13495 }
13496 if (argvars[4].v_type != VAR_UNKNOWN
13497 && get_tv_number_chk(&argvars[4], &error))
13498 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013499 }
13500 }
13501 if (file != NULL && !error)
13502 {
13503 ga_init2(&ga, (int)sizeof(char_u *), 10);
13504 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13505 if (rettv->v_type == VAR_STRING)
13506 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13507 else if (rettv_list_alloc(rettv) != FAIL)
13508 for (i = 0; i < ga.ga_len; ++i)
13509 list_append_string(rettv->vval.v_list,
13510 ((char_u **)(ga.ga_data))[i], -1);
13511 ga_clear_strings(&ga);
13512 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013514 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515}
13516
13517/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013518 * "glob2regpat()" function
13519 */
13520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013521f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013522{
13523 char_u *pat = get_tv_string_chk(&argvars[0]);
13524
13525 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013526 rettv->vval.v_string = (pat == NULL)
13527 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013528}
13529
13530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 * "has()" function
13532 */
13533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013534f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535{
13536 int i;
13537 char_u *name;
13538 int n = FALSE;
13539 static char *(has_list[]) =
13540 {
13541#ifdef AMIGA
13542 "amiga",
13543# ifdef FEAT_ARP
13544 "arp",
13545# endif
13546#endif
13547#ifdef __BEOS__
13548 "beos",
13549#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013550#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 "mac",
13552#endif
13553#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013554 "macunix", /* built with 'darwin' enabled */
13555#endif
13556#if defined(__APPLE__) && __APPLE__ == 1
13557 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559#ifdef __QNX__
13560 "qnx",
13561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562#ifdef UNIX
13563 "unix",
13564#endif
13565#ifdef VMS
13566 "vms",
13567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568#ifdef WIN32
13569 "win32",
13570#endif
13571#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13572 "win32unix",
13573#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013574#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575 "win64",
13576#endif
13577#ifdef EBCDIC
13578 "ebcdic",
13579#endif
13580#ifndef CASE_INSENSITIVE_FILENAME
13581 "fname_case",
13582#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013583#ifdef HAVE_ACL
13584 "acl",
13585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586#ifdef FEAT_ARABIC
13587 "arabic",
13588#endif
13589#ifdef FEAT_AUTOCMD
13590 "autocmd",
13591#endif
13592#ifdef FEAT_BEVAL
13593 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013594# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13595 "balloon_multiline",
13596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597#endif
13598#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13599 "builtin_terms",
13600# ifdef ALL_BUILTIN_TCAPS
13601 "all_builtin_terms",
13602# endif
13603#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013604#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13605 || defined(FEAT_GUI_W32) \
13606 || defined(FEAT_GUI_MOTIF))
13607 "browsefilter",
13608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609#ifdef FEAT_BYTEOFF
13610 "byte_offset",
13611#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013612#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013613 "channel",
13614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615#ifdef FEAT_CINDENT
13616 "cindent",
13617#endif
13618#ifdef FEAT_CLIENTSERVER
13619 "clientserver",
13620#endif
13621#ifdef FEAT_CLIPBOARD
13622 "clipboard",
13623#endif
13624#ifdef FEAT_CMDL_COMPL
13625 "cmdline_compl",
13626#endif
13627#ifdef FEAT_CMDHIST
13628 "cmdline_hist",
13629#endif
13630#ifdef FEAT_COMMENTS
13631 "comments",
13632#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013633#ifdef FEAT_CONCEAL
13634 "conceal",
13635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636#ifdef FEAT_CRYPT
13637 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013638 "crypt-blowfish",
13639 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640#endif
13641#ifdef FEAT_CSCOPE
13642 "cscope",
13643#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013644#ifdef FEAT_CURSORBIND
13645 "cursorbind",
13646#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013647#ifdef CURSOR_SHAPE
13648 "cursorshape",
13649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650#ifdef DEBUG
13651 "debug",
13652#endif
13653#ifdef FEAT_CON_DIALOG
13654 "dialog_con",
13655#endif
13656#ifdef FEAT_GUI_DIALOG
13657 "dialog_gui",
13658#endif
13659#ifdef FEAT_DIFF
13660 "diff",
13661#endif
13662#ifdef FEAT_DIGRAPHS
13663 "digraphs",
13664#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013665#ifdef FEAT_DIRECTX
13666 "directx",
13667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668#ifdef FEAT_DND
13669 "dnd",
13670#endif
13671#ifdef FEAT_EMACS_TAGS
13672 "emacs_tags",
13673#endif
13674 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013675 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676#ifdef FEAT_SEARCH_EXTRA
13677 "extra_search",
13678#endif
13679#ifdef FEAT_FKMAP
13680 "farsi",
13681#endif
13682#ifdef FEAT_SEARCHPATH
13683 "file_in_path",
13684#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013685#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013686 "filterpipe",
13687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688#ifdef FEAT_FIND_ID
13689 "find_in_path",
13690#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013691#ifdef FEAT_FLOAT
13692 "float",
13693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694#ifdef FEAT_FOLDING
13695 "folding",
13696#endif
13697#ifdef FEAT_FOOTER
13698 "footer",
13699#endif
13700#if !defined(USE_SYSTEM) && defined(UNIX)
13701 "fork",
13702#endif
13703#ifdef FEAT_GETTEXT
13704 "gettext",
13705#endif
13706#ifdef FEAT_GUI
13707 "gui",
13708#endif
13709#ifdef FEAT_GUI_ATHENA
13710# ifdef FEAT_GUI_NEXTAW
13711 "gui_neXtaw",
13712# else
13713 "gui_athena",
13714# endif
13715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716#ifdef FEAT_GUI_GTK
13717 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013718# ifdef USE_GTK3
13719 "gui_gtk3",
13720# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013724#ifdef FEAT_GUI_GNOME
13725 "gui_gnome",
13726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727#ifdef FEAT_GUI_MAC
13728 "gui_mac",
13729#endif
13730#ifdef FEAT_GUI_MOTIF
13731 "gui_motif",
13732#endif
13733#ifdef FEAT_GUI_PHOTON
13734 "gui_photon",
13735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736#ifdef FEAT_GUI_W32
13737 "gui_win32",
13738#endif
13739#ifdef FEAT_HANGULIN
13740 "hangul_input",
13741#endif
13742#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13743 "iconv",
13744#endif
13745#ifdef FEAT_INS_EXPAND
13746 "insert_expand",
13747#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013748#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013749 "job",
13750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751#ifdef FEAT_JUMPLIST
13752 "jumplist",
13753#endif
13754#ifdef FEAT_KEYMAP
13755 "keymap",
13756#endif
13757#ifdef FEAT_LANGMAP
13758 "langmap",
13759#endif
13760#ifdef FEAT_LIBCALL
13761 "libcall",
13762#endif
13763#ifdef FEAT_LINEBREAK
13764 "linebreak",
13765#endif
13766#ifdef FEAT_LISP
13767 "lispindent",
13768#endif
13769#ifdef FEAT_LISTCMDS
13770 "listcmds",
13771#endif
13772#ifdef FEAT_LOCALMAP
13773 "localmap",
13774#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013775#ifdef FEAT_LUA
13776# ifndef DYNAMIC_LUA
13777 "lua",
13778# endif
13779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780#ifdef FEAT_MENU
13781 "menu",
13782#endif
13783#ifdef FEAT_SESSION
13784 "mksession",
13785#endif
13786#ifdef FEAT_MODIFY_FNAME
13787 "modify_fname",
13788#endif
13789#ifdef FEAT_MOUSE
13790 "mouse",
13791#endif
13792#ifdef FEAT_MOUSESHAPE
13793 "mouseshape",
13794#endif
13795#if defined(UNIX) || defined(VMS)
13796# ifdef FEAT_MOUSE_DEC
13797 "mouse_dec",
13798# endif
13799# ifdef FEAT_MOUSE_GPM
13800 "mouse_gpm",
13801# endif
13802# ifdef FEAT_MOUSE_JSB
13803 "mouse_jsbterm",
13804# endif
13805# ifdef FEAT_MOUSE_NET
13806 "mouse_netterm",
13807# endif
13808# ifdef FEAT_MOUSE_PTERM
13809 "mouse_pterm",
13810# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013811# ifdef FEAT_MOUSE_SGR
13812 "mouse_sgr",
13813# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013814# ifdef FEAT_SYSMOUSE
13815 "mouse_sysmouse",
13816# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013817# ifdef FEAT_MOUSE_URXVT
13818 "mouse_urxvt",
13819# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820# ifdef FEAT_MOUSE_XTERM
13821 "mouse_xterm",
13822# endif
13823#endif
13824#ifdef FEAT_MBYTE
13825 "multi_byte",
13826#endif
13827#ifdef FEAT_MBYTE_IME
13828 "multi_byte_ime",
13829#endif
13830#ifdef FEAT_MULTI_LANG
13831 "multi_lang",
13832#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013833#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013834#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013835 "mzscheme",
13836#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013837#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013838#ifdef FEAT_NUM64
13839 "num64",
13840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841#ifdef FEAT_OLE
13842 "ole",
13843#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013844 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845#ifdef FEAT_PATH_EXTRA
13846 "path_extra",
13847#endif
13848#ifdef FEAT_PERL
13849#ifndef DYNAMIC_PERL
13850 "perl",
13851#endif
13852#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013853#ifdef FEAT_PERSISTENT_UNDO
13854 "persistent_undo",
13855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856#ifdef FEAT_PYTHON
13857#ifndef DYNAMIC_PYTHON
13858 "python",
13859#endif
13860#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013861#ifdef FEAT_PYTHON3
13862#ifndef DYNAMIC_PYTHON3
13863 "python3",
13864#endif
13865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866#ifdef FEAT_POSTSCRIPT
13867 "postscript",
13868#endif
13869#ifdef FEAT_PRINTER
13870 "printer",
13871#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013872#ifdef FEAT_PROFILE
13873 "profile",
13874#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013875#ifdef FEAT_RELTIME
13876 "reltime",
13877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878#ifdef FEAT_QUICKFIX
13879 "quickfix",
13880#endif
13881#ifdef FEAT_RIGHTLEFT
13882 "rightleft",
13883#endif
13884#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13885 "ruby",
13886#endif
13887#ifdef FEAT_SCROLLBIND
13888 "scrollbind",
13889#endif
13890#ifdef FEAT_CMDL_INFO
13891 "showcmd",
13892 "cmdline_info",
13893#endif
13894#ifdef FEAT_SIGNS
13895 "signs",
13896#endif
13897#ifdef FEAT_SMARTINDENT
13898 "smartindent",
13899#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013900#ifdef STARTUPTIME
13901 "startuptime",
13902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903#ifdef FEAT_STL_OPT
13904 "statusline",
13905#endif
13906#ifdef FEAT_SUN_WORKSHOP
13907 "sun_workshop",
13908#endif
13909#ifdef FEAT_NETBEANS_INTG
13910 "netbeans_intg",
13911#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013912#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013913 "spell",
13914#endif
13915#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 "syntax",
13917#endif
13918#if defined(USE_SYSTEM) || !defined(UNIX)
13919 "system",
13920#endif
13921#ifdef FEAT_TAG_BINS
13922 "tag_binary",
13923#endif
13924#ifdef FEAT_TAG_OLDSTATIC
13925 "tag_old_static",
13926#endif
13927#ifdef FEAT_TAG_ANYWHITE
13928 "tag_any_white",
13929#endif
13930#ifdef FEAT_TCL
13931# ifndef DYNAMIC_TCL
13932 "tcl",
13933# endif
13934#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020013935#ifdef FEAT_TERMGUICOLORS
13936 "termguicolors",
13937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938#ifdef TERMINFO
13939 "terminfo",
13940#endif
13941#ifdef FEAT_TERMRESPONSE
13942 "termresponse",
13943#endif
13944#ifdef FEAT_TEXTOBJ
13945 "textobjects",
13946#endif
13947#ifdef HAVE_TGETENT
13948 "tgetent",
13949#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013950#ifdef FEAT_TIMERS
13951 "timers",
13952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953#ifdef FEAT_TITLE
13954 "title",
13955#endif
13956#ifdef FEAT_TOOLBAR
13957 "toolbar",
13958#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013959#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13960 "unnamedplus",
13961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962#ifdef FEAT_USR_CMDS
13963 "user-commands", /* was accidentally included in 5.4 */
13964 "user_commands",
13965#endif
13966#ifdef FEAT_VIMINFO
13967 "viminfo",
13968#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013969#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970 "vertsplit",
13971#endif
13972#ifdef FEAT_VIRTUALEDIT
13973 "virtualedit",
13974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976#ifdef FEAT_VISUALEXTRA
13977 "visualextra",
13978#endif
13979#ifdef FEAT_VREPLACE
13980 "vreplace",
13981#endif
13982#ifdef FEAT_WILDIGN
13983 "wildignore",
13984#endif
13985#ifdef FEAT_WILDMENU
13986 "wildmenu",
13987#endif
13988#ifdef FEAT_WINDOWS
13989 "windows",
13990#endif
13991#ifdef FEAT_WAK
13992 "winaltkeys",
13993#endif
13994#ifdef FEAT_WRITEBACKUP
13995 "writebackup",
13996#endif
13997#ifdef FEAT_XIM
13998 "xim",
13999#endif
14000#ifdef FEAT_XFONTSET
14001 "xfontset",
14002#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014003#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014004 "xpm",
14005 "xpm_w32", /* for backward compatibility */
14006#else
14007# if defined(HAVE_XPM)
14008 "xpm",
14009# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011#ifdef USE_XSMP
14012 "xsmp",
14013#endif
14014#ifdef USE_XSMP_INTERACT
14015 "xsmp_interact",
14016#endif
14017#ifdef FEAT_XCLIPBOARD
14018 "xterm_clipboard",
14019#endif
14020#ifdef FEAT_XTERM_SAVE
14021 "xterm_save",
14022#endif
14023#if defined(UNIX) && defined(FEAT_X11)
14024 "X11",
14025#endif
14026 NULL
14027 };
14028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014029 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 for (i = 0; has_list[i] != NULL; ++i)
14031 if (STRICMP(name, has_list[i]) == 0)
14032 {
14033 n = TRUE;
14034 break;
14035 }
14036
14037 if (n == FALSE)
14038 {
14039 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014040 {
14041 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014042 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014043 && vim_isdigit(name[6])
14044 && vim_isdigit(name[8])
14045 && vim_isdigit(name[10]))
14046 {
14047 int major = atoi((char *)name + 6);
14048 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014049
14050 /* Expect "patch-9.9.01234". */
14051 n = (major < VIM_VERSION_MAJOR
14052 || (major == VIM_VERSION_MAJOR
14053 && (minor < VIM_VERSION_MINOR
14054 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014055 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014056 }
14057 else
14058 n = has_patch(atoi((char *)name + 5));
14059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060 else if (STRICMP(name, "vim_starting") == 0)
14061 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014062#ifdef FEAT_MBYTE
14063 else if (STRICMP(name, "multi_byte_encoding") == 0)
14064 n = has_mbyte;
14065#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014066#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14067 else if (STRICMP(name, "balloon_multiline") == 0)
14068 n = multiline_balloon_available();
14069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070#ifdef DYNAMIC_TCL
14071 else if (STRICMP(name, "tcl") == 0)
14072 n = tcl_enabled(FALSE);
14073#endif
14074#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14075 else if (STRICMP(name, "iconv") == 0)
14076 n = iconv_enabled(FALSE);
14077#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014078#ifdef DYNAMIC_LUA
14079 else if (STRICMP(name, "lua") == 0)
14080 n = lua_enabled(FALSE);
14081#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014082#ifdef DYNAMIC_MZSCHEME
14083 else if (STRICMP(name, "mzscheme") == 0)
14084 n = mzscheme_enabled(FALSE);
14085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086#ifdef DYNAMIC_RUBY
14087 else if (STRICMP(name, "ruby") == 0)
14088 n = ruby_enabled(FALSE);
14089#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014090#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091#ifdef DYNAMIC_PYTHON
14092 else if (STRICMP(name, "python") == 0)
14093 n = python_enabled(FALSE);
14094#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014095#endif
14096#ifdef FEAT_PYTHON3
14097#ifdef DYNAMIC_PYTHON3
14098 else if (STRICMP(name, "python3") == 0)
14099 n = python3_enabled(FALSE);
14100#endif
14101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102#ifdef DYNAMIC_PERL
14103 else if (STRICMP(name, "perl") == 0)
14104 n = perl_enabled(FALSE);
14105#endif
14106#ifdef FEAT_GUI
14107 else if (STRICMP(name, "gui_running") == 0)
14108 n = (gui.in_use || gui.starting);
14109# ifdef FEAT_GUI_W32
14110 else if (STRICMP(name, "gui_win32s") == 0)
14111 n = gui_is_win32s();
14112# endif
14113# ifdef FEAT_BROWSE
14114 else if (STRICMP(name, "browse") == 0)
14115 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14116# endif
14117#endif
14118#ifdef FEAT_SYN_HL
14119 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014120 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121#endif
14122#if defined(WIN3264)
14123 else if (STRICMP(name, "win95") == 0)
14124 n = mch_windows95();
14125#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014126#ifdef FEAT_NETBEANS_INTG
14127 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014128 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 }
14131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133}
14134
14135/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014136 * "has_key()" function
14137 */
14138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014139f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014140{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014141 if (argvars[0].v_type != VAR_DICT)
14142 {
14143 EMSG(_(e_dictreq));
14144 return;
14145 }
14146 if (argvars[0].vval.v_dict == NULL)
14147 return;
14148
14149 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014150 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014151}
14152
14153/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014154 * "haslocaldir()" function
14155 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014157f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014158{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014159 win_T *wp = NULL;
14160
14161 wp = find_tabwin(&argvars[0], &argvars[1]);
14162 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014163}
14164
14165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166 * "hasmapto()" function
14167 */
14168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014169f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170{
14171 char_u *name;
14172 char_u *mode;
14173 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014174 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014176 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014177 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 mode = (char_u *)"nvo";
14179 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014181 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014182 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014183 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185
Bram Moolenaar2c932302006-03-18 21:42:09 +000014186 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014187 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014189 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190}
14191
14192/*
14193 * "histadd()" function
14194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014196f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197{
14198#ifdef FEAT_CMDHIST
14199 int histype;
14200 char_u *str;
14201 char_u buf[NUMBUFLEN];
14202#endif
14203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014204 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205 if (check_restricted() || check_secure())
14206 return;
14207#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14209 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210 if (histype >= 0)
14211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014212 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213 if (*str != NUL)
14214 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014215 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 return;
14219 }
14220 }
14221#endif
14222}
14223
14224/*
14225 * "histdel()" function
14226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014228f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229{
14230#ifdef FEAT_CMDHIST
14231 int n;
14232 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14236 if (str == NULL)
14237 n = 0;
14238 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014240 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014241 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014244 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 else
14246 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014247 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014248 get_tv_string_buf(&argvars[1], buf));
14249 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250#endif
14251}
14252
14253/*
14254 * "histget()" function
14255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014257f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258{
14259#ifdef FEAT_CMDHIST
14260 int type;
14261 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014262 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014264 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14265 if (str == NULL)
14266 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014268 {
14269 type = get_histtype(str);
14270 if (argvars[1].v_type == VAR_UNKNOWN)
14271 idx = get_history_idx(type);
14272 else
14273 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14274 /* -1 on type error */
14275 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014278 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014280 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281}
14282
14283/*
14284 * "histnr()" function
14285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014287f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288{
14289 int i;
14290
14291#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014292 char_u *history = get_tv_string_chk(&argvars[0]);
14293
14294 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 if (i >= HIST_CMD && i < HIST_COUNT)
14296 i = get_history_idx(i);
14297 else
14298#endif
14299 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014300 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301}
14302
14303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 * "highlightID(name)" function
14305 */
14306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014307f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310}
14311
14312/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014313 * "highlight_exists()" function
14314 */
14315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014316f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014317{
14318 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14319}
14320
14321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 * "hostname()" function
14323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014325f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326{
14327 char_u hostname[256];
14328
14329 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014330 rettv->v_type = VAR_STRING;
14331 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332}
14333
14334/*
14335 * iconv() function
14336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014338f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339{
14340#ifdef FEAT_MBYTE
14341 char_u buf1[NUMBUFLEN];
14342 char_u buf2[NUMBUFLEN];
14343 char_u *from, *to, *str;
14344 vimconv_T vimconv;
14345#endif
14346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014347 rettv->v_type = VAR_STRING;
14348 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349
14350#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351 str = get_tv_string(&argvars[0]);
14352 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14353 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354 vimconv.vc_type = CONV_NONE;
14355 convert_setup(&vimconv, from, to);
14356
14357 /* If the encodings are equal, no conversion needed. */
14358 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014359 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014361 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362
14363 convert_setup(&vimconv, NULL, NULL);
14364 vim_free(from);
14365 vim_free(to);
14366#endif
14367}
14368
14369/*
14370 * "indent()" function
14371 */
14372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014373f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374{
14375 linenr_T lnum;
14376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014377 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014379 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014381 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382}
14383
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014384/*
14385 * "index()" function
14386 */
14387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014388f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014389{
Bram Moolenaar33570922005-01-25 22:26:29 +000014390 list_T *l;
14391 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014392 long idx = 0;
14393 int ic = FALSE;
14394
14395 rettv->vval.v_number = -1;
14396 if (argvars[0].v_type != VAR_LIST)
14397 {
14398 EMSG(_(e_listreq));
14399 return;
14400 }
14401 l = argvars[0].vval.v_list;
14402 if (l != NULL)
14403 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014404 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014405 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014407 int error = FALSE;
14408
Bram Moolenaar758711c2005-02-02 23:11:38 +000014409 /* Start at specified item. Use the cached index that list_find()
14410 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014411 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014412 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014413 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014414 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014415 if (error)
14416 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014417 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014418
Bram Moolenaar758711c2005-02-02 23:11:38 +000014419 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014420 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014421 {
14422 rettv->vval.v_number = idx;
14423 break;
14424 }
14425 }
14426}
14427
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428static int inputsecret_flag = 0;
14429
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014430static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014431
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014433 * This function is used by f_input() and f_inputdialog() functions. The third
14434 * argument to f_input() specifies the type of completion to use at the
14435 * prompt. The third argument to f_inputdialog() specifies the value to return
14436 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 */
14438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014439get_user_input(
14440 typval_T *argvars,
14441 typval_T *rettv,
14442 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014444 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 char_u *p = NULL;
14446 int c;
14447 char_u buf[NUMBUFLEN];
14448 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014450 int xp_type = EXPAND_NOTHING;
14451 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014454 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455
14456#ifdef NO_CONSOLE_INPUT
14457 /* While starting up, there is no place to enter text. */
14458 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460#endif
14461
14462 cmd_silent = FALSE; /* Want to see the prompt. */
14463 if (prompt != NULL)
14464 {
14465 /* Only the part of the message after the last NL is considered as
14466 * prompt for the command line */
14467 p = vim_strrchr(prompt, '\n');
14468 if (p == NULL)
14469 p = prompt;
14470 else
14471 {
14472 ++p;
14473 c = *p;
14474 *p = NUL;
14475 msg_start();
14476 msg_clr_eos();
14477 msg_puts_attr(prompt, echo_attr);
14478 msg_didout = FALSE;
14479 msg_starthere();
14480 *p = c;
14481 }
14482 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014484 if (argvars[1].v_type != VAR_UNKNOWN)
14485 {
14486 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14487 if (defstr != NULL)
14488 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014490 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014491 {
14492 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014493 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014494 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014495
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014496 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014497 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014498
Bram Moolenaar4463f292005-09-25 22:20:24 +000014499 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14500 if (xp_name == NULL)
14501 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014502
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014503 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014504
Bram Moolenaar4463f292005-09-25 22:20:24 +000014505 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14506 &xp_arg) == FAIL)
14507 return;
14508 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014509 }
14510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014511 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014512 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014513 int save_ex_normal_busy = ex_normal_busy;
14514 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014515 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014516 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14517 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014518 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014519 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014520 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014521 && argvars[1].v_type != VAR_UNKNOWN
14522 && argvars[2].v_type != VAR_UNKNOWN)
14523 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14524 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014525
14526 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014528 /* since the user typed this, no need to wait for return */
14529 need_wait_return = FALSE;
14530 msg_didout = FALSE;
14531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 cmd_silent = cmd_silent_save;
14533}
14534
14535/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014536 * "input()" function
14537 * Also handles inputsecret() when inputsecret is set.
14538 */
14539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014540f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014541{
14542 get_user_input(argvars, rettv, FALSE);
14543}
14544
14545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 * "inputdialog()" function
14547 */
14548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014549f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550{
14551#if defined(FEAT_GUI_TEXTDIALOG)
14552 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14553 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14554 {
14555 char_u *message;
14556 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014557 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014559 message = get_tv_string_chk(&argvars[0]);
14560 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014561 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014562 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563 else
14564 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014565 if (message != NULL && defstr != NULL
14566 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014567 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014568 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 else
14570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014571 if (message != NULL && defstr != NULL
14572 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014573 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014574 rettv->vval.v_string = vim_strsave(
14575 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014577 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014579 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 }
14581 else
14582#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014583 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584}
14585
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014586/*
14587 * "inputlist()" function
14588 */
14589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014590f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014591{
14592 listitem_T *li;
14593 int selected;
14594 int mouse_used;
14595
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014596#ifdef NO_CONSOLE_INPUT
14597 /* While starting up, there is no place to enter text. */
14598 if (no_console_input())
14599 return;
14600#endif
14601 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14602 {
14603 EMSG2(_(e_listarg), "inputlist()");
14604 return;
14605 }
14606
14607 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014608 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014609 lines_left = Rows; /* avoid more prompt */
14610 msg_scroll = TRUE;
14611 msg_clr_eos();
14612
14613 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14614 {
14615 msg_puts(get_tv_string(&li->li_tv));
14616 msg_putchar('\n');
14617 }
14618
14619 /* Ask for choice. */
14620 selected = prompt_for_number(&mouse_used);
14621 if (mouse_used)
14622 selected -= lines_left;
14623
14624 rettv->vval.v_number = selected;
14625}
14626
14627
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14629
14630/*
14631 * "inputrestore()" function
14632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014634f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635{
14636 if (ga_userinput.ga_len > 0)
14637 {
14638 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14640 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014641 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 }
14643 else if (p_verbose > 1)
14644 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014645 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014646 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647 }
14648}
14649
14650/*
14651 * "inputsave()" function
14652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014654f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014656 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657 if (ga_grow(&ga_userinput, 1) == OK)
14658 {
14659 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14660 + ga_userinput.ga_len);
14661 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014662 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663 }
14664 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014665 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666}
14667
14668/*
14669 * "inputsecret()" function
14670 */
14671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014672f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673{
14674 ++cmdline_star;
14675 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 --cmdline_star;
14678 --inputsecret_flag;
14679}
14680
14681/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014682 * "insert()" function
14683 */
14684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014685f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014686{
14687 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014688 listitem_T *item;
14689 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014690 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014691
14692 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014693 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014694 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014695 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014696 {
14697 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014698 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014699 if (error)
14700 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014701
Bram Moolenaar758711c2005-02-02 23:11:38 +000014702 if (before == l->lv_len)
14703 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014704 else
14705 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014706 item = list_find(l, before);
14707 if (item == NULL)
14708 {
14709 EMSGN(_(e_listidx), before);
14710 l = NULL;
14711 }
14712 }
14713 if (l != NULL)
14714 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014715 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014716 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014717 }
14718 }
14719}
14720
14721/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014722 * "invert(expr)" function
14723 */
14724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014725f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014726{
14727 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14728}
14729
14730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731 * "isdirectory()" function
14732 */
14733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014734f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014736 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737}
14738
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014739/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014740 * "islocked()" function
14741 */
14742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014743f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014744{
14745 lval_T lv;
14746 char_u *end;
14747 dictitem_T *di;
14748
14749 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014750 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14751 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014752 if (end != NULL && lv.ll_name != NULL)
14753 {
14754 if (*end != NUL)
14755 EMSG(_(e_trailing));
14756 else
14757 {
14758 if (lv.ll_tv == NULL)
14759 {
14760 if (check_changedtick(lv.ll_name))
14761 rettv->vval.v_number = 1; /* always locked */
14762 else
14763 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014764 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014765 if (di != NULL)
14766 {
14767 /* Consider a variable locked when:
14768 * 1. the variable itself is locked
14769 * 2. the value of the variable is locked.
14770 * 3. the List or Dict value is locked.
14771 */
14772 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14773 || tv_islocked(&di->di_tv));
14774 }
14775 }
14776 }
14777 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014778 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014779 else if (lv.ll_newkey != NULL)
14780 EMSG2(_(e_dictkey), lv.ll_newkey);
14781 else if (lv.ll_list != NULL)
14782 /* List item. */
14783 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14784 else
14785 /* Dictionary item. */
14786 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14787 }
14788 }
14789
14790 clear_lval(&lv);
14791}
14792
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014793#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14794/*
14795 * "isnan()" function
14796 */
14797 static void
14798f_isnan(typval_T *argvars, typval_T *rettv)
14799{
14800 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14801 && isnan(argvars[0].vval.v_float);
14802}
14803#endif
14804
Bram Moolenaar8c711452005-01-14 21:53:12 +000014805/*
14806 * "items(dict)" function
14807 */
14808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014809f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014810{
14811 dict_list(argvars, rettv, 2);
14812}
14813
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014814#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014815/*
14816 * Get the job from the argument.
14817 * Returns NULL if the job is invalid.
14818 */
14819 static job_T *
14820get_job_arg(typval_T *tv)
14821{
14822 job_T *job;
14823
14824 if (tv->v_type != VAR_JOB)
14825 {
14826 EMSG2(_(e_invarg2), get_tv_string(tv));
14827 return NULL;
14828 }
14829 job = tv->vval.v_job;
14830
14831 if (job == NULL)
14832 EMSG(_("E916: not a valid job"));
14833 return job;
14834}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014835
Bram Moolenaar835dc632016-02-07 14:27:38 +010014836/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014837 * "job_getchannel()" function
14838 */
14839 static void
14840f_job_getchannel(typval_T *argvars, typval_T *rettv)
14841{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014842 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014843
Bram Moolenaar65edff82016-02-21 16:40:11 +010014844 if (job != NULL)
14845 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014846 rettv->v_type = VAR_CHANNEL;
14847 rettv->vval.v_channel = job->jv_channel;
14848 if (job->jv_channel != NULL)
14849 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014850 }
14851}
14852
14853/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014854 * "job_info()" function
14855 */
14856 static void
14857f_job_info(typval_T *argvars, typval_T *rettv)
14858{
14859 job_T *job = get_job_arg(&argvars[0]);
14860
14861 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14862 job_info(job, rettv->vval.v_dict);
14863}
14864
14865/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014866 * "job_setoptions()" function
14867 */
14868 static void
14869f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14870{
14871 job_T *job = get_job_arg(&argvars[0]);
14872 jobopt_T opt;
14873
14874 if (job == NULL)
14875 return;
14876 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020014877 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
14878 job_set_options(job, &opt);
14879 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010014880}
14881
14882/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014883 * "job_start()" function
14884 */
14885 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014886f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014887{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014888 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020014889 if (check_restricted() || check_secure())
14890 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014891 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014892}
14893
14894/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014895 * "job_status()" function
14896 */
14897 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014898f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014899{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014900 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014901
Bram Moolenaar65edff82016-02-21 16:40:11 +010014902 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014903 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014904 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014905 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014906 }
14907}
14908
14909/*
14910 * "job_stop()" function
14911 */
14912 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014913f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014914{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014915 job_T *job = get_job_arg(&argvars[0]);
14916
14917 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014918 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014919}
14920#endif
14921
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014923 * "join()" function
14924 */
14925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014926f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014927{
14928 garray_T ga;
14929 char_u *sep;
14930
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014931 if (argvars[0].v_type != VAR_LIST)
14932 {
14933 EMSG(_(e_listreq));
14934 return;
14935 }
14936 if (argvars[0].vval.v_list == NULL)
14937 return;
14938 if (argvars[1].v_type == VAR_UNKNOWN)
14939 sep = (char_u *)" ";
14940 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014941 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014942
14943 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014944
14945 if (sep != NULL)
14946 {
14947 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020014948 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014949 ga_append(&ga, NUL);
14950 rettv->vval.v_string = (char_u *)ga.ga_data;
14951 }
14952 else
14953 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014954}
14955
14956/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014957 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014958 */
14959 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014960f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014961{
14962 js_read_T reader;
14963
14964 reader.js_buf = get_tv_string(&argvars[0]);
14965 reader.js_fill = NULL;
14966 reader.js_used = 0;
14967 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14968 EMSG(_(e_invarg));
14969}
14970
14971/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014972 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014973 */
14974 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014975f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014976{
14977 rettv->v_type = VAR_STRING;
14978 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14979}
14980
14981/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014982 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014983 */
14984 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014985f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014986{
14987 js_read_T reader;
14988
14989 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014990 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014991 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014992 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014993 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014994}
14995
14996/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014997 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014998 */
14999 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015000f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015001{
15002 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015003 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015004}
15005
15006/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015007 * "keys()" function
15008 */
15009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015010f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015011{
15012 dict_list(argvars, rettv, 0);
15013}
15014
15015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 * "last_buffer_nr()" function.
15017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015019f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020{
15021 int n = 0;
15022 buf_T *buf;
15023
15024 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15025 if (n < buf->b_fnum)
15026 n = buf->b_fnum;
15027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015029}
15030
15031/*
15032 * "len()" function
15033 */
15034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015035f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015036{
15037 switch (argvars[0].v_type)
15038 {
15039 case VAR_STRING:
15040 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015041 rettv->vval.v_number = (varnumber_T)STRLEN(
15042 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015043 break;
15044 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015045 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015046 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015047 case VAR_DICT:
15048 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15049 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015050 case VAR_UNKNOWN:
15051 case VAR_SPECIAL:
15052 case VAR_FLOAT:
15053 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015054 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015055 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015056 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015057 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015058 break;
15059 }
15060}
15061
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015062static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015063
15064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015065libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015066{
15067#ifdef FEAT_LIBCALL
15068 char_u *string_in;
15069 char_u **string_result;
15070 int nr_result;
15071#endif
15072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015073 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015074 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015075 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015076
15077 if (check_restricted() || check_secure())
15078 return;
15079
15080#ifdef FEAT_LIBCALL
15081 /* The first two args must be strings, otherwise its meaningless */
15082 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15083 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015084 string_in = NULL;
15085 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015086 string_in = argvars[2].vval.v_string;
15087 if (type == VAR_NUMBER)
15088 string_result = NULL;
15089 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015090 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015091 if (mch_libcall(argvars[0].vval.v_string,
15092 argvars[1].vval.v_string,
15093 string_in,
15094 argvars[2].vval.v_number,
15095 string_result,
15096 &nr_result) == OK
15097 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015098 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015099 }
15100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101}
15102
15103/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104 * "libcall()" function
15105 */
15106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015107f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015108{
15109 libcall_common(argvars, rettv, VAR_STRING);
15110}
15111
15112/*
15113 * "libcallnr()" function
15114 */
15115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015116f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015117{
15118 libcall_common(argvars, rettv, VAR_NUMBER);
15119}
15120
15121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122 * "line(string)" function
15123 */
15124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015125f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126{
15127 linenr_T lnum = 0;
15128 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015129 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015131 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132 if (fp != NULL)
15133 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015134 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135}
15136
15137/*
15138 * "line2byte(lnum)" function
15139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015141f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142{
15143#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015144 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145#else
15146 linenr_T lnum;
15147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015148 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015150 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015152 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15153 if (rettv->vval.v_number >= 0)
15154 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155#endif
15156}
15157
15158/*
15159 * "lispindent(lnum)" function
15160 */
15161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015162f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163{
15164#ifdef FEAT_LISP
15165 pos_T pos;
15166 linenr_T lnum;
15167
15168 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015169 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15171 {
15172 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015173 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174 curwin->w_cursor = pos;
15175 }
15176 else
15177#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015178 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179}
15180
15181/*
15182 * "localtime()" function
15183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015185f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015187 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015188}
15189
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015190static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191
15192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015193get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194{
15195 char_u *keys;
15196 char_u *which;
15197 char_u buf[NUMBUFLEN];
15198 char_u *keys_buf = NULL;
15199 char_u *rhs;
15200 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015201 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015202 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015203 mapblock_T *mp;
15204 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205
15206 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015207 rettv->v_type = VAR_STRING;
15208 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015210 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 if (*keys == NUL)
15212 return;
15213
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015214 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015217 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015218 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015219 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015220 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015221 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015222 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015224 else
15225 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015226 if (which == NULL)
15227 return;
15228
Bram Moolenaar071d4272004-06-13 20:20:40 +000015229 mode = get_map_mode(&which, 0);
15230
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015231 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015232 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015234
15235 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015237 /* Return a string. */
15238 if (rhs != NULL)
15239 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240
Bram Moolenaarbd743252010-10-20 21:23:33 +020015241 }
15242 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15243 {
15244 /* Return a dictionary. */
15245 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15246 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15247 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248
Bram Moolenaarbd743252010-10-20 21:23:33 +020015249 dict_add_nr_str(dict, "lhs", 0L, lhs);
15250 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15251 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15252 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15253 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15254 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15255 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015256 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015257 dict_add_nr_str(dict, "mode", 0L, mapmode);
15258
15259 vim_free(lhs);
15260 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261 }
15262}
15263
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015264#ifdef FEAT_FLOAT
15265/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015266 * "log()" function
15267 */
15268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015269f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015270{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015271 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015272
15273 rettv->v_type = VAR_FLOAT;
15274 if (get_float_arg(argvars, &f) == OK)
15275 rettv->vval.v_float = log(f);
15276 else
15277 rettv->vval.v_float = 0.0;
15278}
15279
15280/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015281 * "log10()" function
15282 */
15283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015284f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015285{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015286 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015287
15288 rettv->v_type = VAR_FLOAT;
15289 if (get_float_arg(argvars, &f) == OK)
15290 rettv->vval.v_float = log10(f);
15291 else
15292 rettv->vval.v_float = 0.0;
15293}
15294#endif
15295
Bram Moolenaar1dced572012-04-05 16:54:08 +020015296#ifdef FEAT_LUA
15297/*
15298 * "luaeval()" function
15299 */
15300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015301f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015302{
15303 char_u *str;
15304 char_u buf[NUMBUFLEN];
15305
15306 str = get_tv_string_buf(&argvars[0], buf);
15307 do_luaeval(str, argvars + 1, rettv);
15308}
15309#endif
15310
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015312 * "map()" function
15313 */
15314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015315f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015316{
15317 filter_map(argvars, rettv, TRUE);
15318}
15319
15320/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015321 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 */
15323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015324f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015326 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327}
15328
15329/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015330 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331 */
15332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015333f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015335 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336}
15337
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015338static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339
15340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015341find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015343 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015344 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015345 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 char_u *pat;
15347 regmatch_T regmatch;
15348 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015349 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 char_u *save_cpo;
15351 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015352 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015353 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015354 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015355 list_T *l = NULL;
15356 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015357 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015358 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359
15360 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15361 save_cpo = p_cpo;
15362 p_cpo = (char_u *)"";
15363
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015364 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015365 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015366 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015367 /* type 3: return empty list when there are no matches.
15368 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015369 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015370 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015371 if (type == 4
15372 && (list_append_string(rettv->vval.v_list,
15373 (char_u *)"", 0) == FAIL
15374 || list_append_number(rettv->vval.v_list,
15375 (varnumber_T)-1) == FAIL
15376 || list_append_number(rettv->vval.v_list,
15377 (varnumber_T)-1) == FAIL
15378 || list_append_number(rettv->vval.v_list,
15379 (varnumber_T)-1) == FAIL))
15380 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015381 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015382 rettv->vval.v_list = NULL;
15383 goto theend;
15384 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015385 }
15386 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388 rettv->v_type = VAR_STRING;
15389 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015392 if (argvars[0].v_type == VAR_LIST)
15393 {
15394 if ((l = argvars[0].vval.v_list) == NULL)
15395 goto theend;
15396 li = l->lv_first;
15397 }
15398 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015399 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015400 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015401 len = (long)STRLEN(str);
15402 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015403
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015404 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15405 if (pat == NULL)
15406 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015407
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015408 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015410 int error = FALSE;
15411
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015412 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015413 if (error)
15414 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015415 if (l != NULL)
15416 {
15417 li = list_find(l, start);
15418 if (li == NULL)
15419 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015420 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015421 }
15422 else
15423 {
15424 if (start < 0)
15425 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015426 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015427 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015428 /* When "count" argument is there ignore matches before "start",
15429 * otherwise skip part of the string. Differs when pattern is "^"
15430 * or "\<". */
15431 if (argvars[3].v_type != VAR_UNKNOWN)
15432 startcol = start;
15433 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015434 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015435 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015436 len -= start;
15437 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015438 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015439
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015440 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015441 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015442 if (error)
15443 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 }
15445
15446 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15447 if (regmatch.regprog != NULL)
15448 {
15449 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015450
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015451 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015452 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015453 if (l != NULL)
15454 {
15455 if (li == NULL)
15456 {
15457 match = FALSE;
15458 break;
15459 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015460 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015461 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015462 if (str == NULL)
15463 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015464 }
15465
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015466 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015467
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015468 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015469 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015470 if (l == NULL && !match)
15471 break;
15472
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015473 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015474 if (l != NULL)
15475 {
15476 li = li->li_next;
15477 ++idx;
15478 }
15479 else
15480 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015481#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015482 startcol = (colnr_T)(regmatch.startp[0]
15483 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015484#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015485 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015486#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015487 if (startcol > (colnr_T)len
15488 || str + startcol <= regmatch.startp[0])
15489 {
15490 match = FALSE;
15491 break;
15492 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015493 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015494 }
15495
15496 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015498 if (type == 4)
15499 {
15500 listitem_T *li1 = rettv->vval.v_list->lv_first;
15501 listitem_T *li2 = li1->li_next;
15502 listitem_T *li3 = li2->li_next;
15503 listitem_T *li4 = li3->li_next;
15504
Bram Moolenaar3c809342016-06-01 22:34:48 +020015505 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015506 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15507 (int)(regmatch.endp[0] - regmatch.startp[0]));
15508 li3->li_tv.vval.v_number =
15509 (varnumber_T)(regmatch.startp[0] - expr);
15510 li4->li_tv.vval.v_number =
15511 (varnumber_T)(regmatch.endp[0] - expr);
15512 if (l != NULL)
15513 li2->li_tv.vval.v_number = (varnumber_T)idx;
15514 }
15515 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015516 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015517 int i;
15518
15519 /* return list with matched string and submatches */
15520 for (i = 0; i < NSUBEXP; ++i)
15521 {
15522 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015523 {
15524 if (list_append_string(rettv->vval.v_list,
15525 (char_u *)"", 0) == FAIL)
15526 break;
15527 }
15528 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015529 regmatch.startp[i],
15530 (int)(regmatch.endp[i] - regmatch.startp[i]))
15531 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015532 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015533 }
15534 }
15535 else if (type == 2)
15536 {
15537 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015538 if (l != NULL)
15539 copy_tv(&li->li_tv, rettv);
15540 else
15541 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015543 }
15544 else if (l != NULL)
15545 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 else
15547 {
15548 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 (varnumber_T)(regmatch.startp[0] - str);
15551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015552 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015554 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 }
15556 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015557 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 }
15559
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015560 if (type == 4 && l == NULL)
15561 /* matchstrpos() without a list: drop the second item. */
15562 listitem_remove(rettv->vval.v_list,
15563 rettv->vval.v_list->lv_first->li_next);
15564
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015566 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 p_cpo = save_cpo;
15568}
15569
15570/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015571 * "match()" function
15572 */
15573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015574f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015575{
15576 find_some_match(argvars, rettv, 1);
15577}
15578
15579/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015580 * "matchadd()" function
15581 */
15582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015583f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015584{
15585#ifdef FEAT_SEARCH_EXTRA
15586 char_u buf[NUMBUFLEN];
15587 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15588 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15589 int prio = 10; /* default priority */
15590 int id = -1;
15591 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015592 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015593
15594 rettv->vval.v_number = -1;
15595
15596 if (grp == NULL || pat == NULL)
15597 return;
15598 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015599 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015600 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015601 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015602 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015603 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015604 if (argvars[4].v_type != VAR_UNKNOWN)
15605 {
15606 if (argvars[4].v_type != VAR_DICT)
15607 {
15608 EMSG(_(e_dictreq));
15609 return;
15610 }
15611 if (dict_find(argvars[4].vval.v_dict,
15612 (char_u *)"conceal", -1) != NULL)
15613 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15614 (char_u *)"conceal", FALSE);
15615 }
15616 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015617 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015618 if (error == TRUE)
15619 return;
15620 if (id >= 1 && id <= 3)
15621 {
15622 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15623 return;
15624 }
15625
Bram Moolenaar6561d522015-07-21 15:48:27 +020015626 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15627 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015628#endif
15629}
15630
15631/*
15632 * "matchaddpos()" function
15633 */
15634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015635f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015636{
15637#ifdef FEAT_SEARCH_EXTRA
15638 char_u buf[NUMBUFLEN];
15639 char_u *group;
15640 int prio = 10;
15641 int id = -1;
15642 int error = FALSE;
15643 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015644 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015645
15646 rettv->vval.v_number = -1;
15647
15648 group = get_tv_string_buf_chk(&argvars[0], buf);
15649 if (group == NULL)
15650 return;
15651
15652 if (argvars[1].v_type != VAR_LIST)
15653 {
15654 EMSG2(_(e_listarg), "matchaddpos()");
15655 return;
15656 }
15657 l = argvars[1].vval.v_list;
15658 if (l == NULL)
15659 return;
15660
15661 if (argvars[2].v_type != VAR_UNKNOWN)
15662 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015663 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015664 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015665 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015666 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015667 if (argvars[4].v_type != VAR_UNKNOWN)
15668 {
15669 if (argvars[4].v_type != VAR_DICT)
15670 {
15671 EMSG(_(e_dictreq));
15672 return;
15673 }
15674 if (dict_find(argvars[4].vval.v_dict,
15675 (char_u *)"conceal", -1) != NULL)
15676 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15677 (char_u *)"conceal", FALSE);
15678 }
15679 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015680 }
15681 if (error == TRUE)
15682 return;
15683
15684 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15685 if (id == 1 || id == 2)
15686 {
15687 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15688 return;
15689 }
15690
Bram Moolenaar6561d522015-07-21 15:48:27 +020015691 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15692 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015693#endif
15694}
15695
15696/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015697 * "matcharg()" function
15698 */
15699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015700f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015701{
15702 if (rettv_list_alloc(rettv) == OK)
15703 {
15704#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015705 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015706 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015707
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015708 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015709 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015710 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15711 {
15712 list_append_string(rettv->vval.v_list,
15713 syn_id2name(m->hlg_id), -1);
15714 list_append_string(rettv->vval.v_list, m->pattern, -1);
15715 }
15716 else
15717 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015718 list_append_string(rettv->vval.v_list, NULL, -1);
15719 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015720 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015721 }
15722#endif
15723 }
15724}
15725
15726/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015727 * "matchdelete()" function
15728 */
15729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015730f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015731{
15732#ifdef FEAT_SEARCH_EXTRA
15733 rettv->vval.v_number = match_delete(curwin,
15734 (int)get_tv_number(&argvars[0]), TRUE);
15735#endif
15736}
15737
15738/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739 * "matchend()" function
15740 */
15741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015742f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743{
15744 find_some_match(argvars, rettv, 0);
15745}
15746
15747/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015748 * "matchlist()" function
15749 */
15750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015751f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015752{
15753 find_some_match(argvars, rettv, 3);
15754}
15755
15756/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015757 * "matchstr()" function
15758 */
15759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015760f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015761{
15762 find_some_match(argvars, rettv, 2);
15763}
15764
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015765/*
15766 * "matchstrpos()" function
15767 */
15768 static void
15769f_matchstrpos(typval_T *argvars, typval_T *rettv)
15770{
15771 find_some_match(argvars, rettv, 4);
15772}
15773
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015774static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015775
15776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015777max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015778{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015779 varnumber_T n = 0;
15780 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015781 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015782
15783 if (argvars[0].v_type == VAR_LIST)
15784 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015785 list_T *l;
15786 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015787
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015788 l = argvars[0].vval.v_list;
15789 if (l != NULL)
15790 {
15791 li = l->lv_first;
15792 if (li != NULL)
15793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015794 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015795 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015796 {
15797 li = li->li_next;
15798 if (li == NULL)
15799 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015800 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015801 if (domax ? i > n : i < n)
15802 n = i;
15803 }
15804 }
15805 }
15806 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015807 else if (argvars[0].v_type == VAR_DICT)
15808 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015809 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015810 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015811 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015812 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015813
15814 d = argvars[0].vval.v_dict;
15815 if (d != NULL)
15816 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015817 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015818 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015819 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015820 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015821 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015822 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015823 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015824 if (first)
15825 {
15826 n = i;
15827 first = FALSE;
15828 }
15829 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015830 n = i;
15831 }
15832 }
15833 }
15834 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015835 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015836 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015837 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015838}
15839
15840/*
15841 * "max()" function
15842 */
15843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015844f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015845{
15846 max_min(argvars, rettv, TRUE);
15847}
15848
15849/*
15850 * "min()" function
15851 */
15852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015853f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015854{
15855 max_min(argvars, rettv, FALSE);
15856}
15857
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015858static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015859
15860/*
15861 * Create the directory in which "dir" is located, and higher levels when
15862 * needed.
15863 */
15864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015865mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015866{
15867 char_u *p;
15868 char_u *updir;
15869 int r = FAIL;
15870
15871 /* Get end of directory name in "dir".
15872 * We're done when it's "/" or "c:/". */
15873 p = gettail_sep(dir);
15874 if (p <= get_past_head(dir))
15875 return OK;
15876
15877 /* If the directory exists we're done. Otherwise: create it.*/
15878 updir = vim_strnsave(dir, (int)(p - dir));
15879 if (updir == NULL)
15880 return FAIL;
15881 if (mch_isdir(updir))
15882 r = OK;
15883 else if (mkdir_recurse(updir, prot) == OK)
15884 r = vim_mkdir_emsg(updir, prot);
15885 vim_free(updir);
15886 return r;
15887}
15888
15889#ifdef vim_mkdir
15890/*
15891 * "mkdir()" function
15892 */
15893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015894f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015895{
15896 char_u *dir;
15897 char_u buf[NUMBUFLEN];
15898 int prot = 0755;
15899
15900 rettv->vval.v_number = FAIL;
15901 if (check_restricted() || check_secure())
15902 return;
15903
15904 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015905 if (*dir == NUL)
15906 rettv->vval.v_number = FAIL;
15907 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015908 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015909 if (*gettail(dir) == NUL)
15910 /* remove trailing slashes */
15911 *gettail_sep(dir) = NUL;
15912
15913 if (argvars[1].v_type != VAR_UNKNOWN)
15914 {
15915 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015916 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015917 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15918 mkdir_recurse(dir, prot);
15919 }
15920 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015921 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015922}
15923#endif
15924
Bram Moolenaar0d660222005-01-07 21:51:51 +000015925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926 * "mode()" function
15927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015929f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015931 char_u buf[3];
15932
15933 buf[1] = NUL;
15934 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935
Bram Moolenaare381d3d2016-07-07 14:50:41 +020015936 if (time_for_testing == 93784)
15937 {
15938 /* Testing the two-character code. */
15939 buf[0] = 'x';
15940 buf[1] = '!';
15941 }
15942 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943 {
15944 if (VIsual_select)
15945 buf[0] = VIsual_mode + 's' - 'v';
15946 else
15947 buf[0] = VIsual_mode;
15948 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015949 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015950 || State == CONFIRM)
15951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015953 if (State == ASKMORE)
15954 buf[1] = 'm';
15955 else if (State == CONFIRM)
15956 buf[1] = '?';
15957 }
15958 else if (State == EXTERNCMD)
15959 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960 else if (State & INSERT)
15961 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015962#ifdef FEAT_VREPLACE
15963 if (State & VREPLACE_FLAG)
15964 {
15965 buf[0] = 'R';
15966 buf[1] = 'v';
15967 }
15968 else
15969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 if (State & REPLACE_FLAG)
15971 buf[0] = 'R';
15972 else
15973 buf[0] = 'i';
15974 }
15975 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015978 if (exmode_active)
15979 buf[1] = 'v';
15980 }
15981 else if (exmode_active)
15982 {
15983 buf[0] = 'c';
15984 buf[1] = 'e';
15985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015987 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015989 if (finish_op)
15990 buf[1] = 'o';
15991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015993 /* Clear out the minor mode when the argument is not a non-zero number or
15994 * non-empty string. */
15995 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015996 buf[1] = NUL;
15997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015998 rettv->vval.v_string = vim_strsave(buf);
15999 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000}
16001
Bram Moolenaar429fa852013-04-15 12:27:36 +020016002#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016003/*
16004 * "mzeval()" function
16005 */
16006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016007f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016008{
16009 char_u *str;
16010 char_u buf[NUMBUFLEN];
16011
16012 str = get_tv_string_buf(&argvars[0], buf);
16013 do_mzeval(str, rettv);
16014}
Bram Moolenaar75676462013-01-30 14:55:42 +010016015
16016 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016017mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016018{
16019 typval_T argvars[3];
16020
16021 argvars[0].v_type = VAR_STRING;
16022 argvars[0].vval.v_string = name;
16023 copy_tv(args, &argvars[1]);
16024 argvars[2].v_type = VAR_UNKNOWN;
16025 f_call(argvars, rettv);
16026 clear_tv(&argvars[1]);
16027}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016028#endif
16029
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016031 * "nextnonblank()" function
16032 */
16033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016034f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016035{
16036 linenr_T lnum;
16037
16038 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16039 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016040 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016041 {
16042 lnum = 0;
16043 break;
16044 }
16045 if (*skipwhite(ml_get(lnum)) != NUL)
16046 break;
16047 }
16048 rettv->vval.v_number = lnum;
16049}
16050
16051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 * "nr2char()" function
16053 */
16054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016055f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056{
16057 char_u buf[NUMBUFLEN];
16058
16059#ifdef FEAT_MBYTE
16060 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016061 {
16062 int utf8 = 0;
16063
16064 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016065 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010016066 if (utf8)
16067 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16068 else
16069 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 else
16072#endif
16073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016074 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 buf[1] = NUL;
16076 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016077 rettv->v_type = VAR_STRING;
16078 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016079}
16080
16081/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016082 * "or(expr, expr)" function
16083 */
16084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016085f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016086{
16087 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16088 | get_tv_number_chk(&argvars[1], NULL);
16089}
16090
16091/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016092 * "pathshorten()" function
16093 */
16094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016095f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016096{
16097 char_u *p;
16098
16099 rettv->v_type = VAR_STRING;
16100 p = get_tv_string_chk(&argvars[0]);
16101 if (p == NULL)
16102 rettv->vval.v_string = NULL;
16103 else
16104 {
16105 p = vim_strsave(p);
16106 rettv->vval.v_string = p;
16107 if (p != NULL)
16108 shorten_dir(p);
16109 }
16110}
16111
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016112#ifdef FEAT_PERL
16113/*
16114 * "perleval()" function
16115 */
16116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016117f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016118{
16119 char_u *str;
16120 char_u buf[NUMBUFLEN];
16121
16122 str = get_tv_string_buf(&argvars[0], buf);
16123 do_perleval(str, rettv);
16124}
16125#endif
16126
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016127#ifdef FEAT_FLOAT
16128/*
16129 * "pow()" function
16130 */
16131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016132f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016133{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016134 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016135
16136 rettv->v_type = VAR_FLOAT;
16137 if (get_float_arg(argvars, &fx) == OK
16138 && get_float_arg(&argvars[1], &fy) == OK)
16139 rettv->vval.v_float = pow(fx, fy);
16140 else
16141 rettv->vval.v_float = 0.0;
16142}
16143#endif
16144
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016145/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016146 * "prevnonblank()" function
16147 */
16148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016149f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016150{
16151 linenr_T lnum;
16152
16153 lnum = get_tv_lnum(argvars);
16154 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16155 lnum = 0;
16156 else
16157 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16158 --lnum;
16159 rettv->vval.v_number = lnum;
16160}
16161
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016162/* This dummy va_list is here because:
16163 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16164 * - locally in the function results in a "used before set" warning
16165 * - using va_start() to initialize it gives "function with fixed args" error */
16166static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016167
Bram Moolenaar8c711452005-01-14 21:53:12 +000016168/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016169 * "printf()" function
16170 */
16171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016172f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016173{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016174 char_u buf[NUMBUFLEN];
16175 int len;
16176 char_u *s;
16177 int saved_did_emsg = did_emsg;
16178 char *fmt;
16179
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016180 rettv->v_type = VAR_STRING;
16181 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016182
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016183 /* Get the required length, allocate the buffer and do it for real. */
16184 did_emsg = FALSE;
16185 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16186 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16187 if (!did_emsg)
16188 {
16189 s = alloc(len + 1);
16190 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016191 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016192 rettv->vval.v_string = s;
16193 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016194 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016195 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016196 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016197}
16198
16199/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016200 * "pumvisible()" function
16201 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016203f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016204{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016205#ifdef FEAT_INS_EXPAND
16206 if (pum_visible())
16207 rettv->vval.v_number = 1;
16208#endif
16209}
16210
Bram Moolenaardb913952012-06-29 12:54:53 +020016211#ifdef FEAT_PYTHON3
16212/*
16213 * "py3eval()" function
16214 */
16215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016216f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016217{
16218 char_u *str;
16219 char_u buf[NUMBUFLEN];
16220
16221 str = get_tv_string_buf(&argvars[0], buf);
16222 do_py3eval(str, rettv);
16223}
16224#endif
16225
16226#ifdef FEAT_PYTHON
16227/*
16228 * "pyeval()" function
16229 */
16230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016231f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016232{
16233 char_u *str;
16234 char_u buf[NUMBUFLEN];
16235
16236 str = get_tv_string_buf(&argvars[0], buf);
16237 do_pyeval(str, rettv);
16238}
16239#endif
16240
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016241/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016242 * "range()" function
16243 */
16244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016245f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016246{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016247 varnumber_T start;
16248 varnumber_T end;
16249 varnumber_T stride = 1;
16250 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016251 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016253 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016254 if (argvars[1].v_type == VAR_UNKNOWN)
16255 {
16256 end = start - 1;
16257 start = 0;
16258 }
16259 else
16260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016261 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016262 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016263 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016264 }
16265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016266 if (error)
16267 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016268 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016269 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016270 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016271 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016272 else
16273 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016274 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016275 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016276 if (list_append_number(rettv->vval.v_list,
16277 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016278 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016279 }
16280}
16281
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016282/*
16283 * "readfile()" function
16284 */
16285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016286f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016287{
16288 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016289 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016290 char_u *fname;
16291 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016292 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16293 int io_size = sizeof(buf);
16294 int readlen; /* size of last fread() */
16295 char_u *prev = NULL; /* previously read bytes, if any */
16296 long prevlen = 0; /* length of data in prev */
16297 long prevsize = 0; /* size of prev buffer */
16298 long maxline = MAXLNUM;
16299 long cnt = 0;
16300 char_u *p; /* position in buf */
16301 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016302
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016303 if (argvars[1].v_type != VAR_UNKNOWN)
16304 {
16305 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16306 binary = TRUE;
16307 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016308 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016309 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016310
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016311 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016312 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016313
16314 /* Always open the file in binary mode, library functions have a mind of
16315 * their own about CR-LF conversion. */
16316 fname = get_tv_string(&argvars[0]);
16317 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16318 {
16319 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16320 return;
16321 }
16322
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016323 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016324 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016325 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016326
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016327 /* This for loop processes what was read, but is also entered at end
16328 * of file so that either:
16329 * - an incomplete line gets written
16330 * - a "binary" file gets an empty line at the end if it ends in a
16331 * newline. */
16332 for (p = buf, start = buf;
16333 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16334 ++p)
16335 {
16336 if (*p == '\n' || readlen <= 0)
16337 {
16338 listitem_T *li;
16339 char_u *s = NULL;
16340 long_u len = p - start;
16341
16342 /* Finished a line. Remove CRs before NL. */
16343 if (readlen > 0 && !binary)
16344 {
16345 while (len > 0 && start[len - 1] == '\r')
16346 --len;
16347 /* removal may cross back to the "prev" string */
16348 if (len == 0)
16349 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16350 --prevlen;
16351 }
16352 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016353 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016354 else
16355 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016356 /* Change "prev" buffer to be the right size. This way
16357 * the bytes are only copied once, and very long lines are
16358 * allocated only once. */
16359 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016360 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016361 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016362 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016363 prev = NULL; /* the list will own the string */
16364 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016365 }
16366 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016367 if (s == NULL)
16368 {
16369 do_outofmem_msg((long_u) prevlen + len + 1);
16370 failed = TRUE;
16371 break;
16372 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016373
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016374 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016375 {
16376 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016377 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016378 break;
16379 }
16380 li->li_tv.v_type = VAR_STRING;
16381 li->li_tv.v_lock = 0;
16382 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016383 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016384
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016385 start = p + 1; /* step over newline */
16386 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016387 break;
16388 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016389 else if (*p == NUL)
16390 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016391#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016392 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16393 * when finding the BF and check the previous two bytes. */
16394 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016395 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016396 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16397 * + 1, these may be in the "prev" string. */
16398 char_u back1 = p >= buf + 1 ? p[-1]
16399 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16400 char_u back2 = p >= buf + 2 ? p[-2]
16401 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16402 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016403
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016404 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016405 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016406 char_u *dest = p - 2;
16407
16408 /* Usually a BOM is at the beginning of a file, and so at
16409 * the beginning of a line; then we can just step over it.
16410 */
16411 if (start == dest)
16412 start = p + 1;
16413 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016414 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016415 /* have to shuffle buf to close gap */
16416 int adjust_prevlen = 0;
16417
16418 if (dest < buf)
16419 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016420 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016421 dest = buf;
16422 }
16423 if (readlen > p - buf + 1)
16424 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16425 readlen -= 3 - adjust_prevlen;
16426 prevlen -= adjust_prevlen;
16427 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016428 }
16429 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016430 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016431#endif
16432 } /* for */
16433
16434 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16435 break;
16436 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016437 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016438 /* There's part of a line in buf, store it in "prev". */
16439 if (p - start + prevlen >= prevsize)
16440 {
16441 /* need bigger "prev" buffer */
16442 char_u *newprev;
16443
16444 /* A common use case is ordinary text files and "prev" gets a
16445 * fragment of a line, so the first allocation is made
16446 * small, to avoid repeatedly 'allocing' large and
16447 * 'reallocing' small. */
16448 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016449 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016450 else
16451 {
16452 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016453 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016454 prevsize = grow50pc > growmin ? grow50pc : growmin;
16455 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016456 newprev = prev == NULL ? alloc(prevsize)
16457 : vim_realloc(prev, prevsize);
16458 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016459 {
16460 do_outofmem_msg((long_u)prevsize);
16461 failed = TRUE;
16462 break;
16463 }
16464 prev = newprev;
16465 }
16466 /* Add the line part to end of "prev". */
16467 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016468 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016469 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016470 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016471
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016472 /*
16473 * For a negative line count use only the lines at the end of the file,
16474 * free the rest.
16475 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016476 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016477 while (cnt > -maxline)
16478 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016479 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016480 --cnt;
16481 }
16482
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016483 if (failed)
16484 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016485 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016486 /* readfile doc says an empty list is returned on error */
16487 rettv->vval.v_list = list_alloc();
16488 }
16489
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016490 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016491 fclose(fd);
16492}
16493
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016494#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016495static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016496
16497/*
16498 * Convert a List to proftime_T.
16499 * Return FAIL when there is something wrong.
16500 */
16501 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016502list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016503{
16504 long n1, n2;
16505 int error = FALSE;
16506
16507 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16508 || arg->vval.v_list->lv_len != 2)
16509 return FAIL;
16510 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16511 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16512# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016513 tm->HighPart = n1;
16514 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016515# else
16516 tm->tv_sec = n1;
16517 tm->tv_usec = n2;
16518# endif
16519 return error ? FAIL : OK;
16520}
16521#endif /* FEAT_RELTIME */
16522
16523/*
16524 * "reltime()" function
16525 */
16526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016527f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016528{
16529#ifdef FEAT_RELTIME
16530 proftime_T res;
16531 proftime_T start;
16532
16533 if (argvars[0].v_type == VAR_UNKNOWN)
16534 {
16535 /* No arguments: get current time. */
16536 profile_start(&res);
16537 }
16538 else if (argvars[1].v_type == VAR_UNKNOWN)
16539 {
16540 if (list2proftime(&argvars[0], &res) == FAIL)
16541 return;
16542 profile_end(&res);
16543 }
16544 else
16545 {
16546 /* Two arguments: compute the difference. */
16547 if (list2proftime(&argvars[0], &start) == FAIL
16548 || list2proftime(&argvars[1], &res) == FAIL)
16549 return;
16550 profile_sub(&res, &start);
16551 }
16552
16553 if (rettv_list_alloc(rettv) == OK)
16554 {
16555 long n1, n2;
16556
16557# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016558 n1 = res.HighPart;
16559 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016560# else
16561 n1 = res.tv_sec;
16562 n2 = res.tv_usec;
16563# endif
16564 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16565 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16566 }
16567#endif
16568}
16569
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016570#ifdef FEAT_FLOAT
16571/*
16572 * "reltimefloat()" function
16573 */
16574 static void
16575f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16576{
16577# ifdef FEAT_RELTIME
16578 proftime_T tm;
16579# endif
16580
16581 rettv->v_type = VAR_FLOAT;
16582 rettv->vval.v_float = 0;
16583# ifdef FEAT_RELTIME
16584 if (list2proftime(&argvars[0], &tm) == OK)
16585 rettv->vval.v_float = profile_float(&tm);
16586# endif
16587}
16588#endif
16589
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016590/*
16591 * "reltimestr()" function
16592 */
16593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016594f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016595{
16596#ifdef FEAT_RELTIME
16597 proftime_T tm;
16598#endif
16599
16600 rettv->v_type = VAR_STRING;
16601 rettv->vval.v_string = NULL;
16602#ifdef FEAT_RELTIME
16603 if (list2proftime(&argvars[0], &tm) == OK)
16604 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16605#endif
16606}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016607
Bram Moolenaar0d660222005-01-07 21:51:51 +000016608#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016609static void make_connection(void);
16610static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016611
16612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016613make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016614{
16615 if (X_DISPLAY == NULL
16616# ifdef FEAT_GUI
16617 && !gui.in_use
16618# endif
16619 )
16620 {
16621 x_force_connect = TRUE;
16622 setup_term_clip();
16623 x_force_connect = FALSE;
16624 }
16625}
16626
16627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016628check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016629{
16630 make_connection();
16631 if (X_DISPLAY == NULL)
16632 {
16633 EMSG(_("E240: No connection to Vim server"));
16634 return FAIL;
16635 }
16636 return OK;
16637}
16638#endif
16639
16640#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016642remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016643{
16644 char_u *server_name;
16645 char_u *keys;
16646 char_u *r = NULL;
16647 char_u buf[NUMBUFLEN];
16648# ifdef WIN32
16649 HWND w;
16650# else
16651 Window w;
16652# endif
16653
16654 if (check_restricted() || check_secure())
16655 return;
16656
16657# ifdef FEAT_X11
16658 if (check_connection() == FAIL)
16659 return;
16660# endif
16661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016662 server_name = get_tv_string_chk(&argvars[0]);
16663 if (server_name == NULL)
16664 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016665 keys = get_tv_string_buf(&argvars[1], buf);
16666# ifdef WIN32
16667 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16668# else
16669 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16670 < 0)
16671# endif
16672 {
16673 if (r != NULL)
16674 EMSG(r); /* sending worked but evaluation failed */
16675 else
16676 EMSG2(_("E241: Unable to send to %s"), server_name);
16677 return;
16678 }
16679
16680 rettv->vval.v_string = r;
16681
16682 if (argvars[2].v_type != VAR_UNKNOWN)
16683 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016684 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016685 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016686 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016687
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016688 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016689 v.di_tv.v_type = VAR_STRING;
16690 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016691 idvar = get_tv_string_chk(&argvars[2]);
16692 if (idvar != NULL)
16693 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016694 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016695 }
16696}
16697#endif
16698
16699/*
16700 * "remote_expr()" function
16701 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016703f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016704{
16705 rettv->v_type = VAR_STRING;
16706 rettv->vval.v_string = NULL;
16707#ifdef FEAT_CLIENTSERVER
16708 remote_common(argvars, rettv, TRUE);
16709#endif
16710}
16711
16712/*
16713 * "remote_foreground()" function
16714 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016716f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016717{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016718#ifdef FEAT_CLIENTSERVER
16719# ifdef WIN32
16720 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016721 {
16722 char_u *server_name = get_tv_string_chk(&argvars[0]);
16723
16724 if (server_name != NULL)
16725 serverForeground(server_name);
16726 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016727# else
16728 /* Send a foreground() expression to the server. */
16729 argvars[1].v_type = VAR_STRING;
16730 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16731 argvars[2].v_type = VAR_UNKNOWN;
16732 remote_common(argvars, rettv, TRUE);
16733 vim_free(argvars[1].vval.v_string);
16734# endif
16735#endif
16736}
16737
Bram Moolenaar0d660222005-01-07 21:51:51 +000016738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016739f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016740{
16741#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016742 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016743 char_u *s = NULL;
16744# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016745 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016746# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016747 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016748
16749 if (check_restricted() || check_secure())
16750 {
16751 rettv->vval.v_number = -1;
16752 return;
16753 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016754 serverid = get_tv_string_chk(&argvars[0]);
16755 if (serverid == NULL)
16756 {
16757 rettv->vval.v_number = -1;
16758 return; /* type error; errmsg already given */
16759 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016760# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016761 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016762 if (n == 0)
16763 rettv->vval.v_number = -1;
16764 else
16765 {
16766 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16767 rettv->vval.v_number = (s != NULL);
16768 }
16769# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016770 if (check_connection() == FAIL)
16771 return;
16772
16773 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016774 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016775# endif
16776
16777 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016779 char_u *retvar;
16780
Bram Moolenaar33570922005-01-25 22:26:29 +000016781 v.di_tv.v_type = VAR_STRING;
16782 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016783 retvar = get_tv_string_chk(&argvars[1]);
16784 if (retvar != NULL)
16785 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016786 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016787 }
16788#else
16789 rettv->vval.v_number = -1;
16790#endif
16791}
16792
Bram Moolenaar0d660222005-01-07 21:51:51 +000016793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016794f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016795{
16796 char_u *r = NULL;
16797
16798#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016799 char_u *serverid = get_tv_string_chk(&argvars[0]);
16800
16801 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016802 {
16803# ifdef WIN32
16804 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016805 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016806
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016807 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016808 if (n != 0)
16809 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16810 if (r == NULL)
16811# else
16812 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016813 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016814# endif
16815 EMSG(_("E277: Unable to read a server reply"));
16816 }
16817#endif
16818 rettv->v_type = VAR_STRING;
16819 rettv->vval.v_string = r;
16820}
16821
16822/*
16823 * "remote_send()" function
16824 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016826f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016827{
16828 rettv->v_type = VAR_STRING;
16829 rettv->vval.v_string = NULL;
16830#ifdef FEAT_CLIENTSERVER
16831 remote_common(argvars, rettv, FALSE);
16832#endif
16833}
16834
16835/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016836 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016837 */
16838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016839f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016840{
Bram Moolenaar33570922005-01-25 22:26:29 +000016841 list_T *l;
16842 listitem_T *item, *item2;
16843 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016844 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016845 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016846 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016847 dict_T *d;
16848 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016849 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016850
Bram Moolenaar8c711452005-01-14 21:53:12 +000016851 if (argvars[0].v_type == VAR_DICT)
16852 {
16853 if (argvars[2].v_type != VAR_UNKNOWN)
16854 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016855 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016856 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016858 key = get_tv_string_chk(&argvars[1]);
16859 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016861 di = dict_find(d, key, -1);
16862 if (di == NULL)
16863 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016864 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16865 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016866 {
16867 *rettv = di->di_tv;
16868 init_tv(&di->di_tv);
16869 dictitem_remove(d, di);
16870 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016871 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016872 }
16873 }
16874 else if (argvars[0].v_type != VAR_LIST)
16875 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016876 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016877 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016879 int error = FALSE;
16880
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016881 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016882 if (error)
16883 ; /* type error: do nothing, errmsg already given */
16884 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016885 EMSGN(_(e_listidx), idx);
16886 else
16887 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016888 if (argvars[2].v_type == VAR_UNKNOWN)
16889 {
16890 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016891 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016892 *rettv = item->li_tv;
16893 vim_free(item);
16894 }
16895 else
16896 {
16897 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016898 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016899 if (error)
16900 ; /* type error: do nothing */
16901 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016902 EMSGN(_(e_listidx), end);
16903 else
16904 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016905 int cnt = 0;
16906
16907 for (li = item; li != NULL; li = li->li_next)
16908 {
16909 ++cnt;
16910 if (li == item2)
16911 break;
16912 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016913 if (li == NULL) /* didn't find "item2" after "item" */
16914 EMSG(_(e_invrange));
16915 else
16916 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016917 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016918 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016919 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016920 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016921 l->lv_first = item;
16922 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016923 item->li_prev = NULL;
16924 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016925 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016926 }
16927 }
16928 }
16929 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016930 }
16931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932}
16933
16934/*
16935 * "rename({from}, {to})" function
16936 */
16937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016938f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939{
16940 char_u buf[NUMBUFLEN];
16941
16942 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016943 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016945 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16946 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947}
16948
16949/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016950 * "repeat()" function
16951 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016953f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016954{
16955 char_u *p;
16956 int n;
16957 int slen;
16958 int len;
16959 char_u *r;
16960 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016961
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016962 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016963 if (argvars[0].v_type == VAR_LIST)
16964 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016965 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016966 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016967 if (list_extend(rettv->vval.v_list,
16968 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016969 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016970 }
16971 else
16972 {
16973 p = get_tv_string(&argvars[0]);
16974 rettv->v_type = VAR_STRING;
16975 rettv->vval.v_string = NULL;
16976
16977 slen = (int)STRLEN(p);
16978 len = slen * n;
16979 if (len <= 0)
16980 return;
16981
16982 r = alloc(len + 1);
16983 if (r != NULL)
16984 {
16985 for (i = 0; i < n; i++)
16986 mch_memmove(r + i * slen, p, (size_t)slen);
16987 r[len] = NUL;
16988 }
16989
16990 rettv->vval.v_string = r;
16991 }
16992}
16993
16994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995 * "resolve()" function
16996 */
16997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016998f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999{
17000 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017001#ifdef HAVE_READLINK
17002 char_u *buf = NULL;
17003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017005 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006#ifdef FEAT_SHORTCUT
17007 {
17008 char_u *v = NULL;
17009
17010 v = mch_resolve_shortcut(p);
17011 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017012 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017014 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 }
17016#else
17017# ifdef HAVE_READLINK
17018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019 char_u *cpy;
17020 int len;
17021 char_u *remain = NULL;
17022 char_u *q;
17023 int is_relative_to_current = FALSE;
17024 int has_trailing_pathsep = FALSE;
17025 int limit = 100;
17026
17027 p = vim_strsave(p);
17028
17029 if (p[0] == '.' && (vim_ispathsep(p[1])
17030 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17031 is_relative_to_current = TRUE;
17032
17033 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017034 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017037 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039
17040 q = getnextcomp(p);
17041 if (*q != NUL)
17042 {
17043 /* Separate the first path component in "p", and keep the
17044 * remainder (beginning with the path separator). */
17045 remain = vim_strsave(q - 1);
17046 q[-1] = NUL;
17047 }
17048
Bram Moolenaard9462e32011-04-11 21:35:11 +020017049 buf = alloc(MAXPATHL + 1);
17050 if (buf == NULL)
17051 goto fail;
17052
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053 for (;;)
17054 {
17055 for (;;)
17056 {
17057 len = readlink((char *)p, (char *)buf, MAXPATHL);
17058 if (len <= 0)
17059 break;
17060 buf[len] = NUL;
17061
17062 if (limit-- == 0)
17063 {
17064 vim_free(p);
17065 vim_free(remain);
17066 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017067 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068 goto fail;
17069 }
17070
17071 /* Ensure that the result will have a trailing path separator
17072 * if the argument has one. */
17073 if (remain == NULL && has_trailing_pathsep)
17074 add_pathsep(buf);
17075
17076 /* Separate the first path component in the link value and
17077 * concatenate the remainders. */
17078 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17079 if (*q != NUL)
17080 {
17081 if (remain == NULL)
17082 remain = vim_strsave(q - 1);
17083 else
17084 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017085 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 if (cpy != NULL)
17087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 vim_free(remain);
17089 remain = cpy;
17090 }
17091 }
17092 q[-1] = NUL;
17093 }
17094
17095 q = gettail(p);
17096 if (q > p && *q == NUL)
17097 {
17098 /* Ignore trailing path separator. */
17099 q[-1] = NUL;
17100 q = gettail(p);
17101 }
17102 if (q > p && !mch_isFullName(buf))
17103 {
17104 /* symlink is relative to directory of argument */
17105 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17106 if (cpy != NULL)
17107 {
17108 STRCPY(cpy, p);
17109 STRCPY(gettail(cpy), buf);
17110 vim_free(p);
17111 p = cpy;
17112 }
17113 }
17114 else
17115 {
17116 vim_free(p);
17117 p = vim_strsave(buf);
17118 }
17119 }
17120
17121 if (remain == NULL)
17122 break;
17123
17124 /* Append the first path component of "remain" to "p". */
17125 q = getnextcomp(remain + 1);
17126 len = q - remain - (*q != NUL);
17127 cpy = vim_strnsave(p, STRLEN(p) + len);
17128 if (cpy != NULL)
17129 {
17130 STRNCAT(cpy, remain, len);
17131 vim_free(p);
17132 p = cpy;
17133 }
17134 /* Shorten "remain". */
17135 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017136 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137 else
17138 {
17139 vim_free(remain);
17140 remain = NULL;
17141 }
17142 }
17143
17144 /* If the result is a relative path name, make it explicitly relative to
17145 * the current directory if and only if the argument had this form. */
17146 if (!vim_ispathsep(*p))
17147 {
17148 if (is_relative_to_current
17149 && *p != NUL
17150 && !(p[0] == '.'
17151 && (p[1] == NUL
17152 || vim_ispathsep(p[1])
17153 || (p[1] == '.'
17154 && (p[2] == NUL
17155 || vim_ispathsep(p[2]))))))
17156 {
17157 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017158 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 if (cpy != NULL)
17160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161 vim_free(p);
17162 p = cpy;
17163 }
17164 }
17165 else if (!is_relative_to_current)
17166 {
17167 /* Strip leading "./". */
17168 q = p;
17169 while (q[0] == '.' && vim_ispathsep(q[1]))
17170 q += 2;
17171 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017172 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 }
17174 }
17175
17176 /* Ensure that the result will have no trailing path separator
17177 * if the argument had none. But keep "/" or "//". */
17178 if (!has_trailing_pathsep)
17179 {
17180 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017181 if (after_pathsep(p, q))
17182 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183 }
17184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017185 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186 }
17187# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017188 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189# endif
17190#endif
17191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017192 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193
17194#ifdef HAVE_READLINK
17195fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017196 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017198 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199}
17200
17201/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017202 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203 */
17204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017205f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206{
Bram Moolenaar33570922005-01-25 22:26:29 +000017207 list_T *l;
17208 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209
Bram Moolenaar0d660222005-01-07 21:51:51 +000017210 if (argvars[0].v_type != VAR_LIST)
17211 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017212 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017213 && !tv_check_lock(l->lv_lock,
17214 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017215 {
17216 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017217 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017218 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017219 while (li != NULL)
17220 {
17221 ni = li->li_prev;
17222 list_append(l, li);
17223 li = ni;
17224 }
17225 rettv->vval.v_list = l;
17226 rettv->v_type = VAR_LIST;
17227 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017228 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230}
17231
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017232#define SP_NOMOVE 0x01 /* don't move cursor */
17233#define SP_REPEAT 0x02 /* repeat to find outer pair */
17234#define SP_RETCOUNT 0x04 /* return matchcount */
17235#define SP_SETPCMARK 0x08 /* set previous context mark */
17236#define SP_START 0x10 /* accept match at start position */
17237#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17238#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017239#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017240
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017241static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017242
17243/*
17244 * Get flags for a search function.
17245 * Possibly sets "p_ws".
17246 * Returns BACKWARD, FORWARD or zero (for an error).
17247 */
17248 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017249get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017250{
17251 int dir = FORWARD;
17252 char_u *flags;
17253 char_u nbuf[NUMBUFLEN];
17254 int mask;
17255
17256 if (varp->v_type != VAR_UNKNOWN)
17257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017258 flags = get_tv_string_buf_chk(varp, nbuf);
17259 if (flags == NULL)
17260 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017261 while (*flags != NUL)
17262 {
17263 switch (*flags)
17264 {
17265 case 'b': dir = BACKWARD; break;
17266 case 'w': p_ws = TRUE; break;
17267 case 'W': p_ws = FALSE; break;
17268 default: mask = 0;
17269 if (flagsp != NULL)
17270 switch (*flags)
17271 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017272 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017273 case 'e': mask = SP_END; break;
17274 case 'm': mask = SP_RETCOUNT; break;
17275 case 'n': mask = SP_NOMOVE; break;
17276 case 'p': mask = SP_SUBPAT; break;
17277 case 'r': mask = SP_REPEAT; break;
17278 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017279 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280 }
17281 if (mask == 0)
17282 {
17283 EMSG2(_(e_invarg2), flags);
17284 dir = 0;
17285 }
17286 else
17287 *flagsp |= mask;
17288 }
17289 if (dir == 0)
17290 break;
17291 ++flags;
17292 }
17293 }
17294 return dir;
17295}
17296
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017298 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017300 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017301search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017303 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 char_u *pat;
17305 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017306 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 int save_p_ws = p_ws;
17308 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017309 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017310 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017311 proftime_T tm;
17312#ifdef FEAT_RELTIME
17313 long time_limit = 0;
17314#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017315 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017316 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017318 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017319 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017320 if (dir == 0)
17321 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017322 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017323 if (flags & SP_START)
17324 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017325 if (flags & SP_END)
17326 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017327 if (flags & SP_COLUMN)
17328 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017329
Bram Moolenaar76929292008-01-06 19:07:36 +000017330 /* Optional arguments: line number to stop searching and timeout. */
17331 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017332 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017333 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017334 if (lnum_stop < 0)
17335 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017336#ifdef FEAT_RELTIME
17337 if (argvars[3].v_type != VAR_UNKNOWN)
17338 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017339 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017340 if (time_limit < 0)
17341 goto theend;
17342 }
17343#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017344 }
17345
Bram Moolenaar76929292008-01-06 19:07:36 +000017346#ifdef FEAT_RELTIME
17347 /* Set the time limit, if there is one. */
17348 profile_setlimit(time_limit, &tm);
17349#endif
17350
Bram Moolenaar231334e2005-07-25 20:46:57 +000017351 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017352 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017353 * Check to make sure only those flags are set.
17354 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17355 * flags cannot be set. Check for that condition also.
17356 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017357 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017358 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017360 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017361 goto theend;
17362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017364 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017365 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017366 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017367 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017369 if (flags & SP_SUBPAT)
17370 retval = subpatnum;
17371 else
17372 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017373 if (flags & SP_SETPCMARK)
17374 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017376 if (match_pos != NULL)
17377 {
17378 /* Store the match cursor position */
17379 match_pos->lnum = pos.lnum;
17380 match_pos->col = pos.col + 1;
17381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 /* "/$" will put the cursor after the end of the line, may need to
17383 * correct that here */
17384 check_cursor();
17385 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017386
17387 /* If 'n' flag is used: restore cursor position. */
17388 if (flags & SP_NOMOVE)
17389 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017390 else
17391 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017392theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017394
17395 return retval;
17396}
17397
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017398#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017399
17400/*
17401 * round() is not in C90, use ceil() or floor() instead.
17402 */
17403 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017404vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017405{
17406 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17407}
17408
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017409/*
17410 * "round({float})" function
17411 */
17412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017413f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017414{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017415 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017416
17417 rettv->v_type = VAR_FLOAT;
17418 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017419 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017420 else
17421 rettv->vval.v_float = 0.0;
17422}
17423#endif
17424
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017425/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017426 * "screenattr()" function
17427 */
17428 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017429f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017430{
17431 int row;
17432 int col;
17433 int c;
17434
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017435 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17436 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017437 if (row < 0 || row >= screen_Rows
17438 || col < 0 || col >= screen_Columns)
17439 c = -1;
17440 else
17441 c = ScreenAttrs[LineOffset[row] + col];
17442 rettv->vval.v_number = c;
17443}
17444
17445/*
17446 * "screenchar()" function
17447 */
17448 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017449f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017450{
17451 int row;
17452 int col;
17453 int off;
17454 int c;
17455
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017456 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17457 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017458 if (row < 0 || row >= screen_Rows
17459 || col < 0 || col >= screen_Columns)
17460 c = -1;
17461 else
17462 {
17463 off = LineOffset[row] + col;
17464#ifdef FEAT_MBYTE
17465 if (enc_utf8 && ScreenLinesUC[off] != 0)
17466 c = ScreenLinesUC[off];
17467 else
17468#endif
17469 c = ScreenLines[off];
17470 }
17471 rettv->vval.v_number = c;
17472}
17473
17474/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017475 * "screencol()" function
17476 *
17477 * First column is 1 to be consistent with virtcol().
17478 */
17479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017480f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017481{
17482 rettv->vval.v_number = screen_screencol() + 1;
17483}
17484
17485/*
17486 * "screenrow()" function
17487 */
17488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017489f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017490{
17491 rettv->vval.v_number = screen_screenrow() + 1;
17492}
17493
17494/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017495 * "search()" function
17496 */
17497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017498f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017499{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017500 int flags = 0;
17501
17502 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503}
17504
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017506 * "searchdecl()" function
17507 */
17508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017509f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017510{
17511 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017512 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017513 int error = FALSE;
17514 char_u *name;
17515
17516 rettv->vval.v_number = 1; /* default: FAIL */
17517
17518 name = get_tv_string_chk(&argvars[0]);
17519 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017520 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017521 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017522 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017523 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017524 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017525 if (!error && name != NULL)
17526 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017527 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017528}
17529
17530/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017531 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017534searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535{
17536 char_u *spat, *mpat, *epat;
17537 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 int dir;
17540 int flags = 0;
17541 char_u nbuf1[NUMBUFLEN];
17542 char_u nbuf2[NUMBUFLEN];
17543 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017544 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017545 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017546 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017549 spat = get_tv_string_chk(&argvars[0]);
17550 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17551 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17552 if (spat == NULL || mpat == NULL || epat == NULL)
17553 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 /* Handle the optional fourth argument: flags */
17556 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017557 if (dir == 0)
17558 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017559
17560 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017561 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17562 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017563 if ((flags & (SP_END | SP_SUBPAT)) != 0
17564 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017565 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017566 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017567 goto theend;
17568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017570 /* Using 'r' implies 'W', otherwise it doesn't work. */
17571 if (flags & SP_REPEAT)
17572 p_ws = FALSE;
17573
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017574 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017575 if (argvars[3].v_type == VAR_UNKNOWN
17576 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 skip = (char_u *)"";
17578 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017580 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017581 if (argvars[5].v_type != VAR_UNKNOWN)
17582 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017583 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017584 if (lnum_stop < 0)
17585 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017586#ifdef FEAT_RELTIME
17587 if (argvars[6].v_type != VAR_UNKNOWN)
17588 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017589 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017590 if (time_limit < 0)
17591 goto theend;
17592 }
17593#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017594 }
17595 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017596 if (skip == NULL)
17597 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017599 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017600 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017601
17602theend:
17603 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017604
17605 return retval;
17606}
17607
17608/*
17609 * "searchpair()" function
17610 */
17611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017612f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017613{
17614 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17615}
17616
17617/*
17618 * "searchpairpos()" function
17619 */
17620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017621f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017622{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017623 pos_T match_pos;
17624 int lnum = 0;
17625 int col = 0;
17626
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017627 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017628 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017629
17630 if (searchpair_cmn(argvars, &match_pos) > 0)
17631 {
17632 lnum = match_pos.lnum;
17633 col = match_pos.col;
17634 }
17635
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017636 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17637 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017638}
17639
17640/*
17641 * Search for a start/middle/end thing.
17642 * Used by searchpair(), see its documentation for the details.
17643 * Returns 0 or -1 for no match,
17644 */
17645 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017646do_searchpair(
17647 char_u *spat, /* start pattern */
17648 char_u *mpat, /* middle pattern */
17649 char_u *epat, /* end pattern */
17650 int dir, /* BACKWARD or FORWARD */
17651 char_u *skip, /* skip expression */
17652 int flags, /* SP_SETPCMARK and other SP_ values */
17653 pos_T *match_pos,
17654 linenr_T lnum_stop, /* stop at this line if not zero */
17655 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017656{
17657 char_u *save_cpo;
17658 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17659 long retval = 0;
17660 pos_T pos;
17661 pos_T firstpos;
17662 pos_T foundpos;
17663 pos_T save_cursor;
17664 pos_T save_pos;
17665 int n;
17666 int r;
17667 int nest = 1;
17668 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017669 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017670 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017671
17672 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17673 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017674 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017675
Bram Moolenaar76929292008-01-06 19:07:36 +000017676#ifdef FEAT_RELTIME
17677 /* Set the time limit, if there is one. */
17678 profile_setlimit(time_limit, &tm);
17679#endif
17680
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017681 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17682 * start/middle/end (pat3, for the top pair). */
17683 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17684 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17685 if (pat2 == NULL || pat3 == NULL)
17686 goto theend;
17687 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17688 if (*mpat == NUL)
17689 STRCPY(pat3, pat2);
17690 else
17691 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17692 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017693 if (flags & SP_START)
17694 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017695
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 save_cursor = curwin->w_cursor;
17697 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017698 clearpos(&firstpos);
17699 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 pat = pat3;
17701 for (;;)
17702 {
17703 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017704 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17706 /* didn't find it or found the first match again: FAIL */
17707 break;
17708
17709 if (firstpos.lnum == 0)
17710 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017711 if (equalpos(pos, foundpos))
17712 {
17713 /* Found the same position again. Can happen with a pattern that
17714 * has "\zs" at the end and searching backwards. Advance one
17715 * character and try again. */
17716 if (dir == BACKWARD)
17717 decl(&pos);
17718 else
17719 incl(&pos);
17720 }
17721 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017723 /* clear the start flag to avoid getting stuck here */
17724 options &= ~SEARCH_START;
17725
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 /* If the skip pattern matches, ignore this match. */
17727 if (*skip != NUL)
17728 {
17729 save_pos = curwin->w_cursor;
17730 curwin->w_cursor = pos;
17731 r = eval_to_bool(skip, &err, NULL, FALSE);
17732 curwin->w_cursor = save_pos;
17733 if (err)
17734 {
17735 /* Evaluating {skip} caused an error, break here. */
17736 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017737 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738 break;
17739 }
17740 if (r)
17741 continue;
17742 }
17743
17744 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17745 {
17746 /* Found end when searching backwards or start when searching
17747 * forward: nested pair. */
17748 ++nest;
17749 pat = pat2; /* nested, don't search for middle */
17750 }
17751 else
17752 {
17753 /* Found end when searching forward or start when searching
17754 * backward: end of (nested) pair; or found middle in outer pair. */
17755 if (--nest == 1)
17756 pat = pat3; /* outer level, search for middle */
17757 }
17758
17759 if (nest == 0)
17760 {
17761 /* Found the match: return matchcount or line number. */
17762 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017763 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017765 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017766 if (flags & SP_SETPCMARK)
17767 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 curwin->w_cursor = pos;
17769 if (!(flags & SP_REPEAT))
17770 break;
17771 nest = 1; /* search for next unmatched */
17772 }
17773 }
17774
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017775 if (match_pos != NULL)
17776 {
17777 /* Store the match cursor position */
17778 match_pos->lnum = curwin->w_cursor.lnum;
17779 match_pos->col = curwin->w_cursor.col + 1;
17780 }
17781
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017783 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 curwin->w_cursor = save_cursor;
17785
17786theend:
17787 vim_free(pat2);
17788 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017789 if (p_cpo == empty_option)
17790 p_cpo = save_cpo;
17791 else
17792 /* Darn, evaluating the {skip} expression changed the value. */
17793 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017794
17795 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796}
17797
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017798/*
17799 * "searchpos()" function
17800 */
17801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017802f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017803{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017804 pos_T match_pos;
17805 int lnum = 0;
17806 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017807 int n;
17808 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017809
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017810 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017811 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017812
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017813 n = search_cmn(argvars, &match_pos, &flags);
17814 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017815 {
17816 lnum = match_pos.lnum;
17817 col = match_pos.col;
17818 }
17819
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017820 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17821 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017822 if (flags & SP_SUBPAT)
17823 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017824}
17825
Bram Moolenaar0d660222005-01-07 21:51:51 +000017826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017827f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017829#ifdef FEAT_CLIENTSERVER
17830 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017831 char_u *server = get_tv_string_chk(&argvars[0]);
17832 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833
Bram Moolenaar0d660222005-01-07 21:51:51 +000017834 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017835 if (server == NULL || reply == NULL)
17836 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017837 if (check_restricted() || check_secure())
17838 return;
17839# ifdef FEAT_X11
17840 if (check_connection() == FAIL)
17841 return;
17842# endif
17843
17844 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017846 EMSG(_("E258: Unable to send to client"));
17847 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017849 rettv->vval.v_number = 0;
17850#else
17851 rettv->vval.v_number = -1;
17852#endif
17853}
17854
Bram Moolenaar0d660222005-01-07 21:51:51 +000017855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017856f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017857{
17858 char_u *r = NULL;
17859
17860#ifdef FEAT_CLIENTSERVER
17861# ifdef WIN32
17862 r = serverGetVimNames();
17863# else
17864 make_connection();
17865 if (X_DISPLAY != NULL)
17866 r = serverGetVimNames(X_DISPLAY);
17867# endif
17868#endif
17869 rettv->v_type = VAR_STRING;
17870 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871}
17872
17873/*
17874 * "setbufvar()" function
17875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017877f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878{
17879 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017881 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882 char_u nbuf[NUMBUFLEN];
17883
17884 if (check_restricted() || check_secure())
17885 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017886 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17887 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017888 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 varp = &argvars[2];
17890
17891 if (buf != NULL && varname != NULL && varp != NULL)
17892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 if (*varname == '&')
17894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017895 long numval;
17896 char_u *strval;
17897 int error = FALSE;
Bram Moolenaar93431df2016-07-15 20:14:44 +020017898 aco_save_T aco;
17899
17900 /* set curbuf to be our buf, temporarily */
17901 aucmd_prepbuf(&aco, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017902
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017904 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017905 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017906 if (!error && strval != NULL)
17907 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar93431df2016-07-15 20:14:44 +020017908
17909 /* reset notion of buffer */
17910 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 }
17912 else
17913 {
Bram Moolenaar93431df2016-07-15 20:14:44 +020017914 buf_T *save_curbuf = curbuf;
17915
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17917 if (bufvarname != NULL)
17918 {
Bram Moolenaar93431df2016-07-15 20:14:44 +020017919 curbuf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 STRCPY(bufvarname, "b:");
17921 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017922 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 vim_free(bufvarname);
Bram Moolenaar93431df2016-07-15 20:14:44 +020017924 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 }
17926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928}
17929
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017931f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017932{
17933 dict_T *d;
17934 dictitem_T *di;
17935 char_u *csearch;
17936
17937 if (argvars[0].v_type != VAR_DICT)
17938 {
17939 EMSG(_(e_dictreq));
17940 return;
17941 }
17942
17943 if ((d = argvars[0].vval.v_dict) != NULL)
17944 {
17945 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17946 if (csearch != NULL)
17947 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017948#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017949 if (enc_utf8)
17950 {
17951 int pcc[MAX_MCO];
17952 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017953
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017954 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17955 }
17956 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017957#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017958 set_last_csearch(PTR2CHAR(csearch),
17959 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017960 }
17961
17962 di = dict_find(d, (char_u *)"forward", -1);
17963 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017964 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017965 ? FORWARD : BACKWARD);
17966
17967 di = dict_find(d, (char_u *)"until", -1);
17968 if (di != NULL)
17969 set_csearch_until(!!get_tv_number(&di->di_tv));
17970 }
17971}
17972
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973/*
17974 * "setcmdpos()" function
17975 */
17976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017977f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017979 int pos = (int)get_tv_number(&argvars[0]) - 1;
17980
17981 if (pos >= 0)
17982 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983}
17984
17985/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017986 * "setfperm({fname}, {mode})" function
17987 */
17988 static void
17989f_setfperm(typval_T *argvars, typval_T *rettv)
17990{
17991 char_u *fname;
17992 char_u modebuf[NUMBUFLEN];
17993 char_u *mode_str;
17994 int i;
17995 int mask;
17996 int mode = 0;
17997
17998 rettv->vval.v_number = 0;
17999 fname = get_tv_string_chk(&argvars[0]);
18000 if (fname == NULL)
18001 return;
18002 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18003 if (mode_str == NULL)
18004 return;
18005 if (STRLEN(mode_str) != 9)
18006 {
18007 EMSG2(_(e_invarg2), mode_str);
18008 return;
18009 }
18010
18011 mask = 1;
18012 for (i = 8; i >= 0; --i)
18013 {
18014 if (mode_str[i] != '-')
18015 mode |= mask;
18016 mask = mask << 1;
18017 }
18018 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18019}
18020
18021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 * "setline()" function
18023 */
18024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018025f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026{
18027 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018028 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018029 list_T *l = NULL;
18030 listitem_T *li = NULL;
18031 long added = 0;
18032 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018034 lnum = get_tv_lnum(&argvars[0]);
18035 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018037 l = argvars[1].vval.v_list;
18038 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018040 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018041 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018042
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018043 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018044 for (;;)
18045 {
18046 if (l != NULL)
18047 {
18048 /* list argument, get next string */
18049 if (li == NULL)
18050 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018051 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018052 li = li->li_next;
18053 }
18054
18055 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018056 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018057 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018058
18059 /* When coming here from Insert mode, sync undo, so that this can be
18060 * undone separately from what was previously inserted. */
18061 if (u_sync_once == 2)
18062 {
18063 u_sync_once = 1; /* notify that u_sync() was called */
18064 u_sync(TRUE);
18065 }
18066
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018067 if (lnum <= curbuf->b_ml.ml_line_count)
18068 {
18069 /* existing line, replace it */
18070 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18071 {
18072 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018073 if (lnum == curwin->w_cursor.lnum)
18074 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018075 rettv->vval.v_number = 0; /* OK */
18076 }
18077 }
18078 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18079 {
18080 /* lnum is one past the last line, append the line */
18081 ++added;
18082 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18083 rettv->vval.v_number = 0; /* OK */
18084 }
18085
18086 if (l == NULL) /* only one string argument */
18087 break;
18088 ++lnum;
18089 }
18090
18091 if (added > 0)
18092 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093}
18094
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018095static 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 +000018096
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018098 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018099 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018101set_qf_ll_list(
18102 win_T *wp UNUSED,
18103 typval_T *list_arg UNUSED,
18104 typval_T *action_arg UNUSED,
18105 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018106{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018107#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018108 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018109 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018110 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018111#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018112
Bram Moolenaar2641f772005-03-25 21:58:17 +000018113 rettv->vval.v_number = -1;
18114
18115#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018116 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018117 EMSG(_(e_listreq));
18118 else
18119 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018120 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018121
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018122 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018123 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018124 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018125 if (act == NULL)
18126 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018127 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018128 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018129 else
18130 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018131 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018132 else if (action_arg->v_type == VAR_UNKNOWN)
18133 action = ' ';
18134 else
18135 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018136
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018137 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018138 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018139 rettv->vval.v_number = 0;
18140 }
18141#endif
18142}
18143
18144/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018145 * "setloclist()" function
18146 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018148f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018149{
18150 win_T *win;
18151
18152 rettv->vval.v_number = -1;
18153
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018154 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018155 if (win != NULL)
18156 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18157}
18158
18159/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018160 * "setmatches()" function
18161 */
18162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018163f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018164{
18165#ifdef FEAT_SEARCH_EXTRA
18166 list_T *l;
18167 listitem_T *li;
18168 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018169 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018170
18171 rettv->vval.v_number = -1;
18172 if (argvars[0].v_type != VAR_LIST)
18173 {
18174 EMSG(_(e_listreq));
18175 return;
18176 }
18177 if ((l = argvars[0].vval.v_list) != NULL)
18178 {
18179
18180 /* To some extent make sure that we are dealing with a list from
18181 * "getmatches()". */
18182 li = l->lv_first;
18183 while (li != NULL)
18184 {
18185 if (li->li_tv.v_type != VAR_DICT
18186 || (d = li->li_tv.vval.v_dict) == NULL)
18187 {
18188 EMSG(_(e_invarg));
18189 return;
18190 }
18191 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018192 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18193 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018194 && dict_find(d, (char_u *)"priority", -1) != NULL
18195 && dict_find(d, (char_u *)"id", -1) != NULL))
18196 {
18197 EMSG(_(e_invarg));
18198 return;
18199 }
18200 li = li->li_next;
18201 }
18202
18203 clear_matches(curwin);
18204 li = l->lv_first;
18205 while (li != NULL)
18206 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018207 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018208 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018209 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018210 char_u *group;
18211 int priority;
18212 int id;
18213 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018214
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018215 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018216 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18217 {
18218 if (s == NULL)
18219 {
18220 s = list_alloc();
18221 if (s == NULL)
18222 return;
18223 }
18224
18225 /* match from matchaddpos() */
18226 for (i = 1; i < 9; i++)
18227 {
18228 sprintf((char *)buf, (char *)"pos%d", i);
18229 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18230 {
18231 if (di->di_tv.v_type != VAR_LIST)
18232 return;
18233
18234 list_append_tv(s, &di->di_tv);
18235 s->lv_refcount++;
18236 }
18237 else
18238 break;
18239 }
18240 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018241
18242 group = get_dict_string(d, (char_u *)"group", FALSE);
18243 priority = (int)get_dict_number(d, (char_u *)"priority");
18244 id = (int)get_dict_number(d, (char_u *)"id");
18245 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18246 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18247 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018248 if (i == 0)
18249 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018250 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018251 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018252 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018253 }
18254 else
18255 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018256 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018257 list_unref(s);
18258 s = NULL;
18259 }
18260
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018261 li = li->li_next;
18262 }
18263 rettv->vval.v_number = 0;
18264 }
18265#endif
18266}
18267
18268/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018269 * "setpos()" function
18270 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018272f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018273{
18274 pos_T pos;
18275 int fnum;
18276 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018277 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018278
Bram Moolenaar08250432008-02-13 11:42:46 +000018279 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018280 name = get_tv_string_chk(argvars);
18281 if (name != NULL)
18282 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018283 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018284 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018285 if (--pos.col < 0)
18286 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018287 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018288 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018289 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018290 if (fnum == curbuf->b_fnum)
18291 {
18292 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018293 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018294 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018295 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018296 curwin->w_set_curswant = FALSE;
18297 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018298 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018299 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018300 }
18301 else
18302 EMSG(_(e_invarg));
18303 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018304 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18305 {
18306 /* set mark */
18307 if (setmark_pos(name[1], &pos, fnum) == OK)
18308 rettv->vval.v_number = 0;
18309 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018310 else
18311 EMSG(_(e_invarg));
18312 }
18313 }
18314}
18315
18316/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018317 * "setqflist()" function
18318 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018320f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018321{
18322 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18323}
18324
18325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 * "setreg()" function
18327 */
18328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018329f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330{
18331 int regname;
18332 char_u *strregname;
18333 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018334 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 int append;
18336 char_u yank_type;
18337 long block_len;
18338
18339 block_len = -1;
18340 yank_type = MAUTO;
18341 append = FALSE;
18342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018343 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018344 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018346 if (strregname == NULL)
18347 return; /* type error; errmsg already given */
18348 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 if (regname == 0 || regname == '@')
18350 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018352 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018354 stropt = get_tv_string_chk(&argvars[2]);
18355 if (stropt == NULL)
18356 return; /* type error */
18357 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 switch (*stropt)
18359 {
18360 case 'a': case 'A': /* append */
18361 append = TRUE;
18362 break;
18363 case 'v': case 'c': /* character-wise selection */
18364 yank_type = MCHAR;
18365 break;
18366 case 'V': case 'l': /* line-wise selection */
18367 yank_type = MLINE;
18368 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369 case 'b': case Ctrl_V: /* block-wise selection */
18370 yank_type = MBLOCK;
18371 if (VIM_ISDIGIT(stropt[1]))
18372 {
18373 ++stropt;
18374 block_len = getdigits(&stropt) - 1;
18375 --stropt;
18376 }
18377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 }
18379 }
18380
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018381 if (argvars[1].v_type == VAR_LIST)
18382 {
18383 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018384 char_u **allocval;
18385 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018386 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018387 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018388 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018389 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018390 int len;
18391
18392 /* If the list is NULL handle like an empty list. */
18393 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018394
Bram Moolenaar7d647822014-04-05 21:28:56 +020018395 /* First half: use for pointers to result lines; second half: use for
18396 * pointers to allocated copies. */
18397 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018398 if (lstval == NULL)
18399 return;
18400 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018401 allocval = lstval + len + 2;
18402 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018403
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018404 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018405 li = li->li_next)
18406 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018407 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018408 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018409 goto free_lstval;
18410 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018411 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018412 /* Need to make a copy, next get_tv_string_buf_chk() will
18413 * overwrite the string. */
18414 strval = vim_strsave(buf);
18415 if (strval == NULL)
18416 goto free_lstval;
18417 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018418 }
18419 *curval++ = strval;
18420 }
18421 *curval++ = NULL;
18422
18423 write_reg_contents_lst(regname, lstval, -1,
18424 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018425free_lstval:
18426 while (curallocval > allocval)
18427 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018428 vim_free(lstval);
18429 }
18430 else
18431 {
18432 strval = get_tv_string_chk(&argvars[1]);
18433 if (strval == NULL)
18434 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018435 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018438 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439}
18440
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018441/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018442 * "settabvar()" function
18443 */
18444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018445f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018446{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018447#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018448 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018449 tabpage_T *tp;
18450#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018451 char_u *varname, *tabvarname;
18452 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018453
18454 rettv->vval.v_number = 0;
18455
18456 if (check_restricted() || check_secure())
18457 return;
18458
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018459#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018460 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018461#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018462 varname = get_tv_string_chk(&argvars[1]);
18463 varp = &argvars[2];
18464
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018465 if (varname != NULL && varp != NULL
18466#ifdef FEAT_WINDOWS
18467 && tp != NULL
18468#endif
18469 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018470 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018471#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018472 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018473 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018474#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018475
18476 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18477 if (tabvarname != NULL)
18478 {
18479 STRCPY(tabvarname, "t:");
18480 STRCPY(tabvarname + 2, varname);
18481 set_var(tabvarname, varp, TRUE);
18482 vim_free(tabvarname);
18483 }
18484
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018485#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018486 /* Restore current tabpage */
18487 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018488 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018489#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018490 }
18491}
18492
18493/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018494 * "settabwinvar()" function
18495 */
18496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018497f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018498{
18499 setwinvar(argvars, rettv, 1);
18500}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501
18502/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018503 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018506f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018508 setwinvar(argvars, rettv, 0);
18509}
18510
18511/*
18512 * "setwinvar()" and "settabwinvar()" functions
18513 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018514
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018516setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018517{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 win_T *win;
18519#ifdef FEAT_WINDOWS
18520 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018521 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018522 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523#endif
18524 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018525 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018527 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528
18529 if (check_restricted() || check_secure())
18530 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018531
18532#ifdef FEAT_WINDOWS
18533 if (off == 1)
18534 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18535 else
18536 tp = curtab;
18537#endif
18538 win = find_win_by_nr(&argvars[off], tp);
18539 varname = get_tv_string_chk(&argvars[off + 1]);
18540 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541
18542 if (win != NULL && varname != NULL && varp != NULL)
18543 {
18544#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018545 need_switch_win = !(tp == curtab && win == curwin);
18546 if (!need_switch_win
18547 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018550 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018552 long numval;
18553 char_u *strval;
18554 int error = FALSE;
18555
18556 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018557 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018558 strval = get_tv_string_buf_chk(varp, nbuf);
18559 if (!error && strval != NULL)
18560 set_option_value(varname, numval, strval, OPT_LOCAL);
18561 }
18562 else
18563 {
18564 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18565 if (winvarname != NULL)
18566 {
18567 STRCPY(winvarname, "w:");
18568 STRCPY(winvarname + 2, varname);
18569 set_var(winvarname, varp, TRUE);
18570 vim_free(winvarname);
18571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 }
18573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018575 if (need_switch_win)
18576 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577#endif
18578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579}
18580
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018581#ifdef FEAT_CRYPT
18582/*
18583 * "sha256({string})" function
18584 */
18585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018586f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018587{
18588 char_u *p;
18589
18590 p = get_tv_string(&argvars[0]);
18591 rettv->vval.v_string = vim_strsave(
18592 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18593 rettv->v_type = VAR_STRING;
18594}
18595#endif /* FEAT_CRYPT */
18596
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018598 * "shellescape({string})" function
18599 */
18600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018601f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018602{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018603 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018604 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018605 rettv->v_type = VAR_STRING;
18606}
18607
18608/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018609 * shiftwidth() function
18610 */
18611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018612f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018613{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018614 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018615}
18616
18617/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018618 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 */
18620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018621f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018623 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624
Bram Moolenaar0d660222005-01-07 21:51:51 +000018625 p = get_tv_string(&argvars[0]);
18626 rettv->vval.v_string = vim_strsave(p);
18627 simplify_filename(rettv->vval.v_string); /* simplify in place */
18628 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629}
18630
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018631#ifdef FEAT_FLOAT
18632/*
18633 * "sin()" function
18634 */
18635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018636f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018637{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018638 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018639
18640 rettv->v_type = VAR_FLOAT;
18641 if (get_float_arg(argvars, &f) == OK)
18642 rettv->vval.v_float = sin(f);
18643 else
18644 rettv->vval.v_float = 0.0;
18645}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018646
18647/*
18648 * "sinh()" function
18649 */
18650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018651f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018652{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018653 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018654
18655 rettv->v_type = VAR_FLOAT;
18656 if (get_float_arg(argvars, &f) == OK)
18657 rettv->vval.v_float = sinh(f);
18658 else
18659 rettv->vval.v_float = 0.0;
18660}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018661#endif
18662
Bram Moolenaar0d660222005-01-07 21:51:51 +000018663static int
18664#ifdef __BORLANDC__
18665 _RTLENTRYF
18666#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018667 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018668static int
18669#ifdef __BORLANDC__
18670 _RTLENTRYF
18671#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018672 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018673
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018674/* struct used in the array that's given to qsort() */
18675typedef struct
18676{
18677 listitem_T *item;
18678 int idx;
18679} sortItem_T;
18680
Bram Moolenaar0b962472016-02-22 22:51:33 +010018681/* struct storing information about current sort */
18682typedef struct
18683{
18684 int item_compare_ic;
18685 int item_compare_numeric;
18686 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018687#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018688 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018689#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018690 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018691 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018692 dict_T *item_compare_selfdict;
18693 int item_compare_func_err;
18694 int item_compare_keep_zero;
18695} sortinfo_T;
18696static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018697static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018698#define ITEM_COMPARE_FAIL 999
18699
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018701 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018703 static int
18704#ifdef __BORLANDC__
18705_RTLENTRYF
18706#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018707item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018709 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018710 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018711 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018712 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018713 int res;
18714 char_u numbuf1[NUMBUFLEN];
18715 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018717 si1 = (sortItem_T *)s1;
18718 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018719 tv1 = &si1->item->li_tv;
18720 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018721
Bram Moolenaar0b962472016-02-22 22:51:33 +010018722 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018723 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018724 varnumber_T v1 = get_tv_number(tv1);
18725 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018726
18727 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18728 }
18729
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018730#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018731 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018732 {
18733 float_T v1 = get_tv_float(tv1);
18734 float_T v2 = get_tv_float(tv2);
18735
18736 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18737 }
18738#endif
18739
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018740 /* tv2string() puts quotes around a string and allocates memory. Don't do
18741 * that for string variables. Use a single quote when comparing with a
18742 * non-string to do what the docs promise. */
18743 if (tv1->v_type == VAR_STRING)
18744 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018745 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018746 p1 = (char_u *)"'";
18747 else
18748 p1 = tv1->vval.v_string;
18749 }
18750 else
18751 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18752 if (tv2->v_type == VAR_STRING)
18753 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018754 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018755 p2 = (char_u *)"'";
18756 else
18757 p2 = tv2->vval.v_string;
18758 }
18759 else
18760 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018761 if (p1 == NULL)
18762 p1 = (char_u *)"";
18763 if (p2 == NULL)
18764 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018765 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018766 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018767 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018768 res = STRICMP(p1, p2);
18769 else
18770 res = STRCMP(p1, p2);
18771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018773 {
18774 double n1, n2;
18775 n1 = strtod((char *)p1, (char **)&p1);
18776 n2 = strtod((char *)p2, (char **)&p2);
18777 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18778 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018779
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018780 /* When the result would be zero, compare the item indexes. Makes the
18781 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018782 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018783 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018784
Bram Moolenaar0d660222005-01-07 21:51:51 +000018785 vim_free(tofree1);
18786 vim_free(tofree2);
18787 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788}
18789
18790 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018791#ifdef __BORLANDC__
18792_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018794item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018795{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018796 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018797 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018798 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018799 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018800 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018801 char_u *func_name;
18802 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018804 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018805 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018806 return 0;
18807
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018808 si1 = (sortItem_T *)s1;
18809 si2 = (sortItem_T *)s2;
18810
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018811 if (partial == NULL)
18812 func_name = sortinfo->item_compare_func;
18813 else
18814 func_name = partial->pt_name;
18815
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018816 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018817 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018818 copy_tv(&si1->item->li_tv, &argv[0]);
18819 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018820
18821 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018822 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018823 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018824 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018825 clear_tv(&argv[0]);
18826 clear_tv(&argv[1]);
18827
18828 if (res == FAIL)
18829 res = ITEM_COMPARE_FAIL;
18830 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018831 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010018832 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018833 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018834 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018835
18836 /* When the result would be zero, compare the pointers themselves. Makes
18837 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018838 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018839 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018840
Bram Moolenaar0d660222005-01-07 21:51:51 +000018841 return res;
18842}
18843
18844/*
18845 * "sort({list})" function
18846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018848do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849{
Bram Moolenaar33570922005-01-25 22:26:29 +000018850 list_T *l;
18851 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018852 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018853 sortinfo_T *old_sortinfo;
18854 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018855 long len;
18856 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857
Bram Moolenaar0b962472016-02-22 22:51:33 +010018858 /* Pointer to current info struct used in compare function. Save and
18859 * restore the current one for nested calls. */
18860 old_sortinfo = sortinfo;
18861 sortinfo = &info;
18862
Bram Moolenaar0d660222005-01-07 21:51:51 +000018863 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018864 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 else
18866 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018867 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018868 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018869 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18870 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018871 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018872 rettv->vval.v_list = l;
18873 rettv->v_type = VAR_LIST;
18874 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875
Bram Moolenaar0d660222005-01-07 21:51:51 +000018876 len = list_len(l);
18877 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018878 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879
Bram Moolenaar0b962472016-02-22 22:51:33 +010018880 info.item_compare_ic = FALSE;
18881 info.item_compare_numeric = FALSE;
18882 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018883#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018884 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018885#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018886 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018887 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018888 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018889 if (argvars[1].v_type != VAR_UNKNOWN)
18890 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018891 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018892 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018893 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018894 else if (argvars[1].v_type == VAR_PARTIAL)
18895 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018896 else
18897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018898 int error = FALSE;
18899
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018900 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018901 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018902 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018903 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018904 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018905 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018906 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018907 else if (i != 0)
18908 {
18909 EMSG(_(e_invarg));
18910 goto theend;
18911 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018912 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018913 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018914 if (*info.item_compare_func == NUL)
18915 {
18916 /* empty string means default sort */
18917 info.item_compare_func = NULL;
18918 }
18919 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018920 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018921 info.item_compare_func = NULL;
18922 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018923 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018924 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018925 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018926 info.item_compare_func = NULL;
18927 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018928 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018929#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018930 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018931 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018932 info.item_compare_func = NULL;
18933 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018934 }
18935#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018936 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018937 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018938 info.item_compare_func = NULL;
18939 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018940 }
18941 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018942 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018943
18944 if (argvars[2].v_type != VAR_UNKNOWN)
18945 {
18946 /* optional third argument: {dict} */
18947 if (argvars[2].v_type != VAR_DICT)
18948 {
18949 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018950 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018951 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018952 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018953 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955
Bram Moolenaar0d660222005-01-07 21:51:51 +000018956 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018957 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018958 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018959 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960
Bram Moolenaar327aa022014-03-25 18:24:23 +010018961 i = 0;
18962 if (sort)
18963 {
18964 /* sort(): ptrs will be the list to sort */
18965 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018966 {
18967 ptrs[i].item = li;
18968 ptrs[i].idx = i;
18969 ++i;
18970 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018971
Bram Moolenaar0b962472016-02-22 22:51:33 +010018972 info.item_compare_func_err = FALSE;
18973 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018974 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018975 if ((info.item_compare_func != NULL
18976 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018977 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018978 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018979 EMSG(_("E702: Sort compare function failed"));
18980 else
18981 {
18982 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018983 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018984 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018985 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018986 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018987
Bram Moolenaar0b962472016-02-22 22:51:33 +010018988 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018989 {
18990 /* Clear the List and append the items in sorted order. */
18991 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18992 l->lv_len = 0;
18993 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018994 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018995 }
18996 }
18997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019000 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019001
19002 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019003 info.item_compare_func_err = FALSE;
19004 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019005 item_compare_func_ptr = info.item_compare_func != NULL
19006 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019007 ? item_compare2 : item_compare;
19008
19009 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19010 li = li->li_next)
19011 {
19012 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19013 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019014 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019015 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019016 {
19017 EMSG(_("E882: Uniq compare function failed"));
19018 break;
19019 }
19020 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019021
Bram Moolenaar0b962472016-02-22 22:51:33 +010019022 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019023 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019024 while (--i >= 0)
19025 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019026 li = ptrs[i].item->li_next;
19027 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019028 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019029 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019030 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019031 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019032 list_fix_watch(l, li);
19033 listitem_free(li);
19034 l->lv_len--;
19035 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019036 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019037 }
19038
19039 vim_free(ptrs);
19040 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019041theend:
19042 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019043}
19044
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019045/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019046 * "sort({list})" function
19047 */
19048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019049f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019050{
19051 do_sort_uniq(argvars, rettv, TRUE);
19052}
19053
19054/*
19055 * "uniq({list})" function
19056 */
19057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019058f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019059{
19060 do_sort_uniq(argvars, rettv, FALSE);
19061}
19062
19063/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019064 * "soundfold({word})" function
19065 */
19066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019067f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019068{
19069 char_u *s;
19070
19071 rettv->v_type = VAR_STRING;
19072 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019073#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019074 rettv->vval.v_string = eval_soundfold(s);
19075#else
19076 rettv->vval.v_string = vim_strsave(s);
19077#endif
19078}
19079
19080/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019081 * "spellbadword()" function
19082 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019084f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019085{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019086 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019087 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019088 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019089
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019090 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019091 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019092
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019093#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019094 if (argvars[0].v_type == VAR_UNKNOWN)
19095 {
19096 /* Find the start and length of the badly spelled word. */
19097 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19098 if (len != 0)
19099 word = ml_get_cursor();
19100 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019101 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019102 {
19103 char_u *str = get_tv_string_chk(&argvars[0]);
19104 int capcol = -1;
19105
19106 if (str != NULL)
19107 {
19108 /* Check the argument for spelling. */
19109 while (*str != NUL)
19110 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019111 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019112 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019113 {
19114 word = str;
19115 break;
19116 }
19117 str += len;
19118 }
19119 }
19120 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019121#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019122
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019123 list_append_string(rettv->vval.v_list, word, len);
19124 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019125 attr == HLF_SPB ? "bad" :
19126 attr == HLF_SPR ? "rare" :
19127 attr == HLF_SPL ? "local" :
19128 attr == HLF_SPC ? "caps" :
19129 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019130}
19131
19132/*
19133 * "spellsuggest()" function
19134 */
19135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019136f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019137{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019138#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019139 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019140 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019141 int maxcount;
19142 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019143 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019144 listitem_T *li;
19145 int need_capital = FALSE;
19146#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019147
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019148 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019149 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019150
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019151#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019152 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019153 {
19154 str = get_tv_string(&argvars[0]);
19155 if (argvars[1].v_type != VAR_UNKNOWN)
19156 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019157 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019158 if (maxcount <= 0)
19159 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019160 if (argvars[2].v_type != VAR_UNKNOWN)
19161 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019162 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019163 if (typeerr)
19164 return;
19165 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019166 }
19167 else
19168 maxcount = 25;
19169
Bram Moolenaar4770d092006-01-12 23:22:24 +000019170 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019171
19172 for (i = 0; i < ga.ga_len; ++i)
19173 {
19174 str = ((char_u **)ga.ga_data)[i];
19175
19176 li = listitem_alloc();
19177 if (li == NULL)
19178 vim_free(str);
19179 else
19180 {
19181 li->li_tv.v_type = VAR_STRING;
19182 li->li_tv.v_lock = 0;
19183 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019184 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019185 }
19186 }
19187 ga_clear(&ga);
19188 }
19189#endif
19190}
19191
Bram Moolenaar0d660222005-01-07 21:51:51 +000019192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019193f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019194{
19195 char_u *str;
19196 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019197 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019198 regmatch_T regmatch;
19199 char_u patbuf[NUMBUFLEN];
19200 char_u *save_cpo;
19201 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019202 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019203 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019204 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019205
19206 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19207 save_cpo = p_cpo;
19208 p_cpo = (char_u *)"";
19209
19210 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019211 if (argvars[1].v_type != VAR_UNKNOWN)
19212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019213 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19214 if (pat == NULL)
19215 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019216 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019217 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019218 }
19219 if (pat == NULL || *pat == NUL)
19220 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019221
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019222 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019224 if (typeerr)
19225 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226
Bram Moolenaar0d660222005-01-07 21:51:51 +000019227 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19228 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019230 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019231 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019232 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019233 if (*str == NUL)
19234 match = FALSE; /* empty item at the end */
19235 else
19236 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019237 if (match)
19238 end = regmatch.startp[0];
19239 else
19240 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019241 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19242 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019243 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019244 if (list_append_string(rettv->vval.v_list, str,
19245 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019246 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019247 }
19248 if (!match)
19249 break;
19250 /* Advance to just after the match. */
19251 if (regmatch.endp[0] > str)
19252 col = 0;
19253 else
19254 {
19255 /* Don't get stuck at the same match. */
19256#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019257 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019258#else
19259 col = 1;
19260#endif
19261 }
19262 str = regmatch.endp[0];
19263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264
Bram Moolenaar473de612013-06-08 18:19:48 +020019265 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267
Bram Moolenaar0d660222005-01-07 21:51:51 +000019268 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269}
19270
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019271#ifdef FEAT_FLOAT
19272/*
19273 * "sqrt()" function
19274 */
19275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019276f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019277{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019278 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019279
19280 rettv->v_type = VAR_FLOAT;
19281 if (get_float_arg(argvars, &f) == OK)
19282 rettv->vval.v_float = sqrt(f);
19283 else
19284 rettv->vval.v_float = 0.0;
19285}
19286
19287/*
19288 * "str2float()" function
19289 */
19290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019291f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019292{
19293 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19294
19295 if (*p == '+')
19296 p = skipwhite(p + 1);
19297 (void)string2float(p, &rettv->vval.v_float);
19298 rettv->v_type = VAR_FLOAT;
19299}
19300#endif
19301
Bram Moolenaar2c932302006-03-18 21:42:09 +000019302/*
19303 * "str2nr()" function
19304 */
19305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019306f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019307{
19308 int base = 10;
19309 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019310 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019311 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019312
19313 if (argvars[1].v_type != VAR_UNKNOWN)
19314 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019315 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019316 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019317 {
19318 EMSG(_(e_invarg));
19319 return;
19320 }
19321 }
19322
19323 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019324 if (*p == '+')
19325 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019326 switch (base)
19327 {
19328 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19329 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19330 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19331 default: what = 0;
19332 }
19333 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019334 rettv->vval.v_number = n;
19335}
19336
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337#ifdef HAVE_STRFTIME
19338/*
19339 * "strftime({format}[, {time}])" function
19340 */
19341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019342f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343{
19344 char_u result_buf[256];
19345 struct tm *curtime;
19346 time_t seconds;
19347 char_u *p;
19348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019349 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019351 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019352 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 seconds = time(NULL);
19354 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019355 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356 curtime = localtime(&seconds);
19357 /* MSVC returns NULL for an invalid value of seconds. */
19358 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019359 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 else
19361 {
19362# ifdef FEAT_MBYTE
19363 vimconv_T conv;
19364 char_u *enc;
19365
19366 conv.vc_type = CONV_NONE;
19367 enc = enc_locale();
19368 convert_setup(&conv, p_enc, enc);
19369 if (conv.vc_type != CONV_NONE)
19370 p = string_convert(&conv, p, NULL);
19371# endif
19372 if (p != NULL)
19373 (void)strftime((char *)result_buf, sizeof(result_buf),
19374 (char *)p, curtime);
19375 else
19376 result_buf[0] = NUL;
19377
19378# ifdef FEAT_MBYTE
19379 if (conv.vc_type != CONV_NONE)
19380 vim_free(p);
19381 convert_setup(&conv, enc, p_enc);
19382 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019383 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 else
19385# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019386 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387
19388# ifdef FEAT_MBYTE
19389 /* Release conversion descriptors */
19390 convert_setup(&conv, NULL, NULL);
19391 vim_free(enc);
19392# endif
19393 }
19394}
19395#endif
19396
19397/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019398 * "strgetchar()" function
19399 */
19400 static void
19401f_strgetchar(typval_T *argvars, typval_T *rettv)
19402{
19403 char_u *str;
19404 int len;
19405 int error = FALSE;
19406 int charidx;
19407
19408 rettv->vval.v_number = -1;
19409 str = get_tv_string_chk(&argvars[0]);
19410 if (str == NULL)
19411 return;
19412 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019413 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019414 if (error)
19415 return;
19416#ifdef FEAT_MBYTE
19417 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019418 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019419
19420 while (charidx >= 0 && byteidx < len)
19421 {
19422 if (charidx == 0)
19423 {
19424 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19425 break;
19426 }
19427 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019428 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019429 }
19430 }
19431#else
19432 if (charidx < len)
19433 rettv->vval.v_number = str[charidx];
19434#endif
19435}
19436
19437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 * "stridx()" function
19439 */
19440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019441f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442{
19443 char_u buf[NUMBUFLEN];
19444 char_u *needle;
19445 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019446 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019448 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019450 needle = get_tv_string_chk(&argvars[1]);
19451 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019452 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019453 if (needle == NULL || haystack == NULL)
19454 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455
Bram Moolenaar33570922005-01-25 22:26:29 +000019456 if (argvars[2].v_type != VAR_UNKNOWN)
19457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019458 int error = FALSE;
19459
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019460 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019461 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019462 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019463 if (start_idx >= 0)
19464 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019465 }
19466
19467 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19468 if (pos != NULL)
19469 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470}
19471
19472/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019473 * "string()" function
19474 */
19475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019476f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019477{
19478 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019479 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019482 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19483 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019484 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019485 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019486 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487}
19488
19489/*
19490 * "strlen()" function
19491 */
19492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019493f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019495 rettv->vval.v_number = (varnumber_T)(STRLEN(
19496 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497}
19498
19499/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019500 * "strchars()" function
19501 */
19502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019503f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019504{
19505 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019506 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019507#ifdef FEAT_MBYTE
19508 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019509 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019510#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019511
19512 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019513 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019514 if (skipcc < 0 || skipcc > 1)
19515 EMSG(_(e_invarg));
19516 else
19517 {
19518#ifdef FEAT_MBYTE
19519 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19520 while (*s != NUL)
19521 {
19522 func_mb_ptr2char_adv(&s);
19523 ++len;
19524 }
19525 rettv->vval.v_number = len;
19526#else
19527 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19528#endif
19529 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019530}
19531
19532/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019533 * "strdisplaywidth()" function
19534 */
19535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019536f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019537{
19538 char_u *s = get_tv_string(&argvars[0]);
19539 int col = 0;
19540
19541 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019542 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020019543
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019544 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019545}
19546
19547/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019548 * "strwidth()" function
19549 */
19550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019551f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019552{
19553 char_u *s = get_tv_string(&argvars[0]);
19554
19555 rettv->vval.v_number = (varnumber_T)(
19556#ifdef FEAT_MBYTE
19557 mb_string2cells(s, -1)
19558#else
19559 STRLEN(s)
19560#endif
19561 );
19562}
19563
19564/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019565 * "strcharpart()" function
19566 */
19567 static void
19568f_strcharpart(typval_T *argvars, typval_T *rettv)
19569{
19570#ifdef FEAT_MBYTE
19571 char_u *p;
19572 int nchar;
19573 int nbyte = 0;
19574 int charlen;
19575 int len = 0;
19576 int slen;
19577 int error = FALSE;
19578
19579 p = get_tv_string(&argvars[0]);
19580 slen = (int)STRLEN(p);
19581
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019582 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019583 if (!error)
19584 {
19585 if (nchar > 0)
19586 while (nchar > 0 && nbyte < slen)
19587 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019588 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019589 --nchar;
19590 }
19591 else
19592 nbyte = nchar;
19593 if (argvars[2].v_type != VAR_UNKNOWN)
19594 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019595 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019596 while (charlen > 0 && nbyte + len < slen)
19597 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019598 int off = nbyte + len;
19599
19600 if (off < 0)
19601 len += 1;
19602 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019603 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019604 --charlen;
19605 }
19606 }
19607 else
19608 len = slen - nbyte; /* default: all bytes that are available. */
19609 }
19610
19611 /*
19612 * Only return the overlap between the specified part and the actual
19613 * string.
19614 */
19615 if (nbyte < 0)
19616 {
19617 len += nbyte;
19618 nbyte = 0;
19619 }
19620 else if (nbyte > slen)
19621 nbyte = slen;
19622 if (len < 0)
19623 len = 0;
19624 else if (nbyte + len > slen)
19625 len = slen - nbyte;
19626
19627 rettv->v_type = VAR_STRING;
19628 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19629#else
19630 f_strpart(argvars, rettv);
19631#endif
19632}
19633
19634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 * "strpart()" function
19636 */
19637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019638f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639{
19640 char_u *p;
19641 int n;
19642 int len;
19643 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019644 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019646 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 slen = (int)STRLEN(p);
19648
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019649 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019650 if (error)
19651 len = 0;
19652 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019653 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 else
19655 len = slen - n; /* default len: all bytes that are available. */
19656
19657 /*
19658 * Only return the overlap between the specified part and the actual
19659 * string.
19660 */
19661 if (n < 0)
19662 {
19663 len += n;
19664 n = 0;
19665 }
19666 else if (n > slen)
19667 n = slen;
19668 if (len < 0)
19669 len = 0;
19670 else if (n + len > slen)
19671 len = slen - n;
19672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019673 rettv->v_type = VAR_STRING;
19674 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675}
19676
19677/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019678 * "strridx()" function
19679 */
19680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019681f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019682{
19683 char_u buf[NUMBUFLEN];
19684 char_u *needle;
19685 char_u *haystack;
19686 char_u *rest;
19687 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019688 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019690 needle = get_tv_string_chk(&argvars[1]);
19691 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019692
19693 rettv->vval.v_number = -1;
19694 if (needle == NULL || haystack == NULL)
19695 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019696
19697 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019698 if (argvars[2].v_type != VAR_UNKNOWN)
19699 {
19700 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019701 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019702 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019703 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019704 }
19705 else
19706 end_idx = haystack_len;
19707
Bram Moolenaar0d660222005-01-07 21:51:51 +000019708 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019709 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019710 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019711 lastmatch = haystack + end_idx;
19712 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019713 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019714 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019715 for (rest = haystack; *rest != '\0'; ++rest)
19716 {
19717 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019718 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019719 break;
19720 lastmatch = rest;
19721 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019722 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019723
19724 if (lastmatch == NULL)
19725 rettv->vval.v_number = -1;
19726 else
19727 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19728}
19729
19730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 * "strtrans()" function
19732 */
19733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019734f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019736 rettv->v_type = VAR_STRING;
19737 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738}
19739
19740/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019741 * "submatch()" function
19742 */
19743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019744f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019745{
Bram Moolenaar41571762014-04-02 19:00:58 +020019746 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019747 int no;
19748 int retList = 0;
19749
19750 no = (int)get_tv_number_chk(&argvars[0], &error);
19751 if (error)
19752 return;
19753 error = FALSE;
19754 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019755 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020019756 if (error)
19757 return;
19758
19759 if (retList == 0)
19760 {
19761 rettv->v_type = VAR_STRING;
19762 rettv->vval.v_string = reg_submatch(no);
19763 }
19764 else
19765 {
19766 rettv->v_type = VAR_LIST;
19767 rettv->vval.v_list = reg_submatch_list(no);
19768 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019769}
19770
19771/*
19772 * "substitute()" function
19773 */
19774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019775f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019776{
19777 char_u patbuf[NUMBUFLEN];
19778 char_u subbuf[NUMBUFLEN];
19779 char_u flagsbuf[NUMBUFLEN];
19780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019781 char_u *str = get_tv_string_chk(&argvars[0]);
19782 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19783 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19784 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19785
Bram Moolenaar0d660222005-01-07 21:51:51 +000019786 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019787 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19788 rettv->vval.v_string = NULL;
19789 else
19790 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019791}
19792
19793/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019794 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019797f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798{
19799 int id = 0;
19800#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019801 linenr_T lnum;
19802 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019804 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019806 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019807 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19808 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019810 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019811 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019812 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813#endif
19814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019815 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816}
19817
19818/*
19819 * "synIDattr(id, what [, mode])" function
19820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019822f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823{
19824 char_u *p = NULL;
19825#ifdef FEAT_SYN_HL
19826 int id;
19827 char_u *what;
19828 char_u *mode;
19829 char_u modebuf[NUMBUFLEN];
19830 int modec;
19831
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019832 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019833 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019834 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019838 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 modec = 0; /* replace invalid with current */
19840 }
19841 else
19842 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020019843#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020019844 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 modec = 'g';
19846 else
19847#endif
19848 if (t_colors > 1)
19849 modec = 'c';
19850 else
19851 modec = 't';
19852 }
19853
19854
19855 switch (TOLOWER_ASC(what[0]))
19856 {
19857 case 'b':
19858 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19859 p = highlight_color(id, what, modec);
19860 else /* bold */
19861 p = highlight_has_attr(id, HL_BOLD, modec);
19862 break;
19863
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019864 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 p = highlight_color(id, what, modec);
19866 break;
19867
19868 case 'i':
19869 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19870 p = highlight_has_attr(id, HL_INVERSE, modec);
19871 else /* italic */
19872 p = highlight_has_attr(id, HL_ITALIC, modec);
19873 break;
19874
19875 case 'n': /* name */
19876 p = get_highlight_name(NULL, id - 1);
19877 break;
19878
19879 case 'r': /* reverse */
19880 p = highlight_has_attr(id, HL_INVERSE, modec);
19881 break;
19882
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019883 case 's':
19884 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19885 p = highlight_color(id, what, modec);
19886 else /* standout */
19887 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888 break;
19889
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019890 case 'u':
19891 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19892 /* underline */
19893 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19894 else
19895 /* undercurl */
19896 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 break;
19898 }
19899
19900 if (p != NULL)
19901 p = vim_strsave(p);
19902#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019903 rettv->v_type = VAR_STRING;
19904 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905}
19906
19907/*
19908 * "synIDtrans(id)" function
19909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019911f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912{
19913 int id;
19914
19915#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019916 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917
19918 if (id > 0)
19919 id = syn_get_final_id(id);
19920 else
19921#endif
19922 id = 0;
19923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019924 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925}
19926
19927/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019928 * "synconcealed(lnum, col)" function
19929 */
19930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019931f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019932{
19933#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019934 linenr_T lnum;
19935 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019936 int syntax_flags = 0;
19937 int cchar;
19938 int matchid = 0;
19939 char_u str[NUMBUFLEN];
19940#endif
19941
19942 rettv->v_type = VAR_LIST;
19943 rettv->vval.v_list = NULL;
19944
19945#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19946 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019947 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019948
19949 vim_memset(str, NUL, sizeof(str));
19950
19951 if (rettv_list_alloc(rettv) != FAIL)
19952 {
19953 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19954 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19955 && curwin->w_p_cole > 0)
19956 {
19957 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19958 syntax_flags = get_syntax_info(&matchid);
19959
19960 /* get the conceal character */
19961 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19962 {
19963 cchar = syn_get_sub_char();
19964 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19965 cchar = lcs_conceal;
19966 if (cchar != NUL)
19967 {
19968# ifdef FEAT_MBYTE
19969 if (has_mbyte)
19970 (*mb_char2bytes)(cchar, str);
19971 else
19972# endif
19973 str[0] = cchar;
19974 }
19975 }
19976 }
19977
19978 list_append_number(rettv->vval.v_list,
19979 (syntax_flags & HL_CONCEAL) != 0);
19980 /* -1 to auto-determine strlen */
19981 list_append_string(rettv->vval.v_list, str, -1);
19982 list_append_number(rettv->vval.v_list, matchid);
19983 }
19984#endif
19985}
19986
19987/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019988 * "synstack(lnum, col)" function
19989 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019991f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019992{
19993#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019994 linenr_T lnum;
19995 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019996 int i;
19997 int id;
19998#endif
19999
20000 rettv->v_type = VAR_LIST;
20001 rettv->vval.v_list = NULL;
20002
20003#ifdef FEAT_SYN_HL
20004 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020005 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020006
20007 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020008 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020009 && rettv_list_alloc(rettv) != FAIL)
20010 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020011 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020012 for (i = 0; ; ++i)
20013 {
20014 id = syn_get_stack_item(i);
20015 if (id < 0)
20016 break;
20017 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20018 break;
20019 }
20020 }
20021#endif
20022}
20023
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020025get_cmd_output_as_rettv(
20026 typval_T *argvars,
20027 typval_T *rettv,
20028 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020030 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020032 char_u *infile = NULL;
20033 char_u buf[NUMBUFLEN];
20034 int err = FALSE;
20035 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020036 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020037 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020039 rettv->v_type = VAR_STRING;
20040 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020041 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020042 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020043
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020044 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020045 {
20046 /*
20047 * Write the string to a temp file, to be used for input of the shell
20048 * command.
20049 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020050 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020051 {
20052 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020053 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020054 }
20055
20056 fd = mch_fopen((char *)infile, WRITEBIN);
20057 if (fd == NULL)
20058 {
20059 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020060 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020061 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020062 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020063 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020064 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20065 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020066 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020067 else
20068 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020069 size_t len;
20070
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020071 p = get_tv_string_buf_chk(&argvars[1], buf);
20072 if (p == NULL)
20073 {
20074 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020075 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020076 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020077 len = STRLEN(p);
20078 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020079 err = TRUE;
20080 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020081 if (fclose(fd) != 0)
20082 err = TRUE;
20083 if (err)
20084 {
20085 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020086 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020087 }
20088 }
20089
Bram Moolenaar52a72462014-08-29 15:53:52 +020020090 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20091 * echoes typeahead, that messes up the display. */
20092 if (!msg_silent)
20093 flags += SHELL_COOKED;
20094
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020095 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020097 int len;
20098 listitem_T *li;
20099 char_u *s = NULL;
20100 char_u *start;
20101 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020102 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103
Bram Moolenaar52a72462014-08-29 15:53:52 +020020104 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020105 if (res == NULL)
20106 goto errret;
20107
20108 list = list_alloc();
20109 if (list == NULL)
20110 goto errret;
20111
20112 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020114 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020115 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020116 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020117 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020118
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020119 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020120 if (s == NULL)
20121 goto errret;
20122
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020123 for (p = s; start < end; ++p, ++start)
20124 *p = *start == NUL ? NL : *start;
20125 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020126
20127 li = listitem_alloc();
20128 if (li == NULL)
20129 {
20130 vim_free(s);
20131 goto errret;
20132 }
20133 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020134 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020135 li->li_tv.vval.v_string = s;
20136 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020138
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020139 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020140 rettv->v_type = VAR_LIST;
20141 rettv->vval.v_list = list;
20142 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020144 else
20145 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020146 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020147#ifdef USE_CR
20148 /* translate <CR> into <NL> */
20149 if (res != NULL)
20150 {
20151 char_u *s;
20152
20153 for (s = res; *s; ++s)
20154 {
20155 if (*s == CAR)
20156 *s = NL;
20157 }
20158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159#else
20160# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020161 /* translate <CR><NL> into <NL> */
20162 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020164 char_u *s, *d;
20165
20166 d = res;
20167 for (s = res; *s; ++s)
20168 {
20169 if (s[0] == CAR && s[1] == NL)
20170 ++s;
20171 *d++ = *s;
20172 }
20173 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175# endif
20176#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020177 rettv->vval.v_string = res;
20178 res = NULL;
20179 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020180
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020181errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020182 if (infile != NULL)
20183 {
20184 mch_remove(infile);
20185 vim_free(infile);
20186 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020187 if (res != NULL)
20188 vim_free(res);
20189 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020190 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020191}
20192
20193/*
20194 * "system()" function
20195 */
20196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020197f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020198{
20199 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20200}
20201
20202/*
20203 * "systemlist()" function
20204 */
20205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020206f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020207{
20208 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209}
20210
20211/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020212 * "tabpagebuflist()" function
20213 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020215f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020216{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020217#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020218 tabpage_T *tp;
20219 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020220
20221 if (argvars[0].v_type == VAR_UNKNOWN)
20222 wp = firstwin;
20223 else
20224 {
20225 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20226 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020227 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020228 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020229 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020230 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020231 for (; wp != NULL; wp = wp->w_next)
20232 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020233 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020234 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020235 }
20236#endif
20237}
20238
20239
20240/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020241 * "tabpagenr()" function
20242 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020244f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020245{
20246 int nr = 1;
20247#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020248 char_u *arg;
20249
20250 if (argvars[0].v_type != VAR_UNKNOWN)
20251 {
20252 arg = get_tv_string_chk(&argvars[0]);
20253 nr = 0;
20254 if (arg != NULL)
20255 {
20256 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020257 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020258 else
20259 EMSG2(_(e_invexpr2), arg);
20260 }
20261 }
20262 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020263 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020264#endif
20265 rettv->vval.v_number = nr;
20266}
20267
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020268
20269#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020270static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020271
20272/*
20273 * Common code for tabpagewinnr() and winnr().
20274 */
20275 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020276get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020277{
20278 win_T *twin;
20279 int nr = 1;
20280 win_T *wp;
20281 char_u *arg;
20282
20283 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20284 if (argvar->v_type != VAR_UNKNOWN)
20285 {
20286 arg = get_tv_string_chk(argvar);
20287 if (arg == NULL)
20288 nr = 0; /* type error; errmsg already given */
20289 else if (STRCMP(arg, "$") == 0)
20290 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20291 else if (STRCMP(arg, "#") == 0)
20292 {
20293 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20294 if (twin == NULL)
20295 nr = 0;
20296 }
20297 else
20298 {
20299 EMSG2(_(e_invexpr2), arg);
20300 nr = 0;
20301 }
20302 }
20303
20304 if (nr > 0)
20305 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20306 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020307 {
20308 if (wp == NULL)
20309 {
20310 /* didn't find it in this tabpage */
20311 nr = 0;
20312 break;
20313 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020314 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020315 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020316 return nr;
20317}
20318#endif
20319
20320/*
20321 * "tabpagewinnr()" function
20322 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020324f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020325{
20326 int nr = 1;
20327#ifdef FEAT_WINDOWS
20328 tabpage_T *tp;
20329
20330 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20331 if (tp == NULL)
20332 nr = 0;
20333 else
20334 nr = get_winnr(tp, &argvars[1]);
20335#endif
20336 rettv->vval.v_number = nr;
20337}
20338
20339
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020340/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020341 * "tagfiles()" function
20342 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020344f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020345{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020346 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020347 tagname_T tn;
20348 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020349
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020350 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020351 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020352 fname = alloc(MAXPATHL);
20353 if (fname == NULL)
20354 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020355
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020356 for (first = TRUE; ; first = FALSE)
20357 if (get_tagfname(&tn, first, fname) == FAIL
20358 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020359 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020360 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020361 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020362}
20363
20364/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020365 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020366 */
20367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020368f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020369{
20370 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020371
20372 tag_pattern = get_tv_string(&argvars[0]);
20373
20374 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020375 if (*tag_pattern == NUL)
20376 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020377
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020378 if (rettv_list_alloc(rettv) == OK)
20379 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020380}
20381
20382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 * "tempname()" function
20384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020386f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387{
20388 static int x = 'A';
20389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020390 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020391 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392
20393 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20394 * names. Skip 'I' and 'O', they are used for shell redirection. */
20395 do
20396 {
20397 if (x == 'Z')
20398 x = '0';
20399 else if (x == '9')
20400 x = 'A';
20401 else
20402 {
20403#ifdef EBCDIC
20404 if (x == 'I')
20405 x = 'J';
20406 else if (x == 'R')
20407 x = 'S';
20408 else
20409#endif
20410 ++x;
20411 }
20412 } while (x == 'I' || x == 'O');
20413}
20414
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020415#ifdef FEAT_FLOAT
20416/*
20417 * "tan()" function
20418 */
20419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020420f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020421{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020422 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020423
20424 rettv->v_type = VAR_FLOAT;
20425 if (get_float_arg(argvars, &f) == OK)
20426 rettv->vval.v_float = tan(f);
20427 else
20428 rettv->vval.v_float = 0.0;
20429}
20430
20431/*
20432 * "tanh()" function
20433 */
20434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020435f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020436{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020437 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020438
20439 rettv->v_type = VAR_FLOAT;
20440 if (get_float_arg(argvars, &f) == OK)
20441 rettv->vval.v_float = tanh(f);
20442 else
20443 rettv->vval.v_float = 0.0;
20444}
20445#endif
20446
Bram Moolenaar574860b2016-05-24 17:33:34 +020020447/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020448 * "test_alloc_fail(id, countdown, repeat)" function
20449 */
20450 static void
20451f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20452{
20453 if (argvars[0].v_type != VAR_NUMBER
20454 || argvars[0].vval.v_number <= 0
20455 || argvars[1].v_type != VAR_NUMBER
20456 || argvars[1].vval.v_number < 0
20457 || argvars[2].v_type != VAR_NUMBER)
20458 EMSG(_(e_invarg));
20459 else
20460 {
20461 alloc_fail_id = argvars[0].vval.v_number;
20462 if (alloc_fail_id >= aid_last)
20463 EMSG(_(e_invarg));
20464 alloc_fail_countdown = argvars[1].vval.v_number;
20465 alloc_fail_repeat = argvars[2].vval.v_number;
20466 did_outofmem_msg = FALSE;
20467 }
20468}
20469
20470/*
Bram Moolenaar5c719942016-07-09 23:40:45 +020020471 * "test_autochdir()"
20472 */
20473 static void
20474f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20475{
20476#if defined(FEAT_AUTOCHDIR)
20477 test_autochdir = TRUE;
20478#endif
20479}
20480
20481/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020482 * "test_disable_char_avail({expr})" function
20483 */
20484 static void
20485f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20486{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020487 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020488}
20489
20490/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020491 * "test_garbagecollect_now()" function
20492 */
20493 static void
20494f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20495{
20496 /* This is dangerous, any Lists and Dicts used internally may be freed
20497 * while still in use. */
20498 garbage_collect(TRUE);
20499}
20500
20501#ifdef FEAT_JOB_CHANNEL
20502 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020503f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020504{
20505 rettv->v_type = VAR_CHANNEL;
20506 rettv->vval.v_channel = NULL;
20507}
20508#endif
20509
20510 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020511f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020512{
20513 rettv->v_type = VAR_DICT;
20514 rettv->vval.v_dict = NULL;
20515}
20516
20517#ifdef FEAT_JOB_CHANNEL
20518 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020519f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020520{
20521 rettv->v_type = VAR_JOB;
20522 rettv->vval.v_job = NULL;
20523}
20524#endif
20525
20526 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020527f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020528{
20529 rettv->v_type = VAR_LIST;
20530 rettv->vval.v_list = NULL;
20531}
20532
20533 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020534f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020535{
20536 rettv->v_type = VAR_PARTIAL;
20537 rettv->vval.v_partial = NULL;
20538}
20539
20540 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020541f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020542{
20543 rettv->v_type = VAR_STRING;
20544 rettv->vval.v_string = NULL;
20545}
20546
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020547 static void
20548f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
20549{
20550 time_for_testing = (time_t)get_tv_number(&argvars[0]);
20551}
20552
Bram Moolenaar975b5272016-03-15 23:10:59 +010020553#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20554/*
20555 * Get a callback from "arg". It can be a Funcref or a function name.
20556 * When "arg" is zero return an empty string.
20557 * Return NULL for an invalid argument.
20558 */
20559 char_u *
20560get_callback(typval_T *arg, partial_T **pp)
20561{
20562 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20563 {
20564 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020565 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020566 return (*pp)->pt_name;
20567 }
20568 *pp = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +020020569 if (arg->v_type == VAR_FUNC)
20570 {
20571 func_ref(arg->vval.v_string);
20572 return arg->vval.v_string;
20573 }
20574 if (arg->v_type == VAR_STRING)
Bram Moolenaar975b5272016-03-15 23:10:59 +010020575 return arg->vval.v_string;
20576 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20577 return (char_u *)"";
20578 EMSG(_("E921: Invalid callback argument"));
20579 return NULL;
20580}
Bram Moolenaar1436d8d2016-07-11 22:41:15 +020020581
20582/*
20583 * Unref/free "callback" and "partial" retured by get_callback().
20584 */
20585 void
20586free_callback(char_u *callback, partial_T *partial)
20587{
20588 if (partial != NULL)
20589 partial_unref(partial);
20590 else if (callback != NULL)
20591 {
20592 func_unref(callback);
20593 vim_free(callback);
20594 }
20595}
Bram Moolenaar975b5272016-03-15 23:10:59 +010020596#endif
20597
20598#ifdef FEAT_TIMERS
20599/*
20600 * "timer_start(time, callback [, options])" function
20601 */
20602 static void
20603f_timer_start(typval_T *argvars, typval_T *rettv)
20604{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020605 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010020606 timer_T *timer;
20607 int repeat = 0;
20608 char_u *callback;
20609 dict_T *dict;
20610
Bram Moolenaar38499922016-04-22 20:46:52 +020020611 if (check_secure())
20612 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020613 if (argvars[2].v_type != VAR_UNKNOWN)
20614 {
20615 if (argvars[2].v_type != VAR_DICT
20616 || (dict = argvars[2].vval.v_dict) == NULL)
20617 {
20618 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20619 return;
20620 }
20621 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20622 repeat = get_dict_number(dict, (char_u *)"repeat");
20623 }
20624
20625 timer = create_timer(msec, repeat);
20626 callback = get_callback(&argvars[1], &timer->tr_partial);
20627 if (callback == NULL)
20628 {
20629 stop_timer(timer);
20630 rettv->vval.v_number = -1;
20631 }
20632 else
20633 {
20634 timer->tr_callback = vim_strsave(callback);
20635 rettv->vval.v_number = timer->tr_id;
20636 }
20637}
20638
20639/*
20640 * "timer_stop(timer)" function
20641 */
20642 static void
20643f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20644{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020645 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020646
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020647 if (argvars[0].v_type != VAR_NUMBER)
20648 {
Bram Moolenaared59aa62016-07-09 17:41:12 +020020649 EMSG(_(e_number_exp));
20650 return;
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020651 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020652 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020653 if (timer != NULL)
20654 stop_timer(timer);
20655}
20656#endif
20657
Bram Moolenaard52d9742005-08-21 22:20:28 +000020658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659 * "tolower(string)" function
20660 */
20661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020662f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663{
20664 char_u *p;
20665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020666 p = vim_strsave(get_tv_string(&argvars[0]));
20667 rettv->v_type = VAR_STRING;
20668 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669
20670 if (p != NULL)
20671 while (*p != NUL)
20672 {
20673#ifdef FEAT_MBYTE
20674 int l;
20675
20676 if (enc_utf8)
20677 {
20678 int c, lc;
20679
20680 c = utf_ptr2char(p);
20681 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020682 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 /* TODO: reallocate string when byte count changes. */
20684 if (utf_char2len(lc) == l)
20685 utf_char2bytes(lc, p);
20686 p += l;
20687 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020688 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 p += l; /* skip multi-byte character */
20690 else
20691#endif
20692 {
20693 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20694 ++p;
20695 }
20696 }
20697}
20698
20699/*
20700 * "toupper(string)" function
20701 */
20702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020703f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020705 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020706 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707}
20708
20709/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020710 * "tr(string, fromstr, tostr)" function
20711 */
20712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020713f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020714{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020715 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020716 char_u *fromstr;
20717 char_u *tostr;
20718 char_u *p;
20719#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020720 int inlen;
20721 int fromlen;
20722 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020723 int idx;
20724 char_u *cpstr;
20725 int cplen;
20726 int first = TRUE;
20727#endif
20728 char_u buf[NUMBUFLEN];
20729 char_u buf2[NUMBUFLEN];
20730 garray_T ga;
20731
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020732 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020733 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20734 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020735
20736 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020737 rettv->v_type = VAR_STRING;
20738 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020739 if (fromstr == NULL || tostr == NULL)
20740 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020741 ga_init2(&ga, (int)sizeof(char), 80);
20742
20743#ifdef FEAT_MBYTE
20744 if (!has_mbyte)
20745#endif
20746 /* not multi-byte: fromstr and tostr must be the same length */
20747 if (STRLEN(fromstr) != STRLEN(tostr))
20748 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020749#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020750error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020751#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020752 EMSG2(_(e_invarg2), fromstr);
20753 ga_clear(&ga);
20754 return;
20755 }
20756
20757 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020758 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020759 {
20760#ifdef FEAT_MBYTE
20761 if (has_mbyte)
20762 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020763 inlen = (*mb_ptr2len)(in_str);
20764 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020765 cplen = inlen;
20766 idx = 0;
20767 for (p = fromstr; *p != NUL; p += fromlen)
20768 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020769 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020770 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020771 {
20772 for (p = tostr; *p != NUL; p += tolen)
20773 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020774 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020775 if (idx-- == 0)
20776 {
20777 cplen = tolen;
20778 cpstr = p;
20779 break;
20780 }
20781 }
20782 if (*p == NUL) /* tostr is shorter than fromstr */
20783 goto error;
20784 break;
20785 }
20786 ++idx;
20787 }
20788
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020789 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020790 {
20791 /* Check that fromstr and tostr have the same number of
20792 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020793 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020794 first = FALSE;
20795 for (p = tostr; *p != NUL; p += tolen)
20796 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020797 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020798 --idx;
20799 }
20800 if (idx != 0)
20801 goto error;
20802 }
20803
Bram Moolenaarcde88542015-08-11 19:14:00 +020020804 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020805 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020806 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020807
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020808 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020809 }
20810 else
20811#endif
20812 {
20813 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020814 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020815 if (p != NULL)
20816 ga_append(&ga, tostr[p - fromstr]);
20817 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020818 ga_append(&ga, *in_str);
20819 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020820 }
20821 }
20822
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020823 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020824 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020825 ga_append(&ga, NUL);
20826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020827 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020828}
20829
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020830#ifdef FEAT_FLOAT
20831/*
20832 * "trunc({float})" function
20833 */
20834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020835f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020836{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020837 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020838
20839 rettv->v_type = VAR_FLOAT;
20840 if (get_float_arg(argvars, &f) == OK)
20841 /* trunc() is not in C90, use floor() or ceil() instead. */
20842 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20843 else
20844 rettv->vval.v_float = 0.0;
20845}
20846#endif
20847
Bram Moolenaar8299df92004-07-10 09:47:34 +000020848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 * "type(expr)" function
20850 */
20851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020852f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020854 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020855
20856 switch (argvars[0].v_type)
20857 {
20858 case VAR_NUMBER: n = 0; break;
20859 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020860 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020861 case VAR_FUNC: n = 2; break;
20862 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020863 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020864 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020865 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020866 if (argvars[0].vval.v_number == VVAL_FALSE
20867 || argvars[0].vval.v_number == VVAL_TRUE)
20868 n = 6;
20869 else
20870 n = 7;
20871 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020872 case VAR_JOB: n = 8; break;
20873 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020874 case VAR_UNKNOWN:
20875 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20876 n = -1;
20877 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020878 }
20879 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880}
20881
20882/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020883 * "undofile(name)" function
20884 */
20885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020886f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020887{
20888 rettv->v_type = VAR_STRING;
20889#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020890 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020891 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020892
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020893 if (*fname == NUL)
20894 {
20895 /* If there is no file name there will be no undo file. */
20896 rettv->vval.v_string = NULL;
20897 }
20898 else
20899 {
20900 char_u *ffname = FullName_save(fname, FALSE);
20901
20902 if (ffname != NULL)
20903 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20904 vim_free(ffname);
20905 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020906 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020907#else
20908 rettv->vval.v_string = NULL;
20909#endif
20910}
20911
20912/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020913 * "undotree()" function
20914 */
20915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020916f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020917{
20918 if (rettv_dict_alloc(rettv) == OK)
20919 {
20920 dict_T *dict = rettv->vval.v_dict;
20921 list_T *list;
20922
Bram Moolenaar730cde92010-06-27 05:18:54 +020020923 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020924 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020925 dict_add_nr_str(dict, "save_last",
20926 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020927 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20928 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020929 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020930
20931 list = list_alloc();
20932 if (list != NULL)
20933 {
20934 u_eval_tree(curbuf->b_u_oldhead, list);
20935 dict_add_list(dict, "entries", list);
20936 }
20937 }
20938}
20939
20940/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020941 * "values(dict)" function
20942 */
20943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020944f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020945{
20946 dict_list(argvars, rettv, 1);
20947}
20948
20949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 * "virtcol(string)" function
20951 */
20952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020953f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954{
20955 colnr_T vcol = 0;
20956 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020957 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020959 fp = var2fpos(&argvars[0], FALSE, &fnum);
20960 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20961 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962 {
20963 getvvcol(curwin, fp, NULL, NULL, &vcol);
20964 ++vcol;
20965 }
20966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020967 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968}
20969
20970/*
20971 * "visualmode()" function
20972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020974f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 char_u str[2];
20977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020978 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 str[0] = curbuf->b_visual_mode_eval;
20980 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020981 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982
20983 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020984 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986}
20987
20988/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020989 * "wildmenumode()" function
20990 */
20991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020992f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020993{
20994#ifdef FEAT_WILDMENU
20995 if (wild_menu_showing)
20996 rettv->vval.v_number = 1;
20997#endif
20998}
20999
21000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 * "winbufnr(nr)" function
21002 */
21003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021004f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005{
21006 win_T *wp;
21007
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021008 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021010 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021012 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013}
21014
21015/*
21016 * "wincol()" function
21017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021019f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020{
21021 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021022 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023}
21024
21025/*
21026 * "winheight(nr)" function
21027 */
21028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030{
21031 win_T *wp;
21032
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021033 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021035 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021037 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038}
21039
21040/*
21041 * "winline()" function
21042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021044f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045{
21046 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021047 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048}
21049
21050/*
21051 * "winnr()" function
21052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021054f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055{
21056 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021057
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021059 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021060#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021061 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062}
21063
21064/*
21065 * "winrestcmd()" function
21066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021068f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069{
21070#ifdef FEAT_WINDOWS
21071 win_T *wp;
21072 int winnr = 1;
21073 garray_T ga;
21074 char_u buf[50];
21075
21076 ga_init2(&ga, (int)sizeof(char), 70);
21077 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21078 {
21079 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21080 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21082 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 ++winnr;
21084 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021085 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021087 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021089 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021091 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092}
21093
21094/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021095 * "winrestview()" function
21096 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021098f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021099{
21100 dict_T *dict;
21101
21102 if (argvars[0].v_type != VAR_DICT
21103 || (dict = argvars[0].vval.v_dict) == NULL)
21104 EMSG(_(e_invarg));
21105 else
21106 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021107 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021108 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021109 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021110 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021111#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021112 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021113 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021114#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021115 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21116 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021117 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021118 curwin->w_set_curswant = FALSE;
21119 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021120
Bram Moolenaar82c25852014-05-28 16:47:16 +020021121 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021122 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021123#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021124 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021125 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021126#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021127 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021128 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021129 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021130 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021131
21132 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021133 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021134# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021135 win_new_width(curwin, W_WIDTH(curwin));
21136# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021137 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021138
Bram Moolenaarb851a962014-10-31 15:45:52 +010021139 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021140 curwin->w_topline = 1;
21141 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21142 curwin->w_topline = curbuf->b_ml.ml_line_count;
21143#ifdef FEAT_DIFF
21144 check_topfill(curwin, TRUE);
21145#endif
21146 }
21147}
21148
21149/*
21150 * "winsaveview()" function
21151 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021153f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021154{
21155 dict_T *dict;
21156
Bram Moolenaara800b422010-06-27 01:15:55 +020021157 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021158 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021159 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021160
21161 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21162 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21163#ifdef FEAT_VIRTUALEDIT
21164 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21165#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021166 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021167 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21168
21169 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21170#ifdef FEAT_DIFF
21171 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21172#endif
21173 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21174 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21175}
21176
21177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 * "winwidth(nr)" function
21179 */
21180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021181f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182{
21183 win_T *wp;
21184
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021185 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021187 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021189#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021190 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021192 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193#endif
21194}
21195
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021197 * "wordcount()" function
21198 */
21199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021200f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021201{
21202 if (rettv_dict_alloc(rettv) == FAIL)
21203 return;
21204 cursor_pos_info(rettv->vval.v_dict);
21205}
21206
21207/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021208 * Write list of strings to file
21209 */
21210 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021211write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021212{
21213 listitem_T *li;
21214 int c;
21215 int ret = OK;
21216 char_u *s;
21217
21218 for (li = list->lv_first; li != NULL; li = li->li_next)
21219 {
21220 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21221 {
21222 if (*s == '\n')
21223 c = putc(NUL, fd);
21224 else
21225 c = putc(*s, fd);
21226 if (c == EOF)
21227 {
21228 ret = FAIL;
21229 break;
21230 }
21231 }
21232 if (!binary || li->li_next != NULL)
21233 if (putc('\n', fd) == EOF)
21234 {
21235 ret = FAIL;
21236 break;
21237 }
21238 if (ret == FAIL)
21239 {
21240 EMSG(_(e_write));
21241 break;
21242 }
21243 }
21244 return ret;
21245}
21246
21247/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021248 * "writefile()" function
21249 */
21250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021251f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021252{
21253 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021254 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021255 char_u *fname;
21256 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021257 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021258
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021259 if (check_restricted() || check_secure())
21260 return;
21261
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021262 if (argvars[0].v_type != VAR_LIST)
21263 {
21264 EMSG2(_(e_listarg), "writefile()");
21265 return;
21266 }
21267 if (argvars[0].vval.v_list == NULL)
21268 return;
21269
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021270 if (argvars[2].v_type != VAR_UNKNOWN)
21271 {
21272 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21273 binary = TRUE;
21274 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21275 append = TRUE;
21276 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021277
21278 /* Always open the file in binary mode, library functions have a mind of
21279 * their own about CR-LF conversion. */
21280 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021281 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21282 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021283 {
21284 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21285 ret = -1;
21286 }
21287 else
21288 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021289 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21290 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021291 fclose(fd);
21292 }
21293
21294 rettv->vval.v_number = ret;
21295}
21296
21297/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021298 * "xor(expr, expr)" function
21299 */
21300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021301f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021302{
21303 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21304 ^ get_tv_number_chk(&argvars[1], NULL);
21305}
21306
21307
21308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021310 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 */
21312 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021313var2fpos(
21314 typval_T *varp,
21315 int dollar_lnum, /* TRUE when $ is last line */
21316 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021318 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021320 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321
Bram Moolenaara5525202006-03-02 22:52:09 +000021322 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021323 if (varp->v_type == VAR_LIST)
21324 {
21325 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021326 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021327 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021328 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021329
21330 l = varp->vval.v_list;
21331 if (l == NULL)
21332 return NULL;
21333
21334 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021335 pos.lnum = list_find_nr(l, 0L, &error);
21336 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021337 return NULL; /* invalid line number */
21338
21339 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021340 pos.col = list_find_nr(l, 1L, &error);
21341 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021342 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021343 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021344
21345 /* We accept "$" for the column number: last column. */
21346 li = list_find(l, 1L);
21347 if (li != NULL && li->li_tv.v_type == VAR_STRING
21348 && li->li_tv.vval.v_string != NULL
21349 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21350 pos.col = len + 1;
21351
Bram Moolenaara5525202006-03-02 22:52:09 +000021352 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021353 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021354 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021355 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021356
Bram Moolenaara5525202006-03-02 22:52:09 +000021357#ifdef FEAT_VIRTUALEDIT
21358 /* Get the virtual offset. Defaults to zero. */
21359 pos.coladd = list_find_nr(l, 2L, &error);
21360 if (error)
21361 pos.coladd = 0;
21362#endif
21363
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021364 return &pos;
21365 }
21366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021367 name = get_tv_string_chk(varp);
21368 if (name == NULL)
21369 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021370 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021372 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21373 {
21374 if (VIsual_active)
21375 return &VIsual;
21376 return &curwin->w_cursor;
21377 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021378 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021380 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21382 return NULL;
21383 return pp;
21384 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021385
21386#ifdef FEAT_VIRTUALEDIT
21387 pos.coladd = 0;
21388#endif
21389
Bram Moolenaar477933c2007-07-17 14:32:23 +000021390 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021391 {
21392 pos.col = 0;
21393 if (name[1] == '0') /* "w0": first visible line */
21394 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021395 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021396 pos.lnum = curwin->w_topline;
21397 return &pos;
21398 }
21399 else if (name[1] == '$') /* "w$": last visible line */
21400 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021401 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021402 pos.lnum = curwin->w_botline - 1;
21403 return &pos;
21404 }
21405 }
21406 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021408 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 {
21410 pos.lnum = curbuf->b_ml.ml_line_count;
21411 pos.col = 0;
21412 }
21413 else
21414 {
21415 pos.lnum = curwin->w_cursor.lnum;
21416 pos.col = (colnr_T)STRLEN(ml_get_curline());
21417 }
21418 return &pos;
21419 }
21420 return NULL;
21421}
21422
21423/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021424 * Convert list in "arg" into a position and optional file number.
21425 * When "fnump" is NULL there is no file number, only 3 items.
21426 * Note that the column is passed on as-is, the caller may want to decrement
21427 * it to use 1 for the first column.
21428 * Return FAIL when conversion is not possible, doesn't check the position for
21429 * validity.
21430 */
21431 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021432list2fpos(
21433 typval_T *arg,
21434 pos_T *posp,
21435 int *fnump,
21436 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021437{
21438 list_T *l = arg->vval.v_list;
21439 long i = 0;
21440 long n;
21441
Bram Moolenaar493c1782014-05-28 14:34:46 +020021442 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21443 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021444 if (arg->v_type != VAR_LIST
21445 || l == NULL
21446 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021447 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021448 return FAIL;
21449
21450 if (fnump != NULL)
21451 {
21452 n = list_find_nr(l, i++, NULL); /* fnum */
21453 if (n < 0)
21454 return FAIL;
21455 if (n == 0)
21456 n = curbuf->b_fnum; /* current buffer */
21457 *fnump = n;
21458 }
21459
21460 n = list_find_nr(l, i++, NULL); /* lnum */
21461 if (n < 0)
21462 return FAIL;
21463 posp->lnum = n;
21464
21465 n = list_find_nr(l, i++, NULL); /* col */
21466 if (n < 0)
21467 return FAIL;
21468 posp->col = n;
21469
21470#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021471 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021472 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021473 posp->coladd = 0;
21474 else
21475 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021476#endif
21477
Bram Moolenaar493c1782014-05-28 14:34:46 +020021478 if (curswantp != NULL)
21479 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21480
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021481 return OK;
21482}
21483
21484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 * Get the length of an environment variable name.
21486 * Advance "arg" to the first character after the name.
21487 * Return 0 for error.
21488 */
21489 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021490get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491{
21492 char_u *p;
21493 int len;
21494
21495 for (p = *arg; vim_isIDc(*p); ++p)
21496 ;
21497 if (p == *arg) /* no name found */
21498 return 0;
21499
21500 len = (int)(p - *arg);
21501 *arg = p;
21502 return len;
21503}
21504
21505/*
21506 * Get the length of the name of a function or internal variable.
21507 * "arg" is advanced to the first non-white character after the name.
21508 * Return 0 if something is wrong.
21509 */
21510 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021511get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512{
21513 char_u *p;
21514 int len;
21515
21516 /* Find the end of the name. */
21517 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021518 {
21519 if (*p == ':')
21520 {
21521 /* "s:" is start of "s:var", but "n:" is not and can be used in
21522 * slice "[n:]". Also "xx:" is not a namespace. */
21523 len = (int)(p - *arg);
21524 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21525 || len > 1)
21526 break;
21527 }
21528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 if (p == *arg) /* no name found */
21530 return 0;
21531
21532 len = (int)(p - *arg);
21533 *arg = skipwhite(p);
21534
21535 return len;
21536}
21537
21538/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021539 * Get the length of the name of a variable or function.
21540 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021542 * Return -1 if curly braces expansion failed.
21543 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 * If the name contains 'magic' {}'s, expand them and return the
21545 * expanded name in an allocated string via 'alias' - caller must free.
21546 */
21547 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021548get_name_len(
21549 char_u **arg,
21550 char_u **alias,
21551 int evaluate,
21552 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553{
21554 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 char_u *p;
21556 char_u *expr_start;
21557 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558
21559 *alias = NULL; /* default to no alias */
21560
21561 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21562 && (*arg)[2] == (int)KE_SNR)
21563 {
21564 /* hard coded <SNR>, already translated */
21565 *arg += 3;
21566 return get_id_len(arg) + 3;
21567 }
21568 len = eval_fname_script(*arg);
21569 if (len > 0)
21570 {
21571 /* literal "<SID>", "s:" or "<SNR>" */
21572 *arg += len;
21573 }
21574
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021576 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021578 p = find_name_end(*arg, &expr_start, &expr_end,
21579 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580 if (expr_start != NULL)
21581 {
21582 char_u *temp_string;
21583
21584 if (!evaluate)
21585 {
21586 len += (int)(p - *arg);
21587 *arg = skipwhite(p);
21588 return len;
21589 }
21590
21591 /*
21592 * Include any <SID> etc in the expanded string:
21593 * Thus the -len here.
21594 */
21595 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21596 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021597 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 *alias = temp_string;
21599 *arg = skipwhite(p);
21600 return (int)STRLEN(temp_string);
21601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602
21603 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021604 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 EMSG2(_(e_invexpr2), *arg);
21606
21607 return len;
21608}
21609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021610/*
21611 * Find the end of a variable or function name, taking care of magic braces.
21612 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21613 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021614 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021615 * Return a pointer to just after the name. Equal to "arg" if there is no
21616 * valid name.
21617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021619find_name_end(
21620 char_u *arg,
21621 char_u **expr_start,
21622 char_u **expr_end,
21623 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021625 int mb_nest = 0;
21626 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021628 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021630 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021632 *expr_start = NULL;
21633 *expr_end = NULL;
21634 }
21635
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021636 /* Quick check for valid starting character. */
21637 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21638 return arg;
21639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021640 for (p = arg; *p != NUL
21641 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021642 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021643 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021644 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021645 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021646 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021647 if (*p == '\'')
21648 {
21649 /* skip over 'string' to avoid counting [ and ] inside it. */
21650 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21651 ;
21652 if (*p == NUL)
21653 break;
21654 }
21655 else if (*p == '"')
21656 {
21657 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21658 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21659 if (*p == '\\' && p[1] != NUL)
21660 ++p;
21661 if (*p == NUL)
21662 break;
21663 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021664 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21665 {
21666 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021667 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021668 len = (int)(p - arg);
21669 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021670 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021671 break;
21672 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021674 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021676 if (*p == '[')
21677 ++br_nest;
21678 else if (*p == ']')
21679 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021682 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021684 if (*p == '{')
21685 {
21686 mb_nest++;
21687 if (expr_start != NULL && *expr_start == NULL)
21688 *expr_start = p;
21689 }
21690 else if (*p == '}')
21691 {
21692 mb_nest--;
21693 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21694 *expr_end = p;
21695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 }
21698
21699 return p;
21700}
21701
21702/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021703 * Expands out the 'magic' {}'s in a variable/function name.
21704 * Note that this can call itself recursively, to deal with
21705 * constructs like foo{bar}{baz}{bam}
21706 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21707 * "in_start" ^
21708 * "expr_start" ^
21709 * "expr_end" ^
21710 * "in_end" ^
21711 *
21712 * Returns a new allocated string, which the caller must free.
21713 * Returns NULL for failure.
21714 */
21715 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021716make_expanded_name(
21717 char_u *in_start,
21718 char_u *expr_start,
21719 char_u *expr_end,
21720 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021721{
21722 char_u c1;
21723 char_u *retval = NULL;
21724 char_u *temp_result;
21725 char_u *nextcmd = NULL;
21726
21727 if (expr_end == NULL || in_end == NULL)
21728 return NULL;
21729 *expr_start = NUL;
21730 *expr_end = NUL;
21731 c1 = *in_end;
21732 *in_end = NUL;
21733
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021734 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021735 if (temp_result != NULL && nextcmd == NULL)
21736 {
21737 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21738 + (in_end - expr_end) + 1));
21739 if (retval != NULL)
21740 {
21741 STRCPY(retval, in_start);
21742 STRCAT(retval, temp_result);
21743 STRCAT(retval, expr_end + 1);
21744 }
21745 }
21746 vim_free(temp_result);
21747
21748 *in_end = c1; /* put char back for error messages */
21749 *expr_start = '{';
21750 *expr_end = '}';
21751
21752 if (retval != NULL)
21753 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021754 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021755 if (expr_start != NULL)
21756 {
21757 /* Further expansion! */
21758 temp_result = make_expanded_name(retval, expr_start,
21759 expr_end, temp_result);
21760 vim_free(retval);
21761 retval = temp_result;
21762 }
21763 }
21764
21765 return retval;
21766}
21767
21768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021770 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 */
21772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021773eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021775 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21776}
21777
21778/*
21779 * Return TRUE if character "c" can be used as the first character in a
21780 * variable or function name (excluding '{' and '}').
21781 */
21782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021783eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021784{
21785 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786}
21787
21788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 * Set number v: variable to "val".
21790 */
21791 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021792set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021794 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795}
21796
21797/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021798 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021800 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021801get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021803 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804}
21805
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021806/*
21807 * Get string v: variable value. Uses a static buffer, can only be used once.
21808 */
21809 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021810get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021811{
21812 return get_tv_string(&vimvars[idx].vv_tv);
21813}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021814
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021816 * Get List v: variable value. Caller must take care of reference count when
21817 * needed.
21818 */
21819 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021820get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021821{
21822 return vimvars[idx].vv_list;
21823}
21824
21825/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021826 * Set v:char to character "c".
21827 */
21828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021829set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021830{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021831 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021832
21833#ifdef FEAT_MBYTE
21834 if (has_mbyte)
21835 buf[(*mb_char2bytes)(c, buf)] = NUL;
21836 else
21837#endif
21838 {
21839 buf[0] = c;
21840 buf[1] = NUL;
21841 }
21842 set_vim_var_string(VV_CHAR, buf, -1);
21843}
21844
21845/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021846 * Set v:count to "count" and v:count1 to "count1".
21847 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848 */
21849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021850set_vcount(
21851 long count,
21852 long count1,
21853 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021855 if (set_prevcount)
21856 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021857 vimvars[VV_COUNT].vv_nr = count;
21858 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859}
21860
21861/*
21862 * Set string v: variable to a copy of "val".
21863 */
21864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021865set_vim_var_string(
21866 int idx,
21867 char_u *val,
21868 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869{
Bram Moolenaara542c682016-01-31 16:28:04 +010021870 clear_tv(&vimvars[idx].vv_di.di_tv);
21871 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021873 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021875 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021877 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878}
21879
21880/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021881 * Set List v: variable to "val".
21882 */
21883 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021884set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021885{
Bram Moolenaara542c682016-01-31 16:28:04 +010021886 clear_tv(&vimvars[idx].vv_di.di_tv);
21887 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021888 vimvars[idx].vv_list = val;
21889 if (val != NULL)
21890 ++val->lv_refcount;
21891}
21892
21893/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021894 * Set Dictionary v: variable to "val".
21895 */
21896 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021897set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021898{
21899 int todo;
21900 hashitem_T *hi;
21901
Bram Moolenaara542c682016-01-31 16:28:04 +010021902 clear_tv(&vimvars[idx].vv_di.di_tv);
21903 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021904 vimvars[idx].vv_dict = val;
21905 if (val != NULL)
21906 {
21907 ++val->dv_refcount;
21908
21909 /* Set readonly */
21910 todo = (int)val->dv_hashtab.ht_used;
21911 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21912 {
21913 if (HASHITEM_EMPTY(hi))
21914 continue;
21915 --todo;
21916 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21917 }
21918 }
21919}
21920
21921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 * Set v:register if needed.
21923 */
21924 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021925set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926{
21927 char_u regname;
21928
21929 if (c == 0 || c == ' ')
21930 regname = '"';
21931 else
21932 regname = c;
21933 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021934 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935 set_vim_var_string(VV_REG, &regname, 1);
21936}
21937
21938/*
21939 * Get or set v:exception. If "oldval" == NULL, return the current value.
21940 * Otherwise, restore the value to "oldval" and return NULL.
21941 * Must always be called in pairs to save and restore v:exception! Does not
21942 * take care of memory allocations.
21943 */
21944 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021945v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946{
21947 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021948 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949
Bram Moolenaare9a41262005-01-15 22:18:47 +000021950 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 return NULL;
21952}
21953
21954/*
21955 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21956 * Otherwise, restore the value to "oldval" and return NULL.
21957 * Must always be called in pairs to save and restore v:throwpoint! Does not
21958 * take care of memory allocations.
21959 */
21960 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021961v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962{
21963 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021964 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021965
Bram Moolenaare9a41262005-01-15 22:18:47 +000021966 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967 return NULL;
21968}
21969
21970#if defined(FEAT_AUTOCMD) || defined(PROTO)
21971/*
21972 * Set v:cmdarg.
21973 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21974 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21975 * Must always be called in pairs!
21976 */
21977 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021978set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979{
21980 char_u *oldval;
21981 char_u *newval;
21982 unsigned len;
21983
Bram Moolenaare9a41262005-01-15 22:18:47 +000021984 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021985 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021987 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021988 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021989 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 }
21991
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021992 if (eap->force_bin == FORCE_BIN)
21993 len = 6;
21994 else if (eap->force_bin == FORCE_NOBIN)
21995 len = 8;
21996 else
21997 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021998
21999 if (eap->read_edit)
22000 len += 7;
22001
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022002 if (eap->force_ff != 0)
22003 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22004# ifdef FEAT_MBYTE
22005 if (eap->force_enc != 0)
22006 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022007 if (eap->bad_char != 0)
22008 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022009# endif
22010
22011 newval = alloc(len + 1);
22012 if (newval == NULL)
22013 return NULL;
22014
22015 if (eap->force_bin == FORCE_BIN)
22016 sprintf((char *)newval, " ++bin");
22017 else if (eap->force_bin == FORCE_NOBIN)
22018 sprintf((char *)newval, " ++nobin");
22019 else
22020 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022021
22022 if (eap->read_edit)
22023 STRCAT(newval, " ++edit");
22024
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022025 if (eap->force_ff != 0)
22026 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22027 eap->cmd + eap->force_ff);
22028# ifdef FEAT_MBYTE
22029 if (eap->force_enc != 0)
22030 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22031 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022032 if (eap->bad_char == BAD_KEEP)
22033 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22034 else if (eap->bad_char == BAD_DROP)
22035 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22036 else if (eap->bad_char != 0)
22037 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022038# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022039 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022040 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041}
22042#endif
22043
22044/*
22045 * Get the value of internal variable "name".
22046 * Return OK or FAIL.
22047 */
22048 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022049get_var_tv(
22050 char_u *name,
22051 int len, /* length of "name" */
22052 typval_T *rettv, /* NULL when only checking existence */
22053 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22054 int verbose, /* may give error message */
22055 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056{
22057 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022058 typval_T *tv = NULL;
22059 typval_T atv;
22060 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062
22063 /* truncate the name, so that we can use strcmp() */
22064 cc = name[len];
22065 name[len] = NUL;
22066
22067 /*
22068 * Check for "b:changedtick".
22069 */
22070 if (STRCMP(name, "b:changedtick") == 0)
22071 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022072 atv.v_type = VAR_NUMBER;
22073 atv.vval.v_number = curbuf->b_changedtick;
22074 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 }
22076
22077 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 * Check for user-defined variables.
22079 */
22080 else
22081 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022082 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022084 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022085 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022086 if (dip != NULL)
22087 *dip = v;
22088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 }
22090
Bram Moolenaare9a41262005-01-15 22:18:47 +000022091 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022093 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022094 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 ret = FAIL;
22096 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022097 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022098 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099
22100 name[len] = cc;
22101
22102 return ret;
22103}
22104
22105/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022106 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22107 * Also handle function call with Funcref variable: func(expr)
22108 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22109 */
22110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022111handle_subscript(
22112 char_u **arg,
22113 typval_T *rettv,
22114 int evaluate, /* do more than finding the end */
22115 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022116{
22117 int ret = OK;
22118 dict_T *selfdict = NULL;
22119 char_u *s;
22120 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022121 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022122
22123 while (ret == OK
22124 && (**arg == '['
22125 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022126 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22127 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022128 && !vim_iswhite(*(*arg - 1)))
22129 {
22130 if (**arg == '(')
22131 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022132 partial_T *pt = NULL;
22133
Bram Moolenaard9fba312005-06-26 22:34:35 +000022134 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022135 if (evaluate)
22136 {
22137 functv = *rettv;
22138 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022139
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022140 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022141 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022142 {
22143 pt = functv.vval.v_partial;
22144 s = pt->pt_name;
22145 }
22146 else
22147 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022148 }
22149 else
22150 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022151 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022152 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022153 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022154
22155 /* Clear the funcref afterwards, so that deleting it while
22156 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022157 if (evaluate)
22158 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022159
22160 /* Stop the expression evaluation when immediately aborting on
22161 * error, or when an interrupt occurred or an exception was thrown
22162 * but not caught. */
22163 if (aborting())
22164 {
22165 if (ret == OK)
22166 clear_tv(rettv);
22167 ret = FAIL;
22168 }
22169 dict_unref(selfdict);
22170 selfdict = NULL;
22171 }
22172 else /* **arg == '[' || **arg == '.' */
22173 {
22174 dict_unref(selfdict);
22175 if (rettv->v_type == VAR_DICT)
22176 {
22177 selfdict = rettv->vval.v_dict;
22178 if (selfdict != NULL)
22179 ++selfdict->dv_refcount;
22180 }
22181 else
22182 selfdict = NULL;
22183 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22184 {
22185 clear_tv(rettv);
22186 ret = FAIL;
22187 }
22188 }
22189 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022190
Bram Moolenaar1d429612016-05-24 15:44:17 +020022191 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22192 * Don't do this when "Func" is already a partial that was bound
22193 * explicitly (pt_auto is FALSE). */
22194 if (selfdict != NULL
22195 && (rettv->v_type == VAR_FUNC
22196 || (rettv->v_type == VAR_PARTIAL
22197 && (rettv->vval.v_partial->pt_auto
22198 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022199 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022200 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22201 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022202 char_u *tofree = NULL;
22203 ufunc_T *fp;
22204 char_u fname_buf[FLEN_FIXED + 1];
22205 int error;
22206
22207 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022208 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022209 fp = find_func(fname);
22210 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022211
Bram Moolenaar65639032016-03-16 21:40:30 +010022212 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022213 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022214 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22215
22216 if (pt != NULL)
22217 {
22218 pt->pt_refcount = 1;
22219 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022220 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022221 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022222 if (rettv->v_type == VAR_FUNC)
22223 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022224 /* Just a function: Take over the function name and use
22225 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022226 pt->pt_name = rettv->vval.v_string;
22227 }
22228 else
22229 {
22230 partial_T *ret_pt = rettv->vval.v_partial;
22231 int i;
22232
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022233 /* Partial: copy the function name, use selfdict and copy
22234 * args. Can't take over name or args, the partial might
22235 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022236 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022237 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022238 if (ret_pt->pt_argc > 0)
22239 {
22240 pt->pt_argv = (typval_T *)alloc(
22241 sizeof(typval_T) * ret_pt->pt_argc);
22242 if (pt->pt_argv == NULL)
22243 /* out of memory: drop the arguments */
22244 pt->pt_argc = 0;
22245 else
22246 {
22247 pt->pt_argc = ret_pt->pt_argc;
22248 for (i = 0; i < pt->pt_argc; i++)
22249 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22250 }
22251 }
22252 partial_unref(ret_pt);
22253 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022254 rettv->v_type = VAR_PARTIAL;
22255 rettv->vval.v_partial = pt;
22256 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022257 }
22258 }
22259
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022260 dict_unref(selfdict);
22261 return ret;
22262}
22263
22264/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022265 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022266 * value).
22267 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022268 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022269alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022270{
Bram Moolenaar33570922005-01-25 22:26:29 +000022271 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022272}
22273
22274/*
22275 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 * The string "s" must have been allocated, it is consumed.
22277 * Return NULL for out of memory, the variable otherwise.
22278 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022279 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022280alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281{
Bram Moolenaar33570922005-01-25 22:26:29 +000022282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022284 rettv = alloc_tv();
22285 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022287 rettv->v_type = VAR_STRING;
22288 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 }
22290 else
22291 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022292 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293}
22294
22295/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022296 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022299free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300{
22301 if (varp != NULL)
22302 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022303 switch (varp->v_type)
22304 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022305 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022306 func_unref(varp->vval.v_string);
22307 /*FALLTHROUGH*/
22308 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022309 vim_free(varp->vval.v_string);
22310 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022311 case VAR_PARTIAL:
22312 partial_unref(varp->vval.v_partial);
22313 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022314 case VAR_LIST:
22315 list_unref(varp->vval.v_list);
22316 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022317 case VAR_DICT:
22318 dict_unref(varp->vval.v_dict);
22319 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022320 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022321#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022322 job_unref(varp->vval.v_job);
22323 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022324#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022325 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022327 channel_unref(varp->vval.v_channel);
22328 break;
22329#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022330 case VAR_NUMBER:
22331 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022332 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022333 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022334 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336 vim_free(varp);
22337 }
22338}
22339
22340/*
22341 * Free the memory for a variable value and set the value to NULL or 0.
22342 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022344clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345{
22346 if (varp != NULL)
22347 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022348 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022350 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022351 func_unref(varp->vval.v_string);
22352 /*FALLTHROUGH*/
22353 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022354 vim_free(varp->vval.v_string);
22355 varp->vval.v_string = NULL;
22356 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022357 case VAR_PARTIAL:
22358 partial_unref(varp->vval.v_partial);
22359 varp->vval.v_partial = NULL;
22360 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022361 case VAR_LIST:
22362 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022363 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022364 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022365 case VAR_DICT:
22366 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022367 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022368 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022369 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022370 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022371 varp->vval.v_number = 0;
22372 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022373 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022374#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022375 varp->vval.v_float = 0.0;
22376 break;
22377#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022378 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022379#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022380 job_unref(varp->vval.v_job);
22381 varp->vval.v_job = NULL;
22382#endif
22383 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022384 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022385#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022386 channel_unref(varp->vval.v_channel);
22387 varp->vval.v_channel = NULL;
22388#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022389 case VAR_UNKNOWN:
22390 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022392 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393 }
22394}
22395
22396/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022397 * Set the value of a variable to NULL without freeing items.
22398 */
22399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022400init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022401{
22402 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022403 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022404}
22405
22406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 * Get the number value of a variable.
22408 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022409 * For incompatible types, return 0.
22410 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22411 * caller of incompatible types: it sets *denote to TRUE if "denote"
22412 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022414 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022415get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022417 int error = FALSE;
22418
22419 return get_tv_number_chk(varp, &error); /* return 0L on error */
22420}
22421
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022422 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022423get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022424{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022425 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022427 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022429 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022430 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022431 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022432#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022433 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022434 break;
22435#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022436 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022437 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022438 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022439 break;
22440 case VAR_STRING:
22441 if (varp->vval.v_string != NULL)
22442 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022443 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022444 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022445 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022446 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022447 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022448 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022449 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022450 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022451 case VAR_SPECIAL:
22452 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22453 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022454 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022455#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022456 EMSG(_("E910: Using a Job as a Number"));
22457 break;
22458#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022459 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022460#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022461 EMSG(_("E913: Using a Channel as a Number"));
22462 break;
22463#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022464 case VAR_UNKNOWN:
22465 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022466 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022468 if (denote == NULL) /* useful for values that must be unsigned */
22469 n = -1;
22470 else
22471 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022472 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473}
22474
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022475#ifdef FEAT_FLOAT
22476 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022477get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022478{
22479 switch (varp->v_type)
22480 {
22481 case VAR_NUMBER:
22482 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022483 case VAR_FLOAT:
22484 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022485 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022486 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022487 EMSG(_("E891: Using a Funcref as a Float"));
22488 break;
22489 case VAR_STRING:
22490 EMSG(_("E892: Using a String as a Float"));
22491 break;
22492 case VAR_LIST:
22493 EMSG(_("E893: Using a List as a Float"));
22494 break;
22495 case VAR_DICT:
22496 EMSG(_("E894: Using a Dictionary as a Float"));
22497 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022498 case VAR_SPECIAL:
22499 EMSG(_("E907: Using a special value as a Float"));
22500 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022501 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022502# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022503 EMSG(_("E911: Using a Job as a Float"));
22504 break;
22505# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022506 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022507# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022508 EMSG(_("E914: Using a Channel as a Float"));
22509 break;
22510# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022511 case VAR_UNKNOWN:
22512 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022513 break;
22514 }
22515 return 0;
22516}
22517#endif
22518
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022520 * Get the lnum from the first argument.
22521 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022522 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523 */
22524 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022525get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526{
Bram Moolenaar33570922005-01-25 22:26:29 +000022527 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528 linenr_T lnum;
22529
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022530 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 if (lnum == 0) /* no valid number, try using line() */
22532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022533 rettv.v_type = VAR_NUMBER;
22534 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022535 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022536 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 }
22538 return lnum;
22539}
22540
22541/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022542 * Get the lnum from the first argument.
22543 * Also accepts "$", then "buf" is used.
22544 * Returns 0 on error.
22545 */
22546 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022547get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022548{
22549 if (argvars[0].v_type == VAR_STRING
22550 && argvars[0].vval.v_string != NULL
22551 && argvars[0].vval.v_string[0] == '$'
22552 && buf != NULL)
22553 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022554 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000022555}
22556
22557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 * Get the string value of a variable.
22559 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022560 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22561 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 * If the String variable has never been set, return an empty string.
22563 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022564 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22565 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022567 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022568get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569{
22570 static char_u mybuf[NUMBUFLEN];
22571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022572 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573}
22574
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022575 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022576get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022578 char_u *res = get_tv_string_buf_chk(varp, buf);
22579
22580 return res != NULL ? res : (char_u *)"";
22581}
22582
Bram Moolenaar7d647822014-04-05 21:28:56 +020022583/*
22584 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22585 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022586 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022587get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022588{
22589 static char_u mybuf[NUMBUFLEN];
22590
22591 return get_tv_string_buf_chk(varp, mybuf);
22592}
22593
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022594 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022595get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022596{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022597 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022599 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022600 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
22601 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022602 return buf;
22603 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022604 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022605 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022606 break;
22607 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022608 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022609 break;
22610 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022611 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022612 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022613 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022614#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022615 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022616 break;
22617#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022618 case VAR_STRING:
22619 if (varp->vval.v_string != NULL)
22620 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022621 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022622 case VAR_SPECIAL:
22623 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22624 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022625 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022626#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022627 {
22628 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022629 char *status;
22630
22631 if (job == NULL)
22632 return (char_u *)"no process";
22633 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022634 : job->jv_status == JOB_ENDED ? "dead"
22635 : "run";
22636# ifdef UNIX
22637 vim_snprintf((char *)buf, NUMBUFLEN,
22638 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022639# elif defined(WIN32)
22640 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022641 "process %ld %s",
22642 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022643 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022644# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022645 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022646 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22647# endif
22648 return buf;
22649 }
22650#endif
22651 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022652 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022653#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022654 {
22655 channel_T *channel = varp->vval.v_channel;
22656 char *status = channel_status(channel);
22657
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022658 if (channel == NULL)
22659 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22660 else
22661 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022662 "channel %d %s", channel->ch_id, status);
22663 return buf;
22664 }
22665#endif
22666 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022667 case VAR_UNKNOWN:
22668 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022669 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022671 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672}
22673
22674/*
22675 * Find variable "name" in the list of variables.
22676 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022677 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022678 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022679 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022681 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022682find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022685 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686
Bram Moolenaara7043832005-01-21 11:56:39 +000022687 ht = find_var_ht(name, &varname);
22688 if (htp != NULL)
22689 *htp = ht;
22690 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022692 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693}
22694
22695/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022696 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022697 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022699 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022700find_var_in_ht(
22701 hashtab_T *ht,
22702 int htname,
22703 char_u *varname,
22704 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022705{
Bram Moolenaar33570922005-01-25 22:26:29 +000022706 hashitem_T *hi;
22707
22708 if (*varname == NUL)
22709 {
22710 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022711 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022712 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022713 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022714 case 'g': return &globvars_var;
22715 case 'v': return &vimvars_var;
22716 case 'b': return &curbuf->b_bufvar;
22717 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022718#ifdef FEAT_WINDOWS
22719 case 't': return &curtab->tp_winvar;
22720#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022721 case 'l': return current_funccal == NULL
22722 ? NULL : &current_funccal->l_vars_var;
22723 case 'a': return current_funccal == NULL
22724 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022725 }
22726 return NULL;
22727 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022728
22729 hi = hash_find(ht, varname);
22730 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022731 {
22732 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022733 * worked find the variable again. Don't auto-load a script if it was
22734 * loaded already, otherwise it would be loaded every time when
22735 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022736 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022737 {
22738 /* Note: script_autoload() may make "hi" invalid. It must either
22739 * be obtained again or not used. */
22740 if (!script_autoload(varname, FALSE) || aborting())
22741 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022742 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022743 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022744 if (HASHITEM_EMPTY(hi))
22745 return NULL;
22746 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022747 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022748}
22749
22750/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022751 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022752 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022753 * Set "varname" to the start of name without ':'.
22754 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022755 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022756find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022758 hashitem_T *hi;
22759
Bram Moolenaar73627d02015-08-11 15:46:09 +020022760 if (name[0] == NUL)
22761 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 if (name[1] != ':')
22763 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022764 /* The name must not start with a colon or #. */
22765 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766 return NULL;
22767 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022768
22769 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022770 hi = hash_find(&compat_hashtab, name);
22771 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022772 return &compat_hashtab;
22773
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022776 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 }
22778 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022779 if (*name == 'g') /* global variable */
22780 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022781 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22782 */
22783 if (vim_strchr(name + 2, ':') != NULL
22784 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022785 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022787 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022789 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022790#ifdef FEAT_WINDOWS
22791 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022792 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022793#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022794 if (*name == 'v') /* v: variable */
22795 return &vimvarht;
22796 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022797 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022798 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022799 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800 if (*name == 's' /* script variable */
22801 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22802 return &SCRIPT_VARS(current_SID);
22803 return NULL;
22804}
22805
22806/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022807 * Get function call environment based on bactrace debug level
22808 */
22809 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022810get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022811{
22812 int i;
22813 funccall_T *funccal;
22814 funccall_T *temp_funccal;
22815
22816 funccal = current_funccal;
22817 if (debug_backtrace_level > 0)
22818 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022819 for (i = 0; i < debug_backtrace_level; i++)
22820 {
22821 temp_funccal = funccal->caller;
22822 if (temp_funccal)
22823 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022824 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022825 /* backtrace level overflow. reset to max */
22826 debug_backtrace_level = i;
22827 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022828 }
22829 return funccal;
22830}
22831
22832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022834 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835 * Returns NULL when it doesn't exist.
22836 */
22837 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022838get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839{
Bram Moolenaar33570922005-01-25 22:26:29 +000022840 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022842 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 if (v == NULL)
22844 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022845 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846}
22847
22848/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022849 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850 * sourcing this script and when executing functions defined in the script.
22851 */
22852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022853new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854{
Bram Moolenaara7043832005-01-21 11:56:39 +000022855 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022856 hashtab_T *ht;
22857 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022858
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22860 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022861 /* Re-allocating ga_data means that an ht_array pointing to
22862 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022863 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022864 for (i = 1; i <= ga_scripts.ga_len; ++i)
22865 {
22866 ht = &SCRIPT_VARS(i);
22867 if (ht->ht_mask == HT_INIT_SIZE - 1)
22868 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022869 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022870 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022871 }
22872
Bram Moolenaar071d4272004-06-13 20:20:40 +000022873 while (ga_scripts.ga_len < id)
22874 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022875 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022876 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022877 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 }
22880 }
22881}
22882
22883/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022884 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22885 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 */
22887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022888init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889{
Bram Moolenaar33570922005-01-25 22:26:29 +000022890 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022891 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022892 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022893 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022894 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022895 dict_var->di_tv.vval.v_dict = dict;
22896 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022897 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022898 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22899 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900}
22901
22902/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022903 * Unreference a dictionary initialized by init_var_dict().
22904 */
22905 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022906unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022907{
22908 /* Now the dict needs to be freed if no one else is using it, go back to
22909 * normal reference counting. */
22910 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22911 dict_unref(dict);
22912}
22913
22914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022916 * Frees all allocated variables and the value they contain.
22917 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 */
22919 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022920vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022921{
22922 vars_clear_ext(ht, TRUE);
22923}
22924
22925/*
22926 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22927 */
22928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022929vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022930{
Bram Moolenaara7043832005-01-21 11:56:39 +000022931 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022932 hashitem_T *hi;
22933 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934
Bram Moolenaar33570922005-01-25 22:26:29 +000022935 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022936 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022937 for (hi = ht->ht_array; todo > 0; ++hi)
22938 {
22939 if (!HASHITEM_EMPTY(hi))
22940 {
22941 --todo;
22942
Bram Moolenaar33570922005-01-25 22:26:29 +000022943 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022944 * ht_array might change then. hash_clear() takes care of it
22945 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022946 v = HI2DI(hi);
22947 if (free_val)
22948 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022949 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022950 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022951 }
22952 }
22953 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022954 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955}
22956
Bram Moolenaara7043832005-01-21 11:56:39 +000022957/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022958 * Delete a variable from hashtab "ht" at item "hi".
22959 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022962delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963{
Bram Moolenaar33570922005-01-25 22:26:29 +000022964 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022965
22966 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022967 clear_tv(&di->di_tv);
22968 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969}
22970
22971/*
22972 * List the value of one internal variable.
22973 */
22974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022975list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022977 char_u *tofree;
22978 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022979 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022980
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022981 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022982 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022983 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022984 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985}
22986
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022988list_one_var_a(
22989 char_u *prefix,
22990 char_u *name,
22991 int type,
22992 char_u *string,
22993 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994{
Bram Moolenaar31859182007-08-14 20:41:13 +000022995 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22996 msg_start();
22997 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 if (name != NULL) /* "a:" vars don't have a name stored */
22999 msg_puts(name);
23000 msg_putchar(' ');
23001 msg_advance(22);
23002 if (type == VAR_NUMBER)
23003 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023004 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023005 msg_putchar('*');
23006 else if (type == VAR_LIST)
23007 {
23008 msg_putchar('[');
23009 if (*string == '[')
23010 ++string;
23011 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023012 else if (type == VAR_DICT)
23013 {
23014 msg_putchar('{');
23015 if (*string == '{')
23016 ++string;
23017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 else
23019 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023020
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023022
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023023 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023024 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023025 if (*first)
23026 {
23027 msg_clr_eos();
23028 *first = FALSE;
23029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030}
23031
23032/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023033 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 * If the variable already exists, the value is updated.
23035 * Otherwise the variable is created.
23036 */
23037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023038set_var(
23039 char_u *name,
23040 typval_T *tv,
23041 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042{
Bram Moolenaar33570922005-01-25 22:26:29 +000023043 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023045 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023047 ht = find_var_ht(name, &varname);
23048 if (ht == NULL || *varname == NUL)
23049 {
23050 EMSG2(_(e_illvar), name);
23051 return;
23052 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023053 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023054
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023055 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23056 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023057 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023058
Bram Moolenaar33570922005-01-25 22:26:29 +000023059 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023061 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023062 if (var_check_ro(v->di_flags, name, FALSE)
23063 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023064 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023065
23066 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023067 * Handle setting internal v: variables separately where needed to
23068 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023069 */
23070 if (ht == &vimvarht)
23071 {
23072 if (v->di_tv.v_type == VAR_STRING)
23073 {
23074 vim_free(v->di_tv.vval.v_string);
23075 if (copy || tv->v_type != VAR_STRING)
23076 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23077 else
23078 {
23079 /* Take over the string to avoid an extra alloc/free. */
23080 v->di_tv.vval.v_string = tv->vval.v_string;
23081 tv->vval.v_string = NULL;
23082 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023083 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023084 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023085 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023086 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023087 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023088 if (STRCMP(varname, "searchforward") == 0)
23089 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023090#ifdef FEAT_SEARCH_EXTRA
23091 else if (STRCMP(varname, "hlsearch") == 0)
23092 {
23093 no_hlsearch = !v->di_tv.vval.v_number;
23094 redraw_all_later(SOME_VALID);
23095 }
23096#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023097 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023098 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023099 else if (v->di_tv.v_type != tv->v_type)
23100 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023101 }
23102
23103 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 }
23105 else /* add a new variable */
23106 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023107 /* Can't add "v:" variable. */
23108 if (ht == &vimvarht)
23109 {
23110 EMSG2(_(e_illvar), name);
23111 return;
23112 }
23113
Bram Moolenaar92124a32005-06-17 22:03:40 +000023114 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023115 if (!valid_varname(varname))
23116 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023117
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023118 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23119 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023120 if (v == NULL)
23121 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023122 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023123 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023125 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 return;
23127 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023128 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023130
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023131 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023132 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023133 else
23134 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023135 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023136 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023137 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139}
23140
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023141/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023142 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023143 * Also give an error message.
23144 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020023145 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023146var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023147{
23148 if (flags & DI_FLAGS_RO)
23149 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023150 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023151 return TRUE;
23152 }
23153 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23154 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023155 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023156 return TRUE;
23157 }
23158 return FALSE;
23159}
23160
23161/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023162 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23163 * Also give an error message.
23164 */
23165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023166var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023167{
23168 if (flags & DI_FLAGS_FIX)
23169 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023170 EMSG2(_("E795: Cannot delete variable %s"),
23171 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023172 return TRUE;
23173 }
23174 return FALSE;
23175}
23176
23177/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023178 * Check if a funcref is assigned to a valid variable name.
23179 * Return TRUE and give an error if not.
23180 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020023181 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023182var_check_func_name(
23183 char_u *name, /* points to start of variable name */
23184 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023185{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023186 /* Allow for w: b: s: and t:. */
23187 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023188 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23189 ? name[2] : name[0]))
23190 {
23191 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23192 name);
23193 return TRUE;
23194 }
23195 /* Don't allow hiding a function. When "v" is not NULL we might be
23196 * assigning another function to the same var, the type is checked
23197 * below. */
23198 if (new_var && function_exists(name))
23199 {
23200 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23201 name);
23202 return TRUE;
23203 }
23204 return FALSE;
23205}
23206
23207/*
23208 * Check if a variable name is valid.
23209 * Return FALSE and give an error if not.
23210 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020023211 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023212valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023213{
23214 char_u *p;
23215
23216 for (p = varname; *p != NUL; ++p)
23217 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23218 && *p != AUTOLOAD_CHAR)
23219 {
23220 EMSG2(_(e_illvar), varname);
23221 return FALSE;
23222 }
23223 return TRUE;
23224}
23225
23226/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023227 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023228 * Also give an error message, using "name" or _("name") when use_gettext is
23229 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023230 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020023231 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023232tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023233{
23234 if (lock & VAR_LOCKED)
23235 {
23236 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023237 name == NULL ? (char_u *)_("Unknown")
23238 : use_gettext ? (char_u *)_(name)
23239 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023240 return TRUE;
23241 }
23242 if (lock & VAR_FIXED)
23243 {
23244 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023245 name == NULL ? (char_u *)_("Unknown")
23246 : use_gettext ? (char_u *)_(name)
23247 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023248 return TRUE;
23249 }
23250 return FALSE;
23251}
23252
23253/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023254 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023255 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023256 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023257 * It is OK for "from" and "to" to point to the same item. This is used to
23258 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023259 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023260 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023261copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023263 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023264 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023265 switch (from->v_type)
23266 {
23267 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023268 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023269 to->vval.v_number = from->vval.v_number;
23270 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023271 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023272#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023273 to->vval.v_float = from->vval.v_float;
23274 break;
23275#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023276 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023277#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023278 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023279 if (to->vval.v_job != NULL)
23280 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023281 break;
23282#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023283 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023284#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023285 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023286 if (to->vval.v_channel != NULL)
23287 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023288 break;
23289#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023290 case VAR_STRING:
23291 case VAR_FUNC:
23292 if (from->vval.v_string == NULL)
23293 to->vval.v_string = NULL;
23294 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023295 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023296 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023297 if (from->v_type == VAR_FUNC)
23298 func_ref(to->vval.v_string);
23299 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023300 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023301 case VAR_PARTIAL:
23302 if (from->vval.v_partial == NULL)
23303 to->vval.v_partial = NULL;
23304 else
23305 {
23306 to->vval.v_partial = from->vval.v_partial;
23307 ++to->vval.v_partial->pt_refcount;
23308 }
23309 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023310 case VAR_LIST:
23311 if (from->vval.v_list == NULL)
23312 to->vval.v_list = NULL;
23313 else
23314 {
23315 to->vval.v_list = from->vval.v_list;
23316 ++to->vval.v_list->lv_refcount;
23317 }
23318 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023319 case VAR_DICT:
23320 if (from->vval.v_dict == NULL)
23321 to->vval.v_dict = NULL;
23322 else
23323 {
23324 to->vval.v_dict = from->vval.v_dict;
23325 ++to->vval.v_dict->dv_refcount;
23326 }
23327 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023328 case VAR_UNKNOWN:
23329 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023330 break;
23331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023332}
23333
23334/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023335 * Make a copy of an item.
23336 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023337 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23338 * reference to an already copied list/dict can be used.
23339 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023340 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020023341 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023342item_copy(
23343 typval_T *from,
23344 typval_T *to,
23345 int deep,
23346 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023347{
23348 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023349 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023350
Bram Moolenaar33570922005-01-25 22:26:29 +000023351 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023352 {
23353 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023354 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023355 }
23356 ++recurse;
23357
23358 switch (from->v_type)
23359 {
23360 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023361 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023362 case VAR_STRING:
23363 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023364 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023365 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023366 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023367 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023368 copy_tv(from, to);
23369 break;
23370 case VAR_LIST:
23371 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023372 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023373 if (from->vval.v_list == NULL)
23374 to->vval.v_list = NULL;
23375 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23376 {
23377 /* use the copy made earlier */
23378 to->vval.v_list = from->vval.v_list->lv_copylist;
23379 ++to->vval.v_list->lv_refcount;
23380 }
23381 else
23382 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23383 if (to->vval.v_list == NULL)
23384 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023385 break;
23386 case VAR_DICT:
23387 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023388 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023389 if (from->vval.v_dict == NULL)
23390 to->vval.v_dict = NULL;
23391 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23392 {
23393 /* use the copy made earlier */
23394 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23395 ++to->vval.v_dict->dv_refcount;
23396 }
23397 else
23398 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23399 if (to->vval.v_dict == NULL)
23400 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023401 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023402 case VAR_UNKNOWN:
23403 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023404 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023405 }
23406 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023407 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023408}
23409
23410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411 * ":echo expr1 ..." print each argument separated with a space, add a
23412 * newline at the end.
23413 * ":echon expr1 ..." print each argument plain.
23414 */
23415 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023416ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417{
23418 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023419 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023420 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421 char_u *p;
23422 int needclr = TRUE;
23423 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023424 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425
23426 if (eap->skip)
23427 ++emsg_skip;
23428 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23429 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023430 /* If eval1() causes an error message the text from the command may
23431 * still need to be cleared. E.g., "echo 22,44". */
23432 need_clr_eos = needclr;
23433
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023435 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023436 {
23437 /*
23438 * Report the invalid expression unless the expression evaluation
23439 * has been cancelled due to an aborting error, an interrupt, or an
23440 * exception.
23441 */
23442 if (!aborting())
23443 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023444 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445 break;
23446 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023447 need_clr_eos = FALSE;
23448
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 if (!eap->skip)
23450 {
23451 if (atstart)
23452 {
23453 atstart = FALSE;
23454 /* Call msg_start() after eval1(), evaluating the expression
23455 * may cause a message to appear. */
23456 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023457 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023458 /* Mark the saved text as finishing the line, so that what
23459 * follows is displayed on a new line when scrolling back
23460 * at the more prompt. */
23461 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464 }
23465 else if (eap->cmdidx == CMD_echo)
23466 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023467 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023468 if (p != NULL)
23469 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023471 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023473 if (*p != TAB && needclr)
23474 {
23475 /* remove any text still there from the command */
23476 msg_clr_eos();
23477 needclr = FALSE;
23478 }
23479 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 }
23481 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023482 {
23483#ifdef FEAT_MBYTE
23484 if (has_mbyte)
23485 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023486 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023487
23488 (void)msg_outtrans_len_attr(p, i, echo_attr);
23489 p += i - 1;
23490 }
23491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023493 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023496 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023498 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 arg = skipwhite(arg);
23500 }
23501 eap->nextcmd = check_nextcmd(arg);
23502
23503 if (eap->skip)
23504 --emsg_skip;
23505 else
23506 {
23507 /* remove text that may still be there from the command */
23508 if (needclr)
23509 msg_clr_eos();
23510 if (eap->cmdidx == CMD_echo)
23511 msg_end();
23512 }
23513}
23514
23515/*
23516 * ":echohl {name}".
23517 */
23518 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023519ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520{
23521 int id;
23522
23523 id = syn_name2id(eap->arg);
23524 if (id == 0)
23525 echo_attr = 0;
23526 else
23527 echo_attr = syn_id2attr(id);
23528}
23529
23530/*
23531 * ":execute expr1 ..." execute the result of an expression.
23532 * ":echomsg expr1 ..." Print a message
23533 * ":echoerr expr1 ..." Print an error
23534 * Each gets spaces around each argument and a newline at the end for
23535 * echo commands
23536 */
23537 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023538ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539{
23540 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023541 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 int ret = OK;
23543 char_u *p;
23544 garray_T ga;
23545 int len;
23546 int save_did_emsg;
23547
23548 ga_init2(&ga, 1, 80);
23549
23550 if (eap->skip)
23551 ++emsg_skip;
23552 while (*arg != NUL && *arg != '|' && *arg != '\n')
23553 {
23554 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023555 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556 {
23557 /*
23558 * Report the invalid expression unless the expression evaluation
23559 * has been cancelled due to an aborting error, an interrupt, or an
23560 * exception.
23561 */
23562 if (!aborting())
23563 EMSG2(_(e_invexpr2), p);
23564 ret = FAIL;
23565 break;
23566 }
23567
23568 if (!eap->skip)
23569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023570 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571 len = (int)STRLEN(p);
23572 if (ga_grow(&ga, len + 2) == FAIL)
23573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023574 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 ret = FAIL;
23576 break;
23577 }
23578 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023579 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 ga.ga_len += len;
23582 }
23583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023584 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585 arg = skipwhite(arg);
23586 }
23587
23588 if (ret != FAIL && ga.ga_data != NULL)
23589 {
23590 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023593 out_flush();
23594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 else if (eap->cmdidx == CMD_echoerr)
23596 {
23597 /* We don't want to abort following commands, restore did_emsg. */
23598 save_did_emsg = did_emsg;
23599 EMSG((char_u *)ga.ga_data);
23600 if (!force_abort)
23601 did_emsg = save_did_emsg;
23602 }
23603 else if (eap->cmdidx == CMD_execute)
23604 do_cmdline((char_u *)ga.ga_data,
23605 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23606 }
23607
23608 ga_clear(&ga);
23609
23610 if (eap->skip)
23611 --emsg_skip;
23612
23613 eap->nextcmd = check_nextcmd(arg);
23614}
23615
23616/*
23617 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23618 * "arg" points to the "&" or '+' when called, to "option" when returning.
23619 * Returns NULL when no option name found. Otherwise pointer to the char
23620 * after the option name.
23621 */
23622 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023623find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624{
23625 char_u *p = *arg;
23626
23627 ++p;
23628 if (*p == 'g' && p[1] == ':')
23629 {
23630 *opt_flags = OPT_GLOBAL;
23631 p += 2;
23632 }
23633 else if (*p == 'l' && p[1] == ':')
23634 {
23635 *opt_flags = OPT_LOCAL;
23636 p += 2;
23637 }
23638 else
23639 *opt_flags = 0;
23640
23641 if (!ASCII_ISALPHA(*p))
23642 return NULL;
23643 *arg = p;
23644
23645 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23646 p += 4; /* termcap option */
23647 else
23648 while (ASCII_ISALPHA(*p))
23649 ++p;
23650 return p;
23651}
23652
23653/*
23654 * ":function"
23655 */
23656 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023657ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658{
23659 char_u *theline;
23660 int j;
23661 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023663 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 char_u *name = NULL;
23665 char_u *p;
23666 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023667 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 garray_T newargs;
23669 garray_T newlines;
23670 int varargs = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671 int flags = 0;
23672 ufunc_T *fp;
23673 int indent;
23674 int nesting;
23675 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023676 dictitem_T *v;
23677 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023678 static int func_nr = 0; /* number for nameless function */
23679 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023680 hashtab_T *ht;
23681 int todo;
23682 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023683 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684
23685 /*
23686 * ":function" without argument: list functions.
23687 */
23688 if (ends_excmd(*eap->arg))
23689 {
23690 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023691 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023692 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023693 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023694 {
23695 if (!HASHITEM_EMPTY(hi))
23696 {
23697 --todo;
23698 fp = HI2UF(hi);
23699 if (!isdigit(*fp->uf_name))
23700 list_func_head(fp, FALSE);
23701 }
23702 }
23703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704 eap->nextcmd = check_nextcmd(eap->arg);
23705 return;
23706 }
23707
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023708 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023709 * ":function /pat": list functions matching pattern.
23710 */
23711 if (*eap->arg == '/')
23712 {
23713 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23714 if (!eap->skip)
23715 {
23716 regmatch_T regmatch;
23717
23718 c = *p;
23719 *p = NUL;
23720 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23721 *p = c;
23722 if (regmatch.regprog != NULL)
23723 {
23724 regmatch.rm_ic = p_ic;
23725
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023726 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023727 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23728 {
23729 if (!HASHITEM_EMPTY(hi))
23730 {
23731 --todo;
23732 fp = HI2UF(hi);
23733 if (!isdigit(*fp->uf_name)
23734 && vim_regexec(&regmatch, fp->uf_name, 0))
23735 list_func_head(fp, FALSE);
23736 }
23737 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023738 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023739 }
23740 }
23741 if (*p == '/')
23742 ++p;
23743 eap->nextcmd = check_nextcmd(p);
23744 return;
23745 }
23746
23747 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023748 * Get the function name. There are these situations:
23749 * func normal function name
23750 * "name" == func, "fudi.fd_dict" == NULL
23751 * dict.func new dictionary entry
23752 * "name" == NULL, "fudi.fd_dict" set,
23753 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23754 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023755 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023756 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23757 * dict.func existing dict entry that's not a Funcref
23758 * "name" == NULL, "fudi.fd_dict" set,
23759 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023760 * s:func script-local function name
23761 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023764 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023765 paren = (vim_strchr(p, '(') != NULL);
23766 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 {
23768 /*
23769 * Return on an invalid expression in braces, unless the expression
23770 * evaluation has been cancelled due to an aborting error, an
23771 * interrupt, or an exception.
23772 */
23773 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023774 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023775 if (!eap->skip && fudi.fd_newkey != NULL)
23776 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023777 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023778 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780 else
23781 eap->skip = TRUE;
23782 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023783
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 /* An error in a function call during evaluation of an expression in magic
23785 * braces should not cause the function not to be defined. */
23786 saved_did_emsg = did_emsg;
23787 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788
23789 /*
23790 * ":function func" with only function name: list function.
23791 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023792 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793 {
23794 if (!ends_excmd(*skipwhite(p)))
23795 {
23796 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023797 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 }
23799 eap->nextcmd = check_nextcmd(p);
23800 if (eap->nextcmd != NULL)
23801 *p = NUL;
23802 if (!eap->skip && !got_int)
23803 {
23804 fp = find_func(name);
23805 if (fp != NULL)
23806 {
23807 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023808 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023810 if (FUNCLINE(fp, j) == NULL)
23811 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812 msg_putchar('\n');
23813 msg_outnum((long)(j + 1));
23814 if (j < 9)
23815 msg_putchar(' ');
23816 if (j < 99)
23817 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023818 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 out_flush(); /* show a line at a time */
23820 ui_breakcheck();
23821 }
23822 if (!got_int)
23823 {
23824 msg_putchar('\n');
23825 msg_puts((char_u *)" endfunction");
23826 }
23827 }
23828 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023829 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023831 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832 }
23833
23834 /*
23835 * ":function name(arg1, arg2)" Define function.
23836 */
23837 p = skipwhite(p);
23838 if (*p != '(')
23839 {
23840 if (!eap->skip)
23841 {
23842 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023843 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844 }
23845 /* attempt to continue by skipping some text */
23846 if (vim_strchr(p, '(') != NULL)
23847 p = vim_strchr(p, '(');
23848 }
23849 p = skipwhite(p + 1);
23850
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23852
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023853 if (!eap->skip)
23854 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023855 /* Check the name of the function. Unless it's a dictionary function
23856 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023857 if (name != NULL)
23858 arg = name;
23859 else
23860 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023861 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023862 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23863 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023864 {
23865 if (*arg == K_SPECIAL)
23866 j = 3;
23867 else
23868 j = 0;
23869 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23870 : eval_isnamec(arg[j])))
23871 ++j;
23872 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023873 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023874 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023875 /* Disallow using the g: dict. */
23876 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23877 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023878 }
23879
Bram Moolenaar069c1e72016-07-15 21:25:08 +020023880 if (get_function_args(&p, ')', &newargs, &varargs, eap->skip) == FAIL)
23881 goto errret_2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882
Bram Moolenaare9a41262005-01-15 22:18:47 +000023883 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884 for (;;)
23885 {
23886 p = skipwhite(p);
23887 if (STRNCMP(p, "range", 5) == 0)
23888 {
23889 flags |= FC_RANGE;
23890 p += 5;
23891 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023892 else if (STRNCMP(p, "dict", 4) == 0)
23893 {
23894 flags |= FC_DICT;
23895 p += 4;
23896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 else if (STRNCMP(p, "abort", 5) == 0)
23898 {
23899 flags |= FC_ABORT;
23900 p += 5;
23901 }
23902 else
23903 break;
23904 }
23905
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023906 /* When there is a line break use what follows for the function body.
23907 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23908 if (*p == '\n')
23909 line_arg = p + 1;
23910 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023911 EMSG(_(e_trailing));
23912
23913 /*
23914 * Read the body of the function, until ":endfunction" is found.
23915 */
23916 if (KeyTyped)
23917 {
23918 /* Check if the function already exists, don't let the user type the
23919 * whole function before telling him it doesn't work! For a script we
23920 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023921 if (!eap->skip && !eap->forceit)
23922 {
23923 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23924 EMSG(_(e_funcdict));
23925 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023926 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023928
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023929 if (!eap->skip && did_emsg)
23930 goto erret;
23931
Bram Moolenaar071d4272004-06-13 20:20:40 +000023932 msg_putchar('\n'); /* don't overwrite the function name */
23933 cmdline_row = msg_row;
23934 }
23935
23936 indent = 2;
23937 nesting = 0;
23938 for (;;)
23939 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023940 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023941 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023942 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023943 saved_wait_return = FALSE;
23944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023945 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023946 sourcing_lnum_off = sourcing_lnum;
23947
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023948 if (line_arg != NULL)
23949 {
23950 /* Use eap->arg, split up in parts by line breaks. */
23951 theline = line_arg;
23952 p = vim_strchr(theline, '\n');
23953 if (p == NULL)
23954 line_arg += STRLEN(line_arg);
23955 else
23956 {
23957 *p = NUL;
23958 line_arg = p + 1;
23959 }
23960 }
23961 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 theline = getcmdline(':', 0L, indent);
23963 else
23964 theline = eap->getline(':', eap->cookie, indent);
23965 if (KeyTyped)
23966 lines_left = Rows - 1;
23967 if (theline == NULL)
23968 {
23969 EMSG(_("E126: Missing :endfunction"));
23970 goto erret;
23971 }
23972
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023973 /* Detect line continuation: sourcing_lnum increased more than one. */
23974 if (sourcing_lnum > sourcing_lnum_off + 1)
23975 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23976 else
23977 sourcing_lnum_off = 0;
23978
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 if (skip_until != NULL)
23980 {
23981 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23982 * don't check for ":endfunc". */
23983 if (STRCMP(theline, skip_until) == 0)
23984 {
23985 vim_free(skip_until);
23986 skip_until = NULL;
23987 }
23988 }
23989 else
23990 {
23991 /* skip ':' and blanks*/
23992 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23993 ;
23994
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023995 /* Check for "endfunction". */
23996 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023998 if (line_arg == NULL)
23999 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000 break;
24001 }
24002
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024003 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 * at "end". */
24005 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24006 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024007 else if (STRNCMP(p, "if", 2) == 0
24008 || STRNCMP(p, "wh", 2) == 0
24009 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 || STRNCMP(p, "try", 3) == 0)
24011 indent += 2;
24012
24013 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024014 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024015 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024016 if (*p == '!')
24017 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024019 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024020 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024021 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024022 ++nesting;
24023 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024 }
24025 }
24026
24027 /* Check for ":append" or ":insert". */
24028 p = skip_range(p, NULL);
24029 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24030 || (p[0] == 'i'
24031 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24032 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24033 skip_until = vim_strsave((char_u *)".");
24034
24035 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24036 arg = skipwhite(skiptowhite(p));
24037 if (arg[0] == '<' && arg[1] =='<'
24038 && ((p[0] == 'p' && p[1] == 'y'
24039 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24040 || (p[0] == 'p' && p[1] == 'e'
24041 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24042 || (p[0] == 't' && p[1] == 'c'
24043 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024044 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24045 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024046 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24047 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024048 || (p[0] == 'm' && p[1] == 'z'
24049 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 ))
24051 {
24052 /* ":python <<" continues until a dot, like ":append" */
24053 p = skipwhite(arg + 2);
24054 if (*p == NUL)
24055 skip_until = vim_strsave((char_u *)".");
24056 else
24057 skip_until = vim_strsave(p);
24058 }
24059 }
24060
24061 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024062 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024063 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024064 if (line_arg == NULL)
24065 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024066 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024067 }
24068
24069 /* Copy the line to newly allocated memory. get_one_sourceline()
24070 * allocates 250 bytes per line, this saves 80% on average. The cost
24071 * is an extra alloc/free. */
24072 p = vim_strsave(theline);
24073 if (p != NULL)
24074 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024075 if (line_arg == NULL)
24076 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024077 theline = p;
24078 }
24079
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024080 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24081
24082 /* Add NULL lines for continuation lines, so that the line count is
24083 * equal to the index in the growarray. */
24084 while (sourcing_lnum_off-- > 0)
24085 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024086
24087 /* Check for end of eap->arg. */
24088 if (line_arg != NULL && *line_arg == NUL)
24089 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 }
24091
24092 /* Don't define the function when skipping commands or when an error was
24093 * detected. */
24094 if (eap->skip || did_emsg)
24095 goto erret;
24096
24097 /*
24098 * If there are no errors, add the function
24099 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024100 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024101 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024102 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024103 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024104 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024105 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024106 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024107 goto erret;
24108 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024109
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024110 fp = find_func(name);
24111 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024113 if (!eap->forceit)
24114 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024115 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024116 goto erret;
24117 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024118 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024119 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024120 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024121 name);
24122 goto erret;
24123 }
24124 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024125 ga_clear_strings(&(fp->uf_args));
24126 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024127 vim_free(name);
24128 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 }
24131 else
24132 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024133 char numbuf[20];
24134
24135 fp = NULL;
24136 if (fudi.fd_newkey == NULL && !eap->forceit)
24137 {
24138 EMSG(_(e_funcdict));
24139 goto erret;
24140 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024141 if (fudi.fd_di == NULL)
24142 {
24143 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024144 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024145 goto erret;
24146 }
24147 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024148 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024149 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024150
24151 /* Give the function a sequential number. Can only be used with a
24152 * Funcref! */
24153 vim_free(name);
24154 sprintf(numbuf, "%d", ++func_nr);
24155 name = vim_strsave((char_u *)numbuf);
24156 if (name == NULL)
24157 goto erret;
24158 }
24159
24160 if (fp == NULL)
24161 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024162 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024163 {
24164 int slen, plen;
24165 char_u *scriptname;
24166
24167 /* Check that the autoload name matches the script name. */
24168 j = FAIL;
24169 if (sourcing_name != NULL)
24170 {
24171 scriptname = autoload_name(name);
24172 if (scriptname != NULL)
24173 {
24174 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024175 plen = (int)STRLEN(p);
24176 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024177 if (slen > plen && fnamecmp(p,
24178 sourcing_name + slen - plen) == 0)
24179 j = OK;
24180 vim_free(scriptname);
24181 }
24182 }
24183 if (j == FAIL)
24184 {
24185 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24186 goto erret;
24187 }
24188 }
24189
24190 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191 if (fp == NULL)
24192 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024193
24194 if (fudi.fd_dict != NULL)
24195 {
24196 if (fudi.fd_di == NULL)
24197 {
24198 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024199 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024200 if (fudi.fd_di == NULL)
24201 {
24202 vim_free(fp);
24203 goto erret;
24204 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024205 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24206 {
24207 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024208 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024209 goto erret;
24210 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024211 }
24212 else
24213 /* overwrite existing dict entry */
24214 clear_tv(&fudi.fd_di->di_tv);
24215 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024216 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024217 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024218 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024219
24220 /* behave like "dict" was used */
24221 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024222 }
24223
Bram Moolenaar071d4272004-06-13 20:20:40 +000024224 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024225 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024226 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24227 {
24228 vim_free(fp);
24229 goto erret;
24230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024231 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024232 fp->uf_args = newargs;
24233 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024234#ifdef FEAT_PROFILE
24235 fp->uf_tml_count = NULL;
24236 fp->uf_tml_total = NULL;
24237 fp->uf_tml_self = NULL;
24238 fp->uf_profiling = FALSE;
24239 if (prof_def_func())
24240 func_do_profile(fp);
24241#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024242 fp->uf_varargs = varargs;
24243 fp->uf_flags = flags;
24244 fp->uf_calls = 0;
24245 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024246 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247
24248erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249 ga_clear_strings(&newargs);
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024250errret_2:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024252ret_free:
24253 vim_free(skip_until);
24254 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024256 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024257 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258}
24259
24260/*
24261 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024262 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024264 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024265 * TFN_INT: internal function name OK
24266 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024267 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268 * Advances "pp" to just after the function name (if no error).
24269 */
24270 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024271trans_function_name(
24272 char_u **pp,
24273 int skip, /* only find the end, don't evaluate */
24274 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024275 funcdict_T *fdp, /* return: info about dictionary used */
24276 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024278 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 char_u *start;
24280 char_u *end;
24281 int lead;
24282 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024284 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024285
24286 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024287 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024289
24290 /* Check for hard coded <SNR>: already translated function ID (from a user
24291 * command). */
24292 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24293 && (*pp)[2] == (int)KE_SNR)
24294 {
24295 *pp += 3;
24296 len = get_id_len(pp) + 3;
24297 return vim_strnsave(start, len);
24298 }
24299
24300 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24301 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024303 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024305
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024306 /* Note that TFN_ flags use the same values as GLV_ flags. */
24307 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024308 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024309 if (end == start)
24310 {
24311 if (!skip)
24312 EMSG(_("E129: Function name required"));
24313 goto theend;
24314 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024315 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024316 {
24317 /*
24318 * Report an invalid expression in braces, unless the expression
24319 * evaluation has been cancelled due to an aborting error, an
24320 * interrupt, or an exception.
24321 */
24322 if (!aborting())
24323 {
24324 if (end != NULL)
24325 EMSG2(_(e_invarg2), start);
24326 }
24327 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024328 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024329 goto theend;
24330 }
24331
24332 if (lv.ll_tv != NULL)
24333 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024334 if (fdp != NULL)
24335 {
24336 fdp->fd_dict = lv.ll_dict;
24337 fdp->fd_newkey = lv.ll_newkey;
24338 lv.ll_newkey = NULL;
24339 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024340 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024341 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24342 {
24343 name = vim_strsave(lv.ll_tv->vval.v_string);
24344 *pp = end;
24345 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024346 else if (lv.ll_tv->v_type == VAR_PARTIAL
24347 && lv.ll_tv->vval.v_partial != NULL)
24348 {
24349 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24350 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024351 if (partial != NULL)
24352 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024353 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024354 else
24355 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024356 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24357 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024358 EMSG(_(e_funcref));
24359 else
24360 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024361 name = NULL;
24362 }
24363 goto theend;
24364 }
24365
24366 if (lv.ll_name == NULL)
24367 {
24368 /* Error found, but continue after the function name. */
24369 *pp = end;
24370 goto theend;
24371 }
24372
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024373 /* Check if the name is a Funcref. If so, use the value. */
24374 if (lv.ll_exp_name != NULL)
24375 {
24376 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024377 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024378 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024379 if (name == lv.ll_exp_name)
24380 name = NULL;
24381 }
24382 else
24383 {
24384 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024385 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024386 if (name == *pp)
24387 name = NULL;
24388 }
24389 if (name != NULL)
24390 {
24391 name = vim_strsave(name);
24392 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024393 if (STRNCMP(name, "<SNR>", 5) == 0)
24394 {
24395 /* Change "<SNR>" to the byte sequence. */
24396 name[0] = K_SPECIAL;
24397 name[1] = KS_EXTRA;
24398 name[2] = (int)KE_SNR;
24399 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24400 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024401 goto theend;
24402 }
24403
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024404 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024405 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024406 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024407 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24408 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24409 {
24410 /* When there was "s:" already or the name expanded to get a
24411 * leading "s:" then remove it. */
24412 lv.ll_name += 2;
24413 len -= 2;
24414 lead = 2;
24415 }
24416 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024417 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024418 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024419 /* skip over "s:" and "g:" */
24420 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024421 lv.ll_name += 2;
24422 len = (int)(end - lv.ll_name);
24423 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024424
24425 /*
24426 * Copy the function name to allocated memory.
24427 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24428 * Accept <SNR>123_name() outside a script.
24429 */
24430 if (skip)
24431 lead = 0; /* do nothing */
24432 else if (lead > 0)
24433 {
24434 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024435 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24436 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024437 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024438 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024439 if (current_SID <= 0)
24440 {
24441 EMSG(_(e_usingsid));
24442 goto theend;
24443 }
24444 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24445 lead += (int)STRLEN(sid_buf);
24446 }
24447 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024448 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024449 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024450 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024451 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024452 goto theend;
24453 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024454 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024455 {
24456 char_u *cp = vim_strchr(lv.ll_name, ':');
24457
24458 if (cp != NULL && cp < end)
24459 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024460 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024461 goto theend;
24462 }
24463 }
24464
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024465 name = alloc((unsigned)(len + lead + 1));
24466 if (name != NULL)
24467 {
24468 if (lead > 0)
24469 {
24470 name[0] = K_SPECIAL;
24471 name[1] = KS_EXTRA;
24472 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024473 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024474 STRCPY(name + 3, sid_buf);
24475 }
24476 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024477 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024478 }
24479 *pp = end;
24480
24481theend:
24482 clear_lval(&lv);
24483 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484}
24485
24486/*
24487 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24488 * Return 2 if "p" starts with "s:".
24489 * Return 0 otherwise.
24490 */
24491 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024492eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024494 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24495 * the standard library function. */
24496 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24497 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498 return 5;
24499 if (p[0] == 's' && p[1] == ':')
24500 return 2;
24501 return 0;
24502}
24503
24504/*
24505 * Return TRUE if "p" starts with "<SID>" or "s:".
24506 * Only works if eval_fname_script() returned non-zero for "p"!
24507 */
24508 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024509eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510{
24511 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24512}
24513
24514/*
24515 * List the head of the function: "name(arg1, arg2)".
24516 */
24517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024518list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024519{
24520 int j;
24521
24522 msg_start();
24523 if (indent)
24524 MSG_PUTS(" ");
24525 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024526 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 {
24528 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024529 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530 }
24531 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024532 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024534 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 {
24536 if (j)
24537 MSG_PUTS(", ");
24538 msg_puts(FUNCARG(fp, j));
24539 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024540 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 {
24542 if (j)
24543 MSG_PUTS(", ");
24544 MSG_PUTS("...");
24545 }
24546 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024547 if (fp->uf_flags & FC_ABORT)
24548 MSG_PUTS(" abort");
24549 if (fp->uf_flags & FC_RANGE)
24550 MSG_PUTS(" range");
24551 if (fp->uf_flags & FC_DICT)
24552 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024553 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024554 if (p_verbose > 0)
24555 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024556}
24557
24558/*
24559 * Find a function by name, return pointer to it in ufuncs.
24560 * Return NULL for unknown function.
24561 */
24562 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024563find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024565 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024567 hi = hash_find(&func_hashtab, name);
24568 if (!HASHITEM_EMPTY(hi))
24569 return HI2UF(hi);
24570 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024571}
24572
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024573#if defined(EXITFREE) || defined(PROTO)
24574 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024575free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024576{
24577 hashitem_T *hi;
24578
24579 /* Need to start all over every time, because func_free() may change the
24580 * hash table. */
24581 while (func_hashtab.ht_used > 0)
24582 for (hi = func_hashtab.ht_array; ; ++hi)
24583 if (!HASHITEM_EMPTY(hi))
24584 {
24585 func_free(HI2UF(hi));
24586 break;
24587 }
24588}
24589#endif
24590
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024591 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024592translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024593{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024594 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024595 return find_internal_func(name) >= 0;
24596 return find_func(name) != NULL;
24597}
24598
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024599/*
24600 * Return TRUE if a function "name" exists.
24601 */
24602 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024603function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024604{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024605 char_u *nm = name;
24606 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024607 int n = FALSE;
24608
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024609 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024610 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024611 nm = skipwhite(nm);
24612
24613 /* Only accept "funcname", "funcname ", "funcname (..." and
24614 * "funcname(...", not "funcname!...". */
24615 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024616 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024617 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024618 return n;
24619}
24620
Bram Moolenaara1544c02013-05-30 12:35:52 +020024621 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024622get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024623{
24624 char_u *nm = name;
24625 char_u *p;
24626
Bram Moolenaar65639032016-03-16 21:40:30 +010024627 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024628
24629 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024630 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024631 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024632
Bram Moolenaara1544c02013-05-30 12:35:52 +020024633 vim_free(p);
24634 return NULL;
24635}
24636
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024637/*
24638 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024639 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24640 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024641 */
24642 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024643builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024644{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024645 char_u *p;
24646
24647 if (!ASCII_ISLOWER(name[0]))
24648 return FALSE;
24649 p = vim_strchr(name, AUTOLOAD_CHAR);
24650 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024651}
24652
Bram Moolenaar05159a02005-02-26 23:04:13 +000024653#if defined(FEAT_PROFILE) || defined(PROTO)
24654/*
24655 * Start profiling function "fp".
24656 */
24657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024658func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024659{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024660 int len = fp->uf_lines.ga_len;
24661
24662 if (len == 0)
24663 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024664 fp->uf_tm_count = 0;
24665 profile_zero(&fp->uf_tm_self);
24666 profile_zero(&fp->uf_tm_total);
24667 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024668 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024669 if (fp->uf_tml_total == NULL)
24670 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024671 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024672 if (fp->uf_tml_self == NULL)
24673 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024674 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024675 fp->uf_tml_idx = -1;
24676 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24677 || fp->uf_tml_self == NULL)
24678 return; /* out of memory */
24679
24680 fp->uf_profiling = TRUE;
24681}
24682
24683/*
24684 * Dump the profiling results for all functions in file "fd".
24685 */
24686 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024687func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024688{
24689 hashitem_T *hi;
24690 int todo;
24691 ufunc_T *fp;
24692 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024693 ufunc_T **sorttab;
24694 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024695
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024696 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024697 if (todo == 0)
24698 return; /* nothing to dump */
24699
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024700 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024701
Bram Moolenaar05159a02005-02-26 23:04:13 +000024702 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24703 {
24704 if (!HASHITEM_EMPTY(hi))
24705 {
24706 --todo;
24707 fp = HI2UF(hi);
24708 if (fp->uf_profiling)
24709 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024710 if (sorttab != NULL)
24711 sorttab[st_len++] = fp;
24712
Bram Moolenaar05159a02005-02-26 23:04:13 +000024713 if (fp->uf_name[0] == K_SPECIAL)
24714 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24715 else
24716 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24717 if (fp->uf_tm_count == 1)
24718 fprintf(fd, "Called 1 time\n");
24719 else
24720 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24721 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24722 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24723 fprintf(fd, "\n");
24724 fprintf(fd, "count total (s) self (s)\n");
24725
24726 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24727 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024728 if (FUNCLINE(fp, i) == NULL)
24729 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024730 prof_func_line(fd, fp->uf_tml_count[i],
24731 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024732 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24733 }
24734 fprintf(fd, "\n");
24735 }
24736 }
24737 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024738
24739 if (sorttab != NULL && st_len > 0)
24740 {
24741 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24742 prof_total_cmp);
24743 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24744 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24745 prof_self_cmp);
24746 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24747 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024748
24749 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024750}
Bram Moolenaar73830342005-02-28 22:48:19 +000024751
24752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024753prof_sort_list(
24754 FILE *fd,
24755 ufunc_T **sorttab,
24756 int st_len,
24757 char *title,
24758 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024759{
24760 int i;
24761 ufunc_T *fp;
24762
24763 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24764 fprintf(fd, "count total (s) self (s) function\n");
24765 for (i = 0; i < 20 && i < st_len; ++i)
24766 {
24767 fp = sorttab[i];
24768 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24769 prefer_self);
24770 if (fp->uf_name[0] == K_SPECIAL)
24771 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24772 else
24773 fprintf(fd, " %s()\n", fp->uf_name);
24774 }
24775 fprintf(fd, "\n");
24776}
24777
24778/*
24779 * Print the count and times for one function or function line.
24780 */
24781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024782prof_func_line(
24783 FILE *fd,
24784 int count,
24785 proftime_T *total,
24786 proftime_T *self,
24787 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024788{
24789 if (count > 0)
24790 {
24791 fprintf(fd, "%5d ", count);
24792 if (prefer_self && profile_equal(total, self))
24793 fprintf(fd, " ");
24794 else
24795 fprintf(fd, "%s ", profile_msg(total));
24796 if (!prefer_self && profile_equal(total, self))
24797 fprintf(fd, " ");
24798 else
24799 fprintf(fd, "%s ", profile_msg(self));
24800 }
24801 else
24802 fprintf(fd, " ");
24803}
24804
24805/*
24806 * Compare function for total time sorting.
24807 */
24808 static int
24809#ifdef __BORLANDC__
24810_RTLENTRYF
24811#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024812prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024813{
24814 ufunc_T *p1, *p2;
24815
24816 p1 = *(ufunc_T **)s1;
24817 p2 = *(ufunc_T **)s2;
24818 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24819}
24820
24821/*
24822 * Compare function for self time sorting.
24823 */
24824 static int
24825#ifdef __BORLANDC__
24826_RTLENTRYF
24827#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024828prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024829{
24830 ufunc_T *p1, *p2;
24831
24832 p1 = *(ufunc_T **)s1;
24833 p2 = *(ufunc_T **)s2;
24834 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24835}
24836
Bram Moolenaar05159a02005-02-26 23:04:13 +000024837#endif
24838
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024839/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024840 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024841 * Return TRUE if a package was loaded.
24842 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024843 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024844script_autoload(
24845 char_u *name,
24846 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024847{
24848 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024849 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024850 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024851 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024852
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024853 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024854 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024855 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024856 return FALSE;
24857
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024858 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024859
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024860 /* Find the name in the list of previously loaded package names. Skip
24861 * "autoload/", it's always the same. */
24862 for (i = 0; i < ga_loaded.ga_len; ++i)
24863 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24864 break;
24865 if (!reload && i < ga_loaded.ga_len)
24866 ret = FALSE; /* was loaded already */
24867 else
24868 {
24869 /* Remember the name if it wasn't loaded already. */
24870 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24871 {
24872 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24873 tofree = NULL;
24874 }
24875
24876 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024877 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024878 ret = TRUE;
24879 }
24880
24881 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024882 return ret;
24883}
24884
24885/*
24886 * Return the autoload script name for a function or variable name.
24887 * Returns NULL when out of memory.
24888 */
24889 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024890autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024891{
24892 char_u *p;
24893 char_u *scriptname;
24894
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024895 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024896 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24897 if (scriptname == NULL)
24898 return FALSE;
24899 STRCPY(scriptname, "autoload/");
24900 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024901 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024902 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024903 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024904 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024905 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024906}
24907
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24909
24910/*
24911 * Function given to ExpandGeneric() to obtain the list of user defined
24912 * function names.
24913 */
24914 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024915get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024917 static long_u done;
24918 static hashitem_T *hi;
24919 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920
24921 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024923 done = 0;
24924 hi = func_hashtab.ht_array;
24925 }
24926 if (done < func_hashtab.ht_used)
24927 {
24928 if (done++ > 0)
24929 ++hi;
24930 while (HASHITEM_EMPTY(hi))
24931 ++hi;
24932 fp = HI2UF(hi);
24933
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024934 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024935 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024936
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024937 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24938 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939
24940 cat_func_name(IObuff, fp);
24941 if (xp->xp_context != EXPAND_USER_FUNC)
24942 {
24943 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024944 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024945 STRCAT(IObuff, ")");
24946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024947 return IObuff;
24948 }
24949 return NULL;
24950}
24951
24952#endif /* FEAT_CMDL_COMPL */
24953
24954/*
24955 * Copy the function name of "fp" to buffer "buf".
24956 * "buf" must be able to hold the function name plus three bytes.
24957 * Takes care of script-local function names.
24958 */
24959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024960cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024961{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024962 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963 {
24964 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024965 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024966 }
24967 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024968 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024969}
24970
24971/*
24972 * ":delfunction {name}"
24973 */
24974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024975ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024976{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024977 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024978 char_u *p;
24979 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024980 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981
24982 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024983 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024984 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024986 {
24987 if (fudi.fd_dict != NULL && !eap->skip)
24988 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024989 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024991 if (!ends_excmd(*skipwhite(p)))
24992 {
24993 vim_free(name);
24994 EMSG(_(e_trailing));
24995 return;
24996 }
24997 eap->nextcmd = check_nextcmd(p);
24998 if (eap->nextcmd != NULL)
24999 *p = NUL;
25000
25001 if (!eap->skip)
25002 fp = find_func(name);
25003 vim_free(name);
25004
25005 if (!eap->skip)
25006 {
25007 if (fp == NULL)
25008 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025009 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025010 return;
25011 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025012 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 {
25014 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25015 return;
25016 }
25017
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025018 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025020 /* Delete the dict item that refers to the function, it will
25021 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025022 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025024 else
25025 func_free(fp);
25026 }
25027}
25028
25029/*
25030 * Free a function and remove it from the list of functions.
25031 */
25032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025033func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025034{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025035 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025036
25037 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025038 ga_clear_strings(&(fp->uf_args));
25039 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025040#ifdef FEAT_PROFILE
25041 vim_free(fp->uf_tml_count);
25042 vim_free(fp->uf_tml_total);
25043 vim_free(fp->uf_tml_self);
25044#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025045
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025046 /* remove the function from the function hashtable */
25047 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25048 if (HASHITEM_EMPTY(hi))
25049 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025050 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025051 hash_remove(&func_hashtab, hi);
25052
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025053 vim_free(fp);
25054}
25055
25056/*
25057 * Unreference a Function: decrement the reference count and free it when it
25058 * becomes zero. Only for numbered functions.
25059 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025061func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025062{
25063 ufunc_T *fp;
25064
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025065 if (name == NULL)
25066 return;
25067 else if (isdigit(*name))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025068 {
25069 fp = find_func(name);
25070 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025071 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025072#ifdef EXITFREE
25073 if (!entered_free_all_mem)
25074#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025075 EMSG2(_(e_intern2), "func_unref()");
25076 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025077 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025078 {
25079 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025080 * when "uf_calls" becomes zero. */
25081 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025082 func_free(fp);
25083 }
25084 }
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025085 else if (STRNCMP(name, "<lambda>", 8) == 0)
25086 {
25087 /* fail silently, when lambda function isn't found. */
25088 fp = find_func(name);
25089 if (fp != NULL && --fp->uf_refcount <= 0)
25090 {
25091 /* Only delete it when it's not being used. Otherwise it's done
25092 * when "uf_calls" becomes zero. */
25093 if (fp->uf_calls == 0)
25094 func_free(fp);
25095 }
25096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025097}
25098
25099/*
25100 * Count a reference to a Function.
25101 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025103func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025104{
25105 ufunc_T *fp;
25106
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025107 if (name == NULL)
25108 return;
25109 else if (isdigit(*name))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025110 {
25111 fp = find_func(name);
25112 if (fp == NULL)
25113 EMSG2(_(e_intern2), "func_ref()");
25114 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025115 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025116 }
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025117 else if (STRNCMP(name, "<lambda>", 8) == 0)
25118 {
25119 /* fail silently, when lambda function isn't found. */
25120 fp = find_func(name);
25121 if (fp != NULL)
25122 ++fp->uf_refcount;
25123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124}
25125
25126/*
25127 * Call a user function.
25128 */
25129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025130call_user_func(
25131 ufunc_T *fp, /* pointer to function */
25132 int argcount, /* nr of args */
25133 typval_T *argvars, /* arguments */
25134 typval_T *rettv, /* return value */
25135 linenr_T firstline, /* first line of range */
25136 linenr_T lastline, /* last line of range */
25137 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138{
Bram Moolenaar33570922005-01-25 22:26:29 +000025139 char_u *save_sourcing_name;
25140 linenr_T save_sourcing_lnum;
25141 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025142 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025143 int save_did_emsg;
25144 static int depth = 0;
25145 dictitem_T *v;
25146 int fixvar_idx = 0; /* index in fixvar[] */
25147 int i;
25148 int ai;
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025149 int islambda = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000025150 char_u numbuf[NUMBUFLEN];
25151 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025152 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025153#ifdef FEAT_PROFILE
25154 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025155 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025157
25158 /* If depth of calling is getting too high, don't execute the function */
25159 if (depth >= p_mfd)
25160 {
25161 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025162 rettv->v_type = VAR_NUMBER;
25163 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164 return;
25165 }
25166 ++depth;
25167
25168 line_breakcheck(); /* check for CTRL-C hit */
25169
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025170 fc = (funccall_T *)alloc(sizeof(funccall_T));
25171 fc->caller = current_funccal;
25172 current_funccal = fc;
25173 fc->func = fp;
25174 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025175 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025176 fc->linenr = 0;
25177 fc->returned = FALSE;
25178 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025179 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025180 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25181 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025182
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025183 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
25184 islambda = TRUE;
25185
Bram Moolenaar33570922005-01-25 22:26:29 +000025186 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025187 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025188 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25189 * each argument variable and saves a lot of time.
25190 */
25191 /*
25192 * Init l: variables.
25193 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025194 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025195 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025196 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025197 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25198 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025199 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025200 name = v->di_key;
25201 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025202 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025203 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025204 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025205 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025206 v->di_tv.vval.v_dict = selfdict;
25207 ++selfdict->dv_refcount;
25208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025209
Bram Moolenaar33570922005-01-25 22:26:29 +000025210 /*
25211 * Init a: variables.
25212 * Set a:0 to "argcount".
25213 * Set a:000 to a list with room for the "..." arguments.
25214 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025215 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025216 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025217 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025218 /* Use "name" to avoid a warning from some compiler that checks the
25219 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025220 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025221 name = v->di_key;
25222 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025223 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025224 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025225 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025226 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025227 v->di_tv.vval.v_list = &fc->l_varlist;
25228 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25229 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25230 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025231
25232 /*
25233 * Set a:firstline to "firstline" and a:lastline to "lastline".
25234 * Set a:name to named arguments.
25235 * Set a:N to the "..." arguments.
25236 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025237 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025238 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025239 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025240 (varnumber_T)lastline);
25241 for (i = 0; i < argcount; ++i)
25242 {
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025243 int addlocal = FALSE;
25244 dictitem_T *v2;
25245
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025246 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025247 if (ai < 0)
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025248 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025249 /* named argument a:name */
25250 name = FUNCARG(fp, i);
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025251 if (islambda)
25252 addlocal = TRUE;
25253 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025254 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025255 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025256 /* "..." argument a:1, a:2, etc. */
25257 sprintf((char *)numbuf, "%d", ai + 1);
25258 name = numbuf;
25259 }
25260 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25261 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025262 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025263 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025264
25265 if (addlocal)
25266 v2 = v;
Bram Moolenaar33570922005-01-25 22:26:29 +000025267 }
25268 else
25269 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025270 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25271 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025272 if (v == NULL)
25273 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025274 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025275
25276 if (addlocal)
25277 {
25278 v2 = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25279 + STRLEN(name)));
25280 if (v2 == NULL)
25281 {
25282 vim_free(v);
25283 break;
25284 }
25285 v2->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
25286 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025287 }
25288 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025289 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025290
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025291 /* Note: the values are copied directly to avoid alloc/free.
25292 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025293 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025294 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025295
Bram Moolenaar069c1e72016-07-15 21:25:08 +020025296 /* Named arguments can be accessed without the "a:" prefix in lambda
25297 * expressions. Add to the l: dict. */
25298 if (addlocal)
25299 {
25300 STRCPY(v2->di_key, name);
25301 copy_tv(&v->di_tv, &v2->di_tv);
25302 v2->di_tv.v_lock = VAR_FIXED;
25303 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v2));
25304 }
25305
Bram Moolenaar33570922005-01-25 22:26:29 +000025306 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25307 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025308 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25309 fc->l_listitems[ai].li_tv = argvars[i];
25310 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025311 }
25312 }
25313
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314 /* Don't redraw while executing the function. */
25315 ++RedrawingDisabled;
25316 save_sourcing_name = sourcing_name;
25317 save_sourcing_lnum = sourcing_lnum;
25318 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025319 /* need space for function name + ("function " + 3) or "[number]" */
25320 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25321 + STRLEN(fp->uf_name) + 20;
25322 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323 if (sourcing_name != NULL)
25324 {
25325 if (save_sourcing_name != NULL
25326 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025327 sprintf((char *)sourcing_name, "%s[%d]..",
25328 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329 else
25330 STRCPY(sourcing_name, "function ");
25331 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25332
25333 if (p_verbose >= 12)
25334 {
25335 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025336 verbose_enter_scroll();
25337
Bram Moolenaar555b2802005-05-19 21:08:39 +000025338 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025339 if (p_verbose >= 14)
25340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025341 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025342 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025343 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025344 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345
25346 msg_puts((char_u *)"(");
25347 for (i = 0; i < argcount; ++i)
25348 {
25349 if (i > 0)
25350 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025351 if (argvars[i].v_type == VAR_NUMBER)
25352 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025353 else
25354 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025355 /* Do not want errors such as E724 here. */
25356 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025357 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025358 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025359 if (s != NULL)
25360 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025361 if (vim_strsize(s) > MSG_BUF_CLEN)
25362 {
25363 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25364 s = buf;
25365 }
25366 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025367 vim_free(tofree);
25368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369 }
25370 }
25371 msg_puts((char_u *)")");
25372 }
25373 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025374
25375 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025376 --no_wait_return;
25377 }
25378 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025379#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025380 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025381 {
25382 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25383 func_do_profile(fp);
25384 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025385 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025386 {
25387 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025388 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025389 profile_zero(&fp->uf_tm_children);
25390 }
25391 script_prof_save(&wait_start);
25392 }
25393#endif
25394
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025396 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397 save_did_emsg = did_emsg;
25398 did_emsg = FALSE;
25399
25400 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025401 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025402 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25403
25404 --RedrawingDisabled;
25405
25406 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025407 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025409 clear_tv(rettv);
25410 rettv->v_type = VAR_NUMBER;
25411 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412 }
25413
Bram Moolenaar05159a02005-02-26 23:04:13 +000025414#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025415 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025416 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025417 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025418 profile_end(&call_start);
25419 profile_sub_wait(&wait_start, &call_start);
25420 profile_add(&fp->uf_tm_total, &call_start);
25421 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025422 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025423 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025424 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25425 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025426 }
25427 }
25428#endif
25429
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430 /* when being verbose, mention the return value */
25431 if (p_verbose >= 12)
25432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025434 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435
Bram Moolenaar071d4272004-06-13 20:20:40 +000025436 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025437 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025438 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025439 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025440 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025441 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025442 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025443 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025444 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025445 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025446 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025447
Bram Moolenaar555b2802005-05-19 21:08:39 +000025448 /* The value may be very long. Skip the middle part, so that we
25449 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025450 * truncate it at the end. Don't want errors such as E724 here. */
25451 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025452 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025453 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025454 if (s != NULL)
25455 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025456 if (vim_strsize(s) > MSG_BUF_CLEN)
25457 {
25458 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25459 s = buf;
25460 }
25461 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025462 vim_free(tofree);
25463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464 }
25465 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025466
25467 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025468 --no_wait_return;
25469 }
25470
25471 vim_free(sourcing_name);
25472 sourcing_name = save_sourcing_name;
25473 sourcing_lnum = save_sourcing_lnum;
25474 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025475#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025476 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025477 script_prof_restore(&wait_start);
25478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025479
25480 if (p_verbose >= 12 && sourcing_name != NULL)
25481 {
25482 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025483 verbose_enter_scroll();
25484
Bram Moolenaar555b2802005-05-19 21:08:39 +000025485 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025487
25488 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025489 --no_wait_return;
25490 }
25491
25492 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025493 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025495
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025496 /* If the a:000 list and the l: and a: dicts are not referenced we can
25497 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025498 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25499 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25500 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25501 {
25502 free_funccal(fc, FALSE);
25503 }
25504 else
25505 {
25506 hashitem_T *hi;
25507 listitem_T *li;
25508 int todo;
25509
25510 /* "fc" is still in use. This can happen when returning "a:000" or
25511 * assigning "l:" to a global variable.
25512 * Link "fc" in the list for garbage collection later. */
25513 fc->caller = previous_funccal;
25514 previous_funccal = fc;
25515
25516 /* Make a copy of the a: variables, since we didn't do that above. */
25517 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25518 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25519 {
25520 if (!HASHITEM_EMPTY(hi))
25521 {
25522 --todo;
25523 v = HI2DI(hi);
25524 copy_tv(&v->di_tv, &v->di_tv);
25525 }
25526 }
25527
25528 /* Make a copy of the a:000 items, since we didn't do that above. */
25529 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25530 copy_tv(&li->li_tv, &li->li_tv);
25531 }
25532}
25533
25534/*
25535 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025536 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025537 */
25538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025539can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025540{
25541 return (fc->l_varlist.lv_copyID != copyID
25542 && fc->l_vars.dv_copyID != copyID
25543 && fc->l_avars.dv_copyID != copyID);
25544}
25545
25546/*
25547 * Free "fc" and what it contains.
25548 */
25549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025550free_funccal(
25551 funccall_T *fc,
25552 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025553{
25554 listitem_T *li;
25555
25556 /* The a: variables typevals may not have been allocated, only free the
25557 * allocated variables. */
25558 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25559
25560 /* free all l: variables */
25561 vars_clear(&fc->l_vars.dv_hashtab);
25562
25563 /* Free the a:000 variables if they were allocated. */
25564 if (free_val)
25565 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25566 clear_tv(&li->li_tv);
25567
25568 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025569}
25570
25571/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025572 * Add a number variable "name" to dict "dp" with value "nr".
25573 */
25574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025575add_nr_var(
25576 dict_T *dp,
25577 dictitem_T *v,
25578 char *name,
25579 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025580{
25581 STRCPY(v->di_key, name);
25582 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25583 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25584 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025585 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025586 v->di_tv.vval.v_number = nr;
25587}
25588
25589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590 * ":return [expr]"
25591 */
25592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025593ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594{
25595 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025596 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025597 int returning = FALSE;
25598
25599 if (current_funccal == NULL)
25600 {
25601 EMSG(_("E133: :return not inside a function"));
25602 return;
25603 }
25604
25605 if (eap->skip)
25606 ++emsg_skip;
25607
25608 eap->nextcmd = NULL;
25609 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025610 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611 {
25612 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025613 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025615 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616 }
25617 /* It's safer to return also on error. */
25618 else if (!eap->skip)
25619 {
25620 /*
25621 * Return unless the expression evaluation has been cancelled due to an
25622 * aborting error, an interrupt, or an exception.
25623 */
25624 if (!aborting())
25625 returning = do_return(eap, FALSE, TRUE, NULL);
25626 }
25627
25628 /* When skipping or the return gets pending, advance to the next command
25629 * in this line (!returning). Otherwise, ignore the rest of the line.
25630 * Following lines will be ignored by get_func_line(). */
25631 if (returning)
25632 eap->nextcmd = NULL;
25633 else if (eap->nextcmd == NULL) /* no argument */
25634 eap->nextcmd = check_nextcmd(arg);
25635
25636 if (eap->skip)
25637 --emsg_skip;
25638}
25639
25640/*
25641 * Return from a function. Possibly makes the return pending. Also called
25642 * for a pending return at the ":endtry" or after returning from an extra
25643 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025644 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025645 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025646 * FALSE when the return gets pending.
25647 */
25648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025649do_return(
25650 exarg_T *eap,
25651 int reanimate,
25652 int is_cmd,
25653 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025654{
25655 int idx;
25656 struct condstack *cstack = eap->cstack;
25657
25658 if (reanimate)
25659 /* Undo the return. */
25660 current_funccal->returned = FALSE;
25661
25662 /*
25663 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25664 * not in its finally clause (which then is to be executed next) is found.
25665 * In this case, make the ":return" pending for execution at the ":endtry".
25666 * Otherwise, return normally.
25667 */
25668 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25669 if (idx >= 0)
25670 {
25671 cstack->cs_pending[idx] = CSTP_RETURN;
25672
25673 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025674 /* A pending return again gets pending. "rettv" points to an
25675 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025677 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025678 else
25679 {
25680 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025681 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025683 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025685 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686 {
25687 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025688 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025689 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025690 else
25691 EMSG(_(e_outofmem));
25692 }
25693 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025694 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025695
25696 if (reanimate)
25697 {
25698 /* The pending return value could be overwritten by a ":return"
25699 * without argument in a finally clause; reset the default
25700 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025701 current_funccal->rettv->v_type = VAR_NUMBER;
25702 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703 }
25704 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025705 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025706 }
25707 else
25708 {
25709 current_funccal->returned = TRUE;
25710
25711 /* If the return is carried out now, store the return value. For
25712 * a return immediately after reanimation, the value is already
25713 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025714 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025716 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025717 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025718 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025719 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025720 }
25721 }
25722
25723 return idx < 0;
25724}
25725
25726/*
25727 * Free the variable with a pending return value.
25728 */
25729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025730discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025731{
Bram Moolenaar33570922005-01-25 22:26:29 +000025732 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025733}
25734
25735/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025736 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025737 * is an allocated string. Used by report_pending() for verbose messages.
25738 */
25739 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025740get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025742 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025743 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025744 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025745
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025746 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025747 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025748 if (s == NULL)
25749 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025750
25751 STRCPY(IObuff, ":return ");
25752 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25753 if (STRLEN(s) + 8 >= IOSIZE)
25754 STRCPY(IObuff + IOSIZE - 4, "...");
25755 vim_free(tofree);
25756 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025757}
25758
25759/*
25760 * Get next function line.
25761 * Called by do_cmdline() to get the next line.
25762 * Returns allocated string, or NULL for end of function.
25763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025764 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025765get_func_line(
25766 int c UNUSED,
25767 void *cookie,
25768 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769{
Bram Moolenaar33570922005-01-25 22:26:29 +000025770 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025771 ufunc_T *fp = fcp->func;
25772 char_u *retval;
25773 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774
25775 /* If breakpoints have been added/deleted need to check for it. */
25776 if (fcp->dbg_tick != debug_tick)
25777 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025778 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779 sourcing_lnum);
25780 fcp->dbg_tick = debug_tick;
25781 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025782#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025783 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025784 func_line_end(cookie);
25785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786
Bram Moolenaar05159a02005-02-26 23:04:13 +000025787 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025788 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25789 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025790 retval = NULL;
25791 else
25792 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025793 /* Skip NULL lines (continuation lines). */
25794 while (fcp->linenr < gap->ga_len
25795 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25796 ++fcp->linenr;
25797 if (fcp->linenr >= gap->ga_len)
25798 retval = NULL;
25799 else
25800 {
25801 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25802 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025803#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025804 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025805 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025806#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808 }
25809
25810 /* Did we encounter a breakpoint? */
25811 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25812 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025813 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025814 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025815 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816 sourcing_lnum);
25817 fcp->dbg_tick = debug_tick;
25818 }
25819
25820 return retval;
25821}
25822
Bram Moolenaar05159a02005-02-26 23:04:13 +000025823#if defined(FEAT_PROFILE) || defined(PROTO)
25824/*
25825 * Called when starting to read a function line.
25826 * "sourcing_lnum" must be correct!
25827 * When skipping lines it may not actually be executed, but we won't find out
25828 * until later and we need to store the time now.
25829 */
25830 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025831func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025832{
25833 funccall_T *fcp = (funccall_T *)cookie;
25834 ufunc_T *fp = fcp->func;
25835
25836 if (fp->uf_profiling && sourcing_lnum >= 1
25837 && sourcing_lnum <= fp->uf_lines.ga_len)
25838 {
25839 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025840 /* Skip continuation lines. */
25841 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25842 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025843 fp->uf_tml_execed = FALSE;
25844 profile_start(&fp->uf_tml_start);
25845 profile_zero(&fp->uf_tml_children);
25846 profile_get_wait(&fp->uf_tml_wait);
25847 }
25848}
25849
25850/*
25851 * Called when actually executing a function line.
25852 */
25853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025854func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025855{
25856 funccall_T *fcp = (funccall_T *)cookie;
25857 ufunc_T *fp = fcp->func;
25858
25859 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25860 fp->uf_tml_execed = TRUE;
25861}
25862
25863/*
25864 * Called when done with a function line.
25865 */
25866 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025867func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025868{
25869 funccall_T *fcp = (funccall_T *)cookie;
25870 ufunc_T *fp = fcp->func;
25871
25872 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25873 {
25874 if (fp->uf_tml_execed)
25875 {
25876 ++fp->uf_tml_count[fp->uf_tml_idx];
25877 profile_end(&fp->uf_tml_start);
25878 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025879 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025880 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25881 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025882 }
25883 fp->uf_tml_idx = -1;
25884 }
25885}
25886#endif
25887
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888/*
25889 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025890 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891 */
25892 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025893func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025894{
Bram Moolenaar33570922005-01-25 22:26:29 +000025895 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025896
25897 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25898 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025899 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900 || fcp->returned);
25901}
25902
25903/*
25904 * return TRUE if cookie indicates a function which "abort"s on errors.
25905 */
25906 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025907func_has_abort(
25908 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025909{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025910 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025911}
25912
25913#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25914typedef enum
25915{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025916 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25917 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25918 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025919} var_flavour_T;
25920
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025921static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025922
25923 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025924var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025925{
25926 char_u *p = varname;
25927
25928 if (ASCII_ISUPPER(*p))
25929 {
25930 while (*(++p))
25931 if (ASCII_ISLOWER(*p))
25932 return VAR_FLAVOUR_SESSION;
25933 return VAR_FLAVOUR_VIMINFO;
25934 }
25935 else
25936 return VAR_FLAVOUR_DEFAULT;
25937}
25938#endif
25939
25940#if defined(FEAT_VIMINFO) || defined(PROTO)
25941/*
25942 * Restore global vars that start with a capital from the viminfo file
25943 */
25944 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025945read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025946{
25947 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025948 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025949 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025950 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025951
25952 if (!writing && (find_viminfo_parameter('!') != NULL))
25953 {
25954 tab = vim_strchr(virp->vir_line + 1, '\t');
25955 if (tab != NULL)
25956 {
25957 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025958 switch (*tab)
25959 {
25960 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025961#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025962 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025963#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025964 case 'D': type = VAR_DICT; break;
25965 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025966 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968
25969 tab = vim_strchr(tab, '\t');
25970 if (tab != NULL)
25971 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025972 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025973 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025974 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025975 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025976#ifdef FEAT_FLOAT
25977 else if (type == VAR_FLOAT)
25978 (void)string2float(tab + 1, &tv.vval.v_float);
25979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025981 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025982 if (type == VAR_DICT || type == VAR_LIST)
25983 {
25984 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25985
25986 if (etv == NULL)
25987 /* Failed to parse back the dict or list, use it as a
25988 * string. */
25989 tv.v_type = VAR_STRING;
25990 else
25991 {
25992 vim_free(tv.vval.v_string);
25993 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025994 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025995 }
25996 }
25997
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025998 /* when in a function use global variables */
25999 save_funccal = current_funccal;
26000 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026001 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026002 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026003
26004 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026005 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026006 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26007 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026008 }
26009 }
26010 }
26011
26012 return viminfo_readline(virp);
26013}
26014
26015/*
26016 * Write global vars that start with a capital to the viminfo file
26017 */
26018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026019write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026020{
Bram Moolenaar33570922005-01-25 22:26:29 +000026021 hashitem_T *hi;
26022 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026023 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026024 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026025 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026026 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026027 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026028
26029 if (find_viminfo_parameter('!') == NULL)
26030 return;
26031
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026032 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026033
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026034 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026035 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026036 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026037 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026038 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026039 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026040 this_var = HI2DI(hi);
26041 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026042 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026043 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026044 {
26045 case VAR_STRING: s = "STR"; break;
26046 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026047 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026048 case VAR_DICT: s = "DIC"; break;
26049 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026050 case VAR_SPECIAL: s = "XPL"; break;
26051
26052 case VAR_UNKNOWN:
26053 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026054 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026055 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026056 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026057 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026058 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026059 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026060 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026061 if (p != NULL)
26062 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026063 vim_free(tofree);
26064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026065 }
26066 }
26067}
26068#endif
26069
26070#if defined(FEAT_SESSION) || defined(PROTO)
26071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026072store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026073{
Bram Moolenaar33570922005-01-25 22:26:29 +000026074 hashitem_T *hi;
26075 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026076 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077 char_u *p, *t;
26078
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026079 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026080 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026082 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026083 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026084 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026085 this_var = HI2DI(hi);
26086 if ((this_var->di_tv.v_type == VAR_NUMBER
26087 || this_var->di_tv.v_type == VAR_STRING)
26088 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026089 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026090 /* Escape special characters with a backslash. Turn a LF and
26091 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026092 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026093 (char_u *)"\\\"\n\r");
26094 if (p == NULL) /* out of memory */
26095 break;
26096 for (t = p; *t != NUL; ++t)
26097 if (*t == '\n')
26098 *t = 'n';
26099 else if (*t == '\r')
26100 *t = 'r';
26101 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026102 this_var->di_key,
26103 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26104 : ' ',
26105 p,
26106 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26107 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026108 || put_eol(fd) == FAIL)
26109 {
26110 vim_free(p);
26111 return FAIL;
26112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026113 vim_free(p);
26114 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026115#ifdef FEAT_FLOAT
26116 else if (this_var->di_tv.v_type == VAR_FLOAT
26117 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26118 {
26119 float_T f = this_var->di_tv.vval.v_float;
26120 int sign = ' ';
26121
26122 if (f < 0)
26123 {
26124 f = -f;
26125 sign = '-';
26126 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026127 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026128 this_var->di_key, sign, f) < 0)
26129 || put_eol(fd) == FAIL)
26130 return FAIL;
26131 }
26132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026133 }
26134 }
26135 return OK;
26136}
26137#endif
26138
Bram Moolenaar661b1822005-07-28 22:36:45 +000026139/*
26140 * Display script name where an item was last set.
26141 * Should only be invoked when 'verbose' is non-zero.
26142 */
26143 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026144last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026145{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026146 char_u *p;
26147
Bram Moolenaar661b1822005-07-28 22:36:45 +000026148 if (scriptID != 0)
26149 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026150 p = home_replace_save(NULL, get_scriptname(scriptID));
26151 if (p != NULL)
26152 {
26153 verbose_enter();
26154 MSG_PUTS(_("\n\tLast set from "));
26155 MSG_PUTS(p);
26156 vim_free(p);
26157 verbose_leave();
26158 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026159 }
26160}
26161
Bram Moolenaard812df62008-11-09 12:46:09 +000026162/*
26163 * List v:oldfiles in a nice way.
26164 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026166ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026167{
26168 list_T *l = vimvars[VV_OLDFILES].vv_list;
26169 listitem_T *li;
26170 int nr = 0;
26171
26172 if (l == NULL)
26173 msg((char_u *)_("No old files"));
26174 else
26175 {
26176 msg_start();
26177 msg_scroll = TRUE;
26178 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26179 {
26180 msg_outnum((long)++nr);
26181 MSG_PUTS(": ");
26182 msg_outtrans(get_tv_string(&li->li_tv));
26183 msg_putchar('\n');
26184 out_flush(); /* output one line at a time */
26185 ui_breakcheck();
26186 }
26187 /* Assume "got_int" was set to truncate the listing. */
26188 got_int = FALSE;
26189
26190#ifdef FEAT_BROWSE_CMD
26191 if (cmdmod.browse)
26192 {
26193 quit_more = FALSE;
26194 nr = prompt_for_number(FALSE);
26195 msg_starthere();
26196 if (nr > 0)
26197 {
26198 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26199 (long)nr);
26200
26201 if (p != NULL)
26202 {
26203 p = expand_env_save(p);
26204 eap->arg = p;
26205 eap->cmdidx = CMD_edit;
26206 cmdmod.browse = FALSE;
26207 do_exedit(eap, NULL);
26208 vim_free(p);
26209 }
26210 }
26211 }
26212#endif
26213 }
26214}
26215
Bram Moolenaar53744302015-07-17 17:38:22 +020026216/* reset v:option_new, v:option_old and v:option_type */
26217 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026218reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026219{
26220 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26221 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26222 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26223}
26224
26225
Bram Moolenaar071d4272004-06-13 20:20:40 +000026226#endif /* FEAT_EVAL */
26227
Bram Moolenaar071d4272004-06-13 20:20:40 +000026228
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026229#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026230
26231#ifdef WIN3264
26232/*
26233 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26234 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026235static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26236static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26237static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238
26239/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026240 * Get the short path (8.3) for the filename in "fnamep".
26241 * Only works for a valid file name.
26242 * When the path gets longer "fnamep" is changed and the allocated buffer
26243 * is put in "bufp".
26244 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26245 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026246 */
26247 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026248get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026249{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026250 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026251 char_u *newbuf;
26252
26253 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026254 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026255 if (l > len - 1)
26256 {
26257 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026258 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026259 newbuf = vim_strnsave(*fnamep, l);
26260 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026262
26263 vim_free(*bufp);
26264 *fnamep = *bufp = newbuf;
26265
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026266 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026267 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026268 }
26269
26270 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026271 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026272}
26273
26274/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026275 * Get the short path (8.3) for the filename in "fname". The converted
26276 * path is returned in "bufp".
26277 *
26278 * Some of the directories specified in "fname" may not exist. This function
26279 * will shorten the existing directories at the beginning of the path and then
26280 * append the remaining non-existing path.
26281 *
26282 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026283 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026284 * bufp - Pointer to an allocated buffer for the filename.
26285 * fnamelen - Length of the filename pointed to by fname
26286 *
26287 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026288 */
26289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026290shortpath_for_invalid_fname(
26291 char_u **fname,
26292 char_u **bufp,
26293 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026294{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026295 char_u *short_fname, *save_fname, *pbuf_unused;
26296 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026297 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026298 int old_len, len;
26299 int new_len, sfx_len;
26300 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026301
26302 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026303 old_len = *fnamelen;
26304 save_fname = vim_strnsave(*fname, old_len);
26305 pbuf_unused = NULL;
26306 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026307
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026308 endp = save_fname + old_len - 1; /* Find the end of the copy */
26309 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026311 /*
26312 * Try shortening the supplied path till it succeeds by removing one
26313 * directory at a time from the tail of the path.
26314 */
26315 len = 0;
26316 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026318 /* go back one path-separator */
26319 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26320 --endp;
26321 if (endp <= save_fname)
26322 break; /* processed the complete path */
26323
26324 /*
26325 * Replace the path separator with a NUL and try to shorten the
26326 * resulting path.
26327 */
26328 ch = *endp;
26329 *endp = 0;
26330 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026331 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026332 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26333 {
26334 retval = FAIL;
26335 goto theend;
26336 }
26337 *endp = ch; /* preserve the string */
26338
26339 if (len > 0)
26340 break; /* successfully shortened the path */
26341
26342 /* failed to shorten the path. Skip the path separator */
26343 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026344 }
26345
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026346 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026347 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026348 /*
26349 * Succeeded in shortening the path. Now concatenate the shortened
26350 * path with the remaining path at the tail.
26351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026352
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026353 /* Compute the length of the new path. */
26354 sfx_len = (int)(save_endp - endp) + 1;
26355 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026356
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026357 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026358 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026359 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026361 /* There is not enough space in the currently allocated string,
26362 * copy it to a buffer big enough. */
26363 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026364 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026365 {
26366 retval = FAIL;
26367 goto theend;
26368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026369 }
26370 else
26371 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026372 /* Transfer short_fname to the main buffer (it's big enough),
26373 * unless get_short_pathname() did its work in-place. */
26374 *fname = *bufp = save_fname;
26375 if (short_fname != save_fname)
26376 vim_strncpy(save_fname, short_fname, len);
26377 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026378 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026379
26380 /* concat the not-shortened part of the path */
26381 vim_strncpy(*fname + len, endp, sfx_len);
26382 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026383 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026384
26385theend:
26386 vim_free(pbuf_unused);
26387 vim_free(save_fname);
26388
26389 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026390}
26391
26392/*
26393 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026394 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026395 */
26396 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026397shortpath_for_partial(
26398 char_u **fnamep,
26399 char_u **bufp,
26400 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026401{
26402 int sepcount, len, tflen;
26403 char_u *p;
26404 char_u *pbuf, *tfname;
26405 int hasTilde;
26406
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026407 /* Count up the path separators from the RHS.. so we know which part
26408 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026409 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026410 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026411 if (vim_ispathsep(*p))
26412 ++sepcount;
26413
26414 /* Need full path first (use expand_env() to remove a "~/") */
26415 hasTilde = (**fnamep == '~');
26416 if (hasTilde)
26417 pbuf = tfname = expand_env_save(*fnamep);
26418 else
26419 pbuf = tfname = FullName_save(*fnamep, FALSE);
26420
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026421 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026422
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026423 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26424 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026425
26426 if (len == 0)
26427 {
26428 /* Don't have a valid filename, so shorten the rest of the
26429 * path if we can. This CAN give us invalid 8.3 filenames, but
26430 * there's not a lot of point in guessing what it might be.
26431 */
26432 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026433 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26434 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026435 }
26436
26437 /* Count the paths backward to find the beginning of the desired string. */
26438 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026439 {
26440#ifdef FEAT_MBYTE
26441 if (has_mbyte)
26442 p -= mb_head_off(tfname, p);
26443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026444 if (vim_ispathsep(*p))
26445 {
26446 if (sepcount == 0 || (hasTilde && sepcount == 1))
26447 break;
26448 else
26449 sepcount --;
26450 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452 if (hasTilde)
26453 {
26454 --p;
26455 if (p >= tfname)
26456 *p = '~';
26457 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026458 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026459 }
26460 else
26461 ++p;
26462
26463 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26464 vim_free(*bufp);
26465 *fnamelen = (int)STRLEN(p);
26466 *bufp = pbuf;
26467 *fnamep = p;
26468
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026469 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026470}
26471#endif /* WIN3264 */
26472
26473/*
26474 * Adjust a filename, according to a string of modifiers.
26475 * *fnamep must be NUL terminated when called. When returning, the length is
26476 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026477 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026478 * When there is an error, *fnamep is set to NULL.
26479 */
26480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026481modify_fname(
26482 char_u *src, /* string with modifiers */
26483 int *usedlen, /* characters after src that are used */
26484 char_u **fnamep, /* file name so far */
26485 char_u **bufp, /* buffer for allocated file name or NULL */
26486 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026487{
26488 int valid = 0;
26489 char_u *tail;
26490 char_u *s, *p, *pbuf;
26491 char_u dirname[MAXPATHL];
26492 int c;
26493 int has_fullname = 0;
26494#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026495 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026496 int has_shortname = 0;
26497#endif
26498
26499repeat:
26500 /* ":p" - full path/file_name */
26501 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26502 {
26503 has_fullname = 1;
26504
26505 valid |= VALID_PATH;
26506 *usedlen += 2;
26507
26508 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26509 if ((*fnamep)[0] == '~'
26510#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26511 && ((*fnamep)[1] == '/'
26512# ifdef BACKSLASH_IN_FILENAME
26513 || (*fnamep)[1] == '\\'
26514# endif
26515 || (*fnamep)[1] == NUL)
26516
26517#endif
26518 )
26519 {
26520 *fnamep = expand_env_save(*fnamep);
26521 vim_free(*bufp); /* free any allocated file name */
26522 *bufp = *fnamep;
26523 if (*fnamep == NULL)
26524 return -1;
26525 }
26526
26527 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026528 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026529 {
26530 if (vim_ispathsep(*p)
26531 && p[1] == '.'
26532 && (p[2] == NUL
26533 || vim_ispathsep(p[2])
26534 || (p[2] == '.'
26535 && (p[3] == NUL || vim_ispathsep(p[3])))))
26536 break;
26537 }
26538
26539 /* FullName_save() is slow, don't use it when not needed. */
26540 if (*p != NUL || !vim_isAbsName(*fnamep))
26541 {
26542 *fnamep = FullName_save(*fnamep, *p != NUL);
26543 vim_free(*bufp); /* free any allocated file name */
26544 *bufp = *fnamep;
26545 if (*fnamep == NULL)
26546 return -1;
26547 }
26548
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026549#ifdef WIN3264
26550# if _WIN32_WINNT >= 0x0500
26551 if (vim_strchr(*fnamep, '~') != NULL)
26552 {
26553 /* Expand 8.3 filename to full path. Needed to make sure the same
26554 * file does not have two different names.
26555 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26556 p = alloc(_MAX_PATH + 1);
26557 if (p != NULL)
26558 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026559 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026560 {
26561 vim_free(*bufp);
26562 *bufp = *fnamep = p;
26563 }
26564 else
26565 vim_free(p);
26566 }
26567 }
26568# endif
26569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026570 /* Append a path separator to a directory. */
26571 if (mch_isdir(*fnamep))
26572 {
26573 /* Make room for one or two extra characters. */
26574 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26575 vim_free(*bufp); /* free any allocated file name */
26576 *bufp = *fnamep;
26577 if (*fnamep == NULL)
26578 return -1;
26579 add_pathsep(*fnamep);
26580 }
26581 }
26582
26583 /* ":." - path relative to the current directory */
26584 /* ":~" - path relative to the home directory */
26585 /* ":8" - shortname path - postponed till after */
26586 while (src[*usedlen] == ':'
26587 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26588 {
26589 *usedlen += 2;
26590 if (c == '8')
26591 {
26592#ifdef WIN3264
26593 has_shortname = 1; /* Postpone this. */
26594#endif
26595 continue;
26596 }
26597 pbuf = NULL;
26598 /* Need full path first (use expand_env() to remove a "~/") */
26599 if (!has_fullname)
26600 {
26601 if (c == '.' && **fnamep == '~')
26602 p = pbuf = expand_env_save(*fnamep);
26603 else
26604 p = pbuf = FullName_save(*fnamep, FALSE);
26605 }
26606 else
26607 p = *fnamep;
26608
26609 has_fullname = 0;
26610
26611 if (p != NULL)
26612 {
26613 if (c == '.')
26614 {
26615 mch_dirname(dirname, MAXPATHL);
26616 s = shorten_fname(p, dirname);
26617 if (s != NULL)
26618 {
26619 *fnamep = s;
26620 if (pbuf != NULL)
26621 {
26622 vim_free(*bufp); /* free any allocated file name */
26623 *bufp = pbuf;
26624 pbuf = NULL;
26625 }
26626 }
26627 }
26628 else
26629 {
26630 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26631 /* Only replace it when it starts with '~' */
26632 if (*dirname == '~')
26633 {
26634 s = vim_strsave(dirname);
26635 if (s != NULL)
26636 {
26637 *fnamep = s;
26638 vim_free(*bufp);
26639 *bufp = s;
26640 }
26641 }
26642 }
26643 vim_free(pbuf);
26644 }
26645 }
26646
26647 tail = gettail(*fnamep);
26648 *fnamelen = (int)STRLEN(*fnamep);
26649
26650 /* ":h" - head, remove "/file_name", can be repeated */
26651 /* Don't remove the first "/" or "c:\" */
26652 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26653 {
26654 valid |= VALID_HEAD;
26655 *usedlen += 2;
26656 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026657 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026658 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026659 *fnamelen = (int)(tail - *fnamep);
26660#ifdef VMS
26661 if (*fnamelen > 0)
26662 *fnamelen += 1; /* the path separator is part of the path */
26663#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026664 if (*fnamelen == 0)
26665 {
26666 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26667 p = vim_strsave((char_u *)".");
26668 if (p == NULL)
26669 return -1;
26670 vim_free(*bufp);
26671 *bufp = *fnamep = tail = p;
26672 *fnamelen = 1;
26673 }
26674 else
26675 {
26676 while (tail > s && !after_pathsep(s, tail))
26677 mb_ptr_back(*fnamep, tail);
26678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026679 }
26680
26681 /* ":8" - shortname */
26682 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26683 {
26684 *usedlen += 2;
26685#ifdef WIN3264
26686 has_shortname = 1;
26687#endif
26688 }
26689
26690#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026691 /*
26692 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026693 */
26694 if (has_shortname)
26695 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026696 /* Copy the string if it is shortened by :h and when it wasn't copied
26697 * yet, because we are going to change it in place. Avoids changing
26698 * the buffer name for "%:8". */
26699 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026700 {
26701 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026702 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026703 return -1;
26704 vim_free(*bufp);
26705 *bufp = *fnamep = p;
26706 }
26707
26708 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026709 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026710 if (!has_fullname && !vim_isAbsName(*fnamep))
26711 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026712 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026713 return -1;
26714 }
26715 else
26716 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026717 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026718
Bram Moolenaardc935552011-08-17 15:23:23 +020026719 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026720 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026721 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026722 return -1;
26723
26724 if (l == 0)
26725 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026726 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026727 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026728 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026729 return -1;
26730 }
26731 *fnamelen = l;
26732 }
26733 }
26734#endif /* WIN3264 */
26735
26736 /* ":t" - tail, just the basename */
26737 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26738 {
26739 *usedlen += 2;
26740 *fnamelen -= (int)(tail - *fnamep);
26741 *fnamep = tail;
26742 }
26743
26744 /* ":e" - extension, can be repeated */
26745 /* ":r" - root, without extension, can be repeated */
26746 while (src[*usedlen] == ':'
26747 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26748 {
26749 /* find a '.' in the tail:
26750 * - for second :e: before the current fname
26751 * - otherwise: The last '.'
26752 */
26753 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26754 s = *fnamep - 2;
26755 else
26756 s = *fnamep + *fnamelen - 1;
26757 for ( ; s > tail; --s)
26758 if (s[0] == '.')
26759 break;
26760 if (src[*usedlen + 1] == 'e') /* :e */
26761 {
26762 if (s > tail)
26763 {
26764 *fnamelen += (int)(*fnamep - (s + 1));
26765 *fnamep = s + 1;
26766#ifdef VMS
26767 /* cut version from the extension */
26768 s = *fnamep + *fnamelen - 1;
26769 for ( ; s > *fnamep; --s)
26770 if (s[0] == ';')
26771 break;
26772 if (s > *fnamep)
26773 *fnamelen = s - *fnamep;
26774#endif
26775 }
26776 else if (*fnamep <= tail)
26777 *fnamelen = 0;
26778 }
26779 else /* :r */
26780 {
26781 if (s > tail) /* remove one extension */
26782 *fnamelen = (int)(s - *fnamep);
26783 }
26784 *usedlen += 2;
26785 }
26786
26787 /* ":s?pat?foo?" - substitute */
26788 /* ":gs?pat?foo?" - global substitute */
26789 if (src[*usedlen] == ':'
26790 && (src[*usedlen + 1] == 's'
26791 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26792 {
26793 char_u *str;
26794 char_u *pat;
26795 char_u *sub;
26796 int sep;
26797 char_u *flags;
26798 int didit = FALSE;
26799
26800 flags = (char_u *)"";
26801 s = src + *usedlen + 2;
26802 if (src[*usedlen + 1] == 'g')
26803 {
26804 flags = (char_u *)"g";
26805 ++s;
26806 }
26807
26808 sep = *s++;
26809 if (sep)
26810 {
26811 /* find end of pattern */
26812 p = vim_strchr(s, sep);
26813 if (p != NULL)
26814 {
26815 pat = vim_strnsave(s, (int)(p - s));
26816 if (pat != NULL)
26817 {
26818 s = p + 1;
26819 /* find end of substitution */
26820 p = vim_strchr(s, sep);
26821 if (p != NULL)
26822 {
26823 sub = vim_strnsave(s, (int)(p - s));
26824 str = vim_strnsave(*fnamep, *fnamelen);
26825 if (sub != NULL && str != NULL)
26826 {
26827 *usedlen = (int)(p + 1 - src);
26828 s = do_string_sub(str, pat, sub, flags);
26829 if (s != NULL)
26830 {
26831 *fnamep = s;
26832 *fnamelen = (int)STRLEN(s);
26833 vim_free(*bufp);
26834 *bufp = s;
26835 didit = TRUE;
26836 }
26837 }
26838 vim_free(sub);
26839 vim_free(str);
26840 }
26841 vim_free(pat);
26842 }
26843 }
26844 /* after using ":s", repeat all the modifiers */
26845 if (didit)
26846 goto repeat;
26847 }
26848 }
26849
Bram Moolenaar26df0922014-02-23 23:39:13 +010026850 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26851 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026852 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026853 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026854 if (c != NUL)
26855 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026856 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026857 if (c != NUL)
26858 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026859 if (p == NULL)
26860 return -1;
26861 vim_free(*bufp);
26862 *bufp = *fnamep = p;
26863 *fnamelen = (int)STRLEN(p);
26864 *usedlen += 2;
26865 }
26866
Bram Moolenaar071d4272004-06-13 20:20:40 +000026867 return valid;
26868}
26869
26870/*
26871 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26872 * "flags" can be "g" to do a global substitute.
26873 * Returns an allocated string, NULL for error.
26874 */
26875 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026876do_string_sub(
26877 char_u *str,
26878 char_u *pat,
26879 char_u *sub,
26880 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026881{
26882 int sublen;
26883 regmatch_T regmatch;
26884 int i;
26885 int do_all;
26886 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026887 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026888 garray_T ga;
26889 char_u *ret;
26890 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026891 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026892
26893 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26894 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026895 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026896
26897 ga_init2(&ga, 1, 200);
26898
26899 do_all = (flags[0] == 'g');
26900
26901 regmatch.rm_ic = p_ic;
26902 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26903 if (regmatch.regprog != NULL)
26904 {
26905 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026906 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026907 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26908 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026909 /* Skip empty match except for first match. */
26910 if (regmatch.startp[0] == regmatch.endp[0])
26911 {
26912 if (zero_width == regmatch.startp[0])
26913 {
26914 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026915 i = MB_PTR2LEN(tail);
26916 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26917 (size_t)i);
26918 ga.ga_len += i;
26919 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026920 continue;
26921 }
26922 zero_width = regmatch.startp[0];
26923 }
26924
Bram Moolenaar071d4272004-06-13 20:20:40 +000026925 /*
26926 * Get some space for a temporary buffer to do the substitution
26927 * into. It will contain:
26928 * - The text up to where the match is.
26929 * - The substituted text.
26930 * - The text after the match.
26931 */
26932 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026933 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026934 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26935 {
26936 ga_clear(&ga);
26937 break;
26938 }
26939
26940 /* copy the text up to where the match is */
26941 i = (int)(regmatch.startp[0] - tail);
26942 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26943 /* add the substituted text */
26944 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26945 + ga.ga_len + i, TRUE, TRUE, FALSE);
26946 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026947 tail = regmatch.endp[0];
26948 if (*tail == NUL)
26949 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026950 if (!do_all)
26951 break;
26952 }
26953
26954 if (ga.ga_data != NULL)
26955 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26956
Bram Moolenaar473de612013-06-08 18:19:48 +020026957 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026958 }
26959
26960 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26961 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026962 if (p_cpo == empty_option)
26963 p_cpo = save_cpo;
26964 else
26965 /* Darn, evaluating {sub} expression changed the value. */
26966 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026967
26968 return ret;
26969}
26970
26971#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */