blob: ce83143cee96979a16ae3d6364d4df2b388b9576 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000355 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000356 {VV_NAME("swapname", VAR_STRING), VV_RO},
357 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000358 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200359 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000360 {VV_NAME("mouse_win", VAR_NUMBER), 0},
361 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
362 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000363 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100365 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000366 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200367 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200368 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200369 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200370 {VV_NAME("option_new", VAR_STRING), VV_RO},
371 {VV_NAME("option_old", VAR_STRING), VV_RO},
372 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100373 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100374 {VV_NAME("false", VAR_SPECIAL), VV_RO},
375 {VV_NAME("true", VAR_SPECIAL), VV_RO},
376 {VV_NAME("null", VAR_SPECIAL), VV_RO},
377 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100378 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200379 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000380};
381
382/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_type vv_di.di_tv.v_type
384#define vv_nr vv_di.di_tv.vval.v_number
385#define vv_float vv_di.di_tv.vval.v_float
386#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000387#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200388#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000389#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000390
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200391static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000392#define vimvarht vimvardict.dv_hashtab
393
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100394static void prepare_vimvar(int idx, typval_T *save_tv);
395static void restore_vimvar(int idx, typval_T *save_tv);
396static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
397static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
398static char_u *skip_var_one(char_u *arg);
399static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
400static void list_glob_vars(int *first);
401static void list_buf_vars(int *first);
402static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000403#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100404static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_vim_vars(int *first);
407static void list_script_vars(int *first);
408static void list_func_vars(int *first);
409static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
410static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
411static int check_changedtick(char_u *arg);
412static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
413static void clear_lval(lval_T *lp);
414static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
415static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
416static void list_fix_watch(list_T *l, listitem_T *item);
417static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
418static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
419static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
420static void item_lock(typval_T *tv, int deep, int lock);
421static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100423static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
424static int eval1(char_u **arg, typval_T *rettv, int evaluate);
425static int eval2(char_u **arg, typval_T *rettv, int evaluate);
426static int eval3(char_u **arg, typval_T *rettv, int evaluate);
427static int eval4(char_u **arg, typval_T *rettv, int evaluate);
428static int eval5(char_u **arg, typval_T *rettv, int evaluate);
429static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
430static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000431
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100432static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
433static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
434static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
435static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200437static void list_free_contents(list_T *l);
438static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static long list_len(list_T *l);
440static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
441static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
442static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
443static long list_find_nr(list_T *l, long idx, int *errorp);
444static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100445static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
446static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
447static list_T *list_copy(list_T *orig, int deep, int copyID);
448static char_u *list2string(typval_T *tv, int copyID);
449static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
450static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
451static int free_unref_items(int copyID);
452static dictitem_T *dictitem_copy(dictitem_T *org);
453static void dictitem_remove(dict_T *dict, dictitem_T *item);
454static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
455static long dict_len(dict_T *d);
456static char_u *dict2string(typval_T *tv, int copyID);
457static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
458static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100462static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
463static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200467static void dict_free_contents(dict_T *d);
468static void dict_free_dict(dict_T *d);
469
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_abs(typval_T *argvars, typval_T *rettv);
472static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_add(typval_T *argvars, typval_T *rettv);
475static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
476static void f_and(typval_T *argvars, typval_T *rettv);
477static void f_append(typval_T *argvars, typval_T *rettv);
478static void f_argc(typval_T *argvars, typval_T *rettv);
479static void f_argidx(typval_T *argvars, typval_T *rettv);
480static void f_arglistid(typval_T *argvars, typval_T *rettv);
481static void f_argv(typval_T *argvars, typval_T *rettv);
482static void f_assert_equal(typval_T *argvars, typval_T *rettv);
483static void f_assert_exception(typval_T *argvars, typval_T *rettv);
484static void f_assert_fails(typval_T *argvars, typval_T *rettv);
485static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200486static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200487static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
488static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_asin(typval_T *argvars, typval_T *rettv);
492static void f_atan(typval_T *argvars, typval_T *rettv);
493static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100495static void f_browse(typval_T *argvars, typval_T *rettv);
496static void f_browsedir(typval_T *argvars, typval_T *rettv);
497static void f_bufexists(typval_T *argvars, typval_T *rettv);
498static void f_buflisted(typval_T *argvars, typval_T *rettv);
499static void f_bufloaded(typval_T *argvars, typval_T *rettv);
500static void f_bufname(typval_T *argvars, typval_T *rettv);
501static void f_bufnr(typval_T *argvars, typval_T *rettv);
502static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
503static void f_byte2line(typval_T *argvars, typval_T *rettv);
504static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
505static void f_byteidx(typval_T *argvars, typval_T *rettv);
506static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
507static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100509static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000510#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100513static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
514static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100515static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100516static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100517static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100518static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100519static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
520static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100521static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100523static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
524static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100525static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100526static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100527#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_changenr(typval_T *argvars, typval_T *rettv);
529static void f_char2nr(typval_T *argvars, typval_T *rettv);
530static void f_cindent(typval_T *argvars, typval_T *rettv);
531static void f_clearmatches(typval_T *argvars, typval_T *rettv);
532static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000533#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100534static void f_complete(typval_T *argvars, typval_T *rettv);
535static void f_complete_add(typval_T *argvars, typval_T *rettv);
536static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000537#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100538static void f_confirm(typval_T *argvars, typval_T *rettv);
539static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_cos(typval_T *argvars, typval_T *rettv);
542static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_count(typval_T *argvars, typval_T *rettv);
545static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
546static void f_cursor(typval_T *argsvars, typval_T *rettv);
547static void f_deepcopy(typval_T *argvars, typval_T *rettv);
548static void f_delete(typval_T *argvars, typval_T *rettv);
549static void f_did_filetype(typval_T *argvars, typval_T *rettv);
550static void f_diff_filler(typval_T *argvars, typval_T *rettv);
551static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100552static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100553static void f_empty(typval_T *argvars, typval_T *rettv);
554static void f_escape(typval_T *argvars, typval_T *rettv);
555static void f_eval(typval_T *argvars, typval_T *rettv);
556static void f_eventhandler(typval_T *argvars, typval_T *rettv);
557static void f_executable(typval_T *argvars, typval_T *rettv);
558static void f_exepath(typval_T *argvars, typval_T *rettv);
559static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200562#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_expand(typval_T *argvars, typval_T *rettv);
564static void f_extend(typval_T *argvars, typval_T *rettv);
565static void f_feedkeys(typval_T *argvars, typval_T *rettv);
566static void f_filereadable(typval_T *argvars, typval_T *rettv);
567static void f_filewritable(typval_T *argvars, typval_T *rettv);
568static void f_filter(typval_T *argvars, typval_T *rettv);
569static void f_finddir(typval_T *argvars, typval_T *rettv);
570static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000571#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100572static void f_float2nr(typval_T *argvars, typval_T *rettv);
573static void f_floor(typval_T *argvars, typval_T *rettv);
574static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000575#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100576static void f_fnameescape(typval_T *argvars, typval_T *rettv);
577static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
578static void f_foldclosed(typval_T *argvars, typval_T *rettv);
579static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
580static void f_foldlevel(typval_T *argvars, typval_T *rettv);
581static void f_foldtext(typval_T *argvars, typval_T *rettv);
582static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
583static void f_foreground(typval_T *argvars, typval_T *rettv);
584static void f_function(typval_T *argvars, typval_T *rettv);
585static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200586static void f_garbagecollect_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100587static void f_get(typval_T *argvars, typval_T *rettv);
588static void f_getbufline(typval_T *argvars, typval_T *rettv);
589static void f_getbufvar(typval_T *argvars, typval_T *rettv);
590static void f_getchar(typval_T *argvars, typval_T *rettv);
591static void f_getcharmod(typval_T *argvars, typval_T *rettv);
592static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
593static void f_getcmdline(typval_T *argvars, typval_T *rettv);
594static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
595static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
596static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
597static void f_getcwd(typval_T *argvars, typval_T *rettv);
598static void f_getfontname(typval_T *argvars, typval_T *rettv);
599static void f_getfperm(typval_T *argvars, typval_T *rettv);
600static void f_getfsize(typval_T *argvars, typval_T *rettv);
601static void f_getftime(typval_T *argvars, typval_T *rettv);
602static void f_getftype(typval_T *argvars, typval_T *rettv);
603static void f_getline(typval_T *argvars, typval_T *rettv);
604static void f_getmatches(typval_T *argvars, typval_T *rettv);
605static void f_getpid(typval_T *argvars, typval_T *rettv);
606static void f_getcurpos(typval_T *argvars, typval_T *rettv);
607static void f_getpos(typval_T *argvars, typval_T *rettv);
608static void f_getqflist(typval_T *argvars, typval_T *rettv);
609static void f_getreg(typval_T *argvars, typval_T *rettv);
610static void f_getregtype(typval_T *argvars, typval_T *rettv);
611static void f_gettabvar(typval_T *argvars, typval_T *rettv);
612static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
613static void f_getwinposx(typval_T *argvars, typval_T *rettv);
614static void f_getwinposy(typval_T *argvars, typval_T *rettv);
615static void f_getwinvar(typval_T *argvars, typval_T *rettv);
616static void f_glob(typval_T *argvars, typval_T *rettv);
617static void f_globpath(typval_T *argvars, typval_T *rettv);
618static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
619static void f_has(typval_T *argvars, typval_T *rettv);
620static void f_has_key(typval_T *argvars, typval_T *rettv);
621static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
622static void f_hasmapto(typval_T *argvars, typval_T *rettv);
623static void f_histadd(typval_T *argvars, typval_T *rettv);
624static void f_histdel(typval_T *argvars, typval_T *rettv);
625static void f_histget(typval_T *argvars, typval_T *rettv);
626static void f_histnr(typval_T *argvars, typval_T *rettv);
627static void f_hlID(typval_T *argvars, typval_T *rettv);
628static void f_hlexists(typval_T *argvars, typval_T *rettv);
629static void f_hostname(typval_T *argvars, typval_T *rettv);
630static void f_iconv(typval_T *argvars, typval_T *rettv);
631static void f_indent(typval_T *argvars, typval_T *rettv);
632static void f_index(typval_T *argvars, typval_T *rettv);
633static void f_input(typval_T *argvars, typval_T *rettv);
634static void f_inputdialog(typval_T *argvars, typval_T *rettv);
635static void f_inputlist(typval_T *argvars, typval_T *rettv);
636static void f_inputrestore(typval_T *argvars, typval_T *rettv);
637static void f_inputsave(typval_T *argvars, typval_T *rettv);
638static void f_inputsecret(typval_T *argvars, typval_T *rettv);
639static void f_insert(typval_T *argvars, typval_T *rettv);
640static void f_invert(typval_T *argvars, typval_T *rettv);
641static void f_isdirectory(typval_T *argvars, typval_T *rettv);
642static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100643#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
644static void f_isnan(typval_T *argvars, typval_T *rettv);
645#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100647#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100648static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100649static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100650static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100651static void f_job_start(typval_T *argvars, typval_T *rettv);
652static void f_job_stop(typval_T *argvars, typval_T *rettv);
653static void f_job_status(typval_T *argvars, typval_T *rettv);
654#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100656static void f_js_decode(typval_T *argvars, typval_T *rettv);
657static void f_js_encode(typval_T *argvars, typval_T *rettv);
658static void f_json_decode(typval_T *argvars, typval_T *rettv);
659static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_keys(typval_T *argvars, typval_T *rettv);
661static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
662static void f_len(typval_T *argvars, typval_T *rettv);
663static void f_libcall(typval_T *argvars, typval_T *rettv);
664static void f_libcallnr(typval_T *argvars, typval_T *rettv);
665static void f_line(typval_T *argvars, typval_T *rettv);
666static void f_line2byte(typval_T *argvars, typval_T *rettv);
667static void f_lispindent(typval_T *argvars, typval_T *rettv);
668static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000669#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100670static void f_log(typval_T *argvars, typval_T *rettv);
671static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000672#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200673#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200675#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_map(typval_T *argvars, typval_T *rettv);
677static void f_maparg(typval_T *argvars, typval_T *rettv);
678static void f_mapcheck(typval_T *argvars, typval_T *rettv);
679static void f_match(typval_T *argvars, typval_T *rettv);
680static void f_matchadd(typval_T *argvars, typval_T *rettv);
681static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
682static void f_matcharg(typval_T *argvars, typval_T *rettv);
683static void f_matchdelete(typval_T *argvars, typval_T *rettv);
684static void f_matchend(typval_T *argvars, typval_T *rettv);
685static void f_matchlist(typval_T *argvars, typval_T *rettv);
686static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200687static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_max(typval_T *argvars, typval_T *rettv);
689static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000690#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000692#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100694#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
698static void f_nr2char(typval_T *argvars, typval_T *rettv);
699static void f_or(typval_T *argvars, typval_T *rettv);
700static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100701#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100703#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
708static void f_printf(typval_T *argvars, typval_T *rettv);
709static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200710#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100711static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200712#endif
713#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100714static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200715#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_range(typval_T *argvars, typval_T *rettv);
717static void f_readfile(typval_T *argvars, typval_T *rettv);
718static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100719#ifdef FEAT_FLOAT
720static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
721#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_reltimestr(typval_T *argvars, typval_T *rettv);
723static void f_remote_expr(typval_T *argvars, typval_T *rettv);
724static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
725static void f_remote_peek(typval_T *argvars, typval_T *rettv);
726static void f_remote_read(typval_T *argvars, typval_T *rettv);
727static void f_remote_send(typval_T *argvars, typval_T *rettv);
728static void f_remove(typval_T *argvars, typval_T *rettv);
729static void f_rename(typval_T *argvars, typval_T *rettv);
730static void f_repeat(typval_T *argvars, typval_T *rettv);
731static void f_resolve(typval_T *argvars, typval_T *rettv);
732static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_screenattr(typval_T *argvars, typval_T *rettv);
737static void f_screenchar(typval_T *argvars, typval_T *rettv);
738static void f_screencol(typval_T *argvars, typval_T *rettv);
739static void f_screenrow(typval_T *argvars, typval_T *rettv);
740static void f_search(typval_T *argvars, typval_T *rettv);
741static void f_searchdecl(typval_T *argvars, typval_T *rettv);
742static void f_searchpair(typval_T *argvars, typval_T *rettv);
743static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
744static void f_searchpos(typval_T *argvars, typval_T *rettv);
745static void f_server2client(typval_T *argvars, typval_T *rettv);
746static void f_serverlist(typval_T *argvars, typval_T *rettv);
747static void f_setbufvar(typval_T *argvars, typval_T *rettv);
748static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
749static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100750static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_setline(typval_T *argvars, typval_T *rettv);
752static void f_setloclist(typval_T *argvars, typval_T *rettv);
753static void f_setmatches(typval_T *argvars, typval_T *rettv);
754static void f_setpos(typval_T *argvars, typval_T *rettv);
755static void f_setqflist(typval_T *argvars, typval_T *rettv);
756static void f_setreg(typval_T *argvars, typval_T *rettv);
757static void f_settabvar(typval_T *argvars, typval_T *rettv);
758static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
759static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100760#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100762#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_shellescape(typval_T *argvars, typval_T *rettv);
764static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
765static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_sin(typval_T *argvars, typval_T *rettv);
768static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000769#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_sort(typval_T *argvars, typval_T *rettv);
771static void f_soundfold(typval_T *argvars, typval_T *rettv);
772static void f_spellbadword(typval_T *argvars, typval_T *rettv);
773static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
774static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000775#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_sqrt(typval_T *argvars, typval_T *rettv);
777static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000778#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100779static void f_str2nr(typval_T *argvars, typval_T *rettv);
780static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200784static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100785static void f_stridx(typval_T *argvars, typval_T *rettv);
786static void f_string(typval_T *argvars, typval_T *rettv);
787static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200788static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100789static void f_strpart(typval_T *argvars, typval_T *rettv);
790static void f_strridx(typval_T *argvars, typval_T *rettv);
791static void f_strtrans(typval_T *argvars, typval_T *rettv);
792static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
793static void f_strwidth(typval_T *argvars, typval_T *rettv);
794static void f_submatch(typval_T *argvars, typval_T *rettv);
795static void f_substitute(typval_T *argvars, typval_T *rettv);
796static void f_synID(typval_T *argvars, typval_T *rettv);
797static void f_synIDattr(typval_T *argvars, typval_T *rettv);
798static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
799static void f_synstack(typval_T *argvars, typval_T *rettv);
800static void f_synconcealed(typval_T *argvars, typval_T *rettv);
801static void f_system(typval_T *argvars, typval_T *rettv);
802static void f_systemlist(typval_T *argvars, typval_T *rettv);
803static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
804static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
805static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
806static void f_taglist(typval_T *argvars, typval_T *rettv);
807static void f_tagfiles(typval_T *argvars, typval_T *rettv);
808static void f_tempname(typval_T *argvars, typval_T *rettv);
809static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200810#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100811static void f_tan(typval_T *argvars, typval_T *rettv);
812static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200813#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100814#ifdef FEAT_TIMERS
815static void f_timer_start(typval_T *argvars, typval_T *rettv);
816static void f_timer_stop(typval_T *argvars, typval_T *rettv);
817#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100818static void f_tolower(typval_T *argvars, typval_T *rettv);
819static void f_toupper(typval_T *argvars, typval_T *rettv);
820static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000821#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100822static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000823#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100824static void f_type(typval_T *argvars, typval_T *rettv);
825static void f_undofile(typval_T *argvars, typval_T *rettv);
826static void f_undotree(typval_T *argvars, typval_T *rettv);
827static void f_uniq(typval_T *argvars, typval_T *rettv);
828static void f_values(typval_T *argvars, typval_T *rettv);
829static void f_virtcol(typval_T *argvars, typval_T *rettv);
830static void f_visualmode(typval_T *argvars, typval_T *rettv);
831static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100832static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100833static void f_win_getid(typval_T *argvars, typval_T *rettv);
834static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
835static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
836static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static void f_winbufnr(typval_T *argvars, typval_T *rettv);
838static void f_wincol(typval_T *argvars, typval_T *rettv);
839static void f_winheight(typval_T *argvars, typval_T *rettv);
840static void f_winline(typval_T *argvars, typval_T *rettv);
841static void f_winnr(typval_T *argvars, typval_T *rettv);
842static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
843static void f_winrestview(typval_T *argvars, typval_T *rettv);
844static void f_winsaveview(typval_T *argvars, typval_T *rettv);
845static void f_winwidth(typval_T *argvars, typval_T *rettv);
846static void f_writefile(typval_T *argvars, typval_T *rettv);
847static void f_wordcount(typval_T *argvars, typval_T *rettv);
848static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100850static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
851static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
852static int get_env_len(char_u **arg);
853static int get_id_len(char_u **arg);
854static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
855static 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 +0000856#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
857#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
858 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
860static int eval_isnamec(int c);
861static int eval_isnamec1(int c);
862static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
863static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100864static typval_T *alloc_string_tv(char_u *string);
865static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100866#ifdef FEAT_FLOAT
867static float_T get_tv_float(typval_T *varp);
868#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static linenr_T get_tv_lnum(typval_T *argvars);
870static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100871static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
872static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
873static hashtab_T *find_var_ht(char_u *name, char_u **varname);
874static funccall_T *get_funccal(void);
875static void vars_clear_ext(hashtab_T *ht, int free_val);
876static void delete_var(hashtab_T *ht, hashitem_T *hi);
877static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
878static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
879static void set_var(char_u *name, typval_T *varp, int copy);
880static int var_check_ro(int flags, char_u *name, int use_gettext);
881static int var_check_fixed(int flags, char_u *name, int use_gettext);
882static int var_check_func_name(char_u *name, int new_var);
883static int valid_varname(char_u *varname);
884static int tv_check_lock(int lock, char_u *name, int use_gettext);
885static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
886static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100887static 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 +0100888static int eval_fname_script(char_u *p);
889static int eval_fname_sid(char_u *p);
890static void list_func_head(ufunc_T *fp, int indent);
891static ufunc_T *find_func(char_u *name);
892static int function_exists(char_u *name);
893static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100895static void func_do_profile(ufunc_T *fp);
896static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
897static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000898static int
899# ifdef __BORLANDC__
900 _RTLENTRYF
901# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100902 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000903static int
904# ifdef __BORLANDC__
905 _RTLENTRYF
906# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100907 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000908#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100909static int script_autoload(char_u *name, int reload);
910static char_u *autoload_name(char_u *name);
911static void cat_func_name(char_u *buf, ufunc_T *fp);
912static void func_free(ufunc_T *fp);
913static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
914static int can_free_funccal(funccall_T *fc, int copyID) ;
915static void free_funccal(funccall_T *fc, int free_val);
916static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
917static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
918static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
919static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
920static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
921static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
922static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
923static int write_list(FILE *fd, list_T *list, int binary);
924static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000925
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200926
927#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100928static int compare_func_name(const void *s1, const void *s2);
929static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200930#endif
931
Bram Moolenaar33570922005-01-25 22:26:29 +0000932/*
933 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000934 */
935 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100936eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000937{
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 int i;
939 struct vimvar *p;
940
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200941 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
942 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200943 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000944 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000946
947 for (i = 0; i < VV_LEN; ++i)
948 {
949 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200950 if (STRLEN(p->vv_name) > 16)
951 {
952 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
953 getout(1);
954 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 STRCPY(p->vv_di.di_key, p->vv_name);
956 if (p->vv_flags & VV_RO)
957 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
958 else if (p->vv_flags & VV_RO_SBX)
959 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
960 else
961 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000962
963 /* add to v: scope dict, unless the value is not always available */
964 if (p->vv_type != VAR_UNKNOWN)
965 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000967 /* add to compat scope dict */
968 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100970 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
971
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000972 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100973 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200974 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100975 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100976
977 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
978 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
979 set_vim_var_nr(VV_NONE, VVAL_NONE);
980 set_vim_var_nr(VV_NULL, VVAL_NULL);
981
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200982 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200983
984#ifdef EBCDIC
985 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100986 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200987 */
988 sortFunctions();
989#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000990}
991
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992#if defined(EXITFREE) || defined(PROTO)
993 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100994eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995{
996 int i;
997 struct vimvar *p;
998
999 for (i = 0; i < VV_LEN; ++i)
1000 {
1001 p = &vimvars[i];
1002 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001003 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001004 vim_free(p->vv_str);
1005 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001006 }
1007 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1008 {
1009 list_unref(p->vv_list);
1010 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001011 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001012 }
1013 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001014 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001015 hash_clear(&compat_hashtab);
1016
Bram Moolenaard9fba312005-06-26 22:34:35 +00001017 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001018# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001019 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001020# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001021
1022 /* global variables */
1023 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001024
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001025 /* autoloaded script names */
1026 ga_clear_strings(&ga_loaded);
1027
Bram Moolenaarcca74132013-09-25 21:00:28 +02001028 /* Script-local variables. First clear all the variables and in a second
1029 * loop free the scriptvar_T, because a variable in one script might hold
1030 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001031 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001032 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001033 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001034 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001035 ga_clear(&ga_scripts);
1036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001037 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001038 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001039
1040 /* functions */
1041 free_all_functions();
1042 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001043}
1044#endif
1045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * Return the name of the executed function.
1048 */
1049 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001050func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053}
1054
1055/*
1056 * Return the address holding the next breakpoint line for a funccall cookie.
1057 */
1058 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001059func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar33570922005-01-25 22:26:29 +00001061 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062}
1063
1064/*
1065 * Return the address holding the debug tick for a funccall cookie.
1066 */
1067 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001068func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar33570922005-01-25 22:26:29 +00001070 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071}
1072
1073/*
1074 * Return the nesting level for a funccall cookie.
1075 */
1076 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001077func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
1082/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001083funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001085/* pointer to list of previously used funccal, still around because some
1086 * item in it is still being used. */
1087funccall_T *previous_funccal = NULL;
1088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089/*
1090 * Return TRUE when a function was ended by a ":return" command.
1091 */
1092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001093current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 return current_funccal->returned;
1096}
1097
1098
1099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 * Set an internal variable to a string value. Creates the variable if it does
1101 * not already exist.
1102 */
1103 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001104set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001106 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001107 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 val = vim_strsave(value);
1110 if (val != NULL)
1111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001112 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001113 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001115 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 }
1118 }
1119}
1120
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123static char_u *redir_endp = NULL;
1124static char_u *redir_varname = NULL;
1125
1126/*
1127 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001128 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 * Returns OK if successfully completed the setup. FAIL otherwise.
1130 */
1131 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001132var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133{
1134 int save_emsg;
1135 int err;
1136 typval_T tv;
1137
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001139 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 {
1141 EMSG(_(e_invarg));
1142 return FAIL;
1143 }
1144
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001145 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 redir_varname = vim_strsave(name);
1147 if (redir_varname == NULL)
1148 return FAIL;
1149
1150 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1151 if (redir_lval == NULL)
1152 {
1153 var_redir_stop();
1154 return FAIL;
1155 }
1156
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157 /* The output is stored in growarray "redir_ga" until redirection ends. */
1158 ga_init2(&redir_ga, (int)sizeof(char), 500);
1159
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001161 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001162 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1164 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 if (redir_endp != NULL && *redir_endp != NUL)
1167 /* Trailing characters are present after the variable name */
1168 EMSG(_(e_trailing));
1169 else
1170 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001171 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 var_redir_stop();
1173 return FAIL;
1174 }
1175
1176 /* check if we can write to the variable: set it to or append an empty
1177 * string */
1178 save_emsg = did_emsg;
1179 did_emsg = FALSE;
1180 tv.v_type = VAR_STRING;
1181 tv.vval.v_string = (char_u *)"";
1182 if (append)
1183 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1184 else
1185 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001186 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001188 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189 if (err)
1190 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001191 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 var_redir_stop();
1193 return FAIL;
1194 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195
1196 return OK;
1197}
1198
1199/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001200 * Append "value[value_len]" to the variable set by var_redir_start().
1201 * The actual appending is postponed until redirection ends, because the value
1202 * appended may in fact be the string we write to, changing it may cause freed
1203 * memory to be used:
1204 * :redir => foo
1205 * :let foo
1206 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 */
1208 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001209var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001211 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212
1213 if (redir_lval == NULL)
1214 return;
1215
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001216 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001217 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001218 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001219 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001221 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001222 {
1223 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001224 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001225 }
1226 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228}
1229
1230/*
1231 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001232 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001233 */
1234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001235var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001237 typval_T tv;
1238
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001239 if (redir_lval != NULL)
1240 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001241 /* If there was no error: assign the text to the variable. */
1242 if (redir_endp != NULL)
1243 {
1244 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1245 tv.v_type = VAR_STRING;
1246 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001247 /* Call get_lval() again, if it's inside a Dict or List it may
1248 * have changed. */
1249 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001250 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001251 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1252 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1253 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001254 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001255
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001256 /* free the collected output */
1257 vim_free(redir_ga.ga_data);
1258 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001259
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001260 vim_free(redir_lval);
1261 redir_lval = NULL;
1262 }
1263 vim_free(redir_varname);
1264 redir_varname = NULL;
1265}
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267# if defined(FEAT_MBYTE) || defined(PROTO)
1268 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001269eval_charconvert(
1270 char_u *enc_from,
1271 char_u *enc_to,
1272 char_u *fname_from,
1273 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 int err = FALSE;
1276
1277 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1278 set_vim_var_string(VV_CC_TO, enc_to, -1);
1279 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1280 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1281 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1282 err = TRUE;
1283 set_vim_var_string(VV_CC_FROM, NULL, -1);
1284 set_vim_var_string(VV_CC_TO, NULL, -1);
1285 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1286 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1287
1288 if (err)
1289 return FAIL;
1290 return OK;
1291}
1292# endif
1293
1294# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1295 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001296eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
1298 int err = FALSE;
1299
1300 set_vim_var_string(VV_FNAME_IN, fname, -1);
1301 set_vim_var_string(VV_CMDARG, args, -1);
1302 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1303 err = TRUE;
1304 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1305 set_vim_var_string(VV_CMDARG, NULL, -1);
1306
1307 if (err)
1308 {
1309 mch_remove(fname);
1310 return FAIL;
1311 }
1312 return OK;
1313}
1314# endif
1315
1316# if defined(FEAT_DIFF) || defined(PROTO)
1317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001318eval_diff(
1319 char_u *origfile,
1320 char_u *newfile,
1321 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
1323 int err = FALSE;
1324
1325 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1326 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1327 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1328 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1329 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1330 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1331 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1332}
1333
1334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001335eval_patch(
1336 char_u *origfile,
1337 char_u *difffile,
1338 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 int err;
1341
1342 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1343 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1344 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1345 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1346 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1347 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1348 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1349}
1350# endif
1351
1352/*
1353 * Top level evaluation function, returning a boolean.
1354 * Sets "error" to TRUE if there was an error.
1355 * Return TRUE or FALSE.
1356 */
1357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001358eval_to_bool(
1359 char_u *arg,
1360 int *error,
1361 char_u **nextcmd,
1362 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
Bram Moolenaar33570922005-01-25 22:26:29 +00001364 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 int retval = FALSE;
1366
1367 if (skip)
1368 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 else
1372 {
1373 *error = FALSE;
1374 if (!skip)
1375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001376 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379 }
1380 if (skip)
1381 --emsg_skip;
1382
1383 return retval;
1384}
1385
1386/*
1387 * Top level evaluation function, returning a string. If "skip" is TRUE,
1388 * only parsing to "nextcmd" is done, without reporting errors. Return
1389 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1390 */
1391 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001392eval_to_string_skip(
1393 char_u *arg,
1394 char_u **nextcmd,
1395 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *retval;
1399
1400 if (skip)
1401 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001402 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 retval = NULL;
1404 else
1405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001406 retval = vim_strsave(get_tv_string(&tv));
1407 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 if (skip)
1410 --emsg_skip;
1411
1412 return retval;
1413}
1414
1415/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001416 * Skip over an expression at "*pp".
1417 * Return FAIL for an error, OK otherwise.
1418 */
1419 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001420skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001421{
Bram Moolenaar33570922005-01-25 22:26:29 +00001422 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001423
1424 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001426}
1427
1428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001430 * When "convert" is TRUE convert a List into a sequence of lines and convert
1431 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 * Return pointer to allocated memory, or NULL for failure.
1433 */
1434 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001435eval_to_string(
1436 char_u *arg,
1437 char_u **nextcmd,
1438 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
Bram Moolenaar33570922005-01-25 22:26:29 +00001440 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001442 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001443#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001444 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 retval = NULL;
1449 else
1450 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001451 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001452 {
1453 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001454 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001455 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001456 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001457 if (tv.vval.v_list->lv_len > 0)
1458 ga_append(&ga, NL);
1459 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001460 ga_append(&ga, NUL);
1461 retval = (char_u *)ga.ga_data;
1462 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001463#ifdef FEAT_FLOAT
1464 else if (convert && tv.v_type == VAR_FLOAT)
1465 {
1466 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1467 retval = vim_strsave(numbuf);
1468 }
1469#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001470 else
1471 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001472 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474
1475 return retval;
1476}
1477
1478/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001479 * Call eval_to_string() without using current local variables and using
1480 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 */
1482 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483eval_to_string_safe(
1484 char_u *arg,
1485 char_u **nextcmd,
1486 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 char_u *retval;
1489 void *save_funccalp;
1490
1491 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001492 if (use_sandbox)
1493 ++sandbox;
1494 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001495 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001496 if (use_sandbox)
1497 --sandbox;
1498 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 restore_funccal(save_funccalp);
1500 return retval;
1501}
1502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503/*
1504 * Top level evaluation function, returning a number.
1505 * Evaluates "expr" silently.
1506 * Returns -1 for an error.
1507 */
1508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
Bram Moolenaar33570922005-01-25 22:26:29 +00001511 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001513 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
1515 ++emsg_off;
1516
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001517 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 retval = -1;
1519 else
1520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001521 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001522 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
1524 --emsg_off;
1525
1526 return retval;
1527}
1528
Bram Moolenaara40058a2005-07-11 22:42:07 +00001529/*
1530 * Prepare v: variable "idx" to be used.
1531 * Save the current typeval in "save_tv".
1532 * When not used yet add the variable to the v: hashtable.
1533 */
1534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001535prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001536{
1537 *save_tv = vimvars[idx].vv_tv;
1538 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1539 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1540}
1541
1542/*
1543 * Restore v: variable "idx" to typeval "save_tv".
1544 * When no longer defined, remove the variable from the v: hashtable.
1545 */
1546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001547restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001548{
1549 hashitem_T *hi;
1550
Bram Moolenaara40058a2005-07-11 22:42:07 +00001551 vimvars[idx].vv_tv = *save_tv;
1552 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1553 {
1554 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1555 if (HASHITEM_EMPTY(hi))
1556 EMSG2(_(e_intern2), "restore_vimvar()");
1557 else
1558 hash_remove(&vimvarht, hi);
1559 }
1560}
1561
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001562#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001563/*
1564 * Evaluate an expression to a list with suggestions.
1565 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001566 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001567 */
1568 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001569eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001570{
1571 typval_T save_val;
1572 typval_T rettv;
1573 list_T *list = NULL;
1574 char_u *p = skipwhite(expr);
1575
1576 /* Set "v:val" to the bad word. */
1577 prepare_vimvar(VV_VAL, &save_val);
1578 vimvars[VV_VAL].vv_type = VAR_STRING;
1579 vimvars[VV_VAL].vv_str = badword;
1580 if (p_verbose == 0)
1581 ++emsg_off;
1582
1583 if (eval1(&p, &rettv, TRUE) == OK)
1584 {
1585 if (rettv.v_type != VAR_LIST)
1586 clear_tv(&rettv);
1587 else
1588 list = rettv.vval.v_list;
1589 }
1590
1591 if (p_verbose == 0)
1592 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001593 restore_vimvar(VV_VAL, &save_val);
1594
1595 return list;
1596}
1597
1598/*
1599 * "list" is supposed to contain two items: a word and a number. Return the
1600 * word in "pp" and the number as the return value.
1601 * Return -1 if anything isn't right.
1602 * Used to get the good word and score from the eval_spell_expr() result.
1603 */
1604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001605get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001606{
1607 listitem_T *li;
1608
1609 li = list->lv_first;
1610 if (li == NULL)
1611 return -1;
1612 *pp = get_tv_string(&li->li_tv);
1613
1614 li = li->li_next;
1615 if (li == NULL)
1616 return -1;
1617 return get_tv_number(&li->li_tv);
1618}
1619#endif
1620
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001621/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001622 * Top level evaluation function.
1623 * Returns an allocated typval_T with the result.
1624 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001625 */
1626 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001628{
1629 typval_T *tv;
1630
1631 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001632 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001633 {
1634 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001635 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001636 }
1637
1638 return tv;
1639}
1640
1641
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001644 * Uses argv[argc] for the function arguments. Only Number and String
1645 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001646 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001649call_vim_function(
1650 char_u *func,
1651 int argc,
1652 char_u **argv,
1653 int safe, /* use the sandbox */
1654 int str_arg_only, /* all arguments are strings */
1655 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
Bram Moolenaar33570922005-01-25 22:26:29 +00001657 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 long n;
1659 int len;
1660 int i;
1661 int doesrange;
1662 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001663 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001665 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
1669 for (i = 0; i < argc; i++)
1670 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001671 /* Pass a NULL or empty argument as an empty string */
1672 if (argv[i] == NULL || *argv[i] == NUL)
1673 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001674 argvars[i].v_type = VAR_STRING;
1675 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001676 continue;
1677 }
1678
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001679 if (str_arg_only)
1680 len = 0;
1681 else
1682 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001683 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (len != 0 && len == (int)STRLEN(argv[i]))
1685 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001686 argvars[i].v_type = VAR_NUMBER;
1687 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 }
1689 else
1690 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001691 argvars[i].v_type = VAR_STRING;
1692 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 }
1694 }
1695
1696 if (safe)
1697 {
1698 save_funccalp = save_funccal();
1699 ++sandbox;
1700 }
1701
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1703 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001705 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (safe)
1707 {
1708 --sandbox;
1709 restore_funccal(save_funccalp);
1710 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 vim_free(argvars);
1712
1713 if (ret == FAIL)
1714 clear_tv(rettv);
1715
1716 return ret;
1717}
1718
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001719/*
1720 * Call vimL function "func" and return the result as a number.
1721 * Returns -1 when calling the function fails.
1722 * Uses argv[argc] for the function arguments.
1723 */
1724 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001725call_func_retnr(
1726 char_u *func,
1727 int argc,
1728 char_u **argv,
1729 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001730{
1731 typval_T rettv;
1732 long retval;
1733
1734 /* All arguments are passed as strings, no conversion to number. */
1735 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1736 return -1;
1737
1738 retval = get_tv_number_chk(&rettv, NULL);
1739 clear_tv(&rettv);
1740 return retval;
1741}
1742
1743#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1744 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1745
Bram Moolenaar4f688582007-07-24 12:34:30 +00001746# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001748 * Call vimL function "func" and return the result as a string.
1749 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750 * Uses argv[argc] for the function arguments.
1751 */
1752 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001753call_func_retstr(
1754 char_u *func,
1755 int argc,
1756 char_u **argv,
1757 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758{
1759 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001760 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001762 /* All arguments are passed as strings, no conversion to number. */
1763 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001764 return NULL;
1765
1766 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001767 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 return retval;
1769}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001770# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001771
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001772/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001773 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001774 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001775 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 */
1777 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778call_func_retlist(
1779 char_u *func,
1780 int argc,
1781 char_u **argv,
1782 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783{
1784 typval_T rettv;
1785
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001786 /* All arguments are passed as strings, no conversion to number. */
1787 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001788 return NULL;
1789
1790 if (rettv.v_type != VAR_LIST)
1791 {
1792 clear_tv(&rettv);
1793 return NULL;
1794 }
1795
1796 return rettv.vval.v_list;
1797}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#endif
1799
1800/*
1801 * Save the current function call pointer, and set it to NULL.
1802 * Used when executing autocommands and for ":source".
1803 */
1804 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001805save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001807 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 current_funccal = NULL;
1810 return (void *)fc;
1811}
1812
1813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001816 funccall_T *fc = (funccall_T *)vfc;
1817
1818 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819}
1820
Bram Moolenaar05159a02005-02-26 23:04:13 +00001821#if defined(FEAT_PROFILE) || defined(PROTO)
1822/*
1823 * Prepare profiling for entering a child or something else that is not
1824 * counted for the script/function itself.
1825 * Should always be called in pair with prof_child_exit().
1826 */
1827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001828prof_child_enter(
1829 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001830{
1831 funccall_T *fc = current_funccal;
1832
1833 if (fc != NULL && fc->func->uf_profiling)
1834 profile_start(&fc->prof_child);
1835 script_prof_save(tm);
1836}
1837
1838/*
1839 * Take care of time spent in a child.
1840 * Should always be called after prof_child_enter().
1841 */
1842 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001843prof_child_exit(
1844 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001845{
1846 funccall_T *fc = current_funccal;
1847
1848 if (fc != NULL && fc->func->uf_profiling)
1849 {
1850 profile_end(&fc->prof_child);
1851 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1852 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1853 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1854 }
1855 script_prof_restore(tm);
1856}
1857#endif
1858
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860#ifdef FEAT_FOLDING
1861/*
1862 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1863 * it in "*cp". Doesn't give error messages.
1864 */
1865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001866eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867{
Bram Moolenaar33570922005-01-25 22:26:29 +00001868 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 int retval;
1870 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001871 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1872 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
1874 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001875 if (use_sandbox)
1876 ++sandbox;
1877 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 retval = 0;
1881 else
1882 {
1883 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 if (tv.v_type == VAR_NUMBER)
1885 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001886 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 retval = 0;
1888 else
1889 {
1890 /* If the result is a string, check if there is a non-digit before
1891 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (!VIM_ISDIGIT(*s) && *s != '-')
1894 *cp = *s++;
1895 retval = atol((char *)s);
1896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
1899 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001900 if (use_sandbox)
1901 --sandbox;
1902 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
1904 return retval;
1905}
1906#endif
1907
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 * ":let" list all variable values
1910 * ":let var1 var2" list variable values
1911 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 * ":let var += expr" assignment command.
1913 * ":let var -= expr" assignment command.
1914 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 */
1917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001918ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 int var_count = 0;
1925 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001927 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
Bram Moolenaardb552d602006-03-23 22:59:57 +00001930 argend = skip_var_list(arg, &var_count, &semicolon);
1931 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001933 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1934 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001935 expr = skipwhite(argend);
1936 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1937 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001939 /*
1940 * ":let" without "=": list variables
1941 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 if (*arg == '[')
1943 EMSG(_(e_invarg));
1944 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001946 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001950 list_glob_vars(&first);
1951 list_buf_vars(&first);
1952 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001953#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001954 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001955#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001956 list_script_vars(&first);
1957 list_func_vars(&first);
1958 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 eap->nextcmd = check_nextcmd(arg);
1961 }
1962 else
1963 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 op[0] = '=';
1965 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001966 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001968 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1969 op[0] = *expr; /* +=, -= or .= */
1970 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001972 else
1973 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (eap->skip)
1976 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (eap->skip)
1979 {
1980 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 --emsg_skip;
1983 }
1984 else if (i != FAIL)
1985 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990 }
1991}
1992
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993/*
1994 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1995 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 * When "nextchars" is not NULL it points to a string with characters that
1997 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1998 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 * Returns OK or FAIL;
2000 */
2001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002002ex_let_vars(
2003 char_u *arg_start,
2004 typval_T *tv,
2005 int copy, /* copy values from "tv", don't move */
2006 int semicolon, /* from skip_var_list() */
2007 int var_count, /* from skip_var_list() */
2008 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009{
2010 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002011 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002013 listitem_T *item;
2014 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002015
2016 if (*arg != '[')
2017 {
2018 /*
2019 * ":let var = expr" or ":for var in list"
2020 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 return FAIL;
2023 return OK;
2024 }
2025
2026 /*
2027 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2028 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002029 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002030 {
2031 EMSG(_(e_listreq));
2032 return FAIL;
2033 }
2034
2035 i = list_len(l);
2036 if (semicolon == 0 && var_count < i)
2037 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002038 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 return FAIL;
2040 }
2041 if (var_count - semicolon > i)
2042 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002043 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 return FAIL;
2045 }
2046
2047 item = l->lv_first;
2048 while (*arg != ']')
2049 {
2050 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002051 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 item = item->li_next;
2053 if (arg == NULL)
2054 return FAIL;
2055
2056 arg = skipwhite(arg);
2057 if (*arg == ';')
2058 {
2059 /* Put the rest of the list (may be empty) in the var after ';'.
2060 * Create a new list for this. */
2061 l = list_alloc();
2062 if (l == NULL)
2063 return FAIL;
2064 while (item != NULL)
2065 {
2066 list_append_tv(l, &item->li_tv);
2067 item = item->li_next;
2068 }
2069
2070 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002071 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072 ltv.vval.v_list = l;
2073 l->lv_refcount = 1;
2074
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002075 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2076 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077 clear_tv(&ltv);
2078 if (arg == NULL)
2079 return FAIL;
2080 break;
2081 }
2082 else if (*arg != ',' && *arg != ']')
2083 {
2084 EMSG2(_(e_intern2), "ex_let_vars()");
2085 return FAIL;
2086 }
2087 }
2088
2089 return OK;
2090}
2091
2092/*
2093 * Skip over assignable variable "var" or list of variables "[var, var]".
2094 * Used for ":let varvar = expr" and ":for varvar in expr".
2095 * For "[var, var]" increment "*var_count" for each variable.
2096 * for "[var, var; var]" set "semicolon".
2097 * Return NULL for an error.
2098 */
2099 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002100skip_var_list(
2101 char_u *arg,
2102 int *var_count,
2103 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002104{
2105 char_u *p, *s;
2106
2107 if (*arg == '[')
2108 {
2109 /* "[var, var]": find the matching ']'. */
2110 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002111 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002112 {
2113 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2114 s = skip_var_one(p);
2115 if (s == p)
2116 {
2117 EMSG2(_(e_invarg2), p);
2118 return NULL;
2119 }
2120 ++*var_count;
2121
2122 p = skipwhite(s);
2123 if (*p == ']')
2124 break;
2125 else if (*p == ';')
2126 {
2127 if (*semicolon == 1)
2128 {
2129 EMSG(_("Double ; in list of variables"));
2130 return NULL;
2131 }
2132 *semicolon = 1;
2133 }
2134 else if (*p != ',')
2135 {
2136 EMSG2(_(e_invarg2), p);
2137 return NULL;
2138 }
2139 }
2140 return p + 1;
2141 }
2142 else
2143 return skip_var_one(arg);
2144}
2145
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002147 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002148 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002149 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002150 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002151skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002152{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002153 if (*arg == '@' && arg[1] != NUL)
2154 return arg + 2;
2155 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2156 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002157}
2158
Bram Moolenaara7043832005-01-21 11:56:39 +00002159/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 * List variables for hashtab "ht" with prefix "prefix".
2161 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164list_hashtable_vars(
2165 hashtab_T *ht,
2166 char_u *prefix,
2167 int empty,
2168 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002169{
Bram Moolenaar33570922005-01-25 22:26:29 +00002170 hashitem_T *hi;
2171 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002172 int todo;
2173
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002174 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002175 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2176 {
2177 if (!HASHITEM_EMPTY(hi))
2178 {
2179 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002180 di = HI2DI(hi);
2181 if (empty || di->di_tv.v_type != VAR_STRING
2182 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 }
2185 }
2186}
2187
2188/*
2189 * List global variables.
2190 */
2191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002192list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002193{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002195}
2196
2197/*
2198 * List buffer variables.
2199 */
2200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002202{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 char_u numbuf[NUMBUFLEN];
2204
Bram Moolenaar429fa852013-04-15 12:27:36 +02002205 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207
2208 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2210 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002211}
2212
2213/*
2214 * List window variables.
2215 */
2216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002218{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002219 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002221}
2222
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002223#ifdef FEAT_WINDOWS
2224/*
2225 * List tab page variables.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002229{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002230 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002232}
2233#endif
2234
Bram Moolenaara7043832005-01-21 11:56:39 +00002235/*
2236 * List Vim variables.
2237 */
2238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242}
2243
2244/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002245 * List script-local variables, if there is a script.
2246 */
2247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002249{
2250 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2252 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002253}
2254
2255/*
2256 * List function variables, if there is a function.
2257 */
2258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002259list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002260{
2261 if (current_funccal != NULL)
2262 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002263 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002264}
2265
2266/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 * List variables in "arg".
2268 */
2269 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002270list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271{
2272 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 char_u *name_start;
2276 char_u *arg_subsc;
2277 char_u *tofree;
2278 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279
2280 while (!ends_excmd(*arg) && !got_int)
2281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002284 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2286 {
2287 emsg_severe = TRUE;
2288 EMSG(_(e_trailing));
2289 break;
2290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 /* get_name_len() takes care of expanding curly braces */
2295 name_start = name = arg;
2296 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2297 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 /* This is mainly to keep test 49 working: when expanding
2300 * curly braces fails overrule the exception error message. */
2301 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 emsg_severe = TRUE;
2304 EMSG2(_(e_invarg2), arg);
2305 break;
2306 }
2307 error = TRUE;
2308 }
2309 else
2310 {
2311 if (tofree != NULL)
2312 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002313 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 else
2316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 /* handle d.key, l[idx], f(expr) */
2318 arg_subsc = arg;
2319 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002322 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002326 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002327 case 'g': list_glob_vars(first); break;
2328 case 'b': list_buf_vars(first); break;
2329 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002330#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002331 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002332#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002333 case 'v': list_vim_vars(first); break;
2334 case 's': list_script_vars(first); break;
2335 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336 default:
2337 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002338 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002339 }
2340 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002341 {
2342 char_u numbuf[NUMBUFLEN];
2343 char_u *tf;
2344 int c;
2345 char_u *s;
2346
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002347 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002348 c = *arg;
2349 *arg = NUL;
2350 list_one_var_a((char_u *)"",
2351 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002352 tv.v_type,
2353 s == NULL ? (char_u *)"" : s,
2354 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002355 *arg = c;
2356 vim_free(tf);
2357 }
2358 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002359 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 }
2361 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002362
2363 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002365
2366 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 }
2368
2369 return arg;
2370}
2371
2372/*
2373 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2374 * Returns a pointer to the char just after the var name.
2375 * Returns NULL if there is an error.
2376 */
2377 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002378ex_let_one(
2379 char_u *arg, /* points to variable name */
2380 typval_T *tv, /* value to assign to variable */
2381 int copy, /* copy value from "tv" */
2382 char_u *endchars, /* valid chars after variable name or NULL */
2383 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384{
2385 int c1;
2386 char_u *name;
2387 char_u *p;
2388 char_u *arg_end = NULL;
2389 int len;
2390 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392
2393 /*
2394 * ":let $VAR = expr": Set environment variable.
2395 */
2396 if (*arg == '$')
2397 {
2398 /* Find the end of the name. */
2399 ++arg;
2400 name = arg;
2401 len = get_env_len(&arg);
2402 if (len == 0)
2403 EMSG2(_(e_invarg2), name - 1);
2404 else
2405 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406 if (op != NULL && (*op == '+' || *op == '-'))
2407 EMSG2(_(e_letwrong), op);
2408 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002409 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002411 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 {
2413 c1 = name[len];
2414 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002415 p = get_tv_string_chk(tv);
2416 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 {
2418 int mustfree = FALSE;
2419 char_u *s = vim_getenv(name, &mustfree);
2420
2421 if (s != NULL)
2422 {
2423 p = tofree = concat_str(s, p);
2424 if (mustfree)
2425 vim_free(s);
2426 }
2427 }
2428 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002431 if (STRICMP(name, "HOME") == 0)
2432 init_homedir();
2433 else if (didset_vim && STRICMP(name, "VIM") == 0)
2434 didset_vim = FALSE;
2435 else if (didset_vimruntime
2436 && STRICMP(name, "VIMRUNTIME") == 0)
2437 didset_vimruntime = FALSE;
2438 arg_end = arg;
2439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 }
2443 }
2444 }
2445
2446 /*
2447 * ":let &option = expr": Set option value.
2448 * ":let &l:option = expr": Set local option value.
2449 * ":let &g:option = expr": Set global option value.
2450 */
2451 else if (*arg == '&')
2452 {
2453 /* Find the end of the name. */
2454 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002455 if (p == NULL || (endchars != NULL
2456 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 EMSG(_(e_letunexp));
2458 else
2459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 long n;
2461 int opt_type;
2462 long numval;
2463 char_u *stringval = NULL;
2464 char_u *s;
2465
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 c1 = *p;
2467 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468
2469 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002470 s = get_tv_string_chk(tv); /* != NULL if number or string */
2471 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 {
2473 opt_type = get_option_value(arg, &numval,
2474 &stringval, opt_flags);
2475 if ((opt_type == 1 && *op == '.')
2476 || (opt_type == 0 && *op != '.'))
2477 EMSG2(_(e_letwrong), op);
2478 else
2479 {
2480 if (opt_type == 1) /* number */
2481 {
2482 if (*op == '+')
2483 n = numval + n;
2484 else
2485 n = numval - n;
2486 }
2487 else if (opt_type == 0 && stringval != NULL) /* string */
2488 {
2489 s = concat_str(stringval, s);
2490 vim_free(stringval);
2491 stringval = s;
2492 }
2493 }
2494 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 if (s != NULL)
2496 {
2497 set_option_value(arg, n, s, opt_flags);
2498 arg_end = p;
2499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 }
2503 }
2504
2505 /*
2506 * ":let @r = expr": Set register contents.
2507 */
2508 else if (*arg == '@')
2509 {
2510 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002511 if (op != NULL && (*op == '+' || *op == '-'))
2512 EMSG2(_(e_letwrong), op);
2513 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002514 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 EMSG(_(e_letunexp));
2516 else
2517 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002518 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002519 char_u *s;
2520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 p = get_tv_string_chk(tv);
2522 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002524 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002525 if (s != NULL)
2526 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002527 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002528 vim_free(s);
2529 }
2530 }
2531 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002532 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002534 arg_end = arg + 1;
2535 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002536 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 }
2538 }
2539
2540 /*
2541 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002544 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002546 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2552 EMSG(_(e_letunexp));
2553 else
2554 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002555 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 arg_end = p;
2557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 }
2561
2562 else
2563 EMSG2(_(e_invarg2), arg);
2564
2565 return arg_end;
2566}
2567
2568/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2570 */
2571 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002572check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573{
2574 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2575 {
2576 EMSG2(_(e_readonlyvar), arg);
2577 return TRUE;
2578 }
2579 return FALSE;
2580}
2581
2582/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 * Get an lval: variable, Dict item or List item that can be assigned a value
2584 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2585 * "name.key", "name.key[expr]" etc.
2586 * Indexing only works if "name" is an existing List or Dictionary.
2587 * "name" points to the start of the name.
2588 * If "rettv" is not NULL it points to the value to be assigned.
2589 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2590 * wrong; must end in space or cmd separator.
2591 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002592 * flags:
2593 * GLV_QUIET: do not give error messages
2594 * GLV_NO_AUTOLOAD: do not use script autoloading
2595 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002597 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 */
2600 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002601get_lval(
2602 char_u *name,
2603 typval_T *rettv,
2604 lval_T *lp,
2605 int unlet,
2606 int skip,
2607 int flags, /* GLV_ values */
2608 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 char_u *expr_start, *expr_end;
2612 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002613 dictitem_T *v;
2614 typval_T var1;
2615 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002617 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002621 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002624 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625
2626 if (skip)
2627 {
2628 /* When skipping just find the end of the name. */
2629 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002630 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 }
2632
2633 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002634 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (expr_start != NULL)
2636 {
2637 /* Don't expand the name when we already know there is an error. */
2638 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2639 && *p != '[' && *p != '.')
2640 {
2641 EMSG(_(e_trailing));
2642 return NULL;
2643 }
2644
2645 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2646 if (lp->ll_exp_name == NULL)
2647 {
2648 /* Report an invalid expression in braces, unless the
2649 * expression evaluation has been cancelled due to an
2650 * aborting error, an interrupt, or an exception. */
2651 if (!aborting() && !quiet)
2652 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002653 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 EMSG2(_(e_invarg2), name);
2655 return NULL;
2656 }
2657 }
2658 lp->ll_name = lp->ll_exp_name;
2659 }
2660 else
2661 lp->ll_name = name;
2662
2663 /* Without [idx] or .key we are done. */
2664 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2665 return p;
2666
2667 cc = *p;
2668 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002669 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (v == NULL && !quiet)
2671 EMSG2(_(e_undefvar), lp->ll_name);
2672 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002673 if (v == NULL)
2674 return NULL;
2675
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 /*
2677 * Loop until no more [idx] or .key is following.
2678 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002679 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2683 && !(lp->ll_tv->v_type == VAR_DICT
2684 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!quiet)
2687 EMSG(_("E689: Can only index a List or Dictionary"));
2688 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002689 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_("E708: [:] must come last"));
2694 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 len = -1;
2698 if (*p == '.')
2699 {
2700 key = p + 1;
2701 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2702 ;
2703 if (len == 0)
2704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (!quiet)
2706 EMSG(_(e_emptykey));
2707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
2709 p = key + len;
2710 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 else
2712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (*p == ':')
2716 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 else
2718 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 empty1 = FALSE;
2720 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002722 if (get_tv_string_chk(&var1) == NULL)
2723 {
2724 /* not a number or string */
2725 clear_tv(&var1);
2726 return NULL;
2727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 }
2729
2730 /* Optionally get the second index [ :expr]. */
2731 if (*p == ':')
2732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002736 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 if (!empty1)
2738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002740 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 if (rettv != NULL && (rettv->v_type != VAR_LIST
2742 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (!quiet)
2745 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 if (!empty1)
2747 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 }
2750 p = skipwhite(p + 1);
2751 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 else
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 if (!empty1)
2759 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002762 if (get_tv_string_chk(&var2) == NULL)
2763 {
2764 /* not a number or string */
2765 if (!empty1)
2766 clear_tv(&var1);
2767 clear_tv(&var2);
2768 return NULL;
2769 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002772 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002775
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002777 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (!quiet)
2779 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (!empty1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 }
2786
2787 /* Skip to past ']'. */
2788 ++p;
2789 }
2790
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
2793 if (len == -1)
2794 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002796 key = get_tv_string_chk(&var1); /* is number or string */
2797 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 lp->ll_list = NULL;
2804 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002805 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002806
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002807 /* When assigning to a scope dictionary check that a function and
2808 * variable name is valid (only variable name unless it is l: or
2809 * g: dictionary). Disallow overwriting a builtin function. */
2810 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002812 int prevval;
2813 int wrong;
2814
2815 if (len != -1)
2816 {
2817 prevval = key[len];
2818 key[len] = NUL;
2819 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002820 else
2821 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002822 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2823 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002824 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002825 || !valid_varname(key);
2826 if (len != -1)
2827 key[len] = prevval;
2828 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002829 return NULL;
2830 }
2831
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002834 /* Can't add "v:" variable. */
2835 if (lp->ll_dict == &vimvardict)
2836 {
2837 EMSG2(_(e_illvar), name);
2838 return NULL;
2839 }
2840
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002841 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002845 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 if (len == -1)
2847 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 }
2850 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 if (len == -1)
2855 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 p = NULL;
2858 break;
2859 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002860 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002861 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002862 return NULL;
2863
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 if (len == -1)
2865 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 }
2868 else
2869 {
2870 /*
2871 * Get the number and item for the only or first index of the List.
2872 */
2873 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 else
2876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002877 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 clear_tv(&var1);
2879 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 lp->ll_list = lp->ll_tv->vval.v_list;
2882 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2883 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002885 if (lp->ll_n1 < 0)
2886 {
2887 lp->ll_n1 = 0;
2888 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2889 }
2890 }
2891 if (lp->ll_li == NULL)
2892 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 if (!quiet)
2896 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002898 }
2899
2900 /*
2901 * May need to find the item or absolute index for the second
2902 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 * When no index given: "lp->ll_empty2" is TRUE.
2904 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002908 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002913 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002914 {
2915 if (!quiet)
2916 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002918 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 }
2921
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2923 if (lp->ll_n1 < 0)
2924 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2925 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002926 {
2927 if (!quiet)
2928 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002930 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002931 }
2932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002934 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 }
2936
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 return p;
2938}
2939
2940/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 */
2943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002944clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945{
2946 vim_free(lp->ll_exp_name);
2947 vim_free(lp->ll_newkey);
2948}
2949
2950/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002951 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 */
2955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002956set_var_lval(
2957 lval_T *lp,
2958 char_u *endp,
2959 typval_T *rettv,
2960 int copy,
2961 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962{
2963 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 listitem_T *ri;
2965 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966
2967 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002970 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 cc = *endp;
2972 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002975 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002977 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002978 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002979 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002980 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002982 if ((di == NULL
2983 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2984 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2985 FALSE)))
2986 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 set_var(lp->ll_name, &tv, FALSE);
2988 clear_tv(&tv);
2989 }
2990 }
2991 else
2992 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002996 else if (tv_check_lock(lp->ll_newkey == NULL
2997 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002998 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002999 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 else if (lp->ll_range)
3001 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003002 listitem_T *ll_li = lp->ll_li;
3003 int ll_n1 = lp->ll_n1;
3004
3005 /*
3006 * Check whether any of the list items is locked
3007 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003008 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003009 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003010 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003011 return;
3012 ri = ri->li_next;
3013 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3014 break;
3015 ll_li = ll_li->li_next;
3016 ++ll_n1;
3017 }
3018
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 /*
3020 * Assign the List values to the list items.
3021 */
3022 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003023 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 if (op != NULL && *op != '=')
3025 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3026 else
3027 {
3028 clear_tv(&lp->ll_li->li_tv);
3029 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3030 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 ri = ri->li_next;
3032 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3033 break;
3034 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003035 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003036 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003037 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003038 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 ri = NULL;
3040 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003041 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003042 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 lp->ll_li = lp->ll_li->li_next;
3044 ++lp->ll_n1;
3045 }
3046 if (ri != NULL)
3047 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 else if (lp->ll_empty2
3049 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 : lp->ll_n1 != lp->ll_n2)
3051 EMSG(_("E711: List value has not enough items"));
3052 }
3053 else
3054 {
3055 /*
3056 * Assign to a List or Dictionary item.
3057 */
3058 if (lp->ll_newkey != NULL)
3059 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003060 if (op != NULL && *op != '=')
3061 {
3062 EMSG2(_(e_letwrong), op);
3063 return;
3064 }
3065
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003066 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003067 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003068 if (di == NULL)
3069 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003070 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3071 {
3072 vim_free(di);
3073 return;
3074 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003075 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003076 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003077 else if (op != NULL && *op != '=')
3078 {
3079 tv_op(lp->ll_tv, rettv, op);
3080 return;
3081 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003082 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003083 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003084
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003085 /*
3086 * Assign the value to the variable or list item.
3087 */
3088 if (copy)
3089 copy_tv(rettv, lp->ll_tv);
3090 else
3091 {
3092 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003093 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003095 }
3096 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003097}
3098
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003099/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003100 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3101 * Returns OK or FAIL.
3102 */
3103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003104tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105{
3106 long n;
3107 char_u numbuf[NUMBUFLEN];
3108 char_u *s;
3109
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003110 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3111 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3112 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 {
3114 switch (tv1->v_type)
3115 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003116 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003117 case VAR_DICT:
3118 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003119 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003120 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003121 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003122 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003123 break;
3124
3125 case VAR_LIST:
3126 if (*op != '+' || tv2->v_type != VAR_LIST)
3127 break;
3128 /* List += List */
3129 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3130 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3131 return OK;
3132
3133 case VAR_NUMBER:
3134 case VAR_STRING:
3135 if (tv2->v_type == VAR_LIST)
3136 break;
3137 if (*op == '+' || *op == '-')
3138 {
3139 /* nr += nr or nr -= nr*/
3140 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141#ifdef FEAT_FLOAT
3142 if (tv2->v_type == VAR_FLOAT)
3143 {
3144 float_T f = n;
3145
3146 if (*op == '+')
3147 f += tv2->vval.v_float;
3148 else
3149 f -= tv2->vval.v_float;
3150 clear_tv(tv1);
3151 tv1->v_type = VAR_FLOAT;
3152 tv1->vval.v_float = f;
3153 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155#endif
3156 {
3157 if (*op == '+')
3158 n += get_tv_number(tv2);
3159 else
3160 n -= get_tv_number(tv2);
3161 clear_tv(tv1);
3162 tv1->v_type = VAR_NUMBER;
3163 tv1->vval.v_number = n;
3164 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003165 }
3166 else
3167 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003168 if (tv2->v_type == VAR_FLOAT)
3169 break;
3170
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003171 /* str .= str */
3172 s = get_tv_string(tv1);
3173 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3174 clear_tv(tv1);
3175 tv1->v_type = VAR_STRING;
3176 tv1->vval.v_string = s;
3177 }
3178 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003181#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182 {
3183 float_T f;
3184
3185 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3186 && tv2->v_type != VAR_NUMBER
3187 && tv2->v_type != VAR_STRING))
3188 break;
3189 if (tv2->v_type == VAR_FLOAT)
3190 f = tv2->vval.v_float;
3191 else
3192 f = get_tv_number(tv2);
3193 if (*op == '+')
3194 tv1->vval.v_float += f;
3195 else
3196 tv1->vval.v_float -= f;
3197 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003198#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003199 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003200 }
3201 }
3202
3203 EMSG2(_(e_letwrong), op);
3204 return FAIL;
3205}
3206
3207/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 * Add a watcher to a list.
3209 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
3213 lw->lw_next = l->lv_watch;
3214 l->lv_watch = lw;
3215}
3216
3217/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003218 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 * No warning when it isn't found...
3220 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223{
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225
3226 lwp = &l->lv_watch;
3227 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3228 {
3229 if (lw == lwrem)
3230 {
3231 *lwp = lw->lw_next;
3232 break;
3233 }
3234 lwp = &lw->lw_next;
3235 }
3236}
3237
3238/*
3239 * Just before removing an item from a list: advance watchers to the next
3240 * item.
3241 */
3242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003243list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244{
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246
3247 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3248 if (lw->lw_item == item)
3249 lw->lw_item = item->li_next;
3250}
3251
3252/*
3253 * Evaluate the expression used in a ":for var in expr" command.
3254 * "arg" points to "var".
3255 * Set "*errp" to TRUE for an error, FALSE otherwise;
3256 * Return a pointer that holds the info. Null when there is an error.
3257 */
3258 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003259eval_for_line(
3260 char_u *arg,
3261 int *errp,
3262 char_u **nextcmdp,
3263 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264{
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003267 typval_T tv;
3268 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269
3270 *errp = TRUE; /* default: there is an error */
3271
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 if (fi == NULL)
3274 return NULL;
3275
3276 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3277 if (expr == NULL)
3278 return fi;
3279
3280 expr = skipwhite(expr);
3281 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3282 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003283 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 return fi;
3285 }
3286
3287 if (skip)
3288 ++emsg_skip;
3289 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3290 {
3291 *errp = FALSE;
3292 if (!skip)
3293 {
3294 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003295 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003296 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003298 clear_tv(&tv);
3299 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003300 else if (l == NULL)
3301 {
3302 /* a null list is like an empty list: do nothing */
3303 clear_tv(&tv);
3304 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305 else
3306 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003307 /* No need to increment the refcount, it's already set for the
3308 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309 fi->fi_list = l;
3310 list_add_watch(l, &fi->fi_lw);
3311 fi->fi_lw.lw_item = l->lv_first;
3312 }
3313 }
3314 }
3315 if (skip)
3316 --emsg_skip;
3317
3318 return fi;
3319}
3320
3321/*
3322 * Use the first item in a ":for" list. Advance to the next.
3323 * Assign the values to the variable (list). "arg" points to the first one.
3324 * Return TRUE when a valid item was found, FALSE when at end of list or
3325 * something wrong.
3326 */
3327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003328next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003330 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003332 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333
3334 item = fi->fi_lw.lw_item;
3335 if (item == NULL)
3336 result = FALSE;
3337 else
3338 {
3339 fi->fi_lw.lw_item = item->li_next;
3340 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3341 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3342 }
3343 return result;
3344}
3345
3346/*
3347 * Free the structure used to store info used by ":for".
3348 */
3349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003350free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351{
Bram Moolenaar33570922005-01-25 22:26:29 +00003352 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003353
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003354 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003355 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003357 list_unref(fi->fi_list);
3358 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 vim_free(fi);
3360}
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3363
3364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003365set_context_for_expression(
3366 expand_T *xp,
3367 char_u *arg,
3368 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 int got_eq = FALSE;
3371 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003372 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003374 if (cmdidx == CMD_let)
3375 {
3376 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003377 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003378 {
3379 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003380 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381 {
3382 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003383 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 if (vim_iswhite(*p))
3385 break;
3386 }
3387 return;
3388 }
3389 }
3390 else
3391 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3392 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 while ((xp->xp_pattern = vim_strpbrk(arg,
3394 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3395 {
3396 c = *xp->xp_pattern;
3397 if (c == '&')
3398 {
3399 c = xp->xp_pattern[1];
3400 if (c == '&')
3401 {
3402 ++xp->xp_pattern;
3403 xp->xp_context = cmdidx != CMD_let || got_eq
3404 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3405 }
3406 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003407 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003409 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3410 xp->xp_pattern += 2;
3411
3412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
3414 else if (c == '$')
3415 {
3416 /* environment variable */
3417 xp->xp_context = EXPAND_ENV_VARS;
3418 }
3419 else if (c == '=')
3420 {
3421 got_eq = TRUE;
3422 xp->xp_context = EXPAND_EXPRESSION;
3423 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003424 else if (c == '#'
3425 && xp->xp_context == EXPAND_EXPRESSION)
3426 {
3427 /* Autoload function/variable contains '#'. */
3428 break;
3429 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003430 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 && xp->xp_context == EXPAND_FUNCTIONS
3432 && vim_strchr(xp->xp_pattern, '(') == NULL)
3433 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003434 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 break;
3436 }
3437 else if (cmdidx != CMD_let || got_eq)
3438 {
3439 if (c == '"') /* string */
3440 {
3441 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3442 if (c == '\\' && xp->xp_pattern[1] != NUL)
3443 ++xp->xp_pattern;
3444 xp->xp_context = EXPAND_NOTHING;
3445 }
3446 else if (c == '\'') /* literal string */
3447 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003448 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3450 /* skip */ ;
3451 xp->xp_context = EXPAND_NOTHING;
3452 }
3453 else if (c == '|')
3454 {
3455 if (xp->xp_pattern[1] == '|')
3456 {
3457 ++xp->xp_pattern;
3458 xp->xp_context = EXPAND_EXPRESSION;
3459 }
3460 else
3461 xp->xp_context = EXPAND_COMMANDS;
3462 }
3463 else
3464 xp->xp_context = EXPAND_EXPRESSION;
3465 }
3466 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003467 /* Doesn't look like something valid, expand as an expression
3468 * anyway. */
3469 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 arg = xp->xp_pattern;
3471 if (*arg != NUL)
3472 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3473 /* skip */ ;
3474 }
3475 xp->xp_pattern = arg;
3476}
3477
3478#endif /* FEAT_CMDL_COMPL */
3479
3480/*
3481 * ":1,25call func(arg1, arg2)" function call.
3482 */
3483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003484ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485{
3486 char_u *arg = eap->arg;
3487 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003489 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003491 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 linenr_T lnum;
3493 int doesrange;
3494 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003495 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003496 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003498 if (eap->skip)
3499 {
3500 /* trans_function_name() doesn't work well when skipping, use eval0()
3501 * instead to skip to any following command, e.g. for:
3502 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003503 ++emsg_skip;
3504 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3505 clear_tv(&rettv);
3506 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003507 return;
3508 }
3509
Bram Moolenaar65639032016-03-16 21:40:30 +01003510 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003511 if (fudi.fd_newkey != NULL)
3512 {
3513 /* Still need to give an error message for missing key. */
3514 EMSG2(_(e_dictkey), fudi.fd_newkey);
3515 vim_free(fudi.fd_newkey);
3516 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003517 if (tofree == NULL)
3518 return;
3519
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003520 /* Increase refcount on dictionary, it could get deleted when evaluating
3521 * the arguments. */
3522 if (fudi.fd_dict != NULL)
3523 ++fudi.fd_dict->dv_refcount;
3524
Bram Moolenaar65639032016-03-16 21:40:30 +01003525 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3526 * contents. For VAR_PARTIAL get its partial, unless we already have one
3527 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003529 name = deref_func_name(tofree, &len,
3530 partial != NULL ? NULL : &partial, FALSE);
3531
Bram Moolenaar532c7802005-01-27 14:44:31 +00003532 /* Skip white space to allow ":call func ()". Not good, but required for
3533 * backward compatibility. */
3534 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003535 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537 if (*startarg != '(')
3538 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003539 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 goto end;
3541 }
3542
3543 /*
3544 * When skipping, evaluate the function once, to find the end of the
3545 * arguments.
3546 * When the function takes a range, this is discovered after the first
3547 * call, and the loop is broken.
3548 */
3549 if (eap->skip)
3550 {
3551 ++emsg_skip;
3552 lnum = eap->line2; /* do it once, also with an invalid range */
3553 }
3554 else
3555 lnum = eap->line1;
3556 for ( ; lnum <= eap->line2; ++lnum)
3557 {
3558 if (!eap->skip && eap->addr_count > 0)
3559 {
3560 curwin->w_cursor.lnum = lnum;
3561 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003562#ifdef FEAT_VIRTUALEDIT
3563 curwin->w_cursor.coladd = 0;
3564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003567 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003568 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003569 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
3571 failed = TRUE;
3572 break;
3573 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003574
3575 /* Handle a function returning a Funcref, Dictionary or List. */
3576 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3577 {
3578 failed = TRUE;
3579 break;
3580 }
3581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003582 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (doesrange || eap->skip)
3584 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003587 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003588 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003589 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 if (aborting())
3591 break;
3592 }
3593 if (eap->skip)
3594 --emsg_skip;
3595
3596 if (!failed)
3597 {
3598 /* Check for trailing illegal characters and a following command. */
3599 if (!ends_excmd(*arg))
3600 {
3601 emsg_severe = TRUE;
3602 EMSG(_(e_trailing));
3603 }
3604 else
3605 eap->nextcmd = check_nextcmd(arg);
3606 }
3607
3608end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003609 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003610 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611}
3612
3613/*
3614 * ":unlet[!] var1 ... " command.
3615 */
3616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003617ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003619 ex_unletlock(eap, eap->arg, 0);
3620}
3621
3622/*
3623 * ":lockvar" and ":unlockvar" commands
3624 */
3625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003627{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003629 int deep = 2;
3630
3631 if (eap->forceit)
3632 deep = -1;
3633 else if (vim_isdigit(*arg))
3634 {
3635 deep = getdigits(&arg);
3636 arg = skipwhite(arg);
3637 }
3638
3639 ex_unletlock(eap, arg, deep);
3640}
3641
3642/*
3643 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3644 */
3645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003646ex_unletlock(
3647 exarg_T *eap,
3648 char_u *argstart,
3649 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003650{
3651 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003654 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 do
3657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003659 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003660 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 if (lv.ll_name == NULL)
3662 error = TRUE; /* error but continue parsing */
3663 if (name_end == NULL || (!vim_iswhite(*name_end)
3664 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 if (name_end != NULL)
3667 {
3668 emsg_severe = TRUE;
3669 EMSG(_(e_trailing));
3670 }
3671 if (!(eap->skip || error))
3672 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 break;
3674 }
3675
3676 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 {
3678 if (eap->cmdidx == CMD_unlet)
3679 {
3680 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3681 error = TRUE;
3682 }
3683 else
3684 {
3685 if (do_lock_var(&lv, name_end, deep,
3686 eap->cmdidx == CMD_lockvar) == FAIL)
3687 error = TRUE;
3688 }
3689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003691 if (!eap->skip)
3692 clear_lval(&lv);
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 arg = skipwhite(name_end);
3695 } while (!ends_excmd(*arg));
3696
3697 eap->nextcmd = check_nextcmd(arg);
3698}
3699
Bram Moolenaar8c711452005-01-14 21:53:12 +00003700 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003701do_unlet_var(
3702 lval_T *lp,
3703 char_u *name_end,
3704 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003705{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 int ret = OK;
3707 int cc;
3708
3709 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 cc = *name_end;
3712 *name_end = NUL;
3713
3714 /* Normal name or expanded name. */
3715 if (check_changedtick(lp->ll_name))
3716 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003717 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003720 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003721 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003722 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003723 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003724 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003725 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 else if (lp->ll_range)
3727 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003728 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003729 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003730 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003731
3732 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3733 {
3734 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003735 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003736 return FAIL;
3737 ll_li = li;
3738 ++ll_n1;
3739 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003740
3741 /* Delete a range of List items. */
3742 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3743 {
3744 li = lp->ll_li->li_next;
3745 listitem_remove(lp->ll_list, lp->ll_li);
3746 lp->ll_li = li;
3747 ++lp->ll_n1;
3748 }
3749 }
3750 else
3751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003752 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003753 /* unlet a List item. */
3754 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003755 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003756 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003757 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003758 }
3759
3760 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003761}
3762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763/*
3764 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003765 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 */
3767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003768do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 hashtab_T *ht;
3771 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003772 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003773 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003774 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
Bram Moolenaar33570922005-01-25 22:26:29 +00003776 ht = find_var_ht(name, &varname);
3777 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003779 if (ht == &globvarht)
3780 d = &globvardict;
3781 else if (current_funccal != NULL
3782 && ht == &current_funccal->l_vars.dv_hashtab)
3783 d = &current_funccal->l_vars;
3784 else if (ht == &compat_hashtab)
3785 d = &vimvardict;
3786 else
3787 {
3788 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3789 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3790 }
3791 if (d == NULL)
3792 {
3793 EMSG2(_(e_intern2), "do_unlet()");
3794 return FAIL;
3795 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003796 hi = hash_find(ht, varname);
3797 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003798 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003799 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003800 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003801 || var_check_ro(di->di_flags, name, FALSE)
3802 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003803 return FAIL;
3804
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003805 delete_var(ht, hi);
3806 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003809 if (forceit)
3810 return OK;
3811 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 return FAIL;
3813}
3814
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815/*
3816 * Lock or unlock variable indicated by "lp".
3817 * "deep" is the levels to go (-1 for unlimited);
3818 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3819 */
3820 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003821do_lock_var(
3822 lval_T *lp,
3823 char_u *name_end,
3824 int deep,
3825 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003826{
3827 int ret = OK;
3828 int cc;
3829 dictitem_T *di;
3830
3831 if (deep == 0) /* nothing to do */
3832 return OK;
3833
3834 if (lp->ll_tv == NULL)
3835 {
3836 cc = *name_end;
3837 *name_end = NUL;
3838
3839 /* Normal name or expanded name. */
3840 if (check_changedtick(lp->ll_name))
3841 ret = FAIL;
3842 else
3843 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003844 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003845 if (di == NULL)
3846 ret = FAIL;
3847 else
3848 {
3849 if (lock)
3850 di->di_flags |= DI_FLAGS_LOCK;
3851 else
3852 di->di_flags &= ~DI_FLAGS_LOCK;
3853 item_lock(&di->di_tv, deep, lock);
3854 }
3855 }
3856 *name_end = cc;
3857 }
3858 else if (lp->ll_range)
3859 {
3860 listitem_T *li = lp->ll_li;
3861
3862 /* (un)lock a range of List items. */
3863 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3864 {
3865 item_lock(&li->li_tv, deep, lock);
3866 li = li->li_next;
3867 ++lp->ll_n1;
3868 }
3869 }
3870 else if (lp->ll_list != NULL)
3871 /* (un)lock a List item. */
3872 item_lock(&lp->ll_li->li_tv, deep, lock);
3873 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003874 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003875 item_lock(&lp->ll_di->di_tv, deep, lock);
3876
3877 return ret;
3878}
3879
3880/*
3881 * Lock or unlock an item. "deep" is nr of levels to go.
3882 */
3883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003884item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003885{
3886 static int recurse = 0;
3887 list_T *l;
3888 listitem_T *li;
3889 dict_T *d;
3890 hashitem_T *hi;
3891 int todo;
3892
3893 if (recurse >= DICT_MAXNEST)
3894 {
3895 EMSG(_("E743: variable nested too deep for (un)lock"));
3896 return;
3897 }
3898 if (deep == 0)
3899 return;
3900 ++recurse;
3901
3902 /* lock/unlock the item itself */
3903 if (lock)
3904 tv->v_lock |= VAR_LOCKED;
3905 else
3906 tv->v_lock &= ~VAR_LOCKED;
3907
3908 switch (tv->v_type)
3909 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003910 case VAR_UNKNOWN:
3911 case VAR_NUMBER:
3912 case VAR_STRING:
3913 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003914 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003915 case VAR_FLOAT:
3916 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003917 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003918 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003919 break;
3920
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003921 case VAR_LIST:
3922 if ((l = tv->vval.v_list) != NULL)
3923 {
3924 if (lock)
3925 l->lv_lock |= VAR_LOCKED;
3926 else
3927 l->lv_lock &= ~VAR_LOCKED;
3928 if (deep < 0 || deep > 1)
3929 /* recursive: lock/unlock the items the List contains */
3930 for (li = l->lv_first; li != NULL; li = li->li_next)
3931 item_lock(&li->li_tv, deep - 1, lock);
3932 }
3933 break;
3934 case VAR_DICT:
3935 if ((d = tv->vval.v_dict) != NULL)
3936 {
3937 if (lock)
3938 d->dv_lock |= VAR_LOCKED;
3939 else
3940 d->dv_lock &= ~VAR_LOCKED;
3941 if (deep < 0 || deep > 1)
3942 {
3943 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003944 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003945 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3946 {
3947 if (!HASHITEM_EMPTY(hi))
3948 {
3949 --todo;
3950 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3951 }
3952 }
3953 }
3954 }
3955 }
3956 --recurse;
3957}
3958
Bram Moolenaara40058a2005-07-11 22:42:07 +00003959/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003960 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3961 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003962 */
3963 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003964tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003965{
3966 return (tv->v_lock & VAR_LOCKED)
3967 || (tv->v_type == VAR_LIST
3968 && tv->vval.v_list != NULL
3969 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3970 || (tv->v_type == VAR_DICT
3971 && tv->vval.v_dict != NULL
3972 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3973}
3974
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3976/*
3977 * Delete all "menutrans_" variables.
3978 */
3979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003980del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
Bram Moolenaar33570922005-01-25 22:26:29 +00003982 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003983 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
Bram Moolenaar33570922005-01-25 22:26:29 +00003985 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003986 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003987 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003988 {
3989 if (!HASHITEM_EMPTY(hi))
3990 {
3991 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003992 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3993 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003994 }
3995 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997}
3998#endif
3999
4000#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4001
4002/*
4003 * Local string buffer for the next two functions to store a variable name
4004 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4005 * get_user_var_name().
4006 */
4007
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004008static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010static char_u *varnamebuf = NULL;
4011static int varnamebuflen = 0;
4012
4013/*
4014 * Function to concatenate a prefix and a variable name.
4015 */
4016 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004017cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018{
4019 int len;
4020
4021 len = (int)STRLEN(name) + 3;
4022 if (len > varnamebuflen)
4023 {
4024 vim_free(varnamebuf);
4025 len += 10; /* some additional space */
4026 varnamebuf = alloc(len);
4027 if (varnamebuf == NULL)
4028 {
4029 varnamebuflen = 0;
4030 return NULL;
4031 }
4032 varnamebuflen = len;
4033 }
4034 *varnamebuf = prefix;
4035 varnamebuf[1] = ':';
4036 STRCPY(varnamebuf + 2, name);
4037 return varnamebuf;
4038}
4039
4040/*
4041 * Function given to ExpandGeneric() to obtain the list of user defined
4042 * (global/buffer/window/built-in) variable names.
4043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004045get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004047 static long_u gdone;
4048 static long_u bdone;
4049 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004050#ifdef FEAT_WINDOWS
4051 static long_u tdone;
4052#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004053 static int vidx;
4054 static hashitem_T *hi;
4055 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
4057 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004058 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004059 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060#ifdef FEAT_WINDOWS
4061 tdone = 0;
4062#endif
4063 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004064
4065 /* Global variables */
4066 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004068 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004070 else
4071 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004072 while (HASHITEM_EMPTY(hi))
4073 ++hi;
4074 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4075 return cat_prefix_varname('g', hi->hi_key);
4076 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004078
4079 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004080 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004081 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004083 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004084 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004085 else
4086 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004087 while (HASHITEM_EMPTY(hi))
4088 ++hi;
4089 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004091 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004093 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 return (char_u *)"b:changedtick";
4095 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004096
4097 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004098 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004099 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004101 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004102 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004103 else
4104 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004105 while (HASHITEM_EMPTY(hi))
4106 ++hi;
4107 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004109
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004110#ifdef FEAT_WINDOWS
4111 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004112 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004113 if (tdone < ht->ht_used)
4114 {
4115 if (tdone++ == 0)
4116 hi = ht->ht_array;
4117 else
4118 ++hi;
4119 while (HASHITEM_EMPTY(hi))
4120 ++hi;
4121 return cat_prefix_varname('t', hi->hi_key);
4122 }
4123#endif
4124
Bram Moolenaar33570922005-01-25 22:26:29 +00004125 /* v: variables */
4126 if (vidx < VV_LEN)
4127 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
4129 vim_free(varnamebuf);
4130 varnamebuf = NULL;
4131 varnamebuflen = 0;
4132 return NULL;
4133}
4134
4135#endif /* FEAT_CMDL_COMPL */
4136
4137/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004138 * Return TRUE if "pat" matches "text".
4139 * Does not use 'cpo' and always uses 'magic'.
4140 */
4141 static int
4142pattern_match(char_u *pat, char_u *text, int ic)
4143{
4144 int matches = FALSE;
4145 char_u *save_cpo;
4146 regmatch_T regmatch;
4147
4148 /* avoid 'l' flag in 'cpoptions' */
4149 save_cpo = p_cpo;
4150 p_cpo = (char_u *)"";
4151 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4152 if (regmatch.regprog != NULL)
4153 {
4154 regmatch.rm_ic = ic;
4155 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4156 vim_regfree(regmatch.regprog);
4157 }
4158 p_cpo = save_cpo;
4159 return matches;
4160}
4161
4162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 * types for expressions.
4164 */
4165typedef enum
4166{
4167 TYPE_UNKNOWN = 0
4168 , TYPE_EQUAL /* == */
4169 , TYPE_NEQUAL /* != */
4170 , TYPE_GREATER /* > */
4171 , TYPE_GEQUAL /* >= */
4172 , TYPE_SMALLER /* < */
4173 , TYPE_SEQUAL /* <= */
4174 , TYPE_MATCH /* =~ */
4175 , TYPE_NOMATCH /* !~ */
4176} exptype_T;
4177
4178/*
4179 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4182 */
4183
4184/*
4185 * Handle zero level expression.
4186 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004188 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 * Return OK or FAIL.
4190 */
4191 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004192eval0(
4193 char_u *arg,
4194 typval_T *rettv,
4195 char_u **nextcmd,
4196 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
4198 int ret;
4199 char_u *p;
4200
4201 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if (ret == FAIL || !ends_excmd(*p))
4204 {
4205 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 /*
4208 * Report the invalid expression unless the expression evaluation has
4209 * been cancelled due to an aborting error, an interrupt, or an
4210 * exception.
4211 */
4212 if (!aborting())
4213 EMSG2(_(e_invexpr2), arg);
4214 ret = FAIL;
4215 }
4216 if (nextcmd != NULL)
4217 *nextcmd = check_nextcmd(p);
4218
4219 return ret;
4220}
4221
4222/*
4223 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004224 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 *
4226 * "arg" must point to the first non-white of the expression.
4227 * "arg" is advanced to the next non-white after the recognized expression.
4228 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004229 * Note: "rettv.v_lock" is not set.
4230 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 * Return OK or FAIL.
4232 */
4233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004234eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
4236 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004237 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
4239 /*
4240 * Get the first variable.
4241 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 return FAIL;
4244
4245 if ((*arg)[0] == '?')
4246 {
4247 result = FALSE;
4248 if (evaluate)
4249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 int error = FALSE;
4251
4252 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 if (error)
4256 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258
4259 /*
4260 * Get the second variable.
4261 */
4262 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 return FAIL;
4265
4266 /*
4267 * Check for the ":".
4268 */
4269 if ((*arg)[0] != ':')
4270 {
4271 EMSG(_("E109: Missing ':' after '?'"));
4272 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 return FAIL;
4275 }
4276
4277 /*
4278 * Get the third variable.
4279 */
4280 *arg = skipwhite(*arg + 1);
4281 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4282 {
4283 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return FAIL;
4286 }
4287 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290
4291 return OK;
4292}
4293
4294/*
4295 * Handle first level expression:
4296 * expr2 || expr2 || expr2 logical OR
4297 *
4298 * "arg" must point to the first non-white of the expression.
4299 * "arg" is advanced to the next non-white after the recognized expression.
4300 *
4301 * Return OK or FAIL.
4302 */
4303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004304eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305{
Bram Moolenaar33570922005-01-25 22:26:29 +00004306 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 long result;
4308 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 /*
4312 * Get the first variable.
4313 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 return FAIL;
4316
4317 /*
4318 * Repeat until there is no following "||".
4319 */
4320 first = TRUE;
4321 result = FALSE;
4322 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4323 {
4324 if (evaluate && first)
4325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (error)
4330 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 first = FALSE;
4332 }
4333
4334 /*
4335 * Get the second variable.
4336 */
4337 *arg = skipwhite(*arg + 2);
4338 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4339 return FAIL;
4340
4341 /*
4342 * Compute the result.
4343 */
4344 if (evaluate && !result)
4345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (error)
4350 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 if (evaluate)
4353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 rettv->v_type = VAR_NUMBER;
4355 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 }
4358
4359 return OK;
4360}
4361
4362/*
4363 * Handle second level expression:
4364 * expr3 && expr3 && expr3 logical AND
4365 *
4366 * "arg" must point to the first non-white of the expression.
4367 * "arg" is advanced to the next non-white after the recognized expression.
4368 *
4369 * Return OK or FAIL.
4370 */
4371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004372eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373{
Bram Moolenaar33570922005-01-25 22:26:29 +00004374 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 long result;
4376 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004377 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 /*
4380 * Get the first variable.
4381 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004382 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 return FAIL;
4384
4385 /*
4386 * Repeat until there is no following "&&".
4387 */
4388 first = TRUE;
4389 result = TRUE;
4390 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4391 {
4392 if (evaluate && first)
4393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004394 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004397 if (error)
4398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 first = FALSE;
4400 }
4401
4402 /*
4403 * Get the second variable.
4404 */
4405 *arg = skipwhite(*arg + 2);
4406 if (eval4(arg, &var2, evaluate && result) == FAIL)
4407 return FAIL;
4408
4409 /*
4410 * Compute the result.
4411 */
4412 if (evaluate && result)
4413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004414 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004416 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004417 if (error)
4418 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 if (evaluate)
4421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004422 rettv->v_type = VAR_NUMBER;
4423 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 }
4426
4427 return OK;
4428}
4429
4430/*
4431 * Handle third level expression:
4432 * var1 == var2
4433 * var1 =~ var2
4434 * var1 != var2
4435 * var1 !~ var2
4436 * var1 > var2
4437 * var1 >= var2
4438 * var1 < var2
4439 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 * var1 is var2
4441 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 *
4443 * "arg" must point to the first non-white of the expression.
4444 * "arg" is advanced to the next non-white after the recognized expression.
4445 *
4446 * Return OK or FAIL.
4447 */
4448 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004449eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450{
Bram Moolenaar33570922005-01-25 22:26:29 +00004451 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 char_u *p;
4453 int i;
4454 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004455 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 int len = 2;
4457 long n1, n2;
4458 char_u *s1, *s2;
4459 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462 /*
4463 * Get the first variable.
4464 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 return FAIL;
4467
4468 p = *arg;
4469 switch (p[0])
4470 {
4471 case '=': if (p[1] == '=')
4472 type = TYPE_EQUAL;
4473 else if (p[1] == '~')
4474 type = TYPE_MATCH;
4475 break;
4476 case '!': if (p[1] == '=')
4477 type = TYPE_NEQUAL;
4478 else if (p[1] == '~')
4479 type = TYPE_NOMATCH;
4480 break;
4481 case '>': if (p[1] != '=')
4482 {
4483 type = TYPE_GREATER;
4484 len = 1;
4485 }
4486 else
4487 type = TYPE_GEQUAL;
4488 break;
4489 case '<': if (p[1] != '=')
4490 {
4491 type = TYPE_SMALLER;
4492 len = 1;
4493 }
4494 else
4495 type = TYPE_SEQUAL;
4496 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 case 'i': if (p[1] == 's')
4498 {
4499 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4500 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004501 i = p[len];
4502 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004503 {
4504 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4505 type_is = TRUE;
4506 }
4507 }
4508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510
4511 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004512 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 */
4514 if (type != TYPE_UNKNOWN)
4515 {
4516 /* extra question mark appended: ignore case */
4517 if (p[len] == '?')
4518 {
4519 ic = TRUE;
4520 ++len;
4521 }
4522 /* extra '#' appended: match case */
4523 else if (p[len] == '#')
4524 {
4525 ic = FALSE;
4526 ++len;
4527 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004528 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 else
4530 ic = p_ic;
4531
4532 /*
4533 * Get the second variable.
4534 */
4535 *arg = skipwhite(p + len);
4536 if (eval5(arg, &var2, evaluate) == FAIL)
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 return FAIL;
4540 }
4541
4542 if (evaluate)
4543 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 if (type_is && rettv->v_type != var2.v_type)
4545 {
4546 /* For "is" a different type always means FALSE, for "notis"
4547 * it means TRUE. */
4548 n1 = (type == TYPE_NEQUAL);
4549 }
4550 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4551 {
4552 if (type_is)
4553 {
4554 n1 = (rettv->v_type == var2.v_type
4555 && rettv->vval.v_list == var2.vval.v_list);
4556 if (type == TYPE_NEQUAL)
4557 n1 = !n1;
4558 }
4559 else if (rettv->v_type != var2.v_type
4560 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4561 {
4562 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004563 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004564 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004565 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004566 clear_tv(rettv);
4567 clear_tv(&var2);
4568 return FAIL;
4569 }
4570 else
4571 {
4572 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004573 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4574 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004575 if (type == TYPE_NEQUAL)
4576 n1 = !n1;
4577 }
4578 }
4579
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004580 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4581 {
4582 if (type_is)
4583 {
4584 n1 = (rettv->v_type == var2.v_type
4585 && rettv->vval.v_dict == var2.vval.v_dict);
4586 if (type == TYPE_NEQUAL)
4587 n1 = !n1;
4588 }
4589 else if (rettv->v_type != var2.v_type
4590 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4591 {
4592 if (rettv->v_type != var2.v_type)
4593 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4594 else
4595 EMSG(_("E736: Invalid operation for Dictionary"));
4596 clear_tv(rettv);
4597 clear_tv(&var2);
4598 return FAIL;
4599 }
4600 else
4601 {
4602 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004603 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4604 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004605 if (type == TYPE_NEQUAL)
4606 n1 = !n1;
4607 }
4608 }
4609
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004610 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4611 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004612 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004613 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004614 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004615 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004616 clear_tv(rettv);
4617 clear_tv(&var2);
4618 return FAIL;
4619 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004620 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004621 if (type == TYPE_NEQUAL)
4622 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 }
4624
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004625#ifdef FEAT_FLOAT
4626 /*
4627 * If one of the two variables is a float, compare as a float.
4628 * When using "=~" or "!~", always compare as string.
4629 */
4630 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4631 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4632 {
4633 float_T f1, f2;
4634
4635 if (rettv->v_type == VAR_FLOAT)
4636 f1 = rettv->vval.v_float;
4637 else
4638 f1 = get_tv_number(rettv);
4639 if (var2.v_type == VAR_FLOAT)
4640 f2 = var2.vval.v_float;
4641 else
4642 f2 = get_tv_number(&var2);
4643 n1 = FALSE;
4644 switch (type)
4645 {
4646 case TYPE_EQUAL: n1 = (f1 == f2); break;
4647 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4648 case TYPE_GREATER: n1 = (f1 > f2); break;
4649 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4650 case TYPE_SMALLER: n1 = (f1 < f2); break;
4651 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4652 case TYPE_UNKNOWN:
4653 case TYPE_MATCH:
4654 case TYPE_NOMATCH: break; /* avoid gcc warning */
4655 }
4656 }
4657#endif
4658
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 /*
4660 * If one of the two variables is a number, compare as a number.
4661 * When using "=~" or "!~", always compare as string.
4662 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004663 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004666 n1 = get_tv_number(rettv);
4667 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 switch (type)
4669 {
4670 case TYPE_EQUAL: n1 = (n1 == n2); break;
4671 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4672 case TYPE_GREATER: n1 = (n1 > n2); break;
4673 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4674 case TYPE_SMALLER: n1 = (n1 < n2); break;
4675 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4676 case TYPE_UNKNOWN:
4677 case TYPE_MATCH:
4678 case TYPE_NOMATCH: break; /* avoid gcc warning */
4679 }
4680 }
4681 else
4682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004683 s1 = get_tv_string_buf(rettv, buf1);
4684 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4686 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4687 else
4688 i = 0;
4689 n1 = FALSE;
4690 switch (type)
4691 {
4692 case TYPE_EQUAL: n1 = (i == 0); break;
4693 case TYPE_NEQUAL: n1 = (i != 0); break;
4694 case TYPE_GREATER: n1 = (i > 0); break;
4695 case TYPE_GEQUAL: n1 = (i >= 0); break;
4696 case TYPE_SMALLER: n1 = (i < 0); break;
4697 case TYPE_SEQUAL: n1 = (i <= 0); break;
4698
4699 case TYPE_MATCH:
4700 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004701 n1 = pattern_match(s2, s1, ic);
4702 if (type == TYPE_NOMATCH)
4703 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 break;
4705
4706 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4707 }
4708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004709 clear_tv(rettv);
4710 clear_tv(&var2);
4711 rettv->v_type = VAR_NUMBER;
4712 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
4714 }
4715
4716 return OK;
4717}
4718
4719/*
4720 * Handle fourth level expression:
4721 * + number addition
4722 * - number subtraction
4723 * . string concatenation
4724 *
4725 * "arg" must point to the first non-white of the expression.
4726 * "arg" is advanced to the next non-white after the recognized expression.
4727 *
4728 * Return OK or FAIL.
4729 */
4730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004731eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
Bram Moolenaar33570922005-01-25 22:26:29 +00004733 typval_T var2;
4734 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 int op;
4736 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004737#ifdef FEAT_FLOAT
4738 float_T f1 = 0, f2 = 0;
4739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 char_u *s1, *s2;
4741 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4742 char_u *p;
4743
4744 /*
4745 * Get the first variable.
4746 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004747 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 return FAIL;
4749
4750 /*
4751 * Repeat computing, until no '+', '-' or '.' is following.
4752 */
4753 for (;;)
4754 {
4755 op = **arg;
4756 if (op != '+' && op != '-' && op != '.')
4757 break;
4758
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004759 if ((op != '+' || rettv->v_type != VAR_LIST)
4760#ifdef FEAT_FLOAT
4761 && (op == '.' || rettv->v_type != VAR_FLOAT)
4762#endif
4763 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004764 {
4765 /* For "list + ...", an illegal use of the first operand as
4766 * a number cannot be determined before evaluating the 2nd
4767 * operand: if this is also a list, all is ok.
4768 * For "something . ...", "something - ..." or "non-list + ...",
4769 * we know that the first operand needs to be a string or number
4770 * without evaluating the 2nd operand. So check before to avoid
4771 * side effects after an error. */
4772 if (evaluate && get_tv_string_chk(rettv) == NULL)
4773 {
4774 clear_tv(rettv);
4775 return FAIL;
4776 }
4777 }
4778
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 /*
4780 * Get the second variable.
4781 */
4782 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004783 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004785 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 return FAIL;
4787 }
4788
4789 if (evaluate)
4790 {
4791 /*
4792 * Compute the result.
4793 */
4794 if (op == '.')
4795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004796 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4797 s2 = get_tv_string_buf_chk(&var2, buf2);
4798 if (s2 == NULL) /* type error ? */
4799 {
4800 clear_tv(rettv);
4801 clear_tv(&var2);
4802 return FAIL;
4803 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004804 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004805 clear_tv(rettv);
4806 rettv->v_type = VAR_STRING;
4807 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004809 else if (op == '+' && rettv->v_type == VAR_LIST
4810 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004811 {
4812 /* concatenate Lists */
4813 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4814 &var3) == FAIL)
4815 {
4816 clear_tv(rettv);
4817 clear_tv(&var2);
4818 return FAIL;
4819 }
4820 clear_tv(rettv);
4821 *rettv = var3;
4822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 else
4824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004825 int error = FALSE;
4826
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004827#ifdef FEAT_FLOAT
4828 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830 f1 = rettv->vval.v_float;
4831 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004832 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004833 else
4834#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836 n1 = get_tv_number_chk(rettv, &error);
4837 if (error)
4838 {
4839 /* This can only happen for "list + non-list". For
4840 * "non-list + ..." or "something - ...", we returned
4841 * before evaluating the 2nd operand. */
4842 clear_tv(rettv);
4843 return FAIL;
4844 }
4845#ifdef FEAT_FLOAT
4846 if (var2.v_type == VAR_FLOAT)
4847 f1 = n1;
4848#endif
4849 }
4850#ifdef FEAT_FLOAT
4851 if (var2.v_type == VAR_FLOAT)
4852 {
4853 f2 = var2.vval.v_float;
4854 n2 = 0;
4855 }
4856 else
4857#endif
4858 {
4859 n2 = get_tv_number_chk(&var2, &error);
4860 if (error)
4861 {
4862 clear_tv(rettv);
4863 clear_tv(&var2);
4864 return FAIL;
4865 }
4866#ifdef FEAT_FLOAT
4867 if (rettv->v_type == VAR_FLOAT)
4868 f2 = n2;
4869#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872
4873#ifdef FEAT_FLOAT
4874 /* If there is a float on either side the result is a float. */
4875 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4876 {
4877 if (op == '+')
4878 f1 = f1 + f2;
4879 else
4880 f1 = f1 - f2;
4881 rettv->v_type = VAR_FLOAT;
4882 rettv->vval.v_float = f1;
4883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885#endif
4886 {
4887 if (op == '+')
4888 n1 = n1 + n2;
4889 else
4890 n1 = n1 - n2;
4891 rettv->v_type = VAR_NUMBER;
4892 rettv->vval.v_number = n1;
4893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897 }
4898 return OK;
4899}
4900
4901/*
4902 * Handle fifth level expression:
4903 * * number multiplication
4904 * / number division
4905 * % number modulo
4906 *
4907 * "arg" must point to the first non-white of the expression.
4908 * "arg" is advanced to the next non-white after the recognized expression.
4909 *
4910 * Return OK or FAIL.
4911 */
4912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004913eval6(
4914 char_u **arg,
4915 typval_T *rettv,
4916 int evaluate,
4917 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
Bram Moolenaar33570922005-01-25 22:26:29 +00004919 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 int op;
4921 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922#ifdef FEAT_FLOAT
4923 int use_float = FALSE;
4924 float_T f1 = 0, f2;
4925#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004926 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
4928 /*
4929 * Get the first variable.
4930 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004931 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 return FAIL;
4933
4934 /*
4935 * Repeat computing, until no '*', '/' or '%' is following.
4936 */
4937 for (;;)
4938 {
4939 op = **arg;
4940 if (op != '*' && op != '/' && op != '%')
4941 break;
4942
4943 if (evaluate)
4944 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945#ifdef FEAT_FLOAT
4946 if (rettv->v_type == VAR_FLOAT)
4947 {
4948 f1 = rettv->vval.v_float;
4949 use_float = TRUE;
4950 n1 = 0;
4951 }
4952 else
4953#endif
4954 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004955 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004956 if (error)
4957 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
4959 else
4960 n1 = 0;
4961
4962 /*
4963 * Get the second variable.
4964 */
4965 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004966 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 return FAIL;
4968
4969 if (evaluate)
4970 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971#ifdef FEAT_FLOAT
4972 if (var2.v_type == VAR_FLOAT)
4973 {
4974 if (!use_float)
4975 {
4976 f1 = n1;
4977 use_float = TRUE;
4978 }
4979 f2 = var2.vval.v_float;
4980 n2 = 0;
4981 }
4982 else
4983#endif
4984 {
4985 n2 = get_tv_number_chk(&var2, &error);
4986 clear_tv(&var2);
4987 if (error)
4988 return FAIL;
4989#ifdef FEAT_FLOAT
4990 if (use_float)
4991 f2 = n2;
4992#endif
4993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
4995 /*
4996 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999#ifdef FEAT_FLOAT
5000 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002 if (op == '*')
5003 f1 = f1 * f2;
5004 else if (op == '/')
5005 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005006# ifdef VMS
5007 /* VMS crashes on divide by zero, work around it */
5008 if (f2 == 0.0)
5009 {
5010 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005011 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005012 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005013 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005014 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005015 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005016 }
5017 else
5018 f1 = f1 / f2;
5019# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020 /* We rely on the floating point library to handle divide
5021 * by zero to result in "inf" and not a crash. */
5022 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005023# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005026 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005027 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 return FAIL;
5029 }
5030 rettv->v_type = VAR_FLOAT;
5031 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005036 if (op == '*')
5037 n1 = n1 * n2;
5038 else if (op == '/')
5039 {
5040 if (n2 == 0) /* give an error message? */
5041 {
5042 if (n1 == 0)
5043 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5044 else if (n1 < 0)
5045 n1 = -0x7fffffffL;
5046 else
5047 n1 = 0x7fffffffL;
5048 }
5049 else
5050 n1 = n1 / n2;
5051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005053 {
5054 if (n2 == 0) /* give an error message? */
5055 n1 = 0;
5056 else
5057 n1 = n1 % n2;
5058 }
5059 rettv->v_type = VAR_NUMBER;
5060 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063 }
5064
5065 return OK;
5066}
5067
5068/*
5069 * Handle sixth level expression:
5070 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005071 * "string" string constant
5072 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 * &option-name option value
5074 * @r register contents
5075 * identifier variable value
5076 * function() function call
5077 * $VAR environment variable
5078 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005079 * [expr, expr] List
5080 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 *
5082 * Also handle:
5083 * ! in front logical NOT
5084 * - in front unary minus
5085 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 * trailing [] subscript in String or List
5087 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 *
5089 * "arg" must point to the first non-white of the expression.
5090 * "arg" is advanced to the next non-white after the recognized expression.
5091 *
5092 * Return OK or FAIL.
5093 */
5094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005095eval7(
5096 char_u **arg,
5097 typval_T *rettv,
5098 int evaluate,
5099 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 long n;
5102 int len;
5103 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 char_u *start_leader, *end_leader;
5105 int ret = OK;
5106 char_u *alias;
5107
5108 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005110 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005112 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
5114 /*
5115 * Skip '!' and '-' characters. They are handled later.
5116 */
5117 start_leader = *arg;
5118 while (**arg == '!' || **arg == '-' || **arg == '+')
5119 *arg = skipwhite(*arg + 1);
5120 end_leader = *arg;
5121
5122 switch (**arg)
5123 {
5124 /*
5125 * Number constant.
5126 */
5127 case '0':
5128 case '1':
5129 case '2':
5130 case '3':
5131 case '4':
5132 case '5':
5133 case '6':
5134 case '7':
5135 case '8':
5136 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005137 {
5138#ifdef FEAT_FLOAT
5139 char_u *p = skipdigits(*arg + 1);
5140 int get_float = FALSE;
5141
5142 /* We accept a float when the format matches
5143 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005144 * strict to avoid backwards compatibility problems.
5145 * Don't look for a float after the "." operator, so that
5146 * ":let vers = 1.2.3" doesn't fail. */
5147 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005149 get_float = TRUE;
5150 p = skipdigits(p + 2);
5151 if (*p == 'e' || *p == 'E')
5152 {
5153 ++p;
5154 if (*p == '-' || *p == '+')
5155 ++p;
5156 if (!vim_isdigit(*p))
5157 get_float = FALSE;
5158 else
5159 p = skipdigits(p + 1);
5160 }
5161 if (ASCII_ISALPHA(*p) || *p == '.')
5162 get_float = FALSE;
5163 }
5164 if (get_float)
5165 {
5166 float_T f;
5167
5168 *arg += string2float(*arg, &f);
5169 if (evaluate)
5170 {
5171 rettv->v_type = VAR_FLOAT;
5172 rettv->vval.v_float = f;
5173 }
5174 }
5175 else
5176#endif
5177 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005178 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005179 *arg += len;
5180 if (evaluate)
5181 {
5182 rettv->v_type = VAR_NUMBER;
5183 rettv->vval.v_number = n;
5184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
5189 /*
5190 * String constant: "string".
5191 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005192 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 break;
5194
5195 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005198 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199 break;
5200
5201 /*
5202 * List: [expr, expr]
5203 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005204 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 break;
5206
5207 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 * Dictionary: {key: val, key: val}
5209 */
5210 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5211 break;
5212
5213 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005214 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005216 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 break;
5218
5219 /*
5220 * Environment variable: $VAR.
5221 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005222 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 break;
5224
5225 /*
5226 * Register contents: @r.
5227 */
5228 case '@': ++*arg;
5229 if (evaluate)
5230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005231 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005232 rettv->vval.v_string = get_reg_contents(**arg,
5233 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235 if (**arg != NUL)
5236 ++*arg;
5237 break;
5238
5239 /*
5240 * nested expression: (expression).
5241 */
5242 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005243 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 if (**arg == ')')
5245 ++*arg;
5246 else if (ret == OK)
5247 {
5248 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005249 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 ret = FAIL;
5251 }
5252 break;
5253
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 break;
5256 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257
5258 if (ret == NOTDONE)
5259 {
5260 /*
5261 * Must be a variable or function name.
5262 * Can also be a curly-braces kind of name: {expr}.
5263 */
5264 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 if (alias != NULL)
5267 s = alias;
5268
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 ret = FAIL;
5271 else
5272 {
5273 if (**arg == '(') /* recursive! */
5274 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005275 partial_T *partial;
5276
Bram Moolenaar8c711452005-01-14 21:53:12 +00005277 /* If "s" is the name of a variable of type VAR_FUNC
5278 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005279 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280
5281 /* Invoke the function. */
5282 ret = get_func_tv(s, len, rettv, arg,
5283 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005284 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005285
5286 /* If evaluate is FALSE rettv->v_type was not set in
5287 * get_func_tv, but it's needed in handle_subscript() to parse
5288 * what follows. So set it here. */
5289 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5290 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005291 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005292 rettv->v_type = VAR_FUNC;
5293 }
5294
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 /* Stop the expression evaluation when immediately
5296 * aborting on error, or when an interrupt occurred or
5297 * an exception was thrown but not caught. */
5298 if (aborting())
5299 {
5300 if (ret == OK)
5301 clear_tv(rettv);
5302 ret = FAIL;
5303 }
5304 }
5305 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005306 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005307 else
5308 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005310 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 }
5312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 *arg = skipwhite(*arg);
5314
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005315 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5316 * expr(expr). */
5317 if (ret == OK)
5318 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 /*
5321 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5322 */
5323 if (ret == OK && evaluate && end_leader > start_leader)
5324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005326 int val = 0;
5327#ifdef FEAT_FLOAT
5328 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005330 if (rettv->v_type == VAR_FLOAT)
5331 f = rettv->vval.v_float;
5332 else
5333#endif
5334 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005337 clear_tv(rettv);
5338 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005340 else
5341 {
5342 while (end_leader > start_leader)
5343 {
5344 --end_leader;
5345 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005346 {
5347#ifdef FEAT_FLOAT
5348 if (rettv->v_type == VAR_FLOAT)
5349 f = !f;
5350 else
5351#endif
5352 val = !val;
5353 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005354 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005355 {
5356#ifdef FEAT_FLOAT
5357 if (rettv->v_type == VAR_FLOAT)
5358 f = -f;
5359 else
5360#endif
5361 val = -val;
5362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005364#ifdef FEAT_FLOAT
5365 if (rettv->v_type == VAR_FLOAT)
5366 {
5367 clear_tv(rettv);
5368 rettv->vval.v_float = f;
5369 }
5370 else
5371#endif
5372 {
5373 clear_tv(rettv);
5374 rettv->v_type = VAR_NUMBER;
5375 rettv->vval.v_number = val;
5376 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
5379
5380 return ret;
5381}
5382
5383/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005384 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5385 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5387 */
5388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005389eval_index(
5390 char_u **arg,
5391 typval_T *rettv,
5392 int evaluate,
5393 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394{
5395 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 long len = -1;
5399 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402
Bram Moolenaara03f2332016-02-06 18:09:59 +01005403 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005405 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005406 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005407 if (verbose)
5408 EMSG(_("E695: Cannot index a Funcref"));
5409 return FAIL;
5410 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005411#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005412 if (verbose)
5413 EMSG(_(e_float_as_string));
5414 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005415#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005416 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005417 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005418 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005419 if (verbose)
5420 EMSG(_("E909: Cannot index a special variable"));
5421 return FAIL;
5422 case VAR_UNKNOWN:
5423 if (evaluate)
5424 return FAIL;
5425 /* FALLTHROUGH */
5426
5427 case VAR_STRING:
5428 case VAR_NUMBER:
5429 case VAR_LIST:
5430 case VAR_DICT:
5431 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005432 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005434 init_tv(&var1);
5435 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005436 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005438 /*
5439 * dict.name
5440 */
5441 key = *arg + 1;
5442 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5443 ;
5444 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 }
5448 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 /*
5451 * something[idx]
5452 *
5453 * Get the (first) variable from inside the [].
5454 */
5455 *arg = skipwhite(*arg + 1);
5456 if (**arg == ':')
5457 empty1 = TRUE;
5458 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5459 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005460 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5461 {
5462 /* not a number or string */
5463 clear_tv(&var1);
5464 return FAIL;
5465 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466
5467 /*
5468 * Get the second variable from inside the [:].
5469 */
5470 if (**arg == ':')
5471 {
5472 range = TRUE;
5473 *arg = skipwhite(*arg + 1);
5474 if (**arg == ']')
5475 empty2 = TRUE;
5476 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005478 if (!empty1)
5479 clear_tv(&var1);
5480 return FAIL;
5481 }
5482 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5483 {
5484 /* not a number or string */
5485 if (!empty1)
5486 clear_tv(&var1);
5487 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 return FAIL;
5489 }
5490 }
5491
5492 /* Check for the ']'. */
5493 if (**arg != ']')
5494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005495 if (verbose)
5496 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 clear_tv(&var1);
5498 if (range)
5499 clear_tv(&var2);
5500 return FAIL;
5501 }
5502 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 }
5504
5505 if (evaluate)
5506 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507 n1 = 0;
5508 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005510 n1 = get_tv_number(&var1);
5511 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 }
5513 if (range)
5514 {
5515 if (empty2)
5516 n2 = -1;
5517 else
5518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 n2 = get_tv_number(&var2);
5520 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 }
5522 }
5523
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005526 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005527 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005528 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005529 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005530 case VAR_SPECIAL:
5531 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005532 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005533 break; /* not evaluating, skipping over subscript */
5534
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 case VAR_NUMBER:
5536 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 len = (long)STRLEN(s);
5539 if (range)
5540 {
5541 /* The resulting variable is a substring. If the indexes
5542 * are out of range the result is empty. */
5543 if (n1 < 0)
5544 {
5545 n1 = len + n1;
5546 if (n1 < 0)
5547 n1 = 0;
5548 }
5549 if (n2 < 0)
5550 n2 = len + n2;
5551 else if (n2 >= len)
5552 n2 = len;
5553 if (n1 >= len || n2 < 0 || n1 > n2)
5554 s = NULL;
5555 else
5556 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5557 }
5558 else
5559 {
5560 /* The resulting variable is a string of a single
5561 * character. If the index is too big or negative the
5562 * result is empty. */
5563 if (n1 >= len || n1 < 0)
5564 s = NULL;
5565 else
5566 s = vim_strnsave(s + n1, 1);
5567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 clear_tv(rettv);
5569 rettv->v_type = VAR_STRING;
5570 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 break;
5572
5573 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005575 if (n1 < 0)
5576 n1 = len + n1;
5577 if (!empty1 && (n1 < 0 || n1 >= len))
5578 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005579 /* For a range we allow invalid values and return an empty
5580 * list. A list index out of range is an error. */
5581 if (!range)
5582 {
5583 if (verbose)
5584 EMSGN(_(e_listidx), n1);
5585 return FAIL;
5586 }
5587 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 }
5589 if (range)
5590 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005591 list_T *l;
5592 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593
5594 if (n2 < 0)
5595 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005596 else if (n2 >= len)
5597 n2 = len - 1;
5598 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005599 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600 l = list_alloc();
5601 if (l == NULL)
5602 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005603 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 n1 <= n2; ++n1)
5605 {
5606 if (list_append_tv(l, &item->li_tv) == FAIL)
5607 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005608 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 return FAIL;
5610 }
5611 item = item->li_next;
5612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613 clear_tv(rettv);
5614 rettv->v_type = VAR_LIST;
5615 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005616 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 }
5618 else
5619 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005620 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621 clear_tv(rettv);
5622 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 }
5624 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625
5626 case VAR_DICT:
5627 if (range)
5628 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005629 if (verbose)
5630 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005631 if (len == -1)
5632 clear_tv(&var1);
5633 return FAIL;
5634 }
5635 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005636 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637
5638 if (len == -1)
5639 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005640 key = get_tv_string_chk(&var1);
5641 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643 clear_tv(&var1);
5644 return FAIL;
5645 }
5646 }
5647
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005648 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005650 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005651 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 if (len == -1)
5653 clear_tv(&var1);
5654 if (item == NULL)
5655 return FAIL;
5656
5657 copy_tv(&item->di_tv, &var1);
5658 clear_tv(rettv);
5659 *rettv = var1;
5660 }
5661 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 }
5663 }
5664
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 return OK;
5666}
5667
5668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 * Get an option value.
5670 * "arg" points to the '&' or '+' before the option name.
5671 * "arg" is advanced to character after the option name.
5672 * Return OK or FAIL.
5673 */
5674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005675get_option_tv(
5676 char_u **arg,
5677 typval_T *rettv, /* when NULL, only check if option exists */
5678 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
5680 char_u *option_end;
5681 long numval;
5682 char_u *stringval;
5683 int opt_type;
5684 int c;
5685 int working = (**arg == '+'); /* has("+option") */
5686 int ret = OK;
5687 int opt_flags;
5688
5689 /*
5690 * Isolate the option name and find its value.
5691 */
5692 option_end = find_option_end(arg, &opt_flags);
5693 if (option_end == NULL)
5694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005695 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 EMSG2(_("E112: Option name missing: %s"), *arg);
5697 return FAIL;
5698 }
5699
5700 if (!evaluate)
5701 {
5702 *arg = option_end;
5703 return OK;
5704 }
5705
5706 c = *option_end;
5707 *option_end = NUL;
5708 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005709 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
5711 if (opt_type == -3) /* invalid name */
5712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005713 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 EMSG2(_("E113: Unknown option: %s"), *arg);
5715 ret = FAIL;
5716 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005717 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 if (opt_type == -2) /* hidden string option */
5720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005721 rettv->v_type = VAR_STRING;
5722 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724 else if (opt_type == -1) /* hidden number option */
5725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726 rettv->v_type = VAR_NUMBER;
5727 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729 else if (opt_type == 1) /* number option */
5730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005731 rettv->v_type = VAR_NUMBER;
5732 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734 else /* string option */
5735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005736 rettv->v_type = VAR_STRING;
5737 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739 }
5740 else if (working && (opt_type == -2 || opt_type == -1))
5741 ret = FAIL;
5742
5743 *option_end = c; /* put back for error messages */
5744 *arg = option_end;
5745
5746 return ret;
5747}
5748
5749/*
5750 * Allocate a variable for a string constant.
5751 * Return OK or FAIL.
5752 */
5753 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005754get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
5756 char_u *p;
5757 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 int extra = 0;
5759
5760 /*
5761 * Find the end of the string, skipping backslashed characters.
5762 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
5765 if (*p == '\\' && p[1] != NUL)
5766 {
5767 ++p;
5768 /* A "\<x>" form occupies at least 4 characters, and produces up
5769 * to 6 characters: reserve space for 2 extra */
5770 if (*p == '<')
5771 extra += 2;
5772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
5774
5775 if (*p != '"')
5776 {
5777 EMSG2(_("E114: Missing quote: %s"), *arg);
5778 return FAIL;
5779 }
5780
5781 /* If only parsing, set *arg and return here */
5782 if (!evaluate)
5783 {
5784 *arg = p + 1;
5785 return OK;
5786 }
5787
5788 /*
5789 * Copy the string into allocated memory, handling backslashed
5790 * characters.
5791 */
5792 name = alloc((unsigned)(p - *arg + extra));
5793 if (name == NULL)
5794 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 rettv->v_type = VAR_STRING;
5796 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 {
5800 if (*p == '\\')
5801 {
5802 switch (*++p)
5803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 case 'b': *name++ = BS; ++p; break;
5805 case 'e': *name++ = ESC; ++p; break;
5806 case 'f': *name++ = FF; ++p; break;
5807 case 'n': *name++ = NL; ++p; break;
5808 case 'r': *name++ = CAR; ++p; break;
5809 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
5811 case 'X': /* hex: "\x1", "\x12" */
5812 case 'x':
5813 case 'u': /* Unicode: "\u0023" */
5814 case 'U':
5815 if (vim_isxdigit(p[1]))
5816 {
5817 int n, nr;
5818 int c = toupper(*p);
5819
5820 if (c == 'X')
5821 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005822 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005824 else
5825 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 nr = 0;
5827 while (--n >= 0 && vim_isxdigit(p[1]))
5828 {
5829 ++p;
5830 nr = (nr << 4) + hex2nr(*p);
5831 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#ifdef FEAT_MBYTE
5834 /* For "\u" store the number according to
5835 * 'encoding'. */
5836 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 else
5839#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 break;
5843
5844 /* octal: "\1", "\12", "\123" */
5845 case '0':
5846 case '1':
5847 case '2':
5848 case '3':
5849 case '4':
5850 case '5':
5851 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 case '7': *name = *p++ - '0';
5853 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 *name = (*name << 3) + *p++ - '0';
5856 if (*p >= '0' && *p <= '7')
5857 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 break;
5861
5862 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (extra != 0)
5865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 break;
5868 }
5869 /* FALLTHROUGH */
5870
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 break;
5873 }
5874 }
5875 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 *arg = p + 1;
5881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 return OK;
5883}
5884
5885/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 * Return OK or FAIL.
5888 */
5889 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005890get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 int reduce = 0;
5895
5896 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 */
5899 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5900 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 break;
5905 ++reduce;
5906 ++p;
5907 }
5908 }
5909
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 return FAIL;
5914 }
5915
Bram Moolenaar8c711452005-01-14 21:53:12 +00005916 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917 if (!evaluate)
5918 {
5919 *arg = p + 1;
5920 return OK;
5921 }
5922
5923 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 */
5926 str = alloc((unsigned)((p - *arg) - reduce));
5927 if (str == NULL)
5928 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929 rettv->v_type = VAR_STRING;
5930 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005937 break;
5938 ++p;
5939 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005941 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005943 *arg = p + 1;
5944
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005945 return OK;
5946}
5947
Bram Moolenaarddecc252016-04-06 22:59:37 +02005948 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005949partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005950{
5951 int i;
5952
5953 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005954 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005955 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005956 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005957 func_unref(pt->pt_name);
5958 vim_free(pt->pt_name);
5959 vim_free(pt);
5960}
5961
5962/*
5963 * Unreference a closure: decrement the reference count and free it when it
5964 * becomes zero.
5965 */
5966 void
5967partial_unref(partial_T *pt)
5968{
5969 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005970 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005971}
5972
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005973/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 * Allocate a variable for a List and fill it from "*arg".
5975 * Return OK or FAIL.
5976 */
5977 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005978get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979{
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 list_T *l = NULL;
5981 typval_T tv;
5982 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983
5984 if (evaluate)
5985 {
5986 l = list_alloc();
5987 if (l == NULL)
5988 return FAIL;
5989 }
5990
5991 *arg = skipwhite(*arg + 1);
5992 while (**arg != ']' && **arg != NUL)
5993 {
5994 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5995 goto failret;
5996 if (evaluate)
5997 {
5998 item = listitem_alloc();
5999 if (item != NULL)
6000 {
6001 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006002 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 list_append(l, item);
6004 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006005 else
6006 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 }
6008
6009 if (**arg == ']')
6010 break;
6011 if (**arg != ',')
6012 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006013 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014 goto failret;
6015 }
6016 *arg = skipwhite(*arg + 1);
6017 }
6018
6019 if (**arg != ']')
6020 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006021 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022failret:
6023 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006024 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 return FAIL;
6026 }
6027
6028 *arg = skipwhite(*arg + 1);
6029 if (evaluate)
6030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006031 rettv->v_type = VAR_LIST;
6032 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033 ++l->lv_refcount;
6034 }
6035
6036 return OK;
6037}
6038
6039/*
6040 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006041 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006043 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006044list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006046 list_T *l;
6047
6048 l = (list_T *)alloc_clear(sizeof(list_T));
6049 if (l != NULL)
6050 {
6051 /* Prepend the list to the list of lists for garbage collection. */
6052 if (first_list != NULL)
6053 first_list->lv_used_prev = l;
6054 l->lv_used_prev = NULL;
6055 l->lv_used_next = first_list;
6056 first_list = l;
6057 }
6058 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059}
6060
6061/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006062 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006063 * Returns OK or FAIL.
6064 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006065 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006066rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006067{
6068 list_T *l = list_alloc();
6069
6070 if (l == NULL)
6071 return FAIL;
6072
6073 rettv->vval.v_list = l;
6074 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006075 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006076 ++l->lv_refcount;
6077 return OK;
6078}
6079
6080/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 * Unreference a list: decrement the reference count and free it when it
6082 * becomes zero.
6083 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006087 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006088 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089}
6090
6091/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006092 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 * Ignores the reference count.
6094 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006095 static void
6096list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097{
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006099
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006100 for (item = l->lv_first; item != NULL; item = l->lv_first)
6101 {
6102 /* Remove the item before deleting it. */
6103 l->lv_first = item->li_next;
6104 clear_tv(&item->li_tv);
6105 vim_free(item);
6106 }
6107}
6108
6109 static void
6110list_free_list(list_T *l)
6111{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006112 /* Remove the list from the list of lists for garbage collection. */
6113 if (l->lv_used_prev == NULL)
6114 first_list = l->lv_used_next;
6115 else
6116 l->lv_used_prev->lv_used_next = l->lv_used_next;
6117 if (l->lv_used_next != NULL)
6118 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6119
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120 vim_free(l);
6121}
6122
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006123 void
6124list_free(list_T *l)
6125{
6126 if (!in_free_unref_items)
6127 {
6128 list_free_contents(l);
6129 list_free_list(l);
6130 }
6131}
6132
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133/*
6134 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006135 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006137 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006138listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139{
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141}
6142
6143/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006144 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006147listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006149 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 vim_free(item);
6151}
6152
6153/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006154 * Remove a list item from a List and free it. Also clears the value.
6155 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006158{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006159 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006160 listitem_free(item);
6161}
6162
6163/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 * Get the number of items in a list.
6165 */
6166 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006167list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169 if (l == NULL)
6170 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006171 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172}
6173
6174/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175 * Return TRUE when two lists have exactly the same values.
6176 */
6177 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006178list_equal(
6179 list_T *l1,
6180 list_T *l2,
6181 int ic, /* ignore case for strings */
6182 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006183{
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006186 if (l1 == NULL || l2 == NULL)
6187 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006188 if (l1 == l2)
6189 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006190 if (list_len(l1) != list_len(l2))
6191 return FALSE;
6192
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193 for (item1 = l1->lv_first, item2 = l2->lv_first;
6194 item1 != NULL && item2 != NULL;
6195 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 return FALSE;
6198 return item1 == NULL && item2 == NULL;
6199}
6200
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006201/*
6202 * Return the dictitem that an entry in a hashtable points to.
6203 */
6204 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006205dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006206{
6207 return HI2DI(hi);
6208}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006209
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006210/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006211 * Return TRUE when two dictionaries have exactly the same key/values.
6212 */
6213 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214dict_equal(
6215 dict_T *d1,
6216 dict_T *d2,
6217 int ic, /* ignore case for strings */
6218 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006219{
Bram Moolenaar33570922005-01-25 22:26:29 +00006220 hashitem_T *hi;
6221 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006223
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006224 if (d1 == NULL || d2 == NULL)
6225 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006226 if (d1 == d2)
6227 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006228 if (dict_len(d1) != dict_len(d2))
6229 return FALSE;
6230
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006231 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006232 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006233 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006234 if (!HASHITEM_EMPTY(hi))
6235 {
6236 item2 = dict_find(d2, hi->hi_key, -1);
6237 if (item2 == NULL)
6238 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006239 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006240 return FALSE;
6241 --todo;
6242 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006243 }
6244 return TRUE;
6245}
6246
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006247static int tv_equal_recurse_limit;
6248
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006249/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006250 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006251 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006252 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253 */
6254 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006255tv_equal(
6256 typval_T *tv1,
6257 typval_T *tv2,
6258 int ic, /* ignore case */
6259 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006260{
6261 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006262 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006263 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006264 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006265
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006266 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6267 if ((tv1->v_type == VAR_FUNC
6268 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6269 && (tv2->v_type == VAR_FUNC
6270 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6271 {
6272 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6273 : tv1->vval.v_partial->pt_name;
6274 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6275 : tv2->vval.v_partial->pt_name;
6276 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6277 }
6278
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006279 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006280 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006281
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006282 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006283 * recursiveness to a limit. We guess they are equal then.
6284 * A fixed limit has the problem of still taking an awful long time.
6285 * Reduce the limit every time running into it. That should work fine for
6286 * deeply linked structures that are not recursively linked and catch
6287 * recursiveness quickly. */
6288 if (!recursive)
6289 tv_equal_recurse_limit = 1000;
6290 if (recursive_cnt >= tv_equal_recurse_limit)
6291 {
6292 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006293 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006294 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006295
6296 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006297 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006298 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006299 ++recursive_cnt;
6300 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6301 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006302 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006303
6304 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006305 ++recursive_cnt;
6306 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6307 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006308 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006309
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006310 case VAR_NUMBER:
6311 return tv1->vval.v_number == tv2->vval.v_number;
6312
6313 case VAR_STRING:
6314 s1 = get_tv_string_buf(tv1, buf1);
6315 s2 = get_tv_string_buf(tv2, buf2);
6316 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006317
6318 case VAR_SPECIAL:
6319 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006320
6321 case VAR_FLOAT:
6322#ifdef FEAT_FLOAT
6323 return tv1->vval.v_float == tv2->vval.v_float;
6324#endif
6325 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006327 return tv1->vval.v_job == tv2->vval.v_job;
6328#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006329 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006330#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006331 return tv1->vval.v_channel == tv2->vval.v_channel;
6332#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006333 case VAR_FUNC:
6334 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006335 case VAR_UNKNOWN:
6336 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006337 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006338
Bram Moolenaara03f2332016-02-06 18:09:59 +01006339 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6340 * does not equal anything, not even itself. */
6341 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006342}
6343
6344/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 * Locate item with index "n" in list "l" and return it.
6346 * A negative index is counted from the end; -1 is the last item.
6347 * Returns NULL when "n" is out of range.
6348 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006349 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006350list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351{
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353 long idx;
6354
6355 if (l == NULL)
6356 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006357
6358 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006359 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006360 n = l->lv_len + n;
6361
6362 /* Check for index out of range. */
6363 if (n < 0 || n >= l->lv_len)
6364 return NULL;
6365
6366 /* When there is a cached index may start search from there. */
6367 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006369 if (n < l->lv_idx / 2)
6370 {
6371 /* closest to the start of the list */
6372 item = l->lv_first;
6373 idx = 0;
6374 }
6375 else if (n > (l->lv_idx + l->lv_len) / 2)
6376 {
6377 /* closest to the end of the list */
6378 item = l->lv_last;
6379 idx = l->lv_len - 1;
6380 }
6381 else
6382 {
6383 /* closest to the cached index */
6384 item = l->lv_idx_item;
6385 idx = l->lv_idx;
6386 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387 }
6388 else
6389 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006390 if (n < l->lv_len / 2)
6391 {
6392 /* closest to the start of the list */
6393 item = l->lv_first;
6394 idx = 0;
6395 }
6396 else
6397 {
6398 /* closest to the end of the list */
6399 item = l->lv_last;
6400 idx = l->lv_len - 1;
6401 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006403
6404 while (n > idx)
6405 {
6406 /* search forward */
6407 item = item->li_next;
6408 ++idx;
6409 }
6410 while (n < idx)
6411 {
6412 /* search backward */
6413 item = item->li_prev;
6414 --idx;
6415 }
6416
6417 /* cache the used index */
6418 l->lv_idx = idx;
6419 l->lv_idx_item = item;
6420
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421 return item;
6422}
6423
6424/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006425 * Get list item "l[idx]" as a number.
6426 */
6427 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006428list_find_nr(
6429 list_T *l,
6430 long idx,
6431 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006432{
6433 listitem_T *li;
6434
6435 li = list_find(l, idx);
6436 if (li == NULL)
6437 {
6438 if (errorp != NULL)
6439 *errorp = TRUE;
6440 return -1L;
6441 }
6442 return get_tv_number_chk(&li->li_tv, errorp);
6443}
6444
6445/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006446 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6447 */
6448 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006449list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006450{
6451 listitem_T *li;
6452
6453 li = list_find(l, idx - 1);
6454 if (li == NULL)
6455 {
6456 EMSGN(_(e_listidx), idx);
6457 return NULL;
6458 }
6459 return get_tv_string(&li->li_tv);
6460}
6461
6462/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006463 * Locate "item" list "l" and return its index.
6464 * Returns -1 when "item" is not in the list.
6465 */
6466 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006467list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006468{
6469 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006470 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006471
6472 if (l == NULL)
6473 return -1;
6474 idx = 0;
6475 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6476 ++idx;
6477 if (li == NULL)
6478 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006479 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006480}
6481
6482/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 * Append item "item" to the end of list "l".
6484 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006485 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006486list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006487{
6488 if (l->lv_last == NULL)
6489 {
6490 /* empty list */
6491 l->lv_first = item;
6492 l->lv_last = item;
6493 item->li_prev = NULL;
6494 }
6495 else
6496 {
6497 l->lv_last->li_next = item;
6498 item->li_prev = l->lv_last;
6499 l->lv_last = item;
6500 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006501 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502 item->li_next = NULL;
6503}
6504
6505/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006510list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006512 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513
Bram Moolenaar05159a02005-02-26 23:04:13 +00006514 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006516 copy_tv(tv, &li->li_tv);
6517 list_append(l, li);
6518 return OK;
6519}
6520
6521/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006522 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006523 * Return FAIL when out of memory.
6524 */
6525 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006526list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006527{
6528 listitem_T *li = listitem_alloc();
6529
6530 if (li == NULL)
6531 return FAIL;
6532 li->li_tv.v_type = VAR_DICT;
6533 li->li_tv.v_lock = 0;
6534 li->li_tv.vval.v_dict = dict;
6535 list_append(list, li);
6536 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 return OK;
6538}
6539
6540/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006541 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006542 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006543 * Returns FAIL when out of memory.
6544 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006546list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006547{
6548 listitem_T *li = listitem_alloc();
6549
6550 if (li == NULL)
6551 return FAIL;
6552 list_append(l, li);
6553 li->li_tv.v_type = VAR_STRING;
6554 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006555 if (str == NULL)
6556 li->li_tv.vval.v_string = NULL;
6557 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006558 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006559 return FAIL;
6560 return OK;
6561}
6562
6563/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006564 * Append "n" to list "l".
6565 * Returns FAIL when out of memory.
6566 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006568list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006569{
6570 listitem_T *li;
6571
6572 li = listitem_alloc();
6573 if (li == NULL)
6574 return FAIL;
6575 li->li_tv.v_type = VAR_NUMBER;
6576 li->li_tv.v_lock = 0;
6577 li->li_tv.vval.v_number = n;
6578 list_append(l, li);
6579 return OK;
6580}
6581
6582/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584 * If "item" is NULL append at the end.
6585 * Return FAIL when out of memory.
6586 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006587 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006588list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589{
Bram Moolenaar33570922005-01-25 22:26:29 +00006590 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591
6592 if (ni == NULL)
6593 return FAIL;
6594 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006595 list_insert(l, ni, item);
6596 return OK;
6597}
6598
6599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006600list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006601{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006602 if (item == NULL)
6603 /* Append new item at end of list. */
6604 list_append(l, ni);
6605 else
6606 {
6607 /* Insert new item before existing item. */
6608 ni->li_prev = item->li_prev;
6609 ni->li_next = item;
6610 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006611 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006612 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006613 ++l->lv_idx;
6614 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006615 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006616 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006617 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006618 l->lv_idx_item = NULL;
6619 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006620 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006621 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006622 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006623}
6624
6625/*
6626 * Extend "l1" with "l2".
6627 * If "bef" is NULL append at the end, otherwise insert before this item.
6628 * Returns FAIL when out of memory.
6629 */
6630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006631list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006632{
Bram Moolenaar33570922005-01-25 22:26:29 +00006633 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006634 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006636 /* We also quit the loop when we have inserted the original item count of
6637 * the list, avoid a hang when we extend a list with itself. */
6638 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006639 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6640 return FAIL;
6641 return OK;
6642}
6643
6644/*
6645 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6646 * Return FAIL when out of memory.
6647 */
6648 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006649list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006650{
Bram Moolenaar33570922005-01-25 22:26:29 +00006651 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006653 if (l1 == NULL || l2 == NULL)
6654 return FAIL;
6655
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006656 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006657 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006658 if (l == NULL)
6659 return FAIL;
6660 tv->v_type = VAR_LIST;
6661 tv->vval.v_list = l;
6662
6663 /* append all items from the second list */
6664 return list_extend(l, l2, NULL);
6665}
6666
6667/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006668 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006670 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 * Returns NULL when out of memory.
6672 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006673 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675{
Bram Moolenaar33570922005-01-25 22:26:29 +00006676 list_T *copy;
6677 listitem_T *item;
6678 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679
6680 if (orig == NULL)
6681 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682
6683 copy = list_alloc();
6684 if (copy != NULL)
6685 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006686 if (copyID != 0)
6687 {
6688 /* Do this before adding the items, because one of the items may
6689 * refer back to this list. */
6690 orig->lv_copyID = copyID;
6691 orig->lv_copylist = copy;
6692 }
6693 for (item = orig->lv_first; item != NULL && !got_int;
6694 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695 {
6696 ni = listitem_alloc();
6697 if (ni == NULL)
6698 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006699 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006700 {
6701 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6702 {
6703 vim_free(ni);
6704 break;
6705 }
6706 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006708 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006709 list_append(copy, ni);
6710 }
6711 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712 if (item != NULL)
6713 {
6714 list_unref(copy);
6715 copy = NULL;
6716 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006717 }
6718
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006719 return copy;
6720}
6721
6722/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006723 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006724 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006725 * This used to be called list_remove, but that conflicts with a Sun header
6726 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006727 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006728 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006729vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006730{
Bram Moolenaar33570922005-01-25 22:26:29 +00006731 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006732
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006733 /* notify watchers */
6734 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006736 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006737 list_fix_watch(l, ip);
6738 if (ip == item2)
6739 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006740 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741
6742 if (item2->li_next == NULL)
6743 l->lv_last = item->li_prev;
6744 else
6745 item2->li_next->li_prev = item->li_prev;
6746 if (item->li_prev == NULL)
6747 l->lv_first = item2->li_next;
6748 else
6749 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006750 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006751}
6752
6753/*
6754 * Return an allocated string with the string representation of a list.
6755 * May return NULL.
6756 */
6757 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006758list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006759{
6760 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006761
6762 if (tv->vval.v_list == NULL)
6763 return NULL;
6764 ga_init2(&ga, (int)sizeof(char), 80);
6765 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006766 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 {
6768 vim_free(ga.ga_data);
6769 return NULL;
6770 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006771 ga_append(&ga, ']');
6772 ga_append(&ga, NUL);
6773 return (char_u *)ga.ga_data;
6774}
6775
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006776typedef struct join_S {
6777 char_u *s;
6778 char_u *tofree;
6779} join_T;
6780
6781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006782list_join_inner(
6783 garray_T *gap, /* to store the result in */
6784 list_T *l,
6785 char_u *sep,
6786 int echo_style,
6787 int copyID,
6788 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006789{
6790 int i;
6791 join_T *p;
6792 int len;
6793 int sumlen = 0;
6794 int first = TRUE;
6795 char_u *tofree;
6796 char_u numbuf[NUMBUFLEN];
6797 listitem_T *item;
6798 char_u *s;
6799
6800 /* Stringify each item in the list. */
6801 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6802 {
6803 if (echo_style)
6804 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6805 else
6806 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6807 if (s == NULL)
6808 return FAIL;
6809
6810 len = (int)STRLEN(s);
6811 sumlen += len;
6812
Bram Moolenaarcde88542015-08-11 19:14:00 +02006813 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006814 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6815 if (tofree != NULL || s != numbuf)
6816 {
6817 p->s = s;
6818 p->tofree = tofree;
6819 }
6820 else
6821 {
6822 p->s = vim_strnsave(s, len);
6823 p->tofree = p->s;
6824 }
6825
6826 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006827 if (did_echo_string_emsg) /* recursion error, bail out */
6828 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006829 }
6830
6831 /* Allocate result buffer with its total size, avoid re-allocation and
6832 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6833 if (join_gap->ga_len >= 2)
6834 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6835 if (ga_grow(gap, sumlen + 2) == FAIL)
6836 return FAIL;
6837
6838 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6839 {
6840 if (first)
6841 first = FALSE;
6842 else
6843 ga_concat(gap, sep);
6844 p = ((join_T *)join_gap->ga_data) + i;
6845
6846 if (p->s != NULL)
6847 ga_concat(gap, p->s);
6848 line_breakcheck();
6849 }
6850
6851 return OK;
6852}
6853
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006854/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006855 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006856 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006857 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006858 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006860list_join(
6861 garray_T *gap,
6862 list_T *l,
6863 char_u *sep,
6864 int echo_style,
6865 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006866{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006867 garray_T join_ga;
6868 int retval;
6869 join_T *p;
6870 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006871
Bram Moolenaard39a7512015-04-16 22:51:22 +02006872 if (l->lv_len < 1)
6873 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006874 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6875 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6876
6877 /* Dispose each item in join_ga. */
6878 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006879 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006880 p = (join_T *)join_ga.ga_data;
6881 for (i = 0; i < join_ga.ga_len; ++i)
6882 {
6883 vim_free(p->tofree);
6884 ++p;
6885 }
6886 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006887 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006888
6889 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006890}
6891
6892/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006893 * Return the next (unique) copy ID.
6894 * Used for serializing nested structures.
6895 */
6896 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006897get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006898{
6899 current_copyID += COPYID_INC;
6900 return current_copyID;
6901}
6902
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006903/* Used by get_func_tv() */
6904static garray_T funcargs = GA_EMPTY;
6905
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006906/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 * Garbage collection for lists and dictionaries.
6908 *
6909 * We use reference counts to be able to free most items right away when they
6910 * are no longer used. But for composite items it's possible that it becomes
6911 * unused while the reference count is > 0: When there is a recursive
6912 * reference. Example:
6913 * :let l = [1, 2, 3]
6914 * :let d = {9: l}
6915 * :let l[1] = d
6916 *
6917 * Since this is quite unusual we handle this with garbage collection: every
6918 * once in a while find out which lists and dicts are not referenced from any
6919 * variable.
6920 *
6921 * Here is a good reference text about garbage collection (refers to Python
6922 * but it applies to all reference-counting mechanisms):
6923 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006924 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006925
6926/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927 * Do garbage collection for lists and dicts.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006928 * When "testing" is TRUE this is called from garbagecollect_for_testing().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006930 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006932garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006933{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006934 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 buf_T *buf;
6937 win_T *wp;
6938 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006939 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006940 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006941 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006942#ifdef FEAT_WINDOWS
6943 tabpage_T *tp;
6944#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006946 if (!testing)
6947 {
6948 /* Only do this once. */
6949 want_garbage_collect = FALSE;
6950 may_garbage_collect = FALSE;
6951 garbage_collect_at_exit = FALSE;
6952 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006953
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 /* We advance by two because we add one for items referenced through
6955 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006956 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 /*
6959 * 1. Go through all accessible variables and mark all lists and dicts
6960 * with copyID.
6961 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006962
6963 /* Don't free variables in the previous_funccal list unless they are only
6964 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006965 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6967 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006968 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6969 NULL);
6970 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6971 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 }
6973
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 /* script-local variables */
6975 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006976 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977
6978 /* buffer-local variables */
6979 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006980 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6981 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982
6983 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006984 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006985 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6986 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006987#ifdef FEAT_AUTOCMD
6988 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006989 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6990 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006991#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006993#ifdef FEAT_WINDOWS
6994 /* tabpage-local variables */
6995 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6997 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006998#endif
6999
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007001 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002
7003 /* function-local variables */
7004 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7005 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007006 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7007 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 }
7009
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007010 /* function call arguments, if v:testing is set. */
7011 for (i = 0; i < funcargs.ga_len; ++i)
7012 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7013 copyID, NULL, NULL);
7014
Bram Moolenaard812df62008-11-09 12:46:09 +00007015 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007016 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007017
Bram Moolenaar1dced572012-04-05 16:54:08 +02007018#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007019 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007020#endif
7021
Bram Moolenaardb913952012-06-29 12:54:53 +02007022#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007024#endif
7025
7026#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007028#endif
7029
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007030#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007031 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007032 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007033#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007034#ifdef FEAT_NETBEANS_INTG
7035 abort = abort || set_ref_in_nb_channel(copyID);
7036#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007037
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007039 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 /*
7041 * 2. Free lists and dictionaries that are not referenced.
7042 */
7043 did_free = free_unref_items(copyID);
7044
7045 /*
7046 * 3. Check if any funccal can be freed now.
7047 */
7048 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007049 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 if (can_free_funccal(*pfc, copyID))
7051 {
7052 fc = *pfc;
7053 *pfc = fc->caller;
7054 free_funccal(fc, TRUE);
7055 did_free = TRUE;
7056 did_free_funccal = TRUE;
7057 }
7058 else
7059 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007060 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007061 if (did_free_funccal)
7062 /* When a funccal was freed some more items might be garbage
7063 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007064 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007065 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007066 else if (p_verbose > 0)
7067 {
7068 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7069 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007070
7071 return did_free;
7072}
7073
7074/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007075 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007076 */
7077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007079{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007080 dict_T *dd, *dd_next;
7081 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007082 int did_free = FALSE;
7083
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007084 /* Let all "free" functions know that we are here. This means no
7085 * dictionaries, lists, channels or jobs are to be freed, because we will
7086 * do that here. */
7087 in_free_unref_items = TRUE;
7088
7089 /*
7090 * PASS 1: free the contents of the items. We don't free the items
7091 * themselves yet, so that it is possible to decrement refcount counters
7092 */
7093
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007095 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007097 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007098 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007099 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007100 /* Free the Dictionary and ordinary items it contains, but don't
7101 * recurse into Lists and Dictionaries, they will be in the list
7102 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007103 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007104 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007105 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007106
7107 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007108 * Go through the list of lists and free items without the copyID.
7109 * But don't free a list that has a watcher (used in a for loop), these
7110 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007111 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007112 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7113 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7114 && ll->lv_watch == NULL)
7115 {
7116 /* Free the List and ordinary items it contains, but don't recurse
7117 * into Lists and Dictionaries, they will be in the list of dicts
7118 * or list of lists. */
7119 list_free_contents(ll);
7120 did_free = TRUE;
7121 }
7122
7123#ifdef FEAT_JOB_CHANNEL
7124 /* Go through the list of jobs and free items without the copyID. This
7125 * must happen before doing channels, because jobs refer to channels, but
7126 * the reference from the channel to the job isn't tracked. */
7127 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7128
7129 /* Go through the list of channels and free items without the copyID. */
7130 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7131#endif
7132
7133 /*
7134 * PASS 2: free the items themselves.
7135 */
7136 for (dd = first_dict; dd != NULL; dd = dd_next)
7137 {
7138 dd_next = dd->dv_used_next;
7139 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7140 dict_free_dict(dd);
7141 }
7142
7143 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007144 {
7145 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007146 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7147 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007148 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007149 /* Free the List and ordinary items it contains, but don't recurse
7150 * into Lists and Dictionaries, they will be in the list of dicts
7151 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007152 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007153 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007154 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007155
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007156#ifdef FEAT_JOB_CHANNEL
7157 /* Go through the list of jobs and free items without the copyID. This
7158 * must happen before doing channels, because jobs refer to channels, but
7159 * the reference from the channel to the job isn't tracked. */
7160 free_unused_jobs(copyID, COPYID_MASK);
7161
7162 /* Go through the list of channels and free items without the copyID. */
7163 free_unused_channels(copyID, COPYID_MASK);
7164#endif
7165
7166 in_free_unref_items = FALSE;
7167
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007168 return did_free;
7169}
7170
7171/*
7172 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007173 * "list_stack" is used to add lists to be marked. Can be NULL.
7174 *
7175 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007176 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007177 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007178set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007179{
7180 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007181 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007182 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007183 hashtab_T *cur_ht;
7184 ht_stack_T *ht_stack = NULL;
7185 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007186
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007187 cur_ht = ht;
7188 for (;;)
7189 {
7190 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007191 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007192 /* Mark each item in the hashtab. If the item contains a hashtab
7193 * it is added to ht_stack, if it contains a list it is added to
7194 * list_stack. */
7195 todo = (int)cur_ht->ht_used;
7196 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7197 if (!HASHITEM_EMPTY(hi))
7198 {
7199 --todo;
7200 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7201 &ht_stack, list_stack);
7202 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007203 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007204
7205 if (ht_stack == NULL)
7206 break;
7207
7208 /* take an item from the stack */
7209 cur_ht = ht_stack->ht;
7210 tempitem = ht_stack;
7211 ht_stack = ht_stack->prev;
7212 free(tempitem);
7213 }
7214
7215 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007216}
7217
7218/*
7219 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007220 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7221 *
7222 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007223 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007224 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007225set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007226{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007227 listitem_T *li;
7228 int abort = FALSE;
7229 list_T *cur_l;
7230 list_stack_T *list_stack = NULL;
7231 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007232
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007233 cur_l = l;
7234 for (;;)
7235 {
7236 if (!abort)
7237 /* Mark each item in the list. If the item contains a hashtab
7238 * it is added to ht_stack, if it contains a list it is added to
7239 * list_stack. */
7240 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7241 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7242 ht_stack, &list_stack);
7243 if (list_stack == NULL)
7244 break;
7245
7246 /* take an item from the stack */
7247 cur_l = list_stack->list;
7248 tempitem = list_stack;
7249 list_stack = list_stack->prev;
7250 free(tempitem);
7251 }
7252
7253 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007254}
7255
7256/*
7257 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007258 * "list_stack" is used to add lists to be marked. Can be NULL.
7259 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7260 *
7261 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007262 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007263 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007264set_ref_in_item(
7265 typval_T *tv,
7266 int copyID,
7267 ht_stack_T **ht_stack,
7268 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007269{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007270 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007271
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007272 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007273 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007274 dict_T *dd = tv->vval.v_dict;
7275
Bram Moolenaara03f2332016-02-06 18:09:59 +01007276 if (dd != NULL && dd->dv_copyID != copyID)
7277 {
7278 /* Didn't see this dict yet. */
7279 dd->dv_copyID = copyID;
7280 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007281 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007282 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7283 }
7284 else
7285 {
7286 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7287 if (newitem == NULL)
7288 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007289 else
7290 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007291 newitem->ht = &dd->dv_hashtab;
7292 newitem->prev = *ht_stack;
7293 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007294 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007295 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007296 }
7297 }
7298 else if (tv->v_type == VAR_LIST)
7299 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007300 list_T *ll = tv->vval.v_list;
7301
Bram Moolenaara03f2332016-02-06 18:09:59 +01007302 if (ll != NULL && ll->lv_copyID != copyID)
7303 {
7304 /* Didn't see this list yet. */
7305 ll->lv_copyID = copyID;
7306 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007307 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007308 abort = set_ref_in_list(ll, copyID, ht_stack);
7309 }
7310 else
7311 {
7312 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007313 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007314 if (newitem == NULL)
7315 abort = TRUE;
7316 else
7317 {
7318 newitem->list = ll;
7319 newitem->prev = *list_stack;
7320 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007321 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007322 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007323 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007324 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007325 else if (tv->v_type == VAR_PARTIAL)
7326 {
7327 partial_T *pt = tv->vval.v_partial;
7328 int i;
7329
7330 /* A partial does not have a copyID, because it cannot contain itself.
7331 */
7332 if (pt != NULL)
7333 {
7334 if (pt->pt_dict != NULL)
7335 {
7336 typval_T dtv;
7337
7338 dtv.v_type = VAR_DICT;
7339 dtv.vval.v_dict = pt->pt_dict;
7340 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7341 }
7342
7343 for (i = 0; i < pt->pt_argc; ++i)
7344 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7345 ht_stack, list_stack);
7346 }
7347 }
7348#ifdef FEAT_JOB_CHANNEL
7349 else if (tv->v_type == VAR_JOB)
7350 {
7351 job_T *job = tv->vval.v_job;
7352 typval_T dtv;
7353
7354 if (job != NULL && job->jv_copyID != copyID)
7355 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007356 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007357 if (job->jv_channel != NULL)
7358 {
7359 dtv.v_type = VAR_CHANNEL;
7360 dtv.vval.v_channel = job->jv_channel;
7361 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7362 }
7363 if (job->jv_exit_partial != NULL)
7364 {
7365 dtv.v_type = VAR_PARTIAL;
7366 dtv.vval.v_partial = job->jv_exit_partial;
7367 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7368 }
7369 }
7370 }
7371 else if (tv->v_type == VAR_CHANNEL)
7372 {
7373 channel_T *ch =tv->vval.v_channel;
7374 int part;
7375 typval_T dtv;
7376 jsonq_T *jq;
7377 cbq_T *cq;
7378
7379 if (ch != NULL && ch->ch_copyID != copyID)
7380 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007381 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007382 for (part = PART_SOCK; part <= PART_IN; ++part)
7383 {
7384 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7385 jq = jq->jq_next)
7386 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7387 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7388 cq = cq->cq_next)
7389 if (cq->cq_partial != NULL)
7390 {
7391 dtv.v_type = VAR_PARTIAL;
7392 dtv.vval.v_partial = cq->cq_partial;
7393 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7394 }
7395 if (ch->ch_part[part].ch_partial != NULL)
7396 {
7397 dtv.v_type = VAR_PARTIAL;
7398 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7399 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7400 }
7401 }
7402 if (ch->ch_partial != NULL)
7403 {
7404 dtv.v_type = VAR_PARTIAL;
7405 dtv.vval.v_partial = ch->ch_partial;
7406 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7407 }
7408 if (ch->ch_close_partial != NULL)
7409 {
7410 dtv.v_type = VAR_PARTIAL;
7411 dtv.vval.v_partial = ch->ch_close_partial;
7412 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7413 }
7414 }
7415 }
7416#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007417 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007418}
7419
7420/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 * Allocate an empty header for a dictionary.
7422 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007423 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007424dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425{
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007430 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007431 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007432 if (first_dict != NULL)
7433 first_dict->dv_used_prev = d;
7434 d->dv_used_next = first_dict;
7435 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007436 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007437
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007439 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007440 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007441 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007442 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007443 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007444 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445}
7446
7447/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007448 * Allocate an empty dict for a return value.
7449 * Returns OK or FAIL.
7450 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007451 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007452rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007453{
7454 dict_T *d = dict_alloc();
7455
7456 if (d == NULL)
7457 return FAIL;
7458
7459 rettv->vval.v_dict = d;
7460 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007461 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007462 ++d->dv_refcount;
7463 return OK;
7464}
7465
7466
7467/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468 * Unreference a Dictionary: decrement the reference count and free it when it
7469 * becomes zero.
7470 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007471 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007472dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007474 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007475 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476}
7477
7478/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007479 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480 * Ignores the reference count.
7481 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007482 static void
7483dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007487 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007489 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007490 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007491 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 if (!HASHITEM_EMPTY(hi))
7495 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007496 /* Remove the item before deleting it, just in case there is
7497 * something recursive causing trouble. */
7498 di = HI2DI(hi);
7499 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007500 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007501 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 --todo;
7503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007505 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007506}
7507
7508 static void
7509dict_free_dict(dict_T *d)
7510{
7511 /* Remove the dict from the list of dicts for garbage collection. */
7512 if (d->dv_used_prev == NULL)
7513 first_dict = d->dv_used_next;
7514 else
7515 d->dv_used_prev->dv_used_next = d->dv_used_next;
7516 if (d->dv_used_next != NULL)
7517 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 vim_free(d);
7519}
7520
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007521 void
7522dict_free(dict_T *d)
7523{
7524 if (!in_free_unref_items)
7525 {
7526 dict_free_contents(d);
7527 dict_free_dict(d);
7528 }
7529}
7530
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531/*
7532 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 * The "key" is copied to the new item.
7534 * Note that the value of the item "di_tv" still needs to be initialized!
7535 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007537 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007538dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539{
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007542 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007544 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007546 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007547 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007548 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549}
7550
7551/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 * Make a copy of a Dictionary item.
7553 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007555dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007556{
Bram Moolenaar33570922005-01-25 22:26:29 +00007557 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007558
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007559 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7560 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007561 if (di != NULL)
7562 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007564 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007565 copy_tv(&org->di_tv, &di->di_tv);
7566 }
7567 return di;
7568}
7569
7570/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007571 * Remove item "item" from Dictionary "dict" and free it.
7572 */
7573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007574dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575{
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007579 if (HASHITEM_EMPTY(hi))
7580 EMSG2(_(e_intern2), "dictitem_remove()");
7581 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007583 dictitem_free(item);
7584}
7585
7586/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 * Free a dict item. Also clears the value.
7588 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007590dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007591{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007593 if (item->di_flags & DI_FLAGS_ALLOC)
7594 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595}
7596
7597/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007598 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7599 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007600 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007601 * Returns NULL when out of memory.
7602 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007603 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007604dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007605{
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 dict_T *copy;
7607 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007608 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007610
7611 if (orig == NULL)
7612 return NULL;
7613
7614 copy = dict_alloc();
7615 if (copy != NULL)
7616 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007617 if (copyID != 0)
7618 {
7619 orig->dv_copyID = copyID;
7620 orig->dv_copydict = copy;
7621 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007622 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007623 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007624 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007625 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007626 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 --todo;
7628
7629 di = dictitem_alloc(hi->hi_key);
7630 if (di == NULL)
7631 break;
7632 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007633 {
7634 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7635 copyID) == FAIL)
7636 {
7637 vim_free(di);
7638 break;
7639 }
7640 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007641 else
7642 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7643 if (dict_add(copy, di) == FAIL)
7644 {
7645 dictitem_free(di);
7646 break;
7647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007649 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007650
Bram Moolenaare9a41262005-01-15 22:18:47 +00007651 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007652 if (todo > 0)
7653 {
7654 dict_unref(copy);
7655 copy = NULL;
7656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 }
7658
7659 return copy;
7660}
7661
7662/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007664 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007667dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668{
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670}
7671
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007673 * Add a number or string entry to dictionary "d".
7674 * When "str" is NULL use number "nr", otherwise use "str".
7675 * Returns FAIL when out of memory and when key already exists.
7676 */
7677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007678dict_add_nr_str(
7679 dict_T *d,
7680 char *key,
7681 long nr,
7682 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007683{
7684 dictitem_T *item;
7685
7686 item = dictitem_alloc((char_u *)key);
7687 if (item == NULL)
7688 return FAIL;
7689 item->di_tv.v_lock = 0;
7690 if (str == NULL)
7691 {
7692 item->di_tv.v_type = VAR_NUMBER;
7693 item->di_tv.vval.v_number = nr;
7694 }
7695 else
7696 {
7697 item->di_tv.v_type = VAR_STRING;
7698 item->di_tv.vval.v_string = vim_strsave(str);
7699 }
7700 if (dict_add(d, item) == FAIL)
7701 {
7702 dictitem_free(item);
7703 return FAIL;
7704 }
7705 return OK;
7706}
7707
7708/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007709 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007710 * Returns FAIL when out of memory and when key already exists.
7711 */
7712 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007713dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007714{
7715 dictitem_T *item;
7716
7717 item = dictitem_alloc((char_u *)key);
7718 if (item == NULL)
7719 return FAIL;
7720 item->di_tv.v_lock = 0;
7721 item->di_tv.v_type = VAR_LIST;
7722 item->di_tv.vval.v_list = list;
7723 if (dict_add(d, item) == FAIL)
7724 {
7725 dictitem_free(item);
7726 return FAIL;
7727 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007728 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007729 return OK;
7730}
7731
7732/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007733 * Get the number of items in a Dictionary.
7734 */
7735 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007736dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007737{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007738 if (d == NULL)
7739 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007740 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741}
7742
7743/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 * Find item "key[len]" in Dictionary "d".
7745 * If "len" is negative use strlen(key).
7746 * Returns NULL when not found.
7747 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007748 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007749dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007751#define AKEYLEN 200
7752 char_u buf[AKEYLEN];
7753 char_u *akey;
7754 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007755 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007757 if (len < 0)
7758 akey = key;
7759 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007760 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007761 tofree = akey = vim_strnsave(key, len);
7762 if (akey == NULL)
7763 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007764 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007765 else
7766 {
7767 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007768 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007769 akey = buf;
7770 }
7771
Bram Moolenaar33570922005-01-25 22:26:29 +00007772 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007773 vim_free(tofree);
7774 if (HASHITEM_EMPTY(hi))
7775 return NULL;
7776 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777}
7778
7779/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007780 * Get a string item from a dictionary.
7781 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007782 * Returns NULL if the entry doesn't exist or out of memory.
7783 */
7784 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007785get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007786{
7787 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007788 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007789
7790 di = dict_find(d, key, -1);
7791 if (di == NULL)
7792 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007793 s = get_tv_string(&di->di_tv);
7794 if (save && s != NULL)
7795 s = vim_strsave(s);
7796 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007797}
7798
7799/*
7800 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007801 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007802 */
7803 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007804get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007805{
7806 dictitem_T *di;
7807
7808 di = dict_find(d, key, -1);
7809 if (di == NULL)
7810 return 0;
7811 return get_tv_number(&di->di_tv);
7812}
7813
7814/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007815 * Return an allocated string with the string representation of a Dictionary.
7816 * May return NULL.
7817 */
7818 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007819dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007820{
7821 garray_T ga;
7822 int first = TRUE;
7823 char_u *tofree;
7824 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007826 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007827 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007828 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007829
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007830 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007831 return NULL;
7832 ga_init2(&ga, (int)sizeof(char), 80);
7833 ga_append(&ga, '{');
7834
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007835 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007836 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007837 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007838 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007839 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007840 --todo;
7841
7842 if (first)
7843 first = FALSE;
7844 else
7845 ga_concat(&ga, (char_u *)", ");
7846
7847 tofree = string_quote(hi->hi_key, FALSE);
7848 if (tofree != NULL)
7849 {
7850 ga_concat(&ga, tofree);
7851 vim_free(tofree);
7852 }
7853 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007854 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007855 if (s != NULL)
7856 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007857 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007858 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007859 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007860 line_breakcheck();
7861
Bram Moolenaar8c711452005-01-14 21:53:12 +00007862 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007863 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007864 if (todo > 0)
7865 {
7866 vim_free(ga.ga_data);
7867 return NULL;
7868 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007869
7870 ga_append(&ga, '}');
7871 ga_append(&ga, NUL);
7872 return (char_u *)ga.ga_data;
7873}
7874
7875/*
7876 * Allocate a variable for a Dictionary and fill it from "*arg".
7877 * Return OK or FAIL. Returns NOTDONE for {expr}.
7878 */
7879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007880get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007881{
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 dict_T *d = NULL;
7883 typval_T tvkey;
7884 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007885 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007887 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007888 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007889
7890 /*
7891 * First check if it's not a curly-braces thing: {expr}.
7892 * Must do this without evaluating, otherwise a function may be called
7893 * twice. Unfortunately this means we need to call eval1() twice for the
7894 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007896 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007897 if (*start != '}')
7898 {
7899 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7900 return FAIL;
7901 if (*start == '}')
7902 return NOTDONE;
7903 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007904
7905 if (evaluate)
7906 {
7907 d = dict_alloc();
7908 if (d == NULL)
7909 return FAIL;
7910 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007911 tvkey.v_type = VAR_UNKNOWN;
7912 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913
7914 *arg = skipwhite(*arg + 1);
7915 while (**arg != '}' && **arg != NUL)
7916 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007917 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007918 goto failret;
7919 if (**arg != ':')
7920 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007921 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007922 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007923 goto failret;
7924 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007925 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007926 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007927 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007928 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007929 {
7930 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007931 clear_tv(&tvkey);
7932 goto failret;
7933 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935
7936 *arg = skipwhite(*arg + 1);
7937 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7938 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007939 if (evaluate)
7940 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 goto failret;
7942 }
7943 if (evaluate)
7944 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007945 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 if (item != NULL)
7947 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007948 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007949 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 clear_tv(&tv);
7951 goto failret;
7952 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007953 item = dictitem_alloc(key);
7954 clear_tv(&tvkey);
7955 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007957 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007958 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007959 if (dict_add(d, item) == FAIL)
7960 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007961 }
7962 }
7963
7964 if (**arg == '}')
7965 break;
7966 if (**arg != ',')
7967 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007968 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007969 goto failret;
7970 }
7971 *arg = skipwhite(*arg + 1);
7972 }
7973
7974 if (**arg != '}')
7975 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007976 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977failret:
7978 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007979 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007980 return FAIL;
7981 }
7982
7983 *arg = skipwhite(*arg + 1);
7984 if (evaluate)
7985 {
7986 rettv->v_type = VAR_DICT;
7987 rettv->vval.v_dict = d;
7988 ++d->dv_refcount;
7989 }
7990
7991 return OK;
7992}
7993
Bram Moolenaar17a13432016-01-24 14:22:10 +01007994 static char *
7995get_var_special_name(int nr)
7996{
7997 switch (nr)
7998 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007999 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008000 case VVAL_TRUE: return "v:true";
8001 case VVAL_NONE: return "v:none";
8002 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008003 }
8004 EMSG2(_(e_intern2), "get_var_special_name()");
8005 return "42";
8006}
8007
Bram Moolenaar8c711452005-01-14 21:53:12 +00008008/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008009 * Return a string with the string representation of a variable.
8010 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008011 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008012 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008013 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008014 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008015 */
8016 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008017echo_string(
8018 typval_T *tv,
8019 char_u **tofree,
8020 char_u *numbuf,
8021 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008022{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008023 static int recurse = 0;
8024 char_u *r = NULL;
8025
Bram Moolenaar33570922005-01-25 22:26:29 +00008026 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008027 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008028 if (!did_echo_string_emsg)
8029 {
8030 /* Only give this message once for a recursive call to avoid
8031 * flooding the user with errors. And stop iterating over lists
8032 * and dicts. */
8033 did_echo_string_emsg = TRUE;
8034 EMSG(_("E724: variable nested too deep for displaying"));
8035 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008036 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008037 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008038 }
8039 ++recurse;
8040
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008041 switch (tv->v_type)
8042 {
8043 case VAR_FUNC:
8044 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008045 r = tv->vval.v_string;
8046 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008047
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008048 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008049 {
8050 partial_T *pt = tv->vval.v_partial;
8051 char_u *fname = string_quote(pt == NULL ? NULL
8052 : pt->pt_name, FALSE);
8053 garray_T ga;
8054 int i;
8055 char_u *tf;
8056
8057 ga_init2(&ga, 1, 100);
8058 ga_concat(&ga, (char_u *)"function(");
8059 if (fname != NULL)
8060 {
8061 ga_concat(&ga, fname);
8062 vim_free(fname);
8063 }
8064 if (pt != NULL && pt->pt_argc > 0)
8065 {
8066 ga_concat(&ga, (char_u *)", [");
8067 for (i = 0; i < pt->pt_argc; ++i)
8068 {
8069 if (i > 0)
8070 ga_concat(&ga, (char_u *)", ");
8071 ga_concat(&ga,
8072 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8073 vim_free(tf);
8074 }
8075 ga_concat(&ga, (char_u *)"]");
8076 }
8077 if (pt != NULL && pt->pt_dict != NULL)
8078 {
8079 typval_T dtv;
8080
8081 ga_concat(&ga, (char_u *)", ");
8082 dtv.v_type = VAR_DICT;
8083 dtv.vval.v_dict = pt->pt_dict;
8084 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8085 vim_free(tf);
8086 }
8087 ga_concat(&ga, (char_u *)")");
8088
8089 *tofree = ga.ga_data;
8090 r = *tofree;
8091 break;
8092 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008093
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008094 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008095 if (tv->vval.v_list == NULL)
8096 {
8097 *tofree = NULL;
8098 r = NULL;
8099 }
8100 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8101 {
8102 *tofree = NULL;
8103 r = (char_u *)"[...]";
8104 }
8105 else
8106 {
8107 tv->vval.v_list->lv_copyID = copyID;
8108 *tofree = list2string(tv, copyID);
8109 r = *tofree;
8110 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008111 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008112
Bram Moolenaar8c711452005-01-14 21:53:12 +00008113 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008114 if (tv->vval.v_dict == NULL)
8115 {
8116 *tofree = NULL;
8117 r = NULL;
8118 }
8119 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8120 {
8121 *tofree = NULL;
8122 r = (char_u *)"{...}";
8123 }
8124 else
8125 {
8126 tv->vval.v_dict->dv_copyID = copyID;
8127 *tofree = dict2string(tv, copyID);
8128 r = *tofree;
8129 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008130 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008131
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008132 case VAR_STRING:
8133 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008134 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008135 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008136 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008137 *tofree = NULL;
8138 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008139 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008140
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008141 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008142#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008143 *tofree = NULL;
8144 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8145 r = numbuf;
8146 break;
8147#endif
8148
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008149 case VAR_SPECIAL:
8150 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008151 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008152 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008153 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008154
Bram Moolenaar8502c702014-06-17 12:51:16 +02008155 if (--recurse == 0)
8156 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008157 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008158}
8159
8160/*
8161 * Return a string with the string representation of a variable.
8162 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8163 * "numbuf" is used for a number.
8164 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008165 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008166 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008167 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008168tv2string(
8169 typval_T *tv,
8170 char_u **tofree,
8171 char_u *numbuf,
8172 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008173{
8174 switch (tv->v_type)
8175 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008176 case VAR_FUNC:
8177 *tofree = string_quote(tv->vval.v_string, TRUE);
8178 return *tofree;
8179 case VAR_STRING:
8180 *tofree = string_quote(tv->vval.v_string, FALSE);
8181 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008183#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008184 *tofree = NULL;
8185 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8186 return numbuf;
8187#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008188 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008189 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008190 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008191 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008192 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008193 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008194 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008195 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008196 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008197 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008198 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008199}
8200
8201/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008202 * Return string "str" in ' quotes, doubling ' characters.
8203 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008204 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008205 */
8206 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008207string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008208{
Bram Moolenaar33570922005-01-25 22:26:29 +00008209 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008210 char_u *p, *r, *s;
8211
Bram Moolenaar33570922005-01-25 22:26:29 +00008212 len = (function ? 13 : 3);
8213 if (str != NULL)
8214 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008215 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008216 for (p = str; *p != NUL; mb_ptr_adv(p))
8217 if (*p == '\'')
8218 ++len;
8219 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008220 s = r = alloc(len);
8221 if (r != NULL)
8222 {
8223 if (function)
8224 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008225 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008226 r += 10;
8227 }
8228 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008229 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008230 if (str != NULL)
8231 for (p = str; *p != NUL; )
8232 {
8233 if (*p == '\'')
8234 *r++ = '\'';
8235 MB_COPY_CHAR(p, r);
8236 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008237 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008238 if (function)
8239 *r++ = ')';
8240 *r++ = NUL;
8241 }
8242 return s;
8243}
8244
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008245#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008246/*
8247 * Convert the string "text" to a floating point number.
8248 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8249 * this always uses a decimal point.
8250 * Returns the length of the text that was consumed.
8251 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008252 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008253string2float(
8254 char_u *text,
8255 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008256{
8257 char *s = (char *)text;
8258 float_T f;
8259
8260 f = strtod(s, &s);
8261 *value = f;
8262 return (int)((char_u *)s - text);
8263}
8264#endif
8265
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 * Get the value of an environment variable.
8268 * "arg" is pointing to the '$'. It is advanced to after the name.
8269 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008270 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 */
8272 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008273get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
8275 char_u *string = NULL;
8276 int len;
8277 int cc;
8278 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008279 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280
8281 ++*arg;
8282 name = *arg;
8283 len = get_env_len(arg);
8284 if (evaluate)
8285 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008286 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008287 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008288
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008289 cc = name[len];
8290 name[len] = NUL;
8291 /* first try vim_getenv(), fast for normal environment vars */
8292 string = vim_getenv(name, &mustfree);
8293 if (string != NULL && *string != NUL)
8294 {
8295 if (!mustfree)
8296 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008298 else
8299 {
8300 if (mustfree)
8301 vim_free(string);
8302
8303 /* next try expanding things like $VIM and ${HOME} */
8304 string = expand_env_save(name - 1);
8305 if (string != NULL && *string == '$')
8306 {
8307 vim_free(string);
8308 string = NULL;
8309 }
8310 }
8311 name[len] = cc;
8312
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008313 rettv->v_type = VAR_STRING;
8314 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 }
8316
8317 return OK;
8318}
8319
8320/*
8321 * Array with names and number of arguments of all internal functions
8322 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8323 */
8324static struct fst
8325{
8326 char *f_name; /* function name */
8327 char f_min_argc; /* minimal number of arguments */
8328 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008329 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008330 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331} functions[] =
8332{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008333#ifdef FEAT_FLOAT
8334 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008335 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008336#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008337 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008338 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008339 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {"append", 2, 2, f_append},
8341 {"argc", 0, 0, f_argc},
8342 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008343 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008344 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008345#ifdef FEAT_FLOAT
8346 {"asin", 1, 1, f_asin}, /* WJMc */
8347#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008348 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008349 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008350 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008351 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008352 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008353 {"assert_notequal", 2, 3, f_assert_notequal},
8354 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008355 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008356#ifdef FEAT_FLOAT
8357 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008358 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008361 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"bufexists", 1, 1, f_bufexists},
8363 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8364 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8365 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8366 {"buflisted", 1, 1, f_buflisted},
8367 {"bufloaded", 1, 1, f_bufloaded},
8368 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008369 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {"bufwinnr", 1, 1, f_bufwinnr},
8371 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008372 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008373 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008374 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008375#ifdef FEAT_FLOAT
8376 {"ceil", 1, 1, f_ceil},
8377#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008378#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008379 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008380 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8381 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008382 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008383 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008384 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008385 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008386 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008387 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008388 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008389 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008390 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8391 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008392 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008393 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008394#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008395 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008396 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008398 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008400#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008401 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008402 {"complete_add", 1, 1, f_complete_add},
8403 {"complete_check", 0, 0, f_complete_check},
8404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008406 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008407#ifdef FEAT_FLOAT
8408 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008409 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008410#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008411 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008413 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008414 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008415 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008417 {"diff_filler", 1, 1, f_diff_filler},
8418 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008419 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008420 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008422 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"eventhandler", 0, 0, f_eventhandler},
8424 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008425 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008427#ifdef FEAT_FLOAT
8428 {"exp", 1, 1, f_exp},
8429#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008430 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008431 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008432 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8434 {"filereadable", 1, 1, f_filereadable},
8435 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008436 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008437 {"finddir", 1, 3, f_finddir},
8438 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008439#ifdef FEAT_FLOAT
8440 {"float2nr", 1, 1, f_float2nr},
8441 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008442 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008443#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008444 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"fnamemodify", 2, 2, f_fnamemodify},
8446 {"foldclosed", 1, 1, f_foldclosed},
8447 {"foldclosedend", 1, 1, f_foldclosedend},
8448 {"foldlevel", 1, 1, f_foldlevel},
8449 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008450 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008452 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008453 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008454 {"garbagecollect_for_testing", 0, 0, f_garbagecollect_for_testing},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008455 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008456 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008457 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {"getchar", 0, 1, f_getchar},
8459 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008460 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {"getcmdline", 0, 0, f_getcmdline},
8462 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008463 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008464 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008465 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008466 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008467 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008468 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {"getfsize", 1, 1, f_getfsize},
8470 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008471 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008472 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008473 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008474 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008475 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008476 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008477 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008478 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008480 {"gettabvar", 2, 3, f_gettabvar},
8481 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {"getwinposx", 0, 0, f_getwinposx},
8483 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008484 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008485 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008486 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008487 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008489 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008490 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008491 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8493 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8494 {"histadd", 2, 2, f_histadd},
8495 {"histdel", 1, 2, f_histdel},
8496 {"histget", 1, 2, f_histget},
8497 {"histnr", 1, 1, f_histnr},
8498 {"hlID", 1, 1, f_hlID},
8499 {"hlexists", 1, 1, f_hlexists},
8500 {"hostname", 0, 0, f_hostname},
8501 {"iconv", 3, 3, f_iconv},
8502 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008503 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008504 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008506 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 {"inputrestore", 0, 0, f_inputrestore},
8508 {"inputsave", 0, 0, f_inputsave},
8509 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008510 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008511 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008513 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008514#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8515 {"isnan", 1, 1, f_isnan},
8516#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008517 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008518#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008519 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008520 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008521 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008522 {"job_start", 1, 2, f_job_start},
8523 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008524 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008525#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008526 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008527 {"js_decode", 1, 1, f_js_decode},
8528 {"js_encode", 1, 1, f_js_encode},
8529 {"json_decode", 1, 1, f_json_decode},
8530 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008531 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008533 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 {"libcall", 3, 3, f_libcall},
8535 {"libcallnr", 3, 3, f_libcallnr},
8536 {"line", 1, 1, f_line},
8537 {"line2byte", 1, 1, f_line2byte},
8538 {"lispindent", 1, 1, f_lispindent},
8539 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008540#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008541 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008542 {"log10", 1, 1, f_log10},
8543#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008544#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008545 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008546#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008547 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008548 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008549 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008550 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008551 {"matchadd", 2, 5, f_matchadd},
8552 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008553 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008554 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008555 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008556 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008557 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008558 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008559 {"max", 1, 1, f_max},
8560 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008561#ifdef vim_mkdir
8562 {"mkdir", 1, 3, f_mkdir},
8563#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008564 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008565#ifdef FEAT_MZSCHEME
8566 {"mzeval", 1, 1, f_mzeval},
8567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008569 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008570 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008571 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008572#ifdef FEAT_PERL
8573 {"perleval", 1, 1, f_perleval},
8574#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008575#ifdef FEAT_FLOAT
8576 {"pow", 2, 2, f_pow},
8577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008579 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008580 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008581#ifdef FEAT_PYTHON3
8582 {"py3eval", 1, 1, f_py3eval},
8583#endif
8584#ifdef FEAT_PYTHON
8585 {"pyeval", 1, 1, f_pyeval},
8586#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008587 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008588 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008589 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008590#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008591 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008592#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008593 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 {"remote_expr", 2, 3, f_remote_expr},
8595 {"remote_foreground", 1, 1, f_remote_foreground},
8596 {"remote_peek", 1, 2, f_remote_peek},
8597 {"remote_read", 1, 1, f_remote_read},
8598 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008599 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008601 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008603 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008604#ifdef FEAT_FLOAT
8605 {"round", 1, 1, f_round},
8606#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008607 {"screenattr", 2, 2, f_screenattr},
8608 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008609 {"screencol", 0, 0, f_screencol},
8610 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008611 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008612 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008613 {"searchpair", 3, 7, f_searchpair},
8614 {"searchpairpos", 3, 7, f_searchpairpos},
8615 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {"server2client", 2, 2, f_server2client},
8617 {"serverlist", 0, 0, f_serverlist},
8618 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008619 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008621 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008623 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008624 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008625 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008626 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008628 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008629 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008631#ifdef FEAT_CRYPT
8632 {"sha256", 1, 1, f_sha256},
8633#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008634 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008635 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008637#ifdef FEAT_FLOAT
8638 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008639 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008640#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008641 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008642 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008643 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008644 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008645 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008646#ifdef FEAT_FLOAT
8647 {"sqrt", 1, 1, f_sqrt},
8648 {"str2float", 1, 1, f_str2float},
8649#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008650 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008651 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008652 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008653 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654#ifdef HAVE_STRFTIME
8655 {"strftime", 1, 2, f_strftime},
8656#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008657 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 {"strlen", 1, 1, f_strlen},
8661 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008662 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008664 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008665 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 {"substitute", 4, 4, f_substitute},
8667 {"synID", 3, 3, f_synID},
8668 {"synIDattr", 2, 3, f_synIDattr},
8669 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008670 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008671 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008672 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008673 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008674 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008675 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008676 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008677 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008678 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008679#ifdef FEAT_FLOAT
8680 {"tan", 1, 1, f_tan},
8681 {"tanh", 1, 1, f_tanh},
8682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008684 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008685#ifdef FEAT_TIMERS
8686 {"timer_start", 2, 3, f_timer_start},
8687 {"timer_stop", 1, 1, f_timer_stop},
8688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 {"tolower", 1, 1, f_tolower},
8690 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008691 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008692#ifdef FEAT_FLOAT
8693 {"trunc", 1, 1, f_trunc},
8694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008696 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008697 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008698 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008699 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 {"virtcol", 1, 1, f_virtcol},
8701 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008702 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008703 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008704 {"win_getid", 0, 2, f_win_getid},
8705 {"win_gotoid", 1, 1, f_win_gotoid},
8706 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8707 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {"winbufnr", 1, 1, f_winbufnr},
8709 {"wincol", 0, 0, f_wincol},
8710 {"winheight", 1, 1, f_winheight},
8711 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008712 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008714 {"winrestview", 1, 1, f_winrestview},
8715 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008717 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008718 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008719 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720};
8721
8722#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8723
8724/*
8725 * Function given to ExpandGeneric() to obtain the list of internal
8726 * or user defined function names.
8727 */
8728 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008729get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 static int intidx = -1;
8732 char_u *name;
8733
8734 if (idx == 0)
8735 intidx = -1;
8736 if (intidx < 0)
8737 {
8738 name = get_user_func_name(xp, idx);
8739 if (name != NULL)
8740 return name;
8741 }
8742 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8743 {
8744 STRCPY(IObuff, functions[intidx].f_name);
8745 STRCAT(IObuff, "(");
8746 if (functions[intidx].f_max_argc == 0)
8747 STRCAT(IObuff, ")");
8748 return IObuff;
8749 }
8750
8751 return NULL;
8752}
8753
8754/*
8755 * Function given to ExpandGeneric() to obtain the list of internal or
8756 * user defined variable or function names.
8757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008759get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
8761 static int intidx = -1;
8762 char_u *name;
8763
8764 if (idx == 0)
8765 intidx = -1;
8766 if (intidx < 0)
8767 {
8768 name = get_function_name(xp, idx);
8769 if (name != NULL)
8770 return name;
8771 }
8772 return get_user_var_name(xp, ++intidx);
8773}
8774
8775#endif /* FEAT_CMDL_COMPL */
8776
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008777#if defined(EBCDIC) || defined(PROTO)
8778/*
8779 * Compare struct fst by function name.
8780 */
8781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008782compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008783{
8784 struct fst *p1 = (struct fst *)s1;
8785 struct fst *p2 = (struct fst *)s2;
8786
8787 return STRCMP(p1->f_name, p2->f_name);
8788}
8789
8790/*
8791 * Sort the function table by function name.
8792 * The sorting of the table above is ASCII dependant.
8793 * On machines using EBCDIC we have to sort it.
8794 */
8795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008796sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008797{
8798 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8799
8800 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8801}
8802#endif
8803
8804
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805/*
8806 * Find internal function in table above.
8807 * Return index, or -1 if not found
8808 */
8809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008810find_internal_func(
8811 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812{
8813 int first = 0;
8814 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8815 int cmp;
8816 int x;
8817
8818 /*
8819 * Find the function name in the table. Binary search.
8820 */
8821 while (first <= last)
8822 {
8823 x = first + ((unsigned)(last - first) >> 1);
8824 cmp = STRCMP(name, functions[x].f_name);
8825 if (cmp < 0)
8826 last = x - 1;
8827 else if (cmp > 0)
8828 first = x + 1;
8829 else
8830 return x;
8831 }
8832 return -1;
8833}
8834
8835/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008836 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8837 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008838 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8839 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008840 */
8841 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008842deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008843{
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008845 int cc;
8846
Bram Moolenaar65639032016-03-16 21:40:30 +01008847 if (partialp != NULL)
8848 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008849
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008850 cc = name[*lenp];
8851 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008852 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008853 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008854 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008855 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008857 {
8858 *lenp = 0;
8859 return (char_u *)""; /* just in case */
8860 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008861 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008863 }
8864
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008865 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8866 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008867 partial_T *pt = v->di_tv.vval.v_partial;
8868
8869 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008870 {
8871 *lenp = 0;
8872 return (char_u *)""; /* just in case */
8873 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008874 if (partialp != NULL)
8875 *partialp = pt;
8876 *lenp = (int)STRLEN(pt->pt_name);
8877 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008878 }
8879
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008880 return name;
8881}
8882
8883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 * Allocate a variable for the result of a function.
8885 * Return OK or FAIL.
8886 */
8887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008888get_func_tv(
8889 char_u *name, /* name of the function */
8890 int len, /* length of "name" */
8891 typval_T *rettv,
8892 char_u **arg, /* argument, pointing to the '(' */
8893 linenr_T firstline, /* first line of range */
8894 linenr_T lastline, /* last line of range */
8895 int *doesrange, /* return: function handled range */
8896 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008897 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008898 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900 char_u *argp;
8901 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008902 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 int argcount = 0; /* number of arguments found */
8904
8905 /*
8906 * Get the arguments.
8907 */
8908 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008909 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 {
8911 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8912 if (*argp == ')' || *argp == ',' || *argp == NUL)
8913 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8915 {
8916 ret = FAIL;
8917 break;
8918 }
8919 ++argcount;
8920 if (*argp != ',')
8921 break;
8922 }
8923 if (*argp == ')')
8924 ++argp;
8925 else
8926 ret = FAIL;
8927
8928 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008929 {
8930 int i = 0;
8931
8932 if (get_vim_var_nr(VV_TESTING))
8933 {
8934 /* Prepare for calling garbagecollect_for_testing(), need to know
8935 * what variables are used on the call stack. */
8936 if (funcargs.ga_itemsize == 0)
8937 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8938 for (i = 0; i < argcount; ++i)
8939 if (ga_grow(&funcargs, 1) == OK)
8940 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8941 &argvars[i];
8942 }
8943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008945 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008946
8947 funcargs.ga_len -= i;
8948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008950 {
8951 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008952 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008954 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956
8957 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
8960 *arg = skipwhite(argp);
8961 return ret;
8962}
8963
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008964#define ERROR_UNKNOWN 0
8965#define ERROR_TOOMANY 1
8966#define ERROR_TOOFEW 2
8967#define ERROR_SCRIPT 3
8968#define ERROR_DICT 4
8969#define ERROR_NONE 5
8970#define ERROR_OTHER 6
8971#define FLEN_FIXED 40
8972
8973/*
8974 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8975 * Change <SNR>123_name() to K_SNR 123_name().
8976 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8977 * (slow).
8978 */
8979 static char_u *
8980fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8981{
8982 int llen;
8983 char_u *fname;
8984 int i;
8985
8986 llen = eval_fname_script(name);
8987 if (llen > 0)
8988 {
8989 fname_buf[0] = K_SPECIAL;
8990 fname_buf[1] = KS_EXTRA;
8991 fname_buf[2] = (int)KE_SNR;
8992 i = 3;
8993 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8994 {
8995 if (current_SID <= 0)
8996 *error = ERROR_SCRIPT;
8997 else
8998 {
8999 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9000 i = (int)STRLEN(fname_buf);
9001 }
9002 }
9003 if (i + STRLEN(name + llen) < FLEN_FIXED)
9004 {
9005 STRCPY(fname_buf + i, name + llen);
9006 fname = fname_buf;
9007 }
9008 else
9009 {
9010 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9011 if (fname == NULL)
9012 *error = ERROR_OTHER;
9013 else
9014 {
9015 *tofree = fname;
9016 mch_memmove(fname, fname_buf, (size_t)i);
9017 STRCPY(fname + i, name + llen);
9018 }
9019 }
9020 }
9021 else
9022 fname = name;
9023 return fname;
9024}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025
9026/*
9027 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009028 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009029 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009031 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009032call_func(
9033 char_u *funcname, /* name of the function */
9034 int len, /* length of "name" */
9035 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009036 int argcount_in, /* number of "argvars" */
9037 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009038 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009039 linenr_T firstline, /* first line of range */
9040 linenr_T lastline, /* last line of range */
9041 int *doesrange, /* return: function handled range */
9042 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009043 partial_T *partial, /* optional, can be NULL */
9044 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
9046 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 int error = ERROR_NONE;
9048 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009051 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009053 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009054 int argcount = argcount_in;
9055 typval_T *argvars = argvars_in;
9056 dict_T *selfdict = selfdict_in;
9057 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9058 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009059
9060 /* Make a copy of the name, if it comes from a funcref variable it could
9061 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009062 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009063 if (name == NULL)
9064 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009066 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067
9068 *doesrange = FALSE;
9069
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009070 if (partial != NULL)
9071 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009072 /* When the function has a partial with a dict and there is a dict
9073 * argument, use the dict argument. That is backwards compatible.
9074 * When the dict was bound explicitly use the one from the partial. */
9075 if (partial->pt_dict != NULL
9076 && (selfdict_in == NULL || !partial->pt_auto))
9077 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009078 if (error == ERROR_NONE && partial->pt_argc > 0)
9079 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009080 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9081 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9082 for (i = 0; i < argcount_in; ++i)
9083 argv[i + argv_clear] = argvars_in[i];
9084 argvars = argv;
9085 argcount = partial->pt_argc + argcount_in;
9086 }
9087 }
9088
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
9090 /* execute the function if no errors detected and executing */
9091 if (evaluate && error == ERROR_NONE)
9092 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009093 char_u *rfname = fname;
9094
9095 /* Ignore "g:" before a function name. */
9096 if (fname[0] == 'g' && fname[1] == ':')
9097 rfname = fname + 2;
9098
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009099 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9100 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 error = ERROR_UNKNOWN;
9102
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009103 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 {
9105 /*
9106 * User defined function.
9107 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009108 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009109
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009111 /* Trigger FuncUndefined event, may load the function. */
9112 if (fp == NULL
9113 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009114 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009115 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009117 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009118 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 }
9120#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009121 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009122 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009123 {
9124 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009125 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009126 }
9127
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 if (fp != NULL)
9129 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009130 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009132 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009134 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009136 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009137 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 else
9139 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009140 int did_save_redo = FALSE;
9141
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 /*
9143 * Call the user function.
9144 * Save and restore search patterns, script variables and
9145 * redo buffer.
9146 */
9147 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009148#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009149 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009150#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009151 {
9152 saveRedobuff();
9153 did_save_redo = TRUE;
9154 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009155 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009157 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009158 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9159 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9160 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009161 /* Function was unreferenced while being used, free it
9162 * now. */
9163 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009164 if (did_save_redo)
9165 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 restore_search_patterns();
9167 error = ERROR_NONE;
9168 }
9169 }
9170 }
9171 else
9172 {
9173 /*
9174 * Find the function name in the table, call its implementation.
9175 */
9176 i = find_internal_func(fname);
9177 if (i >= 0)
9178 {
9179 if (argcount < functions[i].f_min_argc)
9180 error = ERROR_TOOFEW;
9181 else if (argcount > functions[i].f_max_argc)
9182 error = ERROR_TOOMANY;
9183 else
9184 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009185 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 error = ERROR_NONE;
9188 }
9189 }
9190 }
9191 /*
9192 * The function call (or "FuncUndefined" autocommand sequence) might
9193 * have been aborted by an error, an interrupt, or an explicitly thrown
9194 * exception that has not been caught so far. This situation can be
9195 * tested for by calling aborting(). For an error in an internal
9196 * function or for the "E132" error in call_user_func(), however, the
9197 * throw point at which the "force_abort" flag (temporarily reset by
9198 * emsg()) is normally updated has not been reached yet. We need to
9199 * update that flag first to make aborting() reliable.
9200 */
9201 update_force_abort();
9202 }
9203 if (error == ERROR_NONE)
9204 ret = OK;
9205
9206 /*
9207 * Report an error unless the argument evaluation or function call has been
9208 * cancelled due to an aborting error, an interrupt, or an exception.
9209 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009210 if (!aborting())
9211 {
9212 switch (error)
9213 {
9214 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009215 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009216 break;
9217 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009218 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009219 break;
9220 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009221 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009222 name);
9223 break;
9224 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009225 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009226 name);
9227 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009228 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009229 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009230 name);
9231 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009232 }
9233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009235 while (argv_clear > 0)
9236 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009237 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009238 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239
9240 return ret;
9241}
9242
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009243/*
9244 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009245 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009246 */
9247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009248emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009249{
9250 char_u *p;
9251
9252 if (*name == K_SPECIAL)
9253 p = concat_str((char_u *)"<SNR>", name + 3);
9254 else
9255 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009256 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009257 if (p != name)
9258 vim_free(p);
9259}
9260
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009261/*
9262 * Return TRUE for a non-zero Number and a non-empty String.
9263 */
9264 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009265non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009266{
9267 return ((argvars[0].v_type == VAR_NUMBER
9268 && argvars[0].vval.v_number != 0)
9269 || (argvars[0].v_type == VAR_STRING
9270 && argvars[0].vval.v_string != NULL
9271 && *argvars[0].vval.v_string != NUL));
9272}
9273
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274/*********************************************
9275 * Implementation of the built-in functions
9276 */
9277
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009278#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009279static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009280
9281/*
9282 * Get the float value of "argvars[0]" into "f".
9283 * Returns FAIL when the argument is not a Number or Float.
9284 */
9285 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009286get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009287{
9288 if (argvars[0].v_type == VAR_FLOAT)
9289 {
9290 *f = argvars[0].vval.v_float;
9291 return OK;
9292 }
9293 if (argvars[0].v_type == VAR_NUMBER)
9294 {
9295 *f = (float_T)argvars[0].vval.v_number;
9296 return OK;
9297 }
9298 EMSG(_("E808: Number or Float required"));
9299 return FAIL;
9300}
9301
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009302/*
9303 * "abs(expr)" function
9304 */
9305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009306f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009307{
9308 if (argvars[0].v_type == VAR_FLOAT)
9309 {
9310 rettv->v_type = VAR_FLOAT;
9311 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9312 }
9313 else
9314 {
9315 varnumber_T n;
9316 int error = FALSE;
9317
9318 n = get_tv_number_chk(&argvars[0], &error);
9319 if (error)
9320 rettv->vval.v_number = -1;
9321 else if (n > 0)
9322 rettv->vval.v_number = n;
9323 else
9324 rettv->vval.v_number = -n;
9325 }
9326}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009327
9328/*
9329 * "acos()" function
9330 */
9331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009332f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009333{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009334 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009335
9336 rettv->v_type = VAR_FLOAT;
9337 if (get_float_arg(argvars, &f) == OK)
9338 rettv->vval.v_float = acos(f);
9339 else
9340 rettv->vval.v_float = 0.0;
9341}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009342#endif
9343
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009345 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 */
9347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009348f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349{
Bram Moolenaar33570922005-01-25 22:26:29 +00009350 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009353 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009355 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009356 && !tv_check_lock(l->lv_lock,
9357 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009358 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009359 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009360 }
9361 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009362 EMSG(_(e_listreq));
9363}
9364
9365/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009366 * "alloc_fail(id, countdown, repeat)" function
9367 */
9368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009369f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009370{
9371 if (argvars[0].v_type != VAR_NUMBER
9372 || argvars[0].vval.v_number <= 0
9373 || argvars[1].v_type != VAR_NUMBER
9374 || argvars[1].vval.v_number < 0
9375 || argvars[2].v_type != VAR_NUMBER)
9376 EMSG(_(e_invarg));
9377 else
9378 {
9379 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009380 if (alloc_fail_id >= aid_last)
9381 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009382 alloc_fail_countdown = argvars[1].vval.v_number;
9383 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009384 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009385 }
9386}
9387
9388/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009389 * "and(expr, expr)" function
9390 */
9391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009392f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009393{
9394 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9395 & get_tv_number_chk(&argvars[1], NULL);
9396}
9397
9398/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009399 * "append(lnum, string/list)" function
9400 */
9401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009402f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009403{
9404 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009405 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009406 list_T *l = NULL;
9407 listitem_T *li = NULL;
9408 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009409 long added = 0;
9410
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009411 /* When coming here from Insert mode, sync undo, so that this can be
9412 * undone separately from what was previously inserted. */
9413 if (u_sync_once == 2)
9414 {
9415 u_sync_once = 1; /* notify that u_sync() was called */
9416 u_sync(TRUE);
9417 }
9418
Bram Moolenaar0d660222005-01-07 21:51:51 +00009419 lnum = get_tv_lnum(argvars);
9420 if (lnum >= 0
9421 && lnum <= curbuf->b_ml.ml_line_count
9422 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009423 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009424 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009425 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009426 l = argvars[1].vval.v_list;
9427 if (l == NULL)
9428 return;
9429 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009430 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009431 for (;;)
9432 {
9433 if (l == NULL)
9434 tv = &argvars[1]; /* append a string */
9435 else if (li == NULL)
9436 break; /* end of list */
9437 else
9438 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009439 line = get_tv_string_chk(tv);
9440 if (line == NULL) /* type error */
9441 {
9442 rettv->vval.v_number = 1; /* Failed */
9443 break;
9444 }
9445 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009446 ++added;
9447 if (l == NULL)
9448 break;
9449 li = li->li_next;
9450 }
9451
9452 appended_lines_mark(lnum, added);
9453 if (curwin->w_cursor.lnum > lnum)
9454 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009456 else
9457 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458}
9459
9460/*
9461 * "argc()" function
9462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009464f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467}
9468
9469/*
9470 * "argidx()" function
9471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009473f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009475 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476}
9477
9478/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009479 * "arglistid()" function
9480 */
9481 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009482f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009483{
9484 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009485
9486 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009487 wp = find_tabwin(&argvars[0], &argvars[1]);
9488 if (wp != NULL)
9489 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009490}
9491
9492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 * "argv(nr)" function
9494 */
9495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009496f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497{
9498 int idx;
9499
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009500 if (argvars[0].v_type != VAR_UNKNOWN)
9501 {
9502 idx = get_tv_number_chk(&argvars[0], NULL);
9503 if (idx >= 0 && idx < ARGCOUNT)
9504 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9505 else
9506 rettv->vval.v_string = NULL;
9507 rettv->v_type = VAR_STRING;
9508 }
9509 else if (rettv_list_alloc(rettv) == OK)
9510 for (idx = 0; idx < ARGCOUNT; ++idx)
9511 list_append_string(rettv->vval.v_list,
9512 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513}
9514
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009515typedef enum
9516{
9517 ASSERT_EQUAL,
9518 ASSERT_NOTEQUAL,
9519 ASSERT_MATCH,
9520 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009521 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009522} assert_type_T;
9523
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009524static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009525static 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 +01009526static void assert_error(garray_T *gap);
9527static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009528
9529/*
9530 * Prepare "gap" for an assert error and add the sourcing position.
9531 */
9532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009533prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009534{
9535 char buf[NUMBUFLEN];
9536
9537 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009538 if (sourcing_name != NULL)
9539 {
9540 ga_concat(gap, sourcing_name);
9541 if (sourcing_lnum > 0)
9542 ga_concat(gap, (char_u *)" ");
9543 }
9544 if (sourcing_lnum > 0)
9545 {
9546 sprintf(buf, "line %ld", (long)sourcing_lnum);
9547 ga_concat(gap, (char_u *)buf);
9548 }
9549 if (sourcing_name != NULL || sourcing_lnum > 0)
9550 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009551}
9552
9553/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009554 * Append "str" to "gap", escaping unprintable characters.
9555 * Changes NL to \n, CR to \r, etc.
9556 */
9557 static void
9558ga_concat_esc(garray_T *gap, char_u *str)
9559{
9560 char_u *p;
9561 char_u buf[NUMBUFLEN];
9562
Bram Moolenaarf1551962016-03-15 12:55:58 +01009563 if (str == NULL)
9564 {
9565 ga_concat(gap, (char_u *)"NULL");
9566 return;
9567 }
9568
Bram Moolenaar23689172016-02-15 22:37:37 +01009569 for (p = str; *p != NUL; ++p)
9570 switch (*p)
9571 {
9572 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9573 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9574 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9575 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9576 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9577 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9578 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9579 default:
9580 if (*p < ' ')
9581 {
9582 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9583 ga_concat(gap, buf);
9584 }
9585 else
9586 ga_append(gap, *p);
9587 break;
9588 }
9589}
9590
9591/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009592 * Fill "gap" with information about an assert error.
9593 */
9594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009595fill_assert_error(
9596 garray_T *gap,
9597 typval_T *opt_msg_tv,
9598 char_u *exp_str,
9599 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009600 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009601 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009602{
9603 char_u numbuf[NUMBUFLEN];
9604 char_u *tofree;
9605
9606 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9607 {
9608 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9609 vim_free(tofree);
9610 }
9611 else
9612 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009613 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009614 ga_concat(gap, (char_u *)"Pattern ");
9615 else
9616 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009617 if (exp_str == NULL)
9618 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009619 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009620 vim_free(tofree);
9621 }
9622 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009623 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009624 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009625 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009626 else if (atype == ASSERT_NOTMATCH)
9627 ga_concat(gap, (char_u *)" does match ");
9628 else if (atype == ASSERT_NOTEQUAL)
9629 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009630 else
9631 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009632 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009633 vim_free(tofree);
9634 }
9635}
Bram Moolenaar43345542015-11-29 17:35:35 +01009636
9637/*
9638 * Add an assert error to v:errors.
9639 */
9640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009641assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009642{
9643 struct vimvar *vp = &vimvars[VV_ERRORS];
9644
9645 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9646 /* Make sure v:errors is a list. */
9647 set_vim_var_list(VV_ERRORS, list_alloc());
9648 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9649}
9650
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009651 static void
9652assert_equal_common(typval_T *argvars, assert_type_T atype)
9653{
9654 garray_T ga;
9655
9656 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9657 != (atype == ASSERT_EQUAL))
9658 {
9659 prepare_assert_error(&ga);
9660 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9661 atype);
9662 assert_error(&ga);
9663 ga_clear(&ga);
9664 }
9665}
9666
Bram Moolenaar43345542015-11-29 17:35:35 +01009667/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009668 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009669 */
9670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009671f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009672{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009673 assert_equal_common(argvars, ASSERT_EQUAL);
9674}
Bram Moolenaar43345542015-11-29 17:35:35 +01009675
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009676/*
9677 * "assert_notequal(expected, actual[, msg])" function
9678 */
9679 static void
9680f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9681{
9682 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009683}
9684
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009685/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009686 * "assert_exception(string[, msg])" function
9687 */
9688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009689f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009690{
9691 garray_T ga;
9692 char *error;
9693
9694 error = (char *)get_tv_string_chk(&argvars[0]);
9695 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9696 {
9697 prepare_assert_error(&ga);
9698 ga_concat(&ga, (char_u *)"v:exception is not set");
9699 assert_error(&ga);
9700 ga_clear(&ga);
9701 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009702 else if (error != NULL
9703 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009704 {
9705 prepare_assert_error(&ga);
9706 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009707 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009708 assert_error(&ga);
9709 ga_clear(&ga);
9710 }
9711}
9712
9713/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009714 * "assert_fails(cmd [, error])" function
9715 */
9716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009717f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009718{
9719 char_u *cmd = get_tv_string_chk(&argvars[0]);
9720 garray_T ga;
9721
9722 called_emsg = FALSE;
9723 suppress_errthrow = TRUE;
9724 emsg_silent = TRUE;
9725 do_cmdline_cmd(cmd);
9726 if (!called_emsg)
9727 {
9728 prepare_assert_error(&ga);
9729 ga_concat(&ga, (char_u *)"command did not fail: ");
9730 ga_concat(&ga, cmd);
9731 assert_error(&ga);
9732 ga_clear(&ga);
9733 }
9734 else if (argvars[1].v_type != VAR_UNKNOWN)
9735 {
9736 char_u buf[NUMBUFLEN];
9737 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9738
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009739 if (error == NULL
9740 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009741 {
9742 prepare_assert_error(&ga);
9743 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009744 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009745 assert_error(&ga);
9746 ga_clear(&ga);
9747 }
9748 }
9749
9750 called_emsg = FALSE;
9751 suppress_errthrow = FALSE;
9752 emsg_silent = FALSE;
9753 emsg_on_display = FALSE;
9754 set_vim_var_string(VV_ERRMSG, NULL, 0);
9755}
9756
9757/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009758 * Common for assert_true() and assert_false().
9759 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009761assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009762{
9763 int error = FALSE;
9764 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009765
Bram Moolenaar37127922016-02-06 20:29:28 +01009766 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009767 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009768 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009769 if (argvars[0].v_type != VAR_NUMBER
9770 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9771 || error)
9772 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009773 prepare_assert_error(&ga);
9774 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009775 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009776 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009777 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009778 ga_clear(&ga);
9779 }
9780}
9781
9782/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009783 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009784 */
9785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009786f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009787{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009788 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009789}
9790
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009791 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009792assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009793{
9794 garray_T ga;
9795 char_u buf1[NUMBUFLEN];
9796 char_u buf2[NUMBUFLEN];
9797 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9798 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9799
Bram Moolenaar72188e92016-03-28 22:48:29 +02009800 if (pat == NULL || text == NULL)
9801 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009802 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009803 {
9804 prepare_assert_error(&ga);
9805 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009806 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009807 assert_error(&ga);
9808 ga_clear(&ga);
9809 }
9810}
9811
9812/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009813 * "assert_match(pattern, actual[, msg])" function
9814 */
9815 static void
9816f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9817{
9818 assert_match_common(argvars, ASSERT_MATCH);
9819}
9820
9821/*
9822 * "assert_notmatch(pattern, actual[, msg])" function
9823 */
9824 static void
9825f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9826{
9827 assert_match_common(argvars, ASSERT_NOTMATCH);
9828}
9829
9830/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009831 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009832 */
9833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009834f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009835{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009836 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009837}
9838
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009839#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009840/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009841 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009842 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009844f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009845{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009846 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009847
9848 rettv->v_type = VAR_FLOAT;
9849 if (get_float_arg(argvars, &f) == OK)
9850 rettv->vval.v_float = asin(f);
9851 else
9852 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009853}
9854
9855/*
9856 * "atan()" function
9857 */
9858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009859f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009860{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009861 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009862
9863 rettv->v_type = VAR_FLOAT;
9864 if (get_float_arg(argvars, &f) == OK)
9865 rettv->vval.v_float = atan(f);
9866 else
9867 rettv->vval.v_float = 0.0;
9868}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009869
9870/*
9871 * "atan2()" function
9872 */
9873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009874f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009875{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009876 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009877
9878 rettv->v_type = VAR_FLOAT;
9879 if (get_float_arg(argvars, &fx) == OK
9880 && get_float_arg(&argvars[1], &fy) == OK)
9881 rettv->vval.v_float = atan2(fx, fy);
9882 else
9883 rettv->vval.v_float = 0.0;
9884}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009885#endif
9886
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887/*
9888 * "browse(save, title, initdir, default)" function
9889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009891f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892{
9893#ifdef FEAT_BROWSE
9894 int save;
9895 char_u *title;
9896 char_u *initdir;
9897 char_u *defname;
9898 char_u buf[NUMBUFLEN];
9899 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009900 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009902 save = get_tv_number_chk(&argvars[0], &error);
9903 title = get_tv_string_chk(&argvars[1]);
9904 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9905 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009907 if (error || title == NULL || initdir == NULL || defname == NULL)
9908 rettv->vval.v_string = NULL;
9909 else
9910 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009911 do_browse(save ? BROWSE_SAVE : 0,
9912 title, defname, NULL, initdir, NULL, curbuf);
9913#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009915#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009916 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009917}
9918
9919/*
9920 * "browsedir(title, initdir)" function
9921 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009923f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009924{
9925#ifdef FEAT_BROWSE
9926 char_u *title;
9927 char_u *initdir;
9928 char_u buf[NUMBUFLEN];
9929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009930 title = get_tv_string_chk(&argvars[0]);
9931 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 if (title == NULL || initdir == NULL)
9934 rettv->vval.v_string = NULL;
9935 else
9936 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009937 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009941 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942}
9943
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009944static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009945
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946/*
9947 * Find a buffer by number or exact name.
9948 */
9949 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009950find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951{
9952 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009954 if (avar->v_type == VAR_NUMBER)
9955 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009956 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009958 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009959 if (buf == NULL)
9960 {
9961 /* No full path name match, try a match with a URL or a "nofile"
9962 * buffer, these don't use the full path. */
9963 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9964 if (buf->b_fname != NULL
9965 && (path_with_url(buf->b_fname)
9966#ifdef FEAT_QUICKFIX
9967 || bt_nofile(buf)
9968#endif
9969 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009970 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009971 break;
9972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 }
9974 return buf;
9975}
9976
9977/*
9978 * "bufexists(expr)" function
9979 */
9980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009981f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984}
9985
9986/*
9987 * "buflisted(expr)" function
9988 */
9989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009990f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991{
9992 buf_T *buf;
9993
9994 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009995 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996}
9997
9998/*
9999 * "bufloaded(expr)" function
10000 */
10001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010002f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004 buf_T *buf;
10005
10006 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008}
10009
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010010 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010011buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 int save_magic;
10014 char_u *save_cpo;
10015 buf_T *buf;
10016
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10018 save_magic = p_magic;
10019 p_magic = TRUE;
10020 save_cpo = p_cpo;
10021 p_cpo = (char_u *)"";
10022
10023 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010024 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025
10026 p_magic = save_magic;
10027 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010028 return buf;
10029}
10030
10031/*
10032 * Get buffer by number or pattern.
10033 */
10034 static buf_T *
10035get_buf_tv(typval_T *tv, int curtab_only)
10036{
10037 char_u *name = tv->vval.v_string;
10038 buf_T *buf;
10039
10040 if (tv->v_type == VAR_NUMBER)
10041 return buflist_findnr((int)tv->vval.v_number);
10042 if (tv->v_type != VAR_STRING)
10043 return NULL;
10044 if (name == NULL || *name == NUL)
10045 return curbuf;
10046 if (name[0] == '$' && name[1] == NUL)
10047 return lastbuf;
10048
10049 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
10051 /* If not found, try expanding the name, like done for bufexists(). */
10052 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054
10055 return buf;
10056}
10057
10058/*
10059 * "bufname(expr)" function
10060 */
10061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010062f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063{
10064 buf_T *buf;
10065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010066 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010068 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010069 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 --emsg_off;
10075}
10076
10077/*
10078 * "bufnr(expr)" function
10079 */
10080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010081f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
10083 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010084 int error = FALSE;
10085 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010087 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010089 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010090 --emsg_off;
10091
10092 /* If the buffer isn't found and the second argument is not zero create a
10093 * new buffer. */
10094 if (buf == NULL
10095 && argvars[1].v_type != VAR_UNKNOWN
10096 && get_tv_number_chk(&argvars[1], &error) != 0
10097 && !error
10098 && (name = get_tv_string_chk(&argvars[0])) != NULL
10099 && !error)
10100 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10101
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010103 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106}
10107
10108/*
10109 * "bufwinnr(nr)" function
10110 */
10111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010112f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113{
10114#ifdef FEAT_WINDOWS
10115 win_T *wp;
10116 int winnr = 0;
10117#endif
10118 buf_T *buf;
10119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010120 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010122 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123#ifdef FEAT_WINDOWS
10124 for (wp = firstwin; wp; wp = wp->w_next)
10125 {
10126 ++winnr;
10127 if (wp->w_buffer == buf)
10128 break;
10129 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133#endif
10134 --emsg_off;
10135}
10136
10137/*
10138 * "byte2line(byte)" function
10139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010141f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142{
10143#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145#else
10146 long boff = 0;
10147
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010148 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 (linenr_T)0, &boff);
10154#endif
10155}
10156
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010158byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010159{
10160#ifdef FEAT_MBYTE
10161 char_u *t;
10162#endif
10163 char_u *str;
10164 long idx;
10165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010166 str = get_tv_string_chk(&argvars[0]);
10167 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010168 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010169 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010170 return;
10171
10172#ifdef FEAT_MBYTE
10173 t = str;
10174 for ( ; idx > 0; idx--)
10175 {
10176 if (*t == NUL) /* EOL reached */
10177 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010178 if (enc_utf8 && comp)
10179 t += utf_ptr2len(t);
10180 else
10181 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010182 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010183 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010184#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010185 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010187#endif
10188}
10189
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010190/*
10191 * "byteidx()" function
10192 */
10193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010194f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010195{
10196 byteidx(argvars, rettv, FALSE);
10197}
10198
10199/*
10200 * "byteidxcomp()" function
10201 */
10202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010203f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010204{
10205 byteidx(argvars, rettv, TRUE);
10206}
10207
Bram Moolenaardb913952012-06-29 12:54:53 +020010208 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010209func_call(
10210 char_u *name,
10211 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010212 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010213 dict_T *selfdict,
10214 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010215{
10216 listitem_T *item;
10217 typval_T argv[MAX_FUNC_ARGS + 1];
10218 int argc = 0;
10219 int dummy;
10220 int r = 0;
10221
10222 for (item = args->vval.v_list->lv_first; item != NULL;
10223 item = item->li_next)
10224 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010225 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010226 {
10227 EMSG(_("E699: Too many arguments"));
10228 break;
10229 }
10230 /* Make a copy of each argument. This is needed to be able to set
10231 * v_lock to VAR_FIXED in the copy without changing the original list.
10232 */
10233 copy_tv(&item->li_tv, &argv[argc++]);
10234 }
10235
10236 if (item == NULL)
10237 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10238 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010239 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010240
10241 /* Free the arguments. */
10242 while (argc > 0)
10243 clear_tv(&argv[--argc]);
10244
10245 return r;
10246}
10247
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010248/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010249 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010250 */
10251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010252f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010253{
10254 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010255 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010256 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010257
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010258 if (argvars[1].v_type != VAR_LIST)
10259 {
10260 EMSG(_(e_listreq));
10261 return;
10262 }
10263 if (argvars[1].vval.v_list == NULL)
10264 return;
10265
10266 if (argvars[0].v_type == VAR_FUNC)
10267 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010268 else if (argvars[0].v_type == VAR_PARTIAL)
10269 {
10270 partial = argvars[0].vval.v_partial;
10271 func = partial->pt_name;
10272 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010273 else
10274 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010275 if (*func == NUL)
10276 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010277
Bram Moolenaare9a41262005-01-15 22:18:47 +000010278 if (argvars[2].v_type != VAR_UNKNOWN)
10279 {
10280 if (argvars[2].v_type != VAR_DICT)
10281 {
10282 EMSG(_(e_dictreq));
10283 return;
10284 }
10285 selfdict = argvars[2].vval.v_dict;
10286 }
10287
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010288 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010289}
10290
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010291#ifdef FEAT_FLOAT
10292/*
10293 * "ceil({float})" function
10294 */
10295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010296f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010297{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010298 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010299
10300 rettv->v_type = VAR_FLOAT;
10301 if (get_float_arg(argvars, &f) == OK)
10302 rettv->vval.v_float = ceil(f);
10303 else
10304 rettv->vval.v_float = 0.0;
10305}
10306#endif
10307
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010308#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010309/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010310 * "ch_close()" function
10311 */
10312 static void
10313f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10314{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010315 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010316
10317 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010318 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010319 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010320 channel_clear(channel);
10321 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010322}
10323
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010324/*
10325 * "ch_getbufnr()" function
10326 */
10327 static void
10328f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10329{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010330 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010331
10332 rettv->vval.v_number = -1;
10333 if (channel != NULL)
10334 {
10335 char_u *what = get_tv_string(&argvars[1]);
10336 int part;
10337
10338 if (STRCMP(what, "err") == 0)
10339 part = PART_ERR;
10340 else if (STRCMP(what, "out") == 0)
10341 part = PART_OUT;
10342 else if (STRCMP(what, "in") == 0)
10343 part = PART_IN;
10344 else
10345 part = PART_SOCK;
10346 if (channel->ch_part[part].ch_buffer != NULL)
10347 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10348 }
10349}
10350
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010351/*
10352 * "ch_getjob()" function
10353 */
10354 static void
10355f_ch_getjob(typval_T *argvars, typval_T *rettv)
10356{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010357 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010358
10359 if (channel != NULL)
10360 {
10361 rettv->v_type = VAR_JOB;
10362 rettv->vval.v_job = channel->ch_job;
10363 if (channel->ch_job != NULL)
10364 ++channel->ch_job->jv_refcount;
10365 }
10366}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010367
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010368/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010369 * "ch_info()" function
10370 */
10371 static void
10372f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10373{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010374 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010375
10376 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10377 channel_info(channel, rettv->vval.v_dict);
10378}
10379
10380/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010381 * "ch_log()" function
10382 */
10383 static void
10384f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10385{
10386 char_u *msg = get_tv_string(&argvars[0]);
10387 channel_T *channel = NULL;
10388
10389 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010390 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010391
10392 ch_log(channel, (char *)msg);
10393}
10394
10395/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010396 * "ch_logfile()" function
10397 */
10398 static void
10399f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10400{
10401 char_u *fname;
10402 char_u *opt = (char_u *)"";
10403 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010404
10405 fname = get_tv_string(&argvars[0]);
10406 if (argvars[1].v_type == VAR_STRING)
10407 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010408 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010409}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010410
10411/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010412 * "ch_open()" function
10413 */
10414 static void
10415f_ch_open(typval_T *argvars, typval_T *rettv)
10416{
Bram Moolenaar77073442016-02-13 23:23:53 +010010417 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010418 if (check_restricted() || check_secure())
10419 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010420 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010421}
10422
10423/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010424 * "ch_read()" function
10425 */
10426 static void
10427f_ch_read(typval_T *argvars, typval_T *rettv)
10428{
10429 common_channel_read(argvars, rettv, FALSE);
10430}
10431
10432/*
10433 * "ch_readraw()" function
10434 */
10435 static void
10436f_ch_readraw(typval_T *argvars, typval_T *rettv)
10437{
10438 common_channel_read(argvars, rettv, TRUE);
10439}
10440
10441/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010442 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010443 */
10444 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010445f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10446{
10447 ch_expr_common(argvars, rettv, TRUE);
10448}
10449
10450/*
10451 * "ch_sendexpr()" function
10452 */
10453 static void
10454f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10455{
10456 ch_expr_common(argvars, rettv, FALSE);
10457}
10458
10459/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010460 * "ch_evalraw()" function
10461 */
10462 static void
10463f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10464{
10465 ch_raw_common(argvars, rettv, TRUE);
10466}
10467
10468/*
10469 * "ch_sendraw()" function
10470 */
10471 static void
10472f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10473{
10474 ch_raw_common(argvars, rettv, FALSE);
10475}
10476
10477/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010478 * "ch_setoptions()" function
10479 */
10480 static void
10481f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10482{
10483 channel_T *channel;
10484 jobopt_T opt;
10485
Bram Moolenaar437905c2016-04-26 19:01:05 +020010486 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010487 if (channel == NULL)
10488 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010489 clear_job_options(&opt);
10490 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010491 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10492 channel_set_options(channel, &opt);
10493 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010494}
10495
10496/*
10497 * "ch_status()" function
10498 */
10499 static void
10500f_ch_status(typval_T *argvars, typval_T *rettv)
10501{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010502 channel_T *channel;
10503
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010504 /* return an empty string by default */
10505 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010506 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010507
Bram Moolenaar437905c2016-04-26 19:01:05 +020010508 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010509 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010510}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010511#endif
10512
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010513/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010514 * "changenr()" function
10515 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010517f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010518{
10519 rettv->vval.v_number = curbuf->b_u_seq_cur;
10520}
10521
10522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 * "char2nr(string)" function
10524 */
10525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010526f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527{
10528#ifdef FEAT_MBYTE
10529 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010530 {
10531 int utf8 = 0;
10532
10533 if (argvars[1].v_type != VAR_UNKNOWN)
10534 utf8 = get_tv_number_chk(&argvars[1], NULL);
10535
10536 if (utf8)
10537 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10538 else
10539 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 else
10542#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544}
10545
10546/*
10547 * "cindent(lnum)" function
10548 */
10549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010550f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
10552#ifdef FEAT_CINDENT
10553 pos_T pos;
10554 linenr_T lnum;
10555
10556 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10559 {
10560 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 curwin->w_cursor = pos;
10563 }
10564 else
10565#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010566 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567}
10568
10569/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010570 * "clearmatches()" function
10571 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010573f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010574{
10575#ifdef FEAT_SEARCH_EXTRA
10576 clear_matches(curwin);
10577#endif
10578}
10579
10580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 * "col(string)" function
10582 */
10583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010584f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
10586 colnr_T col = 0;
10587 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010588 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010590 fp = var2fpos(&argvars[0], FALSE, &fnum);
10591 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 {
10593 if (fp->col == MAXCOL)
10594 {
10595 /* '> can be MAXCOL, get the length of the line then */
10596 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010597 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 else
10599 col = MAXCOL;
10600 }
10601 else
10602 {
10603 col = fp->col + 1;
10604#ifdef FEAT_VIRTUALEDIT
10605 /* col(".") when the cursor is on the NUL at the end of the line
10606 * because of "coladd" can be seen as an extra column. */
10607 if (virtual_active() && fp == &curwin->w_cursor)
10608 {
10609 char_u *p = ml_get_cursor();
10610
10611 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10612 curwin->w_virtcol - curwin->w_cursor.coladd))
10613 {
10614# ifdef FEAT_MBYTE
10615 int l;
10616
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010617 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 col += l;
10619# else
10620 if (*p != NUL && p[1] == NUL)
10621 ++col;
10622# endif
10623 }
10624 }
10625#endif
10626 }
10627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010628 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629}
10630
Bram Moolenaar572cb562005-08-05 21:35:02 +000010631#if defined(FEAT_INS_EXPAND)
10632/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010633 * "complete()" function
10634 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010636f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010637{
10638 int startcol;
10639
10640 if ((State & INSERT) == 0)
10641 {
10642 EMSG(_("E785: complete() can only be used in Insert mode"));
10643 return;
10644 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010645
10646 /* Check for undo allowed here, because if something was already inserted
10647 * the line was already saved for undo and this check isn't done. */
10648 if (!undo_allowed())
10649 return;
10650
Bram Moolenaarade00832006-03-10 21:46:58 +000010651 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10652 {
10653 EMSG(_(e_invarg));
10654 return;
10655 }
10656
10657 startcol = get_tv_number_chk(&argvars[0], NULL);
10658 if (startcol <= 0)
10659 return;
10660
10661 set_completion(startcol - 1, argvars[1].vval.v_list);
10662}
10663
10664/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010665 * "complete_add()" function
10666 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010668f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010669{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010670 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010671}
10672
10673/*
10674 * "complete_check()" function
10675 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010677f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010678{
10679 int saved = RedrawingDisabled;
10680
10681 RedrawingDisabled = 0;
10682 ins_compl_check_keys(0);
10683 rettv->vval.v_number = compl_interrupted;
10684 RedrawingDisabled = saved;
10685}
10686#endif
10687
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688/*
10689 * "confirm(message, buttons[, default [, type]])" function
10690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010692f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693{
10694#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10695 char_u *message;
10696 char_u *buttons = NULL;
10697 char_u buf[NUMBUFLEN];
10698 char_u buf2[NUMBUFLEN];
10699 int def = 1;
10700 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701 char_u *typestr;
10702 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010704 message = get_tv_string_chk(&argvars[0]);
10705 if (message == NULL)
10706 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010709 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10710 if (buttons == NULL)
10711 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010712 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010714 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010715 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010717 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10718 if (typestr == NULL)
10719 error = TRUE;
10720 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010722 switch (TOUPPER_ASC(*typestr))
10723 {
10724 case 'E': type = VIM_ERROR; break;
10725 case 'Q': type = VIM_QUESTION; break;
10726 case 'I': type = VIM_INFO; break;
10727 case 'W': type = VIM_WARNING; break;
10728 case 'G': type = VIM_GENERIC; break;
10729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 }
10731 }
10732 }
10733 }
10734
10735 if (buttons == NULL || *buttons == NUL)
10736 buttons = (char_u *)_("&Ok");
10737
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010738 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010739 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010740 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741#endif
10742}
10743
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010744/*
10745 * "copy()" function
10746 */
10747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010748f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010749{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010750 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010751}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010753#ifdef FEAT_FLOAT
10754/*
10755 * "cos()" function
10756 */
10757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010758f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010759{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010760 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010761
10762 rettv->v_type = VAR_FLOAT;
10763 if (get_float_arg(argvars, &f) == OK)
10764 rettv->vval.v_float = cos(f);
10765 else
10766 rettv->vval.v_float = 0.0;
10767}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010768
10769/*
10770 * "cosh()" function
10771 */
10772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010773f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010774{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010775 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010776
10777 rettv->v_type = VAR_FLOAT;
10778 if (get_float_arg(argvars, &f) == OK)
10779 rettv->vval.v_float = cosh(f);
10780 else
10781 rettv->vval.v_float = 0.0;
10782}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010783#endif
10784
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010786 * "count()" function
10787 */
10788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010789f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010790{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010791 long n = 0;
10792 int ic = FALSE;
10793
Bram Moolenaare9a41262005-01-15 22:18:47 +000010794 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010795 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010796 listitem_T *li;
10797 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010798 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010799
Bram Moolenaare9a41262005-01-15 22:18:47 +000010800 if ((l = argvars[0].vval.v_list) != NULL)
10801 {
10802 li = l->lv_first;
10803 if (argvars[2].v_type != VAR_UNKNOWN)
10804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 int error = FALSE;
10806
10807 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010808 if (argvars[3].v_type != VAR_UNKNOWN)
10809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010810 idx = get_tv_number_chk(&argvars[3], &error);
10811 if (!error)
10812 {
10813 li = list_find(l, idx);
10814 if (li == NULL)
10815 EMSGN(_(e_listidx), idx);
10816 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010817 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010818 if (error)
10819 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820 }
10821
10822 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010823 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010824 ++n;
10825 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010826 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010827 else if (argvars[0].v_type == VAR_DICT)
10828 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010829 int todo;
10830 dict_T *d;
10831 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010832
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010833 if ((d = argvars[0].vval.v_dict) != NULL)
10834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010835 int error = FALSE;
10836
Bram Moolenaare9a41262005-01-15 22:18:47 +000010837 if (argvars[2].v_type != VAR_UNKNOWN)
10838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010839 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010840 if (argvars[3].v_type != VAR_UNKNOWN)
10841 EMSG(_(e_invarg));
10842 }
10843
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010844 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010845 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010846 {
10847 if (!HASHITEM_EMPTY(hi))
10848 {
10849 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010850 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010851 ++n;
10852 }
10853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010854 }
10855 }
10856 else
10857 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010858 rettv->vval.v_number = n;
10859}
10860
10861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10863 *
10864 * Checks the existence of a cscope connection.
10865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010867f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868{
10869#ifdef FEAT_CSCOPE
10870 int num = 0;
10871 char_u *dbpath = NULL;
10872 char_u *prepend = NULL;
10873 char_u buf[NUMBUFLEN];
10874
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010875 if (argvars[0].v_type != VAR_UNKNOWN
10876 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 num = (int)get_tv_number(&argvars[0]);
10879 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010880 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 }
10883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885#endif
10886}
10887
10888/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010889 * "cursor(lnum, col)" function, or
10890 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010892 * Moves the cursor to the specified line and column.
10893 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010896f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897{
10898 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010899#ifdef FEAT_VIRTUALEDIT
10900 long coladd = 0;
10901#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010902 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010904 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010905 if (argvars[1].v_type == VAR_UNKNOWN)
10906 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010907 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010908 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010909
Bram Moolenaar493c1782014-05-28 14:34:46 +020010910 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010911 {
10912 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010913 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010914 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010915 line = pos.lnum;
10916 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010917#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010918 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010919#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010920 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010921 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010922 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010923 set_curswant = FALSE;
10924 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010925 }
10926 else
10927 {
10928 line = get_tv_lnum(argvars);
10929 col = get_tv_number_chk(&argvars[1], NULL);
10930#ifdef FEAT_VIRTUALEDIT
10931 if (argvars[2].v_type != VAR_UNKNOWN)
10932 coladd = get_tv_number_chk(&argvars[2], NULL);
10933#endif
10934 }
10935 if (line < 0 || col < 0
10936#ifdef FEAT_VIRTUALEDIT
10937 || coladd < 0
10938#endif
10939 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 if (line > 0)
10942 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 if (col > 0)
10944 curwin->w_cursor.col = col - 1;
10945#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010946 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947#endif
10948
10949 /* Make sure the cursor is in a valid position. */
10950 check_cursor();
10951#ifdef FEAT_MBYTE
10952 /* Correct cursor for multi-byte character. */
10953 if (has_mbyte)
10954 mb_adjust_cursor();
10955#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010956
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010957 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010958 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959}
10960
10961/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010962 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 */
10964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010965f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010967 int noref = 0;
10968
10969 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010970 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010971 if (noref < 0 || noref > 1)
10972 EMSG(_(e_invarg));
10973 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010974 {
10975 current_copyID += COPYID_INC;
10976 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978}
10979
10980/*
10981 * "delete()" function
10982 */
10983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010984f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010986 char_u nbuf[NUMBUFLEN];
10987 char_u *name;
10988 char_u *flags;
10989
10990 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010992 return;
10993
10994 name = get_tv_string(&argvars[0]);
10995 if (name == NULL || *name == NUL)
10996 {
10997 EMSG(_(e_invarg));
10998 return;
10999 }
11000
11001 if (argvars[1].v_type != VAR_UNKNOWN)
11002 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011004 flags = (char_u *)"";
11005
11006 if (*flags == NUL)
11007 /* delete a file */
11008 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11009 else if (STRCMP(flags, "d") == 0)
11010 /* delete an empty directory */
11011 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11012 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011013 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011014 rettv->vval.v_number = delete_recursive(name);
11015 else
11016 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017}
11018
11019/*
11020 * "did_filetype()" function
11021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011023f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024{
11025#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011026 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027#endif
11028}
11029
11030/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011031 * "diff_filler()" function
11032 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011034f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011035{
11036#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011038#endif
11039}
11040
11041/*
11042 * "diff_hlID()" function
11043 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011045f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011046{
11047#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011049 static linenr_T prev_lnum = 0;
11050 static int changedtick = 0;
11051 static int fnum = 0;
11052 static int change_start = 0;
11053 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011054 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011055 int filler_lines;
11056 int col;
11057
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011058 if (lnum < 0) /* ignore type error in {lnum} arg */
11059 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011060 if (lnum != prev_lnum
11061 || changedtick != curbuf->b_changedtick
11062 || fnum != curbuf->b_fnum)
11063 {
11064 /* New line, buffer, change: need to get the values. */
11065 filler_lines = diff_check(curwin, lnum);
11066 if (filler_lines < 0)
11067 {
11068 if (filler_lines == -1)
11069 {
11070 change_start = MAXCOL;
11071 change_end = -1;
11072 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11073 hlID = HLF_ADD; /* added line */
11074 else
11075 hlID = HLF_CHD; /* changed line */
11076 }
11077 else
11078 hlID = HLF_ADD; /* added line */
11079 }
11080 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011081 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011082 prev_lnum = lnum;
11083 changedtick = curbuf->b_changedtick;
11084 fnum = curbuf->b_fnum;
11085 }
11086
11087 if (hlID == HLF_CHD || hlID == HLF_TXD)
11088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011090 if (col >= change_start && col <= change_end)
11091 hlID = HLF_TXD; /* changed text */
11092 else
11093 hlID = HLF_CHD; /* changed line */
11094 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011095 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011096#endif
11097}
11098
11099/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011100 * "disable_char_avail_for_testing({expr})" function
11101 */
11102 static void
11103f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11104{
11105 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11106}
11107
11108/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011109 * "empty({expr})" function
11110 */
11111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011112f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011113{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011114 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011115
11116 switch (argvars[0].v_type)
11117 {
11118 case VAR_STRING:
11119 case VAR_FUNC:
11120 n = argvars[0].vval.v_string == NULL
11121 || *argvars[0].vval.v_string == NUL;
11122 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011123 case VAR_PARTIAL:
11124 n = FALSE;
11125 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011126 case VAR_NUMBER:
11127 n = argvars[0].vval.v_number == 0;
11128 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011129 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011130#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011131 n = argvars[0].vval.v_float == 0.0;
11132 break;
11133#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011134 case VAR_LIST:
11135 n = argvars[0].vval.v_list == NULL
11136 || argvars[0].vval.v_list->lv_first == NULL;
11137 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 case VAR_DICT:
11139 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011140 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011141 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011142 case VAR_SPECIAL:
11143 n = argvars[0].vval.v_number != VVAL_TRUE;
11144 break;
11145
Bram Moolenaar835dc632016-02-07 14:27:38 +010011146 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011147#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011148 n = argvars[0].vval.v_job == NULL
11149 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11150 break;
11151#endif
11152 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011153#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011154 n = argvars[0].vval.v_channel == NULL
11155 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011156 break;
11157#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011158 case VAR_UNKNOWN:
11159 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11160 n = TRUE;
11161 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011162 }
11163
11164 rettv->vval.v_number = n;
11165}
11166
11167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 * "escape({string}, {chars})" function
11169 */
11170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011171f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172{
11173 char_u buf[NUMBUFLEN];
11174
Bram Moolenaar758711c2005-02-02 23:11:38 +000011175 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11176 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178}
11179
11180/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011181 * "eval()" function
11182 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011184f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011185{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011186 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011188 s = get_tv_string_chk(&argvars[0]);
11189 if (s != NULL)
11190 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011191
Bram Moolenaar615b9972015-01-14 17:15:05 +010011192 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011193 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11194 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011195 if (p != NULL && !aborting())
11196 EMSG2(_(e_invexpr2), p);
11197 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011198 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011199 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011200 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011201 else if (*s != NUL)
11202 EMSG(_(e_trailing));
11203}
11204
11205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 * "eventhandler()" function
11207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011209f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212}
11213
11214/*
11215 * "executable()" function
11216 */
11217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011218f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011220 char_u *name = get_tv_string(&argvars[0]);
11221
11222 /* Check in $PATH and also check directly if there is a directory name. */
11223 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11224 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011225}
11226
11227/*
11228 * "exepath()" function
11229 */
11230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011231f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011232{
11233 char_u *p = NULL;
11234
Bram Moolenaarb5971142015-03-21 17:32:19 +010011235 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011236 rettv->v_type = VAR_STRING;
11237 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238}
11239
11240/*
11241 * "exists()" function
11242 */
11243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011244f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245{
11246 char_u *p;
11247 char_u *name;
11248 int n = FALSE;
11249 int len = 0;
11250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 if (*p == '$') /* environment variable */
11253 {
11254 /* first try "normal" environment variables (fast) */
11255 if (mch_getenv(p + 1) != NULL)
11256 n = TRUE;
11257 else
11258 {
11259 /* try expanding things like $VIM and ${HOME} */
11260 p = expand_env_save(p);
11261 if (p != NULL && *p != '$')
11262 n = TRUE;
11263 vim_free(p);
11264 }
11265 }
11266 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011267 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011269 if (*skipwhite(p) != NUL)
11270 n = FALSE; /* trailing garbage */
11271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 else if (*p == '*') /* internal or user defined function */
11273 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011274 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 }
11276 else if (*p == ':')
11277 {
11278 n = cmd_exists(p + 1);
11279 }
11280 else if (*p == '#')
11281 {
11282#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011283 if (p[1] == '#')
11284 n = autocmd_supported(p + 2);
11285 else
11286 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287#endif
11288 }
11289 else /* internal variable */
11290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011291 char_u *tofree;
11292 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011294 /* get_name_len() takes care of expanding curly braces */
11295 name = p;
11296 len = get_name_len(&p, &tofree, TRUE, FALSE);
11297 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011299 if (tofree != NULL)
11300 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011301 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011302 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011304 /* handle d.key, l[idx], f(expr) */
11305 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11306 if (n)
11307 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 }
11309 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011310 if (*p != NUL)
11311 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011313 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314 }
11315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317}
11318
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011319#ifdef FEAT_FLOAT
11320/*
11321 * "exp()" function
11322 */
11323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011324f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011325{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011326 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011327
11328 rettv->v_type = VAR_FLOAT;
11329 if (get_float_arg(argvars, &f) == OK)
11330 rettv->vval.v_float = exp(f);
11331 else
11332 rettv->vval.v_float = 0.0;
11333}
11334#endif
11335
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336/*
11337 * "expand()" function
11338 */
11339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011340f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341{
11342 char_u *s;
11343 int len;
11344 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011345 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011347 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011348 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011351 if (argvars[1].v_type != VAR_UNKNOWN
11352 && argvars[2].v_type != VAR_UNKNOWN
11353 && get_tv_number_chk(&argvars[2], &error)
11354 && !error)
11355 {
11356 rettv->v_type = VAR_LIST;
11357 rettv->vval.v_list = NULL;
11358 }
11359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 if (*s == '%' || *s == '#' || *s == '<')
11362 {
11363 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011364 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011366 if (rettv->v_type == VAR_LIST)
11367 {
11368 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11369 list_append_string(rettv->vval.v_list, result, -1);
11370 else
11371 vim_free(result);
11372 }
11373 else
11374 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 }
11376 else
11377 {
11378 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011379 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011380 if (argvars[1].v_type != VAR_UNKNOWN
11381 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011382 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011383 if (!error)
11384 {
11385 ExpandInit(&xpc);
11386 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011387 if (p_wic)
11388 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011389 if (rettv->v_type == VAR_STRING)
11390 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11391 options, WILD_ALL);
11392 else if (rettv_list_alloc(rettv) != FAIL)
11393 {
11394 int i;
11395
11396 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11397 for (i = 0; i < xpc.xp_numfiles; i++)
11398 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11399 ExpandCleanup(&xpc);
11400 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011401 }
11402 else
11403 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 }
11405}
11406
11407/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011408 * Go over all entries in "d2" and add them to "d1".
11409 * When "action" is "error" then a duplicate key is an error.
11410 * When "action" is "force" then a duplicate key is overwritten.
11411 * Otherwise duplicate keys are ignored ("action" is "keep").
11412 */
11413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011414dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011415{
11416 dictitem_T *di1;
11417 hashitem_T *hi2;
11418 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011419 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011420
11421 todo = (int)d2->dv_hashtab.ht_used;
11422 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11423 {
11424 if (!HASHITEM_EMPTY(hi2))
11425 {
11426 --todo;
11427 di1 = dict_find(d1, hi2->hi_key, -1);
11428 if (d1->dv_scope != 0)
11429 {
11430 /* Disallow replacing a builtin function in l: and g:.
11431 * Check the key to be valid when adding to any
11432 * scope. */
11433 if (d1->dv_scope == VAR_DEF_SCOPE
11434 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11435 && var_check_func_name(hi2->hi_key,
11436 di1 == NULL))
11437 break;
11438 if (!valid_varname(hi2->hi_key))
11439 break;
11440 }
11441 if (di1 == NULL)
11442 {
11443 di1 = dictitem_copy(HI2DI(hi2));
11444 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11445 dictitem_free(di1);
11446 }
11447 else if (*action == 'e')
11448 {
11449 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11450 break;
11451 }
11452 else if (*action == 'f' && HI2DI(hi2) != di1)
11453 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011454 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11455 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011456 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011457 clear_tv(&di1->di_tv);
11458 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11459 }
11460 }
11461 }
11462}
11463
11464/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011465 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011466 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011467 */
11468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011469f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011470{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011471 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011472
Bram Moolenaare9a41262005-01-15 22:18:47 +000011473 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011474 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011475 list_T *l1, *l2;
11476 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011477 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011479
Bram Moolenaare9a41262005-01-15 22:18:47 +000011480 l1 = argvars[0].vval.v_list;
11481 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011482 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011483 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011484 {
11485 if (argvars[2].v_type != VAR_UNKNOWN)
11486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011487 before = get_tv_number_chk(&argvars[2], &error);
11488 if (error)
11489 return; /* type error; errmsg already given */
11490
Bram Moolenaar758711c2005-02-02 23:11:38 +000011491 if (before == l1->lv_len)
11492 item = NULL;
11493 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011494 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011495 item = list_find(l1, before);
11496 if (item == NULL)
11497 {
11498 EMSGN(_(e_listidx), before);
11499 return;
11500 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011501 }
11502 }
11503 else
11504 item = NULL;
11505 list_extend(l1, l2, item);
11506
Bram Moolenaare9a41262005-01-15 22:18:47 +000011507 copy_tv(&argvars[0], rettv);
11508 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011509 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011510 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11511 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011512 dict_T *d1, *d2;
11513 char_u *action;
11514 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011515
11516 d1 = argvars[0].vval.v_dict;
11517 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011518 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011519 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011520 {
11521 /* Check the third argument. */
11522 if (argvars[2].v_type != VAR_UNKNOWN)
11523 {
11524 static char *(av[]) = {"keep", "force", "error"};
11525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011526 action = get_tv_string_chk(&argvars[2]);
11527 if (action == NULL)
11528 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011529 for (i = 0; i < 3; ++i)
11530 if (STRCMP(action, av[i]) == 0)
11531 break;
11532 if (i == 3)
11533 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011534 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011535 return;
11536 }
11537 }
11538 else
11539 action = (char_u *)"force";
11540
Bram Moolenaara9922d62013-05-30 13:01:18 +020011541 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011542
Bram Moolenaare9a41262005-01-15 22:18:47 +000011543 copy_tv(&argvars[0], rettv);
11544 }
11545 }
11546 else
11547 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011548}
11549
11550/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011551 * "feedkeys()" function
11552 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011554f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011555{
11556 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011557 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011558 char_u *keys, *flags;
11559 char_u nbuf[NUMBUFLEN];
11560 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011561 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011562 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011563 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011564
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011565 /* This is not allowed in the sandbox. If the commands would still be
11566 * executed in the sandbox it would be OK, but it probably happens later,
11567 * when "sandbox" is no longer set. */
11568 if (check_secure())
11569 return;
11570
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011571 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011572
11573 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011574 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011575 flags = get_tv_string_buf(&argvars[1], nbuf);
11576 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011577 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011578 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011579 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011580 case 'n': remap = FALSE; break;
11581 case 'm': remap = TRUE; break;
11582 case 't': typed = TRUE; break;
11583 case 'i': insert = TRUE; break;
11584 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011585 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011586 }
11587 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011588 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011589
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011590 if (*keys != NUL || execute)
11591 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011592 /* Need to escape K_SPECIAL and CSI before putting the string in the
11593 * typeahead buffer. */
11594 keys_esc = vim_strsave_escape_csi(keys);
11595 if (keys_esc != NULL)
11596 {
11597 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011598 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011599 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011600 if (vgetc_busy)
11601 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011602 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011603 {
11604 int save_msg_scroll = msg_scroll;
11605
11606 /* Avoid a 1 second delay when the keys start Insert mode. */
11607 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011608
Bram Moolenaar245c4102016-04-20 17:37:41 +020011609 if (!dangerous)
11610 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011611 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011612 if (!dangerous)
11613 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011614 msg_scroll |= save_msg_scroll;
11615 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011616 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011617 }
11618}
11619
11620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 * "filereadable()" function
11622 */
11623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011624f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011626 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 char_u *p;
11628 int n;
11629
Bram Moolenaarc236c162008-07-13 17:41:49 +000011630#ifndef O_NONBLOCK
11631# define O_NONBLOCK 0
11632#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011633 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011634 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11635 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 {
11637 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011638 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 }
11640 else
11641 n = FALSE;
11642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011643 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644}
11645
11646/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011647 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648 * rights to write into.
11649 */
11650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011651f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011653 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654}
11655
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011656 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011657findfilendir(
11658 typval_T *argvars UNUSED,
11659 typval_T *rettv,
11660 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011661{
11662#ifdef FEAT_SEARCHPATH
11663 char_u *fname;
11664 char_u *fresult = NULL;
11665 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11666 char_u *p;
11667 char_u pathbuf[NUMBUFLEN];
11668 int count = 1;
11669 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011670 int error = FALSE;
11671#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011672
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011673 rettv->vval.v_string = NULL;
11674 rettv->v_type = VAR_STRING;
11675
11676#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011677 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011678
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011679 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011681 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11682 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011683 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011684 else
11685 {
11686 if (*p != NUL)
11687 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011689 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011690 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011692 }
11693
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011694 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11695 error = TRUE;
11696
11697 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699 do
11700 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011701 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011702 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011703 fresult = find_file_in_path_option(first ? fname : NULL,
11704 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011705 0, first, path,
11706 find_what,
11707 curbuf->b_ffname,
11708 find_what == FINDFILE_DIR
11709 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011710 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011711
11712 if (fresult != NULL && rettv->v_type == VAR_LIST)
11713 list_append_string(rettv->vval.v_list, fresult, -1);
11714
11715 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011716 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011717
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011718 if (rettv->v_type == VAR_STRING)
11719 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011720#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011721}
11722
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011723static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11724static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011725
11726/*
11727 * Implementation of map() and filter().
11728 */
11729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011730filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011731{
11732 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011733 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011734 listitem_T *li, *nli;
11735 list_T *l = NULL;
11736 dictitem_T *di;
11737 hashtab_T *ht;
11738 hashitem_T *hi;
11739 dict_T *d = NULL;
11740 typval_T save_val;
11741 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011742 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011743 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011744 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011745 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011746 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011747 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011748 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011749
Bram Moolenaare9a41262005-01-15 22:18:47 +000011750 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011751 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011752 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011753 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011754 return;
11755 }
11756 else if (argvars[0].v_type == VAR_DICT)
11757 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011758 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011759 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011760 return;
11761 }
11762 else
11763 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011764 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011765 return;
11766 }
11767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011768 expr = get_tv_string_buf_chk(&argvars[1], buf);
11769 /* On type errors, the preceding call has already displayed an error
11770 * message. Avoid a misleading error message for an empty string that
11771 * was not passed as argument. */
11772 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011774 prepare_vimvar(VV_VAL, &save_val);
11775 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011776
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011777 /* We reset "did_emsg" to be able to detect whether an error
11778 * occurred during evaluation of the expression. */
11779 save_did_emsg = did_emsg;
11780 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011781
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011782 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011783 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 vimvars[VV_KEY].vv_type = VAR_STRING;
11786
11787 ht = &d->dv_hashtab;
11788 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011789 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011790 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011791 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011792 if (!HASHITEM_EMPTY(hi))
11793 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011794 int r;
11795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011796 --todo;
11797 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011798 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011799 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11800 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 break;
11802 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011803 r = filter_map_one(&di->di_tv, expr, map, &rem);
11804 clear_tv(&vimvars[VV_KEY].vv_tv);
11805 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011806 break;
11807 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011808 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011809 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11810 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011811 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011813 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011814 }
11815 }
11816 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011817 }
11818 else
11819 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011820 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 for (li = l->lv_first; li != NULL; li = nli)
11823 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011824 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011825 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011826 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011827 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011828 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011829 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011830 break;
11831 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011832 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011833 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011834 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011835 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011836
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011837 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011838 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011839
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011840 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011842
11843 copy_tv(&argvars[0], rettv);
11844}
11845
11846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011847filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011848{
Bram Moolenaar33570922005-01-25 22:26:29 +000011849 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011850 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011851 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011852
Bram Moolenaar33570922005-01-25 22:26:29 +000011853 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011854 s = expr;
11855 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011856 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011857 if (*s != NUL) /* check for trailing chars after expr */
11858 {
11859 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011860 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011861 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011862 }
11863 if (map)
11864 {
11865 /* map(): replace the list item value */
11866 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011867 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011868 *tv = rettv;
11869 }
11870 else
11871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011872 int error = FALSE;
11873
Bram Moolenaare9a41262005-01-15 22:18:47 +000011874 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011875 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011876 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011877 /* On type error, nothing has been removed; return FAIL to stop the
11878 * loop. The error message was given by get_tv_number_chk(). */
11879 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011880 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011881 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011882 retval = OK;
11883theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011884 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011885 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011886}
11887
11888/*
11889 * "filter()" function
11890 */
11891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011892f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011893{
11894 filter_map(argvars, rettv, FALSE);
11895}
11896
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011897/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011898 * "finddir({fname}[, {path}[, {count}]])" function
11899 */
11900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011901f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011902{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011903 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011904}
11905
11906/*
11907 * "findfile({fname}[, {path}[, {count}]])" function
11908 */
11909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011910f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011911{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011912 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011913}
11914
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011915#ifdef FEAT_FLOAT
11916/*
11917 * "float2nr({float})" function
11918 */
11919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011920f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011921{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011922 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011923
11924 if (get_float_arg(argvars, &f) == OK)
11925 {
11926 if (f < -0x7fffffff)
11927 rettv->vval.v_number = -0x7fffffff;
11928 else if (f > 0x7fffffff)
11929 rettv->vval.v_number = 0x7fffffff;
11930 else
11931 rettv->vval.v_number = (varnumber_T)f;
11932 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011933}
11934
11935/*
11936 * "floor({float})" function
11937 */
11938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011939f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011940{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011941 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011942
11943 rettv->v_type = VAR_FLOAT;
11944 if (get_float_arg(argvars, &f) == OK)
11945 rettv->vval.v_float = floor(f);
11946 else
11947 rettv->vval.v_float = 0.0;
11948}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011949
11950/*
11951 * "fmod()" function
11952 */
11953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011954f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011955{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011956 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011957
11958 rettv->v_type = VAR_FLOAT;
11959 if (get_float_arg(argvars, &fx) == OK
11960 && get_float_arg(&argvars[1], &fy) == OK)
11961 rettv->vval.v_float = fmod(fx, fy);
11962 else
11963 rettv->vval.v_float = 0.0;
11964}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011965#endif
11966
Bram Moolenaar0d660222005-01-07 21:51:51 +000011967/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011968 * "fnameescape({string})" function
11969 */
11970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011971f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011972{
11973 rettv->vval.v_string = vim_strsave_fnameescape(
11974 get_tv_string(&argvars[0]), FALSE);
11975 rettv->v_type = VAR_STRING;
11976}
11977
11978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 * "fnamemodify({fname}, {mods})" function
11980 */
11981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011982f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983{
11984 char_u *fname;
11985 char_u *mods;
11986 int usedlen = 0;
11987 int len;
11988 char_u *fbuf = NULL;
11989 char_u buf[NUMBUFLEN];
11990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011991 fname = get_tv_string_chk(&argvars[0]);
11992 mods = get_tv_string_buf_chk(&argvars[1], buf);
11993 if (fname == NULL || mods == NULL)
11994 fname = NULL;
11995 else
11996 {
11997 len = (int)STRLEN(fname);
11998 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006 vim_free(fbuf);
12007}
12008
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012009static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010
12011/*
12012 * "foldclosed()" function
12013 */
12014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012015foldclosed_both(
12016 typval_T *argvars UNUSED,
12017 typval_T *rettv,
12018 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019{
12020#ifdef FEAT_FOLDING
12021 linenr_T lnum;
12022 linenr_T first, last;
12023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012024 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12026 {
12027 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12028 {
12029 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012030 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012032 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 return;
12034 }
12035 }
12036#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038}
12039
12040/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012041 * "foldclosed()" function
12042 */
12043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012044f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012045{
12046 foldclosed_both(argvars, rettv, FALSE);
12047}
12048
12049/*
12050 * "foldclosedend()" function
12051 */
12052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012053f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012054{
12055 foldclosed_both(argvars, rettv, TRUE);
12056}
12057
12058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 * "foldlevel()" function
12060 */
12061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012062f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063{
12064#ifdef FEAT_FOLDING
12065 linenr_T lnum;
12066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012067 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012069 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071}
12072
12073/*
12074 * "foldtext()" function
12075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012077f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078{
12079#ifdef FEAT_FOLDING
12080 linenr_T lnum;
12081 char_u *s;
12082 char_u *r;
12083 int len;
12084 char *txt;
12085#endif
12086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 rettv->v_type = VAR_STRING;
12088 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012090 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12091 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12092 <= curbuf->b_ml.ml_line_count
12093 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 {
12095 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012096 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12097 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 {
12099 if (!linewhite(lnum))
12100 break;
12101 ++lnum;
12102 }
12103
12104 /* Find interesting text in this line. */
12105 s = skipwhite(ml_get(lnum));
12106 /* skip C comment-start */
12107 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012110 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012111 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012112 {
12113 s = skipwhite(ml_get(lnum + 1));
12114 if (*s == '*')
12115 s = skipwhite(s + 1);
12116 }
12117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 txt = _("+-%s%3ld lines: ");
12119 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012120 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121 + 20 /* for %3ld */
12122 + STRLEN(s))); /* concatenated */
12123 if (r != NULL)
12124 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012125 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12126 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12127 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 len = (int)STRLEN(r);
12129 STRCAT(r, s);
12130 /* remove 'foldmarker' and 'commentstring' */
12131 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012132 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133 }
12134 }
12135#endif
12136}
12137
12138/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012139 * "foldtextresult(lnum)" function
12140 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012142f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012143{
12144#ifdef FEAT_FOLDING
12145 linenr_T lnum;
12146 char_u *text;
12147 char_u buf[51];
12148 foldinfo_T foldinfo;
12149 int fold_count;
12150#endif
12151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152 rettv->v_type = VAR_STRING;
12153 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012154#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012156 /* treat illegal types and illegal string values for {lnum} the same */
12157 if (lnum < 0)
12158 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012159 fold_count = foldedCount(curwin, lnum, &foldinfo);
12160 if (fold_count > 0)
12161 {
12162 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12163 &foldinfo, buf);
12164 if (text == buf)
12165 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012166 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012167 }
12168#endif
12169}
12170
12171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 * "foreground()" function
12173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012175f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177#ifdef FEAT_GUI
12178 if (gui.in_use)
12179 gui_mch_set_foreground();
12180#else
12181# ifdef WIN32
12182 win32_set_foreground();
12183# endif
12184#endif
12185}
12186
12187/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012188 * "function()" function
12189 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012191f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012192{
12193 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012194 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012195 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012196 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012197
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012198 if (argvars[0].v_type == VAR_FUNC)
12199 {
12200 /* function(MyFunc, [arg], dict) */
12201 s = argvars[0].vval.v_string;
12202 }
12203 else if (argvars[0].v_type == VAR_PARTIAL
12204 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012205 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012206 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012207 arg_pt = argvars[0].vval.v_partial;
12208 s = arg_pt->pt_name;
12209 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012210 else
12211 {
12212 /* function('MyFunc', [arg], dict) */
12213 s = get_tv_string(&argvars[0]);
12214 use_string = TRUE;
12215 }
12216
12217 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012218 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012219 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012220 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12221 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012222 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012223 else
12224 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012225 int dict_idx = 0;
12226 int arg_idx = 0;
12227 list_T *list = NULL;
12228
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012229 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012230 {
12231 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012232 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012233
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012234 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12235 * also be called from another script. Using trans_function_name()
12236 * would also work, but some plugins depend on the name being
12237 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012238 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012239 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12240 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012241 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012242 STRCPY(name, sid_buf);
12243 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012244 }
12245 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012246 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012247 name = vim_strsave(s);
12248
12249 if (argvars[1].v_type != VAR_UNKNOWN)
12250 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012251 if (argvars[2].v_type != VAR_UNKNOWN)
12252 {
12253 /* function(name, [args], dict) */
12254 arg_idx = 1;
12255 dict_idx = 2;
12256 }
12257 else if (argvars[1].v_type == VAR_DICT)
12258 /* function(name, dict) */
12259 dict_idx = 1;
12260 else
12261 /* function(name, [args]) */
12262 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012263 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012264 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012265 if (argvars[dict_idx].v_type != VAR_DICT)
12266 {
12267 EMSG(_("E922: expected a dict"));
12268 vim_free(name);
12269 return;
12270 }
12271 if (argvars[dict_idx].vval.v_dict == NULL)
12272 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012273 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012274 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012275 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012276 if (argvars[arg_idx].v_type != VAR_LIST)
12277 {
12278 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12279 vim_free(name);
12280 return;
12281 }
12282 list = argvars[arg_idx].vval.v_list;
12283 if (list == NULL || list->lv_len == 0)
12284 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012285 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012286 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012287 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012288 {
12289 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012290
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012291 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012292 if (pt == NULL)
12293 vim_free(name);
12294 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012295 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012296 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012297 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012298 listitem_T *li;
12299 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012300 int arg_len = 0;
12301 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012302
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012303 if (arg_pt != NULL)
12304 arg_len = arg_pt->pt_argc;
12305 if (list != NULL)
12306 lv_len = list->lv_len;
12307 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012308 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012309 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012310 if (pt->pt_argv == NULL)
12311 {
12312 vim_free(pt);
12313 vim_free(name);
12314 return;
12315 }
12316 else
12317 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012318 for (i = 0; i < arg_len; i++)
12319 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12320 if (lv_len > 0)
12321 for (li = list->lv_first; li != NULL;
12322 li = li->li_next)
12323 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012324 }
12325 }
12326
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012327 /* For "function(dict.func, [], dict)" and "func" is a partial
12328 * use "dict". That is backwards compatible. */
12329 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012330 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012331 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012332 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12333 ++pt->pt_dict->dv_refcount;
12334 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012335 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012336 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012337 /* If the dict was bound automatically the result is also
12338 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012339 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012340 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012341 if (pt->pt_dict != NULL)
12342 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012343 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012344
12345 pt->pt_refcount = 1;
12346 pt->pt_name = name;
12347 func_ref(pt->pt_name);
12348 }
12349 rettv->v_type = VAR_PARTIAL;
12350 rettv->vval.v_partial = pt;
12351 }
12352 else
12353 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012354 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012355 rettv->v_type = VAR_FUNC;
12356 rettv->vval.v_string = name;
12357 func_ref(name);
12358 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012359 }
12360}
12361
12362/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012363 * "garbagecollect()" function
12364 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012366f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012367{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012368 /* This is postponed until we are back at the toplevel, because we may be
12369 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12370 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012371
12372 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12373 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012374}
12375
12376/*
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +020012377 * "garbagecollect_for_testing()" function
12378 */
12379 static void
12380f_garbagecollect_for_testing(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12381{
12382 /* This is dangerous, any Lists and Dicts used internally may be freed
12383 * while still in use. */
12384 garbage_collect(TRUE);
12385}
12386
12387/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012388 * "get()" function
12389 */
12390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012391f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012392{
Bram Moolenaar33570922005-01-25 22:26:29 +000012393 listitem_T *li;
12394 list_T *l;
12395 dictitem_T *di;
12396 dict_T *d;
12397 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012398
Bram Moolenaare9a41262005-01-15 22:18:47 +000012399 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012400 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012401 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012403 int error = FALSE;
12404
12405 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12406 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012407 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012408 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012410 else if (argvars[0].v_type == VAR_DICT)
12411 {
12412 if ((d = argvars[0].vval.v_dict) != NULL)
12413 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012414 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012415 if (di != NULL)
12416 tv = &di->di_tv;
12417 }
12418 }
12419 else
12420 EMSG2(_(e_listdictarg), "get()");
12421
12422 if (tv == NULL)
12423 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012424 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012425 copy_tv(&argvars[2], rettv);
12426 }
12427 else
12428 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012429}
12430
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012431static 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 +000012432
12433/*
12434 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012435 * Return a range (from start to end) of lines in rettv from the specified
12436 * buffer.
12437 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012438 */
12439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012440get_buffer_lines(
12441 buf_T *buf,
12442 linenr_T start,
12443 linenr_T end,
12444 int retlist,
12445 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012446{
12447 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012448
Bram Moolenaar959a1432013-12-14 12:17:38 +010012449 rettv->v_type = VAR_STRING;
12450 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012451 if (retlist && rettv_list_alloc(rettv) == FAIL)
12452 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012453
12454 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12455 return;
12456
12457 if (!retlist)
12458 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012459 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12460 p = ml_get_buf(buf, start, FALSE);
12461 else
12462 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012463 rettv->vval.v_string = vim_strsave(p);
12464 }
12465 else
12466 {
12467 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012468 return;
12469
12470 if (start < 1)
12471 start = 1;
12472 if (end > buf->b_ml.ml_line_count)
12473 end = buf->b_ml.ml_line_count;
12474 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012475 if (list_append_string(rettv->vval.v_list,
12476 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012477 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012478 }
12479}
12480
12481/*
12482 * "getbufline()" function
12483 */
12484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012485f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012486{
12487 linenr_T lnum;
12488 linenr_T end;
12489 buf_T *buf;
12490
12491 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12492 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012493 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012494 --emsg_off;
12495
Bram Moolenaar661b1822005-07-28 22:36:45 +000012496 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012497 if (argvars[2].v_type == VAR_UNKNOWN)
12498 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012499 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012500 end = get_tv_lnum_buf(&argvars[2], buf);
12501
Bram Moolenaar342337a2005-07-21 21:11:17 +000012502 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012503}
12504
Bram Moolenaar0d660222005-01-07 21:51:51 +000012505/*
12506 * "getbufvar()" function
12507 */
12508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012509f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012510{
12511 buf_T *buf;
12512 buf_T *save_curbuf;
12513 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012514 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012515 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12518 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012519 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012520 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012521
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012522 rettv->v_type = VAR_STRING;
12523 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012524
12525 if (buf != NULL && varname != NULL)
12526 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012527 /* set curbuf to be our buf, temporarily */
12528 save_curbuf = curbuf;
12529 curbuf = buf;
12530
Bram Moolenaar0d660222005-01-07 21:51:51 +000012531 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012532 {
12533 if (get_option_tv(&varname, rettv, TRUE) == OK)
12534 done = TRUE;
12535 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012536 else if (STRCMP(varname, "changedtick") == 0)
12537 {
12538 rettv->v_type = VAR_NUMBER;
12539 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012540 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012541 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542 else
12543 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012544 /* Look up the variable. */
12545 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12546 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12547 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012548 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012549 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012550 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012551 done = TRUE;
12552 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012553 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012554
12555 /* restore previous notion of curbuf */
12556 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012557 }
12558
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012559 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12560 /* use the default value */
12561 copy_tv(&argvars[2], rettv);
12562
Bram Moolenaar0d660222005-01-07 21:51:51 +000012563 --emsg_off;
12564}
12565
12566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 * "getchar()" function
12568 */
12569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012570f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571{
12572 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012573 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012575 /* Position the cursor. Needed after a message that ends in a space. */
12576 windgoto(msg_row, msg_col);
12577
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578 ++no_mapping;
12579 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012580 for (;;)
12581 {
12582 if (argvars[0].v_type == VAR_UNKNOWN)
12583 /* getchar(): blocking wait. */
12584 n = safe_vgetc();
12585 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12586 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012587 n = vpeekc_any();
12588 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012589 /* illegal argument or getchar(0) and no char avail: return zero */
12590 n = 0;
12591 else
12592 /* getchar(0) and char avail: return char */
12593 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012594
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012595 if (n == K_IGNORE)
12596 continue;
12597 break;
12598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599 --no_mapping;
12600 --allow_keys;
12601
Bram Moolenaar219b8702006-11-01 14:32:36 +000012602 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12603 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12604 vimvars[VV_MOUSE_COL].vv_nr = 0;
12605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012606 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607 if (IS_SPECIAL(n) || mod_mask != 0)
12608 {
12609 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12610 int i = 0;
12611
12612 /* Turn a special key into three bytes, plus modifier. */
12613 if (mod_mask != 0)
12614 {
12615 temp[i++] = K_SPECIAL;
12616 temp[i++] = KS_MODIFIER;
12617 temp[i++] = mod_mask;
12618 }
12619 if (IS_SPECIAL(n))
12620 {
12621 temp[i++] = K_SPECIAL;
12622 temp[i++] = K_SECOND(n);
12623 temp[i++] = K_THIRD(n);
12624 }
12625#ifdef FEAT_MBYTE
12626 else if (has_mbyte)
12627 i += (*mb_char2bytes)(n, temp + i);
12628#endif
12629 else
12630 temp[i++] = n;
12631 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012632 rettv->v_type = VAR_STRING;
12633 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012634
12635#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012636 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012637 {
12638 int row = mouse_row;
12639 int col = mouse_col;
12640 win_T *win;
12641 linenr_T lnum;
12642# ifdef FEAT_WINDOWS
12643 win_T *wp;
12644# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012645 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012646
12647 if (row >= 0 && col >= 0)
12648 {
12649 /* Find the window at the mouse coordinates and compute the
12650 * text position. */
12651 win = mouse_find_win(&row, &col);
12652 (void)mouse_comp_pos(win, &row, &col, &lnum);
12653# ifdef FEAT_WINDOWS
12654 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012655 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012656# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012657 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012658 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12659 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12660 }
12661 }
12662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663 }
12664}
12665
12666/*
12667 * "getcharmod()" function
12668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012670f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012672 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673}
12674
12675/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012676 * "getcharsearch()" function
12677 */
12678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012679f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012680{
12681 if (rettv_dict_alloc(rettv) != FAIL)
12682 {
12683 dict_T *dict = rettv->vval.v_dict;
12684
12685 dict_add_nr_str(dict, "char", 0L, last_csearch());
12686 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12687 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12688 }
12689}
12690
12691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 * "getcmdline()" function
12693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012695f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 rettv->v_type = VAR_STRING;
12698 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699}
12700
12701/*
12702 * "getcmdpos()" function
12703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012705f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708}
12709
12710/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012711 * "getcmdtype()" function
12712 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012714f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012715{
12716 rettv->v_type = VAR_STRING;
12717 rettv->vval.v_string = alloc(2);
12718 if (rettv->vval.v_string != NULL)
12719 {
12720 rettv->vval.v_string[0] = get_cmdline_type();
12721 rettv->vval.v_string[1] = NUL;
12722 }
12723}
12724
12725/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012726 * "getcmdwintype()" function
12727 */
12728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012729f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012730{
12731 rettv->v_type = VAR_STRING;
12732 rettv->vval.v_string = NULL;
12733#ifdef FEAT_CMDWIN
12734 rettv->vval.v_string = alloc(2);
12735 if (rettv->vval.v_string != NULL)
12736 {
12737 rettv->vval.v_string[0] = cmdwin_type;
12738 rettv->vval.v_string[1] = NUL;
12739 }
12740#endif
12741}
12742
12743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 * "getcwd()" function
12745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012747f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012749 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012750 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012752 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012753 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012754
12755 wp = find_tabwin(&argvars[0], &argvars[1]);
12756 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012758 if (wp->w_localdir != NULL)
12759 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12760 else if(globaldir != NULL)
12761 rettv->vval.v_string = vim_strsave(globaldir);
12762 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012763 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012764 cwd = alloc(MAXPATHL);
12765 if (cwd != NULL)
12766 {
12767 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12768 rettv->vval.v_string = vim_strsave(cwd);
12769 vim_free(cwd);
12770 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012771 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012772#ifdef BACKSLASH_IN_FILENAME
12773 if (rettv->vval.v_string != NULL)
12774 slash_adjust(rettv->vval.v_string);
12775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776 }
12777}
12778
12779/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012780 * "getfontname()" function
12781 */
12782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012783f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012784{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785 rettv->v_type = VAR_STRING;
12786 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012787#ifdef FEAT_GUI
12788 if (gui.in_use)
12789 {
12790 GuiFont font;
12791 char_u *name = NULL;
12792
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012793 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012794 {
12795 /* Get the "Normal" font. Either the name saved by
12796 * hl_set_font_name() or from the font ID. */
12797 font = gui.norm_font;
12798 name = hl_get_font_name();
12799 }
12800 else
12801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012803 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12804 return;
12805 font = gui_mch_get_font(name, FALSE);
12806 if (font == NOFONT)
12807 return; /* Invalid font name, return empty string. */
12808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012810 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012811 gui_mch_free_font(font);
12812 }
12813#endif
12814}
12815
12816/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012817 * "getfperm({fname})" function
12818 */
12819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012820f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012821{
12822 char_u *fname;
12823 struct stat st;
12824 char_u *perm = NULL;
12825 char_u flags[] = "rwx";
12826 int i;
12827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012831 if (mch_stat((char *)fname, &st) >= 0)
12832 {
12833 perm = vim_strsave((char_u *)"---------");
12834 if (perm != NULL)
12835 {
12836 for (i = 0; i < 9; i++)
12837 {
12838 if (st.st_mode & (1 << (8 - i)))
12839 perm[i] = flags[i % 3];
12840 }
12841 }
12842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012844}
12845
12846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847 * "getfsize({fname})" function
12848 */
12849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012850f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851{
12852 char_u *fname;
12853 struct stat st;
12854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858
12859 if (mch_stat((char *)fname, &st) >= 0)
12860 {
12861 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012866
12867 /* non-perfect check for overflow */
12868 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12869 rettv->vval.v_number = -2;
12870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 }
12872 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874}
12875
12876/*
12877 * "getftime({fname})" function
12878 */
12879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012880f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881{
12882 char_u *fname;
12883 struct stat st;
12884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886
12887 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891}
12892
12893/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012894 * "getftype({fname})" function
12895 */
12896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012897f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012898{
12899 char_u *fname;
12900 struct stat st;
12901 char_u *type = NULL;
12902 char *t;
12903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012906 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012907 if (mch_lstat((char *)fname, &st) >= 0)
12908 {
12909#ifdef S_ISREG
12910 if (S_ISREG(st.st_mode))
12911 t = "file";
12912 else if (S_ISDIR(st.st_mode))
12913 t = "dir";
12914# ifdef S_ISLNK
12915 else if (S_ISLNK(st.st_mode))
12916 t = "link";
12917# endif
12918# ifdef S_ISBLK
12919 else if (S_ISBLK(st.st_mode))
12920 t = "bdev";
12921# endif
12922# ifdef S_ISCHR
12923 else if (S_ISCHR(st.st_mode))
12924 t = "cdev";
12925# endif
12926# ifdef S_ISFIFO
12927 else if (S_ISFIFO(st.st_mode))
12928 t = "fifo";
12929# endif
12930# ifdef S_ISSOCK
12931 else if (S_ISSOCK(st.st_mode))
12932 t = "fifo";
12933# endif
12934 else
12935 t = "other";
12936#else
12937# ifdef S_IFMT
12938 switch (st.st_mode & S_IFMT)
12939 {
12940 case S_IFREG: t = "file"; break;
12941 case S_IFDIR: t = "dir"; break;
12942# ifdef S_IFLNK
12943 case S_IFLNK: t = "link"; break;
12944# endif
12945# ifdef S_IFBLK
12946 case S_IFBLK: t = "bdev"; break;
12947# endif
12948# ifdef S_IFCHR
12949 case S_IFCHR: t = "cdev"; break;
12950# endif
12951# ifdef S_IFIFO
12952 case S_IFIFO: t = "fifo"; break;
12953# endif
12954# ifdef S_IFSOCK
12955 case S_IFSOCK: t = "socket"; break;
12956# endif
12957 default: t = "other";
12958 }
12959# else
12960 if (mch_isdir(fname))
12961 t = "dir";
12962 else
12963 t = "file";
12964# endif
12965#endif
12966 type = vim_strsave((char_u *)t);
12967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012968 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012969}
12970
12971/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012972 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012973 */
12974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012975f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012976{
12977 linenr_T lnum;
12978 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012979 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012980
12981 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012982 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012983 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012984 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012985 retlist = FALSE;
12986 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012987 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012988 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012989 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012990 retlist = TRUE;
12991 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012992
Bram Moolenaar342337a2005-07-21 21:11:17 +000012993 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012994}
12995
12996/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012997 * "getmatches()" function
12998 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013000f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013001{
13002#ifdef FEAT_SEARCH_EXTRA
13003 dict_T *dict;
13004 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013005 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013006
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013007 if (rettv_list_alloc(rettv) == OK)
13008 {
13009 while (cur != NULL)
13010 {
13011 dict = dict_alloc();
13012 if (dict == NULL)
13013 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013014 if (cur->match.regprog == NULL)
13015 {
13016 /* match added with matchaddpos() */
13017 for (i = 0; i < MAXPOSMATCH; ++i)
13018 {
13019 llpos_T *llpos;
13020 char buf[6];
13021 list_T *l;
13022
13023 llpos = &cur->pos.pos[i];
13024 if (llpos->lnum == 0)
13025 break;
13026 l = list_alloc();
13027 if (l == NULL)
13028 break;
13029 list_append_number(l, (varnumber_T)llpos->lnum);
13030 if (llpos->col > 0)
13031 {
13032 list_append_number(l, (varnumber_T)llpos->col);
13033 list_append_number(l, (varnumber_T)llpos->len);
13034 }
13035 sprintf(buf, "pos%d", i + 1);
13036 dict_add_list(dict, buf, l);
13037 }
13038 }
13039 else
13040 {
13041 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13042 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013043 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013044 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13045 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013046# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013047 if (cur->conceal_char)
13048 {
13049 char_u buf[MB_MAXBYTES + 1];
13050
13051 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13052 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13053 }
13054# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013055 list_append_dict(rettv->vval.v_list, dict);
13056 cur = cur->next;
13057 }
13058 }
13059#endif
13060}
13061
13062/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013063 * "getpid()" function
13064 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013066f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013067{
13068 rettv->vval.v_number = mch_get_pid();
13069}
13070
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013072getpos_both(
13073 typval_T *argvars,
13074 typval_T *rettv,
13075 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013076{
Bram Moolenaara5525202006-03-02 22:52:09 +000013077 pos_T *fp;
13078 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013079 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013080
13081 if (rettv_list_alloc(rettv) == OK)
13082 {
13083 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013084 if (getcurpos)
13085 fp = &curwin->w_cursor;
13086 else
13087 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013088 if (fnum != -1)
13089 list_append_number(l, (varnumber_T)fnum);
13090 else
13091 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013092 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13093 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013094 list_append_number(l, (fp != NULL)
13095 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013096 : (varnumber_T)0);
13097 list_append_number(l,
13098#ifdef FEAT_VIRTUALEDIT
13099 (fp != NULL) ? (varnumber_T)fp->coladd :
13100#endif
13101 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013102 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013103 {
13104 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013105 list_append_number(l, curwin->w_curswant == MAXCOL ?
13106 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013107 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013108 }
13109 else
13110 rettv->vval.v_number = FALSE;
13111}
13112
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013113
13114/*
13115 * "getcurpos()" function
13116 */
13117 static void
13118f_getcurpos(typval_T *argvars, typval_T *rettv)
13119{
13120 getpos_both(argvars, rettv, TRUE);
13121}
13122
13123/*
13124 * "getpos(string)" function
13125 */
13126 static void
13127f_getpos(typval_T *argvars, typval_T *rettv)
13128{
13129 getpos_both(argvars, rettv, FALSE);
13130}
13131
Bram Moolenaara5525202006-03-02 22:52:09 +000013132/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013133 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013134 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013136f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013137{
13138#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013139 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013140#endif
13141
Bram Moolenaar2641f772005-03-25 21:58:17 +000013142#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013143 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013144 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013145 wp = NULL;
13146 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13147 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013148 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013149 if (wp == NULL)
13150 return;
13151 }
13152
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013153 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013154 }
13155#endif
13156}
13157
13158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 * "getreg()" function
13160 */
13161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013162f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163{
13164 char_u *strregname;
13165 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013166 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013167 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013168 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013170 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013172 strregname = get_tv_string_chk(&argvars[0]);
13173 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013174 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013176 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013177 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13178 return_list = get_tv_number_chk(&argvars[2], &error);
13179 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013182 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013183
13184 if (error)
13185 return;
13186
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 regname = (strregname == NULL ? '"' : *strregname);
13188 if (regname == 0)
13189 regname = '"';
13190
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013191 if (return_list)
13192 {
13193 rettv->v_type = VAR_LIST;
13194 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13195 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013196 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013197 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013198 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013199 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013200 }
13201 else
13202 {
13203 rettv->v_type = VAR_STRING;
13204 rettv->vval.v_string = get_reg_contents(regname,
13205 arg2 ? GREG_EXPR_SRC : 0);
13206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207}
13208
13209/*
13210 * "getregtype()" function
13211 */
13212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013213f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214{
13215 char_u *strregname;
13216 int regname;
13217 char_u buf[NUMBUFLEN + 2];
13218 long reglen = 0;
13219
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013220 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013221 {
13222 strregname = get_tv_string_chk(&argvars[0]);
13223 if (strregname == NULL) /* type error; errmsg already given */
13224 {
13225 rettv->v_type = VAR_STRING;
13226 rettv->vval.v_string = NULL;
13227 return;
13228 }
13229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 else
13231 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013232 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233
13234 regname = (strregname == NULL ? '"' : *strregname);
13235 if (regname == 0)
13236 regname = '"';
13237
13238 buf[0] = NUL;
13239 buf[1] = NUL;
13240 switch (get_reg_type(regname, &reglen))
13241 {
13242 case MLINE: buf[0] = 'V'; break;
13243 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 case MBLOCK:
13245 buf[0] = Ctrl_V;
13246 sprintf((char *)buf + 1, "%ld", reglen + 1);
13247 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249 rettv->v_type = VAR_STRING;
13250 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251}
13252
13253/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013254 * "gettabvar()" function
13255 */
13256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013257f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013258{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013259 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013260 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013261 dictitem_T *v;
13262 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013263 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013264
13265 rettv->v_type = VAR_STRING;
13266 rettv->vval.v_string = NULL;
13267
13268 varname = get_tv_string_chk(&argvars[1]);
13269 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13270 if (tp != NULL && varname != NULL)
13271 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013272 /* Set tp to be our tabpage, temporarily. Also set the window to the
13273 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013274 if (switch_win(&oldcurwin, &oldtabpage,
13275 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013276 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013277 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013278 /* look up the variable */
13279 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13280 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13281 if (v != NULL)
13282 {
13283 copy_tv(&v->di_tv, rettv);
13284 done = TRUE;
13285 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013286 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013287
13288 /* restore previous notion of curwin */
13289 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013290 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013291
13292 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013293 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013294}
13295
13296/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013297 * "gettabwinvar()" function
13298 */
13299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013300f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013301{
13302 getwinvar(argvars, rettv, 1);
13303}
13304
13305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 * "getwinposx()" function
13307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013309f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312#ifdef FEAT_GUI
13313 if (gui.in_use)
13314 {
13315 int x, y;
13316
13317 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013318 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 }
13320#endif
13321}
13322
13323/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013324 * "win_findbuf()" function
13325 */
13326 static void
13327f_win_findbuf(typval_T *argvars, typval_T *rettv)
13328{
13329 if (rettv_list_alloc(rettv) != FAIL)
13330 win_findbuf(argvars, rettv->vval.v_list);
13331}
13332
13333/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013334 * "win_getid()" function
13335 */
13336 static void
13337f_win_getid(typval_T *argvars, typval_T *rettv)
13338{
13339 rettv->vval.v_number = win_getid(argvars);
13340}
13341
13342/*
13343 * "win_gotoid()" function
13344 */
13345 static void
13346f_win_gotoid(typval_T *argvars, typval_T *rettv)
13347{
13348 rettv->vval.v_number = win_gotoid(argvars);
13349}
13350
13351/*
13352 * "win_id2tabwin()" function
13353 */
13354 static void
13355f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13356{
13357 if (rettv_list_alloc(rettv) != FAIL)
13358 win_id2tabwin(argvars, rettv->vval.v_list);
13359}
13360
13361/*
13362 * "win_id2win()" function
13363 */
13364 static void
13365f_win_id2win(typval_T *argvars, typval_T *rettv)
13366{
13367 rettv->vval.v_number = win_id2win(argvars);
13368}
13369
13370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371 * "getwinposy()" function
13372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013374f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377#ifdef FEAT_GUI
13378 if (gui.in_use)
13379 {
13380 int x, y;
13381
13382 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013383 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 }
13385#endif
13386}
13387
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013388/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013389 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013390 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013391 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013392find_win_by_nr(
13393 typval_T *vp,
13394 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013395{
13396#ifdef FEAT_WINDOWS
13397 win_T *wp;
13398#endif
13399 int nr;
13400
13401 nr = get_tv_number_chk(vp, NULL);
13402
13403#ifdef FEAT_WINDOWS
13404 if (nr < 0)
13405 return NULL;
13406 if (nr == 0)
13407 return curwin;
13408
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013409 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13410 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013411 if (--nr <= 0)
13412 break;
13413 return wp;
13414#else
13415 if (nr == 0 || nr == 1)
13416 return curwin;
13417 return NULL;
13418#endif
13419}
13420
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013422 * Find window specified by "wvp" in tabpage "tvp".
13423 */
13424 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013425find_tabwin(
13426 typval_T *wvp, /* VAR_UNKNOWN for current window */
13427 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013428{
13429 win_T *wp = NULL;
13430 tabpage_T *tp = NULL;
13431 long n;
13432
13433 if (wvp->v_type != VAR_UNKNOWN)
13434 {
13435 if (tvp->v_type != VAR_UNKNOWN)
13436 {
13437 n = get_tv_number(tvp);
13438 if (n >= 0)
13439 tp = find_tabpage(n);
13440 }
13441 else
13442 tp = curtab;
13443
13444 if (tp != NULL)
13445 wp = find_win_by_nr(wvp, tp);
13446 }
13447 else
13448 wp = curwin;
13449
13450 return wp;
13451}
13452
13453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454 * "getwinvar()" function
13455 */
13456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013457f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013459 getwinvar(argvars, rettv, 0);
13460}
13461
13462/*
13463 * getwinvar() and gettabwinvar()
13464 */
13465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013466getwinvar(
13467 typval_T *argvars,
13468 typval_T *rettv,
13469 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013470{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013471 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013473 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013474 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013475 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013476#ifdef FEAT_WINDOWS
13477 win_T *oldcurwin;
13478 tabpage_T *oldtabpage;
13479 int need_switch_win;
13480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013482#ifdef FEAT_WINDOWS
13483 if (off == 1)
13484 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13485 else
13486 tp = curtab;
13487#endif
13488 win = find_win_by_nr(&argvars[off], tp);
13489 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013490 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013492 rettv->v_type = VAR_STRING;
13493 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494
13495 if (win != NULL && varname != NULL)
13496 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013497#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013498 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013499 * otherwise the window is not valid. Only do this when needed,
13500 * autocommands get blocked. */
13501 need_switch_win = !(tp == curtab && win == curwin);
13502 if (!need_switch_win
13503 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13504#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013505 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013506 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013507 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013508 if (get_option_tv(&varname, rettv, 1) == OK)
13509 done = TRUE;
13510 }
13511 else
13512 {
13513 /* Look up the variable. */
13514 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13515 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13516 varname, FALSE);
13517 if (v != NULL)
13518 {
13519 copy_tv(&v->di_tv, rettv);
13520 done = TRUE;
13521 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013524
Bram Moolenaarba117c22015-09-29 16:53:22 +020013525#ifdef FEAT_WINDOWS
13526 if (need_switch_win)
13527 /* restore previous notion of curwin */
13528 restore_win(oldcurwin, oldtabpage, TRUE);
13529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 }
13531
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013532 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13533 /* use the default return value */
13534 copy_tv(&argvars[off + 2], rettv);
13535
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536 --emsg_off;
13537}
13538
13539/*
13540 * "glob()" function
13541 */
13542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013543f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013545 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013547 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013549 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013550 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013552 if (argvars[1].v_type != VAR_UNKNOWN)
13553 {
13554 if (get_tv_number_chk(&argvars[1], &error))
13555 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013557 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013558 if (get_tv_number_chk(&argvars[2], &error))
13559 {
13560 rettv->v_type = VAR_LIST;
13561 rettv->vval.v_list = NULL;
13562 }
13563 if (argvars[3].v_type != VAR_UNKNOWN
13564 && get_tv_number_chk(&argvars[3], &error))
13565 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013566 }
13567 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013568 if (!error)
13569 {
13570 ExpandInit(&xpc);
13571 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013572 if (p_wic)
13573 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013574 if (rettv->v_type == VAR_STRING)
13575 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013576 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013577 else if (rettv_list_alloc(rettv) != FAIL)
13578 {
13579 int i;
13580
13581 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13582 NULL, options, WILD_ALL_KEEP);
13583 for (i = 0; i < xpc.xp_numfiles; i++)
13584 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13585
13586 ExpandCleanup(&xpc);
13587 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013588 }
13589 else
13590 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591}
13592
13593/*
13594 * "globpath()" function
13595 */
13596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013597f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013599 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013601 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013602 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013603 garray_T ga;
13604 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013606 /* When the optional second argument is non-zero, don't remove matches
13607 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013609 if (argvars[2].v_type != VAR_UNKNOWN)
13610 {
13611 if (get_tv_number_chk(&argvars[2], &error))
13612 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013613 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013614 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013615 if (get_tv_number_chk(&argvars[3], &error))
13616 {
13617 rettv->v_type = VAR_LIST;
13618 rettv->vval.v_list = NULL;
13619 }
13620 if (argvars[4].v_type != VAR_UNKNOWN
13621 && get_tv_number_chk(&argvars[4], &error))
13622 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013623 }
13624 }
13625 if (file != NULL && !error)
13626 {
13627 ga_init2(&ga, (int)sizeof(char_u *), 10);
13628 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13629 if (rettv->v_type == VAR_STRING)
13630 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13631 else if (rettv_list_alloc(rettv) != FAIL)
13632 for (i = 0; i < ga.ga_len; ++i)
13633 list_append_string(rettv->vval.v_list,
13634 ((char_u **)(ga.ga_data))[i], -1);
13635 ga_clear_strings(&ga);
13636 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013637 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013638 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639}
13640
13641/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013642 * "glob2regpat()" function
13643 */
13644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013645f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013646{
13647 char_u *pat = get_tv_string_chk(&argvars[0]);
13648
13649 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013650 rettv->vval.v_string = (pat == NULL)
13651 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013652}
13653
13654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655 * "has()" function
13656 */
13657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013658f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659{
13660 int i;
13661 char_u *name;
13662 int n = FALSE;
13663 static char *(has_list[]) =
13664 {
13665#ifdef AMIGA
13666 "amiga",
13667# ifdef FEAT_ARP
13668 "arp",
13669# endif
13670#endif
13671#ifdef __BEOS__
13672 "beos",
13673#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013674#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 "mac",
13676#endif
13677#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013678 "macunix", /* built with 'darwin' enabled */
13679#endif
13680#if defined(__APPLE__) && __APPLE__ == 1
13681 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683#ifdef __QNX__
13684 "qnx",
13685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686#ifdef UNIX
13687 "unix",
13688#endif
13689#ifdef VMS
13690 "vms",
13691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692#ifdef WIN32
13693 "win32",
13694#endif
13695#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13696 "win32unix",
13697#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013698#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 "win64",
13700#endif
13701#ifdef EBCDIC
13702 "ebcdic",
13703#endif
13704#ifndef CASE_INSENSITIVE_FILENAME
13705 "fname_case",
13706#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013707#ifdef HAVE_ACL
13708 "acl",
13709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710#ifdef FEAT_ARABIC
13711 "arabic",
13712#endif
13713#ifdef FEAT_AUTOCMD
13714 "autocmd",
13715#endif
13716#ifdef FEAT_BEVAL
13717 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013718# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13719 "balloon_multiline",
13720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721#endif
13722#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13723 "builtin_terms",
13724# ifdef ALL_BUILTIN_TCAPS
13725 "all_builtin_terms",
13726# endif
13727#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013728#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13729 || defined(FEAT_GUI_W32) \
13730 || defined(FEAT_GUI_MOTIF))
13731 "browsefilter",
13732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733#ifdef FEAT_BYTEOFF
13734 "byte_offset",
13735#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013736#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013737 "channel",
13738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739#ifdef FEAT_CINDENT
13740 "cindent",
13741#endif
13742#ifdef FEAT_CLIENTSERVER
13743 "clientserver",
13744#endif
13745#ifdef FEAT_CLIPBOARD
13746 "clipboard",
13747#endif
13748#ifdef FEAT_CMDL_COMPL
13749 "cmdline_compl",
13750#endif
13751#ifdef FEAT_CMDHIST
13752 "cmdline_hist",
13753#endif
13754#ifdef FEAT_COMMENTS
13755 "comments",
13756#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013757#ifdef FEAT_CONCEAL
13758 "conceal",
13759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760#ifdef FEAT_CRYPT
13761 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013762 "crypt-blowfish",
13763 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764#endif
13765#ifdef FEAT_CSCOPE
13766 "cscope",
13767#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013768#ifdef FEAT_CURSORBIND
13769 "cursorbind",
13770#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013771#ifdef CURSOR_SHAPE
13772 "cursorshape",
13773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774#ifdef DEBUG
13775 "debug",
13776#endif
13777#ifdef FEAT_CON_DIALOG
13778 "dialog_con",
13779#endif
13780#ifdef FEAT_GUI_DIALOG
13781 "dialog_gui",
13782#endif
13783#ifdef FEAT_DIFF
13784 "diff",
13785#endif
13786#ifdef FEAT_DIGRAPHS
13787 "digraphs",
13788#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013789#ifdef FEAT_DIRECTX
13790 "directx",
13791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792#ifdef FEAT_DND
13793 "dnd",
13794#endif
13795#ifdef FEAT_EMACS_TAGS
13796 "emacs_tags",
13797#endif
13798 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013799 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800#ifdef FEAT_SEARCH_EXTRA
13801 "extra_search",
13802#endif
13803#ifdef FEAT_FKMAP
13804 "farsi",
13805#endif
13806#ifdef FEAT_SEARCHPATH
13807 "file_in_path",
13808#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013809#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013810 "filterpipe",
13811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812#ifdef FEAT_FIND_ID
13813 "find_in_path",
13814#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013815#ifdef FEAT_FLOAT
13816 "float",
13817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818#ifdef FEAT_FOLDING
13819 "folding",
13820#endif
13821#ifdef FEAT_FOOTER
13822 "footer",
13823#endif
13824#if !defined(USE_SYSTEM) && defined(UNIX)
13825 "fork",
13826#endif
13827#ifdef FEAT_GETTEXT
13828 "gettext",
13829#endif
13830#ifdef FEAT_GUI
13831 "gui",
13832#endif
13833#ifdef FEAT_GUI_ATHENA
13834# ifdef FEAT_GUI_NEXTAW
13835 "gui_neXtaw",
13836# else
13837 "gui_athena",
13838# endif
13839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840#ifdef FEAT_GUI_GTK
13841 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013842# ifdef USE_GTK3
13843 "gui_gtk3",
13844# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013848#ifdef FEAT_GUI_GNOME
13849 "gui_gnome",
13850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851#ifdef FEAT_GUI_MAC
13852 "gui_mac",
13853#endif
13854#ifdef FEAT_GUI_MOTIF
13855 "gui_motif",
13856#endif
13857#ifdef FEAT_GUI_PHOTON
13858 "gui_photon",
13859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860#ifdef FEAT_GUI_W32
13861 "gui_win32",
13862#endif
13863#ifdef FEAT_HANGULIN
13864 "hangul_input",
13865#endif
13866#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13867 "iconv",
13868#endif
13869#ifdef FEAT_INS_EXPAND
13870 "insert_expand",
13871#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013872#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013873 "job",
13874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875#ifdef FEAT_JUMPLIST
13876 "jumplist",
13877#endif
13878#ifdef FEAT_KEYMAP
13879 "keymap",
13880#endif
13881#ifdef FEAT_LANGMAP
13882 "langmap",
13883#endif
13884#ifdef FEAT_LIBCALL
13885 "libcall",
13886#endif
13887#ifdef FEAT_LINEBREAK
13888 "linebreak",
13889#endif
13890#ifdef FEAT_LISP
13891 "lispindent",
13892#endif
13893#ifdef FEAT_LISTCMDS
13894 "listcmds",
13895#endif
13896#ifdef FEAT_LOCALMAP
13897 "localmap",
13898#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013899#ifdef FEAT_LUA
13900# ifndef DYNAMIC_LUA
13901 "lua",
13902# endif
13903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904#ifdef FEAT_MENU
13905 "menu",
13906#endif
13907#ifdef FEAT_SESSION
13908 "mksession",
13909#endif
13910#ifdef FEAT_MODIFY_FNAME
13911 "modify_fname",
13912#endif
13913#ifdef FEAT_MOUSE
13914 "mouse",
13915#endif
13916#ifdef FEAT_MOUSESHAPE
13917 "mouseshape",
13918#endif
13919#if defined(UNIX) || defined(VMS)
13920# ifdef FEAT_MOUSE_DEC
13921 "mouse_dec",
13922# endif
13923# ifdef FEAT_MOUSE_GPM
13924 "mouse_gpm",
13925# endif
13926# ifdef FEAT_MOUSE_JSB
13927 "mouse_jsbterm",
13928# endif
13929# ifdef FEAT_MOUSE_NET
13930 "mouse_netterm",
13931# endif
13932# ifdef FEAT_MOUSE_PTERM
13933 "mouse_pterm",
13934# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013935# ifdef FEAT_MOUSE_SGR
13936 "mouse_sgr",
13937# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013938# ifdef FEAT_SYSMOUSE
13939 "mouse_sysmouse",
13940# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013941# ifdef FEAT_MOUSE_URXVT
13942 "mouse_urxvt",
13943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944# ifdef FEAT_MOUSE_XTERM
13945 "mouse_xterm",
13946# endif
13947#endif
13948#ifdef FEAT_MBYTE
13949 "multi_byte",
13950#endif
13951#ifdef FEAT_MBYTE_IME
13952 "multi_byte_ime",
13953#endif
13954#ifdef FEAT_MULTI_LANG
13955 "multi_lang",
13956#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013957#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013958#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013959 "mzscheme",
13960#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962#ifdef FEAT_OLE
13963 "ole",
13964#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013965 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966#ifdef FEAT_PATH_EXTRA
13967 "path_extra",
13968#endif
13969#ifdef FEAT_PERL
13970#ifndef DYNAMIC_PERL
13971 "perl",
13972#endif
13973#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013974#ifdef FEAT_PERSISTENT_UNDO
13975 "persistent_undo",
13976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977#ifdef FEAT_PYTHON
13978#ifndef DYNAMIC_PYTHON
13979 "python",
13980#endif
13981#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013982#ifdef FEAT_PYTHON3
13983#ifndef DYNAMIC_PYTHON3
13984 "python3",
13985#endif
13986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987#ifdef FEAT_POSTSCRIPT
13988 "postscript",
13989#endif
13990#ifdef FEAT_PRINTER
13991 "printer",
13992#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013993#ifdef FEAT_PROFILE
13994 "profile",
13995#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013996#ifdef FEAT_RELTIME
13997 "reltime",
13998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999#ifdef FEAT_QUICKFIX
14000 "quickfix",
14001#endif
14002#ifdef FEAT_RIGHTLEFT
14003 "rightleft",
14004#endif
14005#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14006 "ruby",
14007#endif
14008#ifdef FEAT_SCROLLBIND
14009 "scrollbind",
14010#endif
14011#ifdef FEAT_CMDL_INFO
14012 "showcmd",
14013 "cmdline_info",
14014#endif
14015#ifdef FEAT_SIGNS
14016 "signs",
14017#endif
14018#ifdef FEAT_SMARTINDENT
14019 "smartindent",
14020#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014021#ifdef STARTUPTIME
14022 "startuptime",
14023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024#ifdef FEAT_STL_OPT
14025 "statusline",
14026#endif
14027#ifdef FEAT_SUN_WORKSHOP
14028 "sun_workshop",
14029#endif
14030#ifdef FEAT_NETBEANS_INTG
14031 "netbeans_intg",
14032#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014033#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014034 "spell",
14035#endif
14036#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037 "syntax",
14038#endif
14039#if defined(USE_SYSTEM) || !defined(UNIX)
14040 "system",
14041#endif
14042#ifdef FEAT_TAG_BINS
14043 "tag_binary",
14044#endif
14045#ifdef FEAT_TAG_OLDSTATIC
14046 "tag_old_static",
14047#endif
14048#ifdef FEAT_TAG_ANYWHITE
14049 "tag_any_white",
14050#endif
14051#ifdef FEAT_TCL
14052# ifndef DYNAMIC_TCL
14053 "tcl",
14054# endif
14055#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014056#ifdef FEAT_TERMGUICOLORS
14057 "termguicolors",
14058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059#ifdef TERMINFO
14060 "terminfo",
14061#endif
14062#ifdef FEAT_TERMRESPONSE
14063 "termresponse",
14064#endif
14065#ifdef FEAT_TEXTOBJ
14066 "textobjects",
14067#endif
14068#ifdef HAVE_TGETENT
14069 "tgetent",
14070#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014071#ifdef FEAT_TIMERS
14072 "timers",
14073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074#ifdef FEAT_TITLE
14075 "title",
14076#endif
14077#ifdef FEAT_TOOLBAR
14078 "toolbar",
14079#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014080#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14081 "unnamedplus",
14082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083#ifdef FEAT_USR_CMDS
14084 "user-commands", /* was accidentally included in 5.4 */
14085 "user_commands",
14086#endif
14087#ifdef FEAT_VIMINFO
14088 "viminfo",
14089#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014090#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091 "vertsplit",
14092#endif
14093#ifdef FEAT_VIRTUALEDIT
14094 "virtualedit",
14095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097#ifdef FEAT_VISUALEXTRA
14098 "visualextra",
14099#endif
14100#ifdef FEAT_VREPLACE
14101 "vreplace",
14102#endif
14103#ifdef FEAT_WILDIGN
14104 "wildignore",
14105#endif
14106#ifdef FEAT_WILDMENU
14107 "wildmenu",
14108#endif
14109#ifdef FEAT_WINDOWS
14110 "windows",
14111#endif
14112#ifdef FEAT_WAK
14113 "winaltkeys",
14114#endif
14115#ifdef FEAT_WRITEBACKUP
14116 "writebackup",
14117#endif
14118#ifdef FEAT_XIM
14119 "xim",
14120#endif
14121#ifdef FEAT_XFONTSET
14122 "xfontset",
14123#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014124#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014125 "xpm",
14126 "xpm_w32", /* for backward compatibility */
14127#else
14128# if defined(HAVE_XPM)
14129 "xpm",
14130# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132#ifdef USE_XSMP
14133 "xsmp",
14134#endif
14135#ifdef USE_XSMP_INTERACT
14136 "xsmp_interact",
14137#endif
14138#ifdef FEAT_XCLIPBOARD
14139 "xterm_clipboard",
14140#endif
14141#ifdef FEAT_XTERM_SAVE
14142 "xterm_save",
14143#endif
14144#if defined(UNIX) && defined(FEAT_X11)
14145 "X11",
14146#endif
14147 NULL
14148 };
14149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014150 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 for (i = 0; has_list[i] != NULL; ++i)
14152 if (STRICMP(name, has_list[i]) == 0)
14153 {
14154 n = TRUE;
14155 break;
14156 }
14157
14158 if (n == FALSE)
14159 {
14160 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014161 {
14162 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014163 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014164 && vim_isdigit(name[6])
14165 && vim_isdigit(name[8])
14166 && vim_isdigit(name[10]))
14167 {
14168 int major = atoi((char *)name + 6);
14169 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014170
14171 /* Expect "patch-9.9.01234". */
14172 n = (major < VIM_VERSION_MAJOR
14173 || (major == VIM_VERSION_MAJOR
14174 && (minor < VIM_VERSION_MINOR
14175 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014176 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014177 }
14178 else
14179 n = has_patch(atoi((char *)name + 5));
14180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181 else if (STRICMP(name, "vim_starting") == 0)
14182 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014183#ifdef FEAT_MBYTE
14184 else if (STRICMP(name, "multi_byte_encoding") == 0)
14185 n = has_mbyte;
14186#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014187#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14188 else if (STRICMP(name, "balloon_multiline") == 0)
14189 n = multiline_balloon_available();
14190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191#ifdef DYNAMIC_TCL
14192 else if (STRICMP(name, "tcl") == 0)
14193 n = tcl_enabled(FALSE);
14194#endif
14195#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14196 else if (STRICMP(name, "iconv") == 0)
14197 n = iconv_enabled(FALSE);
14198#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014199#ifdef DYNAMIC_LUA
14200 else if (STRICMP(name, "lua") == 0)
14201 n = lua_enabled(FALSE);
14202#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014203#ifdef DYNAMIC_MZSCHEME
14204 else if (STRICMP(name, "mzscheme") == 0)
14205 n = mzscheme_enabled(FALSE);
14206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207#ifdef DYNAMIC_RUBY
14208 else if (STRICMP(name, "ruby") == 0)
14209 n = ruby_enabled(FALSE);
14210#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014211#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212#ifdef DYNAMIC_PYTHON
14213 else if (STRICMP(name, "python") == 0)
14214 n = python_enabled(FALSE);
14215#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014216#endif
14217#ifdef FEAT_PYTHON3
14218#ifdef DYNAMIC_PYTHON3
14219 else if (STRICMP(name, "python3") == 0)
14220 n = python3_enabled(FALSE);
14221#endif
14222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223#ifdef DYNAMIC_PERL
14224 else if (STRICMP(name, "perl") == 0)
14225 n = perl_enabled(FALSE);
14226#endif
14227#ifdef FEAT_GUI
14228 else if (STRICMP(name, "gui_running") == 0)
14229 n = (gui.in_use || gui.starting);
14230# ifdef FEAT_GUI_W32
14231 else if (STRICMP(name, "gui_win32s") == 0)
14232 n = gui_is_win32s();
14233# endif
14234# ifdef FEAT_BROWSE
14235 else if (STRICMP(name, "browse") == 0)
14236 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14237# endif
14238#endif
14239#ifdef FEAT_SYN_HL
14240 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014241 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242#endif
14243#if defined(WIN3264)
14244 else if (STRICMP(name, "win95") == 0)
14245 n = mch_windows95();
14246#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014247#ifdef FEAT_NETBEANS_INTG
14248 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014249 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 }
14252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014253 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254}
14255
14256/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014257 * "has_key()" function
14258 */
14259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014260f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014261{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014262 if (argvars[0].v_type != VAR_DICT)
14263 {
14264 EMSG(_(e_dictreq));
14265 return;
14266 }
14267 if (argvars[0].vval.v_dict == NULL)
14268 return;
14269
14270 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014271 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014272}
14273
14274/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014275 * "haslocaldir()" function
14276 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014278f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014279{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014280 win_T *wp = NULL;
14281
14282 wp = find_tabwin(&argvars[0], &argvars[1]);
14283 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014284}
14285
14286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 * "hasmapto()" function
14288 */
14289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014290f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291{
14292 char_u *name;
14293 char_u *mode;
14294 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014295 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014298 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299 mode = (char_u *)"nvo";
14300 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014301 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014302 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014303 if (argvars[2].v_type != VAR_UNKNOWN)
14304 abbr = get_tv_number(&argvars[2]);
14305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306
Bram Moolenaar2c932302006-03-18 21:42:09 +000014307 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014308 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014310 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311}
14312
14313/*
14314 * "histadd()" function
14315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014317f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318{
14319#ifdef FEAT_CMDHIST
14320 int histype;
14321 char_u *str;
14322 char_u buf[NUMBUFLEN];
14323#endif
14324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014325 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 if (check_restricted() || check_secure())
14327 return;
14328#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014329 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14330 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 if (histype >= 0)
14332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014333 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 if (*str != NUL)
14335 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014336 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014338 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 return;
14340 }
14341 }
14342#endif
14343}
14344
14345/*
14346 * "histdel()" function
14347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014349f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350{
14351#ifdef FEAT_CMDHIST
14352 int n;
14353 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014356 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14357 if (str == NULL)
14358 n = 0;
14359 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014362 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014364 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 else
14367 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014368 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014369 get_tv_string_buf(&argvars[1], buf));
14370 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371#endif
14372}
14373
14374/*
14375 * "histget()" function
14376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014378f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379{
14380#ifdef FEAT_CMDHIST
14381 int type;
14382 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014383 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014385 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14386 if (str == NULL)
14387 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 {
14390 type = get_histtype(str);
14391 if (argvars[1].v_type == VAR_UNKNOWN)
14392 idx = get_history_idx(type);
14393 else
14394 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14395 /* -1 on type error */
14396 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014401 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402}
14403
14404/*
14405 * "histnr()" function
14406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014408f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409{
14410 int i;
14411
14412#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 char_u *history = get_tv_string_chk(&argvars[0]);
14414
14415 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 if (i >= HIST_CMD && i < HIST_COUNT)
14417 i = get_history_idx(i);
14418 else
14419#endif
14420 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422}
14423
14424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 * "highlightID(name)" function
14426 */
14427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014428f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014430 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431}
14432
14433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014434 * "highlight_exists()" function
14435 */
14436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014437f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014438{
14439 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14440}
14441
14442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 * "hostname()" function
14444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014446f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447{
14448 char_u hostname[256];
14449
14450 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014451 rettv->v_type = VAR_STRING;
14452 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453}
14454
14455/*
14456 * iconv() function
14457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014459f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460{
14461#ifdef FEAT_MBYTE
14462 char_u buf1[NUMBUFLEN];
14463 char_u buf2[NUMBUFLEN];
14464 char_u *from, *to, *str;
14465 vimconv_T vimconv;
14466#endif
14467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014468 rettv->v_type = VAR_STRING;
14469 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470
14471#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014472 str = get_tv_string(&argvars[0]);
14473 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14474 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 vimconv.vc_type = CONV_NONE;
14476 convert_setup(&vimconv, from, to);
14477
14478 /* If the encodings are equal, no conversion needed. */
14479 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014482 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483
14484 convert_setup(&vimconv, NULL, NULL);
14485 vim_free(from);
14486 vim_free(to);
14487#endif
14488}
14489
14490/*
14491 * "indent()" function
14492 */
14493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014494f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495{
14496 linenr_T lnum;
14497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014498 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014502 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503}
14504
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014505/*
14506 * "index()" function
14507 */
14508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014509f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014510{
Bram Moolenaar33570922005-01-25 22:26:29 +000014511 list_T *l;
14512 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014513 long idx = 0;
14514 int ic = FALSE;
14515
14516 rettv->vval.v_number = -1;
14517 if (argvars[0].v_type != VAR_LIST)
14518 {
14519 EMSG(_(e_listreq));
14520 return;
14521 }
14522 l = argvars[0].vval.v_list;
14523 if (l != NULL)
14524 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014525 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014526 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014528 int error = FALSE;
14529
Bram Moolenaar758711c2005-02-02 23:11:38 +000014530 /* Start at specified item. Use the cached index that list_find()
14531 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014532 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014533 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014534 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014535 ic = get_tv_number_chk(&argvars[3], &error);
14536 if (error)
14537 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014538 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014539
Bram Moolenaar758711c2005-02-02 23:11:38 +000014540 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014541 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014542 {
14543 rettv->vval.v_number = idx;
14544 break;
14545 }
14546 }
14547}
14548
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549static int inputsecret_flag = 0;
14550
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014551static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014552
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014554 * This function is used by f_input() and f_inputdialog() functions. The third
14555 * argument to f_input() specifies the type of completion to use at the
14556 * prompt. The third argument to f_inputdialog() specifies the value to return
14557 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 */
14559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014560get_user_input(
14561 typval_T *argvars,
14562 typval_T *rettv,
14563 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014565 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566 char_u *p = NULL;
14567 int c;
14568 char_u buf[NUMBUFLEN];
14569 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014570 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014571 int xp_type = EXPAND_NOTHING;
14572 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014574 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014575 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576
14577#ifdef NO_CONSOLE_INPUT
14578 /* While starting up, there is no place to enter text. */
14579 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581#endif
14582
14583 cmd_silent = FALSE; /* Want to see the prompt. */
14584 if (prompt != NULL)
14585 {
14586 /* Only the part of the message after the last NL is considered as
14587 * prompt for the command line */
14588 p = vim_strrchr(prompt, '\n');
14589 if (p == NULL)
14590 p = prompt;
14591 else
14592 {
14593 ++p;
14594 c = *p;
14595 *p = NUL;
14596 msg_start();
14597 msg_clr_eos();
14598 msg_puts_attr(prompt, echo_attr);
14599 msg_didout = FALSE;
14600 msg_starthere();
14601 *p = c;
14602 }
14603 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014605 if (argvars[1].v_type != VAR_UNKNOWN)
14606 {
14607 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14608 if (defstr != NULL)
14609 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014611 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014612 {
14613 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014614 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014615 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014616
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014617 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014618 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014619
Bram Moolenaar4463f292005-09-25 22:20:24 +000014620 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14621 if (xp_name == NULL)
14622 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014623
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014624 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014625
Bram Moolenaar4463f292005-09-25 22:20:24 +000014626 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14627 &xp_arg) == FAIL)
14628 return;
14629 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014630 }
14631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014632 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014633 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014634 int save_ex_normal_busy = ex_normal_busy;
14635 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014637 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14638 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014639 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014640 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014641 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014642 && argvars[1].v_type != VAR_UNKNOWN
14643 && argvars[2].v_type != VAR_UNKNOWN)
14644 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14645 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014646
14647 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014649 /* since the user typed this, no need to wait for return */
14650 need_wait_return = FALSE;
14651 msg_didout = FALSE;
14652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653 cmd_silent = cmd_silent_save;
14654}
14655
14656/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014657 * "input()" function
14658 * Also handles inputsecret() when inputsecret is set.
14659 */
14660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014661f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014662{
14663 get_user_input(argvars, rettv, FALSE);
14664}
14665
14666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667 * "inputdialog()" function
14668 */
14669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014670f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671{
14672#if defined(FEAT_GUI_TEXTDIALOG)
14673 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14674 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14675 {
14676 char_u *message;
14677 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014678 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014680 message = get_tv_string_chk(&argvars[0]);
14681 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014682 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014683 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684 else
14685 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014686 if (message != NULL && defstr != NULL
14687 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014688 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014689 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 else
14691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014692 if (message != NULL && defstr != NULL
14693 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014694 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014695 rettv->vval.v_string = vim_strsave(
14696 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014698 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014700 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 }
14702 else
14703#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014704 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705}
14706
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014707/*
14708 * "inputlist()" function
14709 */
14710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014711f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014712{
14713 listitem_T *li;
14714 int selected;
14715 int mouse_used;
14716
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014717#ifdef NO_CONSOLE_INPUT
14718 /* While starting up, there is no place to enter text. */
14719 if (no_console_input())
14720 return;
14721#endif
14722 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14723 {
14724 EMSG2(_(e_listarg), "inputlist()");
14725 return;
14726 }
14727
14728 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014729 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014730 lines_left = Rows; /* avoid more prompt */
14731 msg_scroll = TRUE;
14732 msg_clr_eos();
14733
14734 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14735 {
14736 msg_puts(get_tv_string(&li->li_tv));
14737 msg_putchar('\n');
14738 }
14739
14740 /* Ask for choice. */
14741 selected = prompt_for_number(&mouse_used);
14742 if (mouse_used)
14743 selected -= lines_left;
14744
14745 rettv->vval.v_number = selected;
14746}
14747
14748
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14750
14751/*
14752 * "inputrestore()" function
14753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014755f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756{
14757 if (ga_userinput.ga_len > 0)
14758 {
14759 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14761 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014762 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 }
14764 else if (p_verbose > 1)
14765 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014766 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014767 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768 }
14769}
14770
14771/*
14772 * "inputsave()" function
14773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014775f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014777 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778 if (ga_grow(&ga_userinput, 1) == OK)
14779 {
14780 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14781 + ga_userinput.ga_len);
14782 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014783 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784 }
14785 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014786 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787}
14788
14789/*
14790 * "inputsecret()" function
14791 */
14792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014793f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794{
14795 ++cmdline_star;
14796 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014797 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 --cmdline_star;
14799 --inputsecret_flag;
14800}
14801
14802/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014803 * "insert()" function
14804 */
14805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014806f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014807{
14808 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014809 listitem_T *item;
14810 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014811 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014812
14813 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014814 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014815 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014816 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014817 {
14818 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014819 before = get_tv_number_chk(&argvars[2], &error);
14820 if (error)
14821 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014822
Bram Moolenaar758711c2005-02-02 23:11:38 +000014823 if (before == l->lv_len)
14824 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014825 else
14826 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014827 item = list_find(l, before);
14828 if (item == NULL)
14829 {
14830 EMSGN(_(e_listidx), before);
14831 l = NULL;
14832 }
14833 }
14834 if (l != NULL)
14835 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014836 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014837 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014838 }
14839 }
14840}
14841
14842/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014843 * "invert(expr)" function
14844 */
14845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014846f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014847{
14848 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14849}
14850
14851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 * "isdirectory()" function
14853 */
14854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014855f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014857 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858}
14859
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014860/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014861 * "islocked()" function
14862 */
14863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014864f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014865{
14866 lval_T lv;
14867 char_u *end;
14868 dictitem_T *di;
14869
14870 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014871 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14872 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014873 if (end != NULL && lv.ll_name != NULL)
14874 {
14875 if (*end != NUL)
14876 EMSG(_(e_trailing));
14877 else
14878 {
14879 if (lv.ll_tv == NULL)
14880 {
14881 if (check_changedtick(lv.ll_name))
14882 rettv->vval.v_number = 1; /* always locked */
14883 else
14884 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014885 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014886 if (di != NULL)
14887 {
14888 /* Consider a variable locked when:
14889 * 1. the variable itself is locked
14890 * 2. the value of the variable is locked.
14891 * 3. the List or Dict value is locked.
14892 */
14893 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14894 || tv_islocked(&di->di_tv));
14895 }
14896 }
14897 }
14898 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014899 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014900 else if (lv.ll_newkey != NULL)
14901 EMSG2(_(e_dictkey), lv.ll_newkey);
14902 else if (lv.ll_list != NULL)
14903 /* List item. */
14904 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14905 else
14906 /* Dictionary item. */
14907 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14908 }
14909 }
14910
14911 clear_lval(&lv);
14912}
14913
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014914#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14915/*
14916 * "isnan()" function
14917 */
14918 static void
14919f_isnan(typval_T *argvars, typval_T *rettv)
14920{
14921 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14922 && isnan(argvars[0].vval.v_float);
14923}
14924#endif
14925
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014926static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014927
14928/*
14929 * Turn a dict into a list:
14930 * "what" == 0: list of keys
14931 * "what" == 1: list of values
14932 * "what" == 2: list of items
14933 */
14934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014935dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014936{
Bram Moolenaar33570922005-01-25 22:26:29 +000014937 list_T *l2;
14938 dictitem_T *di;
14939 hashitem_T *hi;
14940 listitem_T *li;
14941 listitem_T *li2;
14942 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014943 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014944
Bram Moolenaar8c711452005-01-14 21:53:12 +000014945 if (argvars[0].v_type != VAR_DICT)
14946 {
14947 EMSG(_(e_dictreq));
14948 return;
14949 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014950 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014951 return;
14952
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014953 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014954 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014955
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014956 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014957 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014958 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014959 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014960 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014961 --todo;
14962 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014963
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014964 li = listitem_alloc();
14965 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014966 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014967 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014968
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014969 if (what == 0)
14970 {
14971 /* keys() */
14972 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014973 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014974 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14975 }
14976 else if (what == 1)
14977 {
14978 /* values() */
14979 copy_tv(&di->di_tv, &li->li_tv);
14980 }
14981 else
14982 {
14983 /* items() */
14984 l2 = list_alloc();
14985 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014986 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014987 li->li_tv.vval.v_list = l2;
14988 if (l2 == NULL)
14989 break;
14990 ++l2->lv_refcount;
14991
14992 li2 = listitem_alloc();
14993 if (li2 == NULL)
14994 break;
14995 list_append(l2, li2);
14996 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014997 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014998 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14999
15000 li2 = listitem_alloc();
15001 if (li2 == NULL)
15002 break;
15003 list_append(l2, li2);
15004 copy_tv(&di->di_tv, &li2->li_tv);
15005 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015006 }
15007 }
15008}
15009
15010/*
15011 * "items(dict)" function
15012 */
15013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015014f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015015{
15016 dict_list(argvars, rettv, 2);
15017}
15018
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015019#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015020/*
15021 * Get the job from the argument.
15022 * Returns NULL if the job is invalid.
15023 */
15024 static job_T *
15025get_job_arg(typval_T *tv)
15026{
15027 job_T *job;
15028
15029 if (tv->v_type != VAR_JOB)
15030 {
15031 EMSG2(_(e_invarg2), get_tv_string(tv));
15032 return NULL;
15033 }
15034 job = tv->vval.v_job;
15035
15036 if (job == NULL)
15037 EMSG(_("E916: not a valid job"));
15038 return job;
15039}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015040
Bram Moolenaar835dc632016-02-07 14:27:38 +010015041/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015042 * "job_getchannel()" function
15043 */
15044 static void
15045f_job_getchannel(typval_T *argvars, typval_T *rettv)
15046{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015047 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015048
Bram Moolenaar65edff82016-02-21 16:40:11 +010015049 if (job != NULL)
15050 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015051 rettv->v_type = VAR_CHANNEL;
15052 rettv->vval.v_channel = job->jv_channel;
15053 if (job->jv_channel != NULL)
15054 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015055 }
15056}
15057
15058/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015059 * "job_info()" function
15060 */
15061 static void
15062f_job_info(typval_T *argvars, typval_T *rettv)
15063{
15064 job_T *job = get_job_arg(&argvars[0]);
15065
15066 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15067 job_info(job, rettv->vval.v_dict);
15068}
15069
15070/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015071 * "job_setoptions()" function
15072 */
15073 static void
15074f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15075{
15076 job_T *job = get_job_arg(&argvars[0]);
15077 jobopt_T opt;
15078
15079 if (job == NULL)
15080 return;
15081 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015082 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15083 job_set_options(job, &opt);
15084 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015085}
15086
15087/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015088 * "job_start()" function
15089 */
15090 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015091f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015092{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015093 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015094 if (check_restricted() || check_secure())
15095 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015096 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015097}
15098
15099/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015100 * "job_status()" function
15101 */
15102 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015103f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015104{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015105 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015106
Bram Moolenaar65edff82016-02-21 16:40:11 +010015107 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015108 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015109 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015110 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015111 }
15112}
15113
15114/*
15115 * "job_stop()" function
15116 */
15117 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015118f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015119{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015120 job_T *job = get_job_arg(&argvars[0]);
15121
15122 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015123 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015124}
15125#endif
15126
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015128 * "join()" function
15129 */
15130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015131f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015132{
15133 garray_T ga;
15134 char_u *sep;
15135
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015136 if (argvars[0].v_type != VAR_LIST)
15137 {
15138 EMSG(_(e_listreq));
15139 return;
15140 }
15141 if (argvars[0].vval.v_list == NULL)
15142 return;
15143 if (argvars[1].v_type == VAR_UNKNOWN)
15144 sep = (char_u *)" ";
15145 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015146 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015147
15148 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015149
15150 if (sep != NULL)
15151 {
15152 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015153 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015154 ga_append(&ga, NUL);
15155 rettv->vval.v_string = (char_u *)ga.ga_data;
15156 }
15157 else
15158 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015159}
15160
15161/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015162 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015163 */
15164 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015165f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015166{
15167 js_read_T reader;
15168
15169 reader.js_buf = get_tv_string(&argvars[0]);
15170 reader.js_fill = NULL;
15171 reader.js_used = 0;
15172 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15173 EMSG(_(e_invarg));
15174}
15175
15176/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015177 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015178 */
15179 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015180f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015181{
15182 rettv->v_type = VAR_STRING;
15183 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15184}
15185
15186/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015187 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015188 */
15189 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015190f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015191{
15192 js_read_T reader;
15193
15194 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015195 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015196 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015197 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015198 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015199}
15200
15201/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015202 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015203 */
15204 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015205f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015206{
15207 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015208 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015209}
15210
15211/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015212 * "keys()" function
15213 */
15214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015215f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015216{
15217 dict_list(argvars, rettv, 0);
15218}
15219
15220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 * "last_buffer_nr()" function.
15222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015224f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225{
15226 int n = 0;
15227 buf_T *buf;
15228
15229 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15230 if (n < buf->b_fnum)
15231 n = buf->b_fnum;
15232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015233 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015234}
15235
15236/*
15237 * "len()" function
15238 */
15239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015240f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015241{
15242 switch (argvars[0].v_type)
15243 {
15244 case VAR_STRING:
15245 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015246 rettv->vval.v_number = (varnumber_T)STRLEN(
15247 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015248 break;
15249 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015250 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015251 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015252 case VAR_DICT:
15253 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15254 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015255 case VAR_UNKNOWN:
15256 case VAR_SPECIAL:
15257 case VAR_FLOAT:
15258 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015259 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015260 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015261 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015262 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015263 break;
15264 }
15265}
15266
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015267static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015268
15269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015270libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015271{
15272#ifdef FEAT_LIBCALL
15273 char_u *string_in;
15274 char_u **string_result;
15275 int nr_result;
15276#endif
15277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015278 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015279 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015280 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015281
15282 if (check_restricted() || check_secure())
15283 return;
15284
15285#ifdef FEAT_LIBCALL
15286 /* The first two args must be strings, otherwise its meaningless */
15287 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15288 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015289 string_in = NULL;
15290 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015291 string_in = argvars[2].vval.v_string;
15292 if (type == VAR_NUMBER)
15293 string_result = NULL;
15294 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015295 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015296 if (mch_libcall(argvars[0].vval.v_string,
15297 argvars[1].vval.v_string,
15298 string_in,
15299 argvars[2].vval.v_number,
15300 string_result,
15301 &nr_result) == OK
15302 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015303 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015304 }
15305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306}
15307
15308/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015309 * "libcall()" function
15310 */
15311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015312f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015313{
15314 libcall_common(argvars, rettv, VAR_STRING);
15315}
15316
15317/*
15318 * "libcallnr()" function
15319 */
15320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015321f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322{
15323 libcall_common(argvars, rettv, VAR_NUMBER);
15324}
15325
15326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 * "line(string)" function
15328 */
15329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015330f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331{
15332 linenr_T lnum = 0;
15333 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015334 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015336 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337 if (fp != NULL)
15338 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015339 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340}
15341
15342/*
15343 * "line2byte(lnum)" function
15344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015346f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347{
15348#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015349 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350#else
15351 linenr_T lnum;
15352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015353 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015355 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15358 if (rettv->vval.v_number >= 0)
15359 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360#endif
15361}
15362
15363/*
15364 * "lispindent(lnum)" function
15365 */
15366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015367f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368{
15369#ifdef FEAT_LISP
15370 pos_T pos;
15371 linenr_T lnum;
15372
15373 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15376 {
15377 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015378 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 curwin->w_cursor = pos;
15380 }
15381 else
15382#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015383 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384}
15385
15386/*
15387 * "localtime()" function
15388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015390f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015392 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393}
15394
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015395static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396
15397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015398get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399{
15400 char_u *keys;
15401 char_u *which;
15402 char_u buf[NUMBUFLEN];
15403 char_u *keys_buf = NULL;
15404 char_u *rhs;
15405 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015406 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015407 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015408 mapblock_T *mp;
15409 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410
15411 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015412 rettv->v_type = VAR_STRING;
15413 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416 if (*keys == NUL)
15417 return;
15418
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015419 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015421 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015422 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015423 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015424 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015425 if (argvars[3].v_type != VAR_UNKNOWN)
15426 get_dict = get_tv_number(&argvars[3]);
15427 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 else
15430 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015431 if (which == NULL)
15432 return;
15433
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 mode = get_map_mode(&which, 0);
15435
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015436 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015437 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015439
15440 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015442 /* Return a string. */
15443 if (rhs != NULL)
15444 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445
Bram Moolenaarbd743252010-10-20 21:23:33 +020015446 }
15447 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15448 {
15449 /* Return a dictionary. */
15450 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15451 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15452 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453
Bram Moolenaarbd743252010-10-20 21:23:33 +020015454 dict_add_nr_str(dict, "lhs", 0L, lhs);
15455 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15456 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15457 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15458 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15459 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15460 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015461 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015462 dict_add_nr_str(dict, "mode", 0L, mapmode);
15463
15464 vim_free(lhs);
15465 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 }
15467}
15468
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015469#ifdef FEAT_FLOAT
15470/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015471 * "log()" function
15472 */
15473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015474f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015475{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015476 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015477
15478 rettv->v_type = VAR_FLOAT;
15479 if (get_float_arg(argvars, &f) == OK)
15480 rettv->vval.v_float = log(f);
15481 else
15482 rettv->vval.v_float = 0.0;
15483}
15484
15485/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015486 * "log10()" function
15487 */
15488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015489f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015490{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015491 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015492
15493 rettv->v_type = VAR_FLOAT;
15494 if (get_float_arg(argvars, &f) == OK)
15495 rettv->vval.v_float = log10(f);
15496 else
15497 rettv->vval.v_float = 0.0;
15498}
15499#endif
15500
Bram Moolenaar1dced572012-04-05 16:54:08 +020015501#ifdef FEAT_LUA
15502/*
15503 * "luaeval()" function
15504 */
15505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015506f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015507{
15508 char_u *str;
15509 char_u buf[NUMBUFLEN];
15510
15511 str = get_tv_string_buf(&argvars[0], buf);
15512 do_luaeval(str, argvars + 1, rettv);
15513}
15514#endif
15515
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015517 * "map()" function
15518 */
15519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015520f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015521{
15522 filter_map(argvars, rettv, TRUE);
15523}
15524
15525/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015526 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527 */
15528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015529f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015531 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532}
15533
15534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015535 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 */
15537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015538f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015540 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541}
15542
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015543static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544
15545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015546find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015548 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015549 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015550 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 char_u *pat;
15552 regmatch_T regmatch;
15553 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015554 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 char_u *save_cpo;
15556 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015557 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015558 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015559 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015560 list_T *l = NULL;
15561 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015562 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015563 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564
15565 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15566 save_cpo = p_cpo;
15567 p_cpo = (char_u *)"";
15568
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015569 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015570 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015571 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015572 /* type 3: return empty list when there are no matches.
15573 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015574 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015575 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015576 if (type == 4
15577 && (list_append_string(rettv->vval.v_list,
15578 (char_u *)"", 0) == FAIL
15579 || list_append_number(rettv->vval.v_list,
15580 (varnumber_T)-1) == FAIL
15581 || list_append_number(rettv->vval.v_list,
15582 (varnumber_T)-1) == FAIL
15583 || list_append_number(rettv->vval.v_list,
15584 (varnumber_T)-1) == FAIL))
15585 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015586 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015587 rettv->vval.v_list = NULL;
15588 goto theend;
15589 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015590 }
15591 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015593 rettv->v_type = VAR_STRING;
15594 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015597 if (argvars[0].v_type == VAR_LIST)
15598 {
15599 if ((l = argvars[0].vval.v_list) == NULL)
15600 goto theend;
15601 li = l->lv_first;
15602 }
15603 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015604 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015605 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015606 len = (long)STRLEN(str);
15607 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015609 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15610 if (pat == NULL)
15611 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015612
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015613 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015615 int error = FALSE;
15616
15617 start = get_tv_number_chk(&argvars[2], &error);
15618 if (error)
15619 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015620 if (l != NULL)
15621 {
15622 li = list_find(l, start);
15623 if (li == NULL)
15624 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015625 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015626 }
15627 else
15628 {
15629 if (start < 0)
15630 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015631 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015632 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015633 /* When "count" argument is there ignore matches before "start",
15634 * otherwise skip part of the string. Differs when pattern is "^"
15635 * or "\<". */
15636 if (argvars[3].v_type != VAR_UNKNOWN)
15637 startcol = start;
15638 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015639 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015640 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015641 len -= start;
15642 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015643 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015644
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015645 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015646 nth = get_tv_number_chk(&argvars[3], &error);
15647 if (error)
15648 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649 }
15650
15651 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15652 if (regmatch.regprog != NULL)
15653 {
15654 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015655
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015656 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015657 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015658 if (l != NULL)
15659 {
15660 if (li == NULL)
15661 {
15662 match = FALSE;
15663 break;
15664 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015665 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015666 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015667 if (str == NULL)
15668 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015669 }
15670
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015671 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015672
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015673 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015674 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015675 if (l == NULL && !match)
15676 break;
15677
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015678 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015679 if (l != NULL)
15680 {
15681 li = li->li_next;
15682 ++idx;
15683 }
15684 else
15685 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015686#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015687 startcol = (colnr_T)(regmatch.startp[0]
15688 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015689#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015690 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015691#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015692 if (startcol > (colnr_T)len
15693 || str + startcol <= regmatch.startp[0])
15694 {
15695 match = FALSE;
15696 break;
15697 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015698 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015699 }
15700
15701 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015703 if (type == 4)
15704 {
15705 listitem_T *li1 = rettv->vval.v_list->lv_first;
15706 listitem_T *li2 = li1->li_next;
15707 listitem_T *li3 = li2->li_next;
15708 listitem_T *li4 = li3->li_next;
15709
15710 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15711 (int)(regmatch.endp[0] - regmatch.startp[0]));
15712 li3->li_tv.vval.v_number =
15713 (varnumber_T)(regmatch.startp[0] - expr);
15714 li4->li_tv.vval.v_number =
15715 (varnumber_T)(regmatch.endp[0] - expr);
15716 if (l != NULL)
15717 li2->li_tv.vval.v_number = (varnumber_T)idx;
15718 }
15719 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015720 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015721 int i;
15722
15723 /* return list with matched string and submatches */
15724 for (i = 0; i < NSUBEXP; ++i)
15725 {
15726 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015727 {
15728 if (list_append_string(rettv->vval.v_list,
15729 (char_u *)"", 0) == FAIL)
15730 break;
15731 }
15732 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015733 regmatch.startp[i],
15734 (int)(regmatch.endp[i] - regmatch.startp[i]))
15735 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015736 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015737 }
15738 }
15739 else if (type == 2)
15740 {
15741 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015742 if (l != NULL)
15743 copy_tv(&li->li_tv, rettv);
15744 else
15745 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015747 }
15748 else if (l != NULL)
15749 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 else
15751 {
15752 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015753 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 (varnumber_T)(regmatch.startp[0] - str);
15755 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015756 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015758 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 }
15760 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015761 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 }
15763
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015764 if (type == 4 && l == NULL)
15765 /* matchstrpos() without a list: drop the second item. */
15766 listitem_remove(rettv->vval.v_list,
15767 rettv->vval.v_list->lv_first->li_next);
15768
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015770 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 p_cpo = save_cpo;
15772}
15773
15774/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015775 * "match()" function
15776 */
15777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015778f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015779{
15780 find_some_match(argvars, rettv, 1);
15781}
15782
15783/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015784 * "matchadd()" function
15785 */
15786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015787f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015788{
15789#ifdef FEAT_SEARCH_EXTRA
15790 char_u buf[NUMBUFLEN];
15791 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15792 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15793 int prio = 10; /* default priority */
15794 int id = -1;
15795 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015796 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015797
15798 rettv->vval.v_number = -1;
15799
15800 if (grp == NULL || pat == NULL)
15801 return;
15802 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015803 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015804 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015805 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015806 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015807 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015808 if (argvars[4].v_type != VAR_UNKNOWN)
15809 {
15810 if (argvars[4].v_type != VAR_DICT)
15811 {
15812 EMSG(_(e_dictreq));
15813 return;
15814 }
15815 if (dict_find(argvars[4].vval.v_dict,
15816 (char_u *)"conceal", -1) != NULL)
15817 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15818 (char_u *)"conceal", FALSE);
15819 }
15820 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015821 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015822 if (error == TRUE)
15823 return;
15824 if (id >= 1 && id <= 3)
15825 {
15826 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15827 return;
15828 }
15829
Bram Moolenaar6561d522015-07-21 15:48:27 +020015830 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15831 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015832#endif
15833}
15834
15835/*
15836 * "matchaddpos()" function
15837 */
15838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015839f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015840{
15841#ifdef FEAT_SEARCH_EXTRA
15842 char_u buf[NUMBUFLEN];
15843 char_u *group;
15844 int prio = 10;
15845 int id = -1;
15846 int error = FALSE;
15847 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015848 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015849
15850 rettv->vval.v_number = -1;
15851
15852 group = get_tv_string_buf_chk(&argvars[0], buf);
15853 if (group == NULL)
15854 return;
15855
15856 if (argvars[1].v_type != VAR_LIST)
15857 {
15858 EMSG2(_(e_listarg), "matchaddpos()");
15859 return;
15860 }
15861 l = argvars[1].vval.v_list;
15862 if (l == NULL)
15863 return;
15864
15865 if (argvars[2].v_type != VAR_UNKNOWN)
15866 {
15867 prio = get_tv_number_chk(&argvars[2], &error);
15868 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015869 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015870 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015871 if (argvars[4].v_type != VAR_UNKNOWN)
15872 {
15873 if (argvars[4].v_type != VAR_DICT)
15874 {
15875 EMSG(_(e_dictreq));
15876 return;
15877 }
15878 if (dict_find(argvars[4].vval.v_dict,
15879 (char_u *)"conceal", -1) != NULL)
15880 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15881 (char_u *)"conceal", FALSE);
15882 }
15883 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015884 }
15885 if (error == TRUE)
15886 return;
15887
15888 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15889 if (id == 1 || id == 2)
15890 {
15891 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15892 return;
15893 }
15894
Bram Moolenaar6561d522015-07-21 15:48:27 +020015895 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15896 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015897#endif
15898}
15899
15900/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015901 * "matcharg()" function
15902 */
15903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015904f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015905{
15906 if (rettv_list_alloc(rettv) == OK)
15907 {
15908#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015909 int id = get_tv_number(&argvars[0]);
15910 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015911
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015912 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015913 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015914 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15915 {
15916 list_append_string(rettv->vval.v_list,
15917 syn_id2name(m->hlg_id), -1);
15918 list_append_string(rettv->vval.v_list, m->pattern, -1);
15919 }
15920 else
15921 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015922 list_append_string(rettv->vval.v_list, NULL, -1);
15923 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015924 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015925 }
15926#endif
15927 }
15928}
15929
15930/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015931 * "matchdelete()" function
15932 */
15933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015934f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015935{
15936#ifdef FEAT_SEARCH_EXTRA
15937 rettv->vval.v_number = match_delete(curwin,
15938 (int)get_tv_number(&argvars[0]), TRUE);
15939#endif
15940}
15941
15942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015943 * "matchend()" function
15944 */
15945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015946f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015947{
15948 find_some_match(argvars, rettv, 0);
15949}
15950
15951/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015952 * "matchlist()" function
15953 */
15954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015955f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015956{
15957 find_some_match(argvars, rettv, 3);
15958}
15959
15960/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015961 * "matchstr()" function
15962 */
15963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015964f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015965{
15966 find_some_match(argvars, rettv, 2);
15967}
15968
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015969/*
15970 * "matchstrpos()" function
15971 */
15972 static void
15973f_matchstrpos(typval_T *argvars, typval_T *rettv)
15974{
15975 find_some_match(argvars, rettv, 4);
15976}
15977
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015978static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015979
15980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015981max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015982{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015983 long n = 0;
15984 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015985 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015986
15987 if (argvars[0].v_type == VAR_LIST)
15988 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015989 list_T *l;
15990 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015991
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015992 l = argvars[0].vval.v_list;
15993 if (l != NULL)
15994 {
15995 li = l->lv_first;
15996 if (li != NULL)
15997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015998 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015999 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016000 {
16001 li = li->li_next;
16002 if (li == NULL)
16003 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016004 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016005 if (domax ? i > n : i < n)
16006 n = i;
16007 }
16008 }
16009 }
16010 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016011 else if (argvars[0].v_type == VAR_DICT)
16012 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016013 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016014 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016015 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016016 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016017
16018 d = argvars[0].vval.v_dict;
16019 if (d != NULL)
16020 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016021 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016022 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016023 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016024 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016025 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016026 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016027 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016028 if (first)
16029 {
16030 n = i;
16031 first = FALSE;
16032 }
16033 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016034 n = i;
16035 }
16036 }
16037 }
16038 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016039 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016040 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016042}
16043
16044/*
16045 * "max()" function
16046 */
16047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016048f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016049{
16050 max_min(argvars, rettv, TRUE);
16051}
16052
16053/*
16054 * "min()" function
16055 */
16056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016057f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016058{
16059 max_min(argvars, rettv, FALSE);
16060}
16061
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016062static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016063
16064/*
16065 * Create the directory in which "dir" is located, and higher levels when
16066 * needed.
16067 */
16068 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016069mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016070{
16071 char_u *p;
16072 char_u *updir;
16073 int r = FAIL;
16074
16075 /* Get end of directory name in "dir".
16076 * We're done when it's "/" or "c:/". */
16077 p = gettail_sep(dir);
16078 if (p <= get_past_head(dir))
16079 return OK;
16080
16081 /* If the directory exists we're done. Otherwise: create it.*/
16082 updir = vim_strnsave(dir, (int)(p - dir));
16083 if (updir == NULL)
16084 return FAIL;
16085 if (mch_isdir(updir))
16086 r = OK;
16087 else if (mkdir_recurse(updir, prot) == OK)
16088 r = vim_mkdir_emsg(updir, prot);
16089 vim_free(updir);
16090 return r;
16091}
16092
16093#ifdef vim_mkdir
16094/*
16095 * "mkdir()" function
16096 */
16097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016098f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016099{
16100 char_u *dir;
16101 char_u buf[NUMBUFLEN];
16102 int prot = 0755;
16103
16104 rettv->vval.v_number = FAIL;
16105 if (check_restricted() || check_secure())
16106 return;
16107
16108 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016109 if (*dir == NUL)
16110 rettv->vval.v_number = FAIL;
16111 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016112 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016113 if (*gettail(dir) == NUL)
16114 /* remove trailing slashes */
16115 *gettail_sep(dir) = NUL;
16116
16117 if (argvars[1].v_type != VAR_UNKNOWN)
16118 {
16119 if (argvars[2].v_type != VAR_UNKNOWN)
16120 prot = get_tv_number_chk(&argvars[2], NULL);
16121 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16122 mkdir_recurse(dir, prot);
16123 }
16124 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016125 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016126}
16127#endif
16128
Bram Moolenaar0d660222005-01-07 21:51:51 +000016129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130 * "mode()" function
16131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016133f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016135 char_u buf[3];
16136
16137 buf[1] = NUL;
16138 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 if (VIsual_active)
16141 {
16142 if (VIsual_select)
16143 buf[0] = VIsual_mode + 's' - 'v';
16144 else
16145 buf[0] = VIsual_mode;
16146 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016147 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016148 || State == CONFIRM)
16149 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016151 if (State == ASKMORE)
16152 buf[1] = 'm';
16153 else if (State == CONFIRM)
16154 buf[1] = '?';
16155 }
16156 else if (State == EXTERNCMD)
16157 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 else if (State & INSERT)
16159 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016160#ifdef FEAT_VREPLACE
16161 if (State & VREPLACE_FLAG)
16162 {
16163 buf[0] = 'R';
16164 buf[1] = 'v';
16165 }
16166 else
16167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 if (State & REPLACE_FLAG)
16169 buf[0] = 'R';
16170 else
16171 buf[0] = 'i';
16172 }
16173 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016174 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016176 if (exmode_active)
16177 buf[1] = 'v';
16178 }
16179 else if (exmode_active)
16180 {
16181 buf[0] = 'c';
16182 buf[1] = 'e';
16183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016187 if (finish_op)
16188 buf[1] = 'o';
16189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016191 /* Clear out the minor mode when the argument is not a non-zero number or
16192 * non-empty string. */
16193 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016194 buf[1] = NUL;
16195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016196 rettv->vval.v_string = vim_strsave(buf);
16197 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198}
16199
Bram Moolenaar429fa852013-04-15 12:27:36 +020016200#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016201/*
16202 * "mzeval()" function
16203 */
16204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016205f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016206{
16207 char_u *str;
16208 char_u buf[NUMBUFLEN];
16209
16210 str = get_tv_string_buf(&argvars[0], buf);
16211 do_mzeval(str, rettv);
16212}
Bram Moolenaar75676462013-01-30 14:55:42 +010016213
16214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016215mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016216{
16217 typval_T argvars[3];
16218
16219 argvars[0].v_type = VAR_STRING;
16220 argvars[0].vval.v_string = name;
16221 copy_tv(args, &argvars[1]);
16222 argvars[2].v_type = VAR_UNKNOWN;
16223 f_call(argvars, rettv);
16224 clear_tv(&argvars[1]);
16225}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016226#endif
16227
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229 * "nextnonblank()" function
16230 */
16231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016232f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016233{
16234 linenr_T lnum;
16235
16236 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016238 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016239 {
16240 lnum = 0;
16241 break;
16242 }
16243 if (*skipwhite(ml_get(lnum)) != NUL)
16244 break;
16245 }
16246 rettv->vval.v_number = lnum;
16247}
16248
16249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 * "nr2char()" function
16251 */
16252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016253f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254{
16255 char_u buf[NUMBUFLEN];
16256
16257#ifdef FEAT_MBYTE
16258 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016259 {
16260 int utf8 = 0;
16261
16262 if (argvars[1].v_type != VAR_UNKNOWN)
16263 utf8 = get_tv_number_chk(&argvars[1], NULL);
16264 if (utf8)
16265 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16266 else
16267 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 else
16270#endif
16271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016272 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 buf[1] = NUL;
16274 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016275 rettv->v_type = VAR_STRING;
16276 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016277}
16278
16279/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016280 * "or(expr, expr)" function
16281 */
16282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016283f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016284{
16285 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16286 | get_tv_number_chk(&argvars[1], NULL);
16287}
16288
16289/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016290 * "pathshorten()" function
16291 */
16292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016293f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016294{
16295 char_u *p;
16296
16297 rettv->v_type = VAR_STRING;
16298 p = get_tv_string_chk(&argvars[0]);
16299 if (p == NULL)
16300 rettv->vval.v_string = NULL;
16301 else
16302 {
16303 p = vim_strsave(p);
16304 rettv->vval.v_string = p;
16305 if (p != NULL)
16306 shorten_dir(p);
16307 }
16308}
16309
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016310#ifdef FEAT_PERL
16311/*
16312 * "perleval()" function
16313 */
16314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016315f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016316{
16317 char_u *str;
16318 char_u buf[NUMBUFLEN];
16319
16320 str = get_tv_string_buf(&argvars[0], buf);
16321 do_perleval(str, rettv);
16322}
16323#endif
16324
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016325#ifdef FEAT_FLOAT
16326/*
16327 * "pow()" function
16328 */
16329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016330f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016331{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016332 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016333
16334 rettv->v_type = VAR_FLOAT;
16335 if (get_float_arg(argvars, &fx) == OK
16336 && get_float_arg(&argvars[1], &fy) == OK)
16337 rettv->vval.v_float = pow(fx, fy);
16338 else
16339 rettv->vval.v_float = 0.0;
16340}
16341#endif
16342
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016343/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344 * "prevnonblank()" function
16345 */
16346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016347f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016348{
16349 linenr_T lnum;
16350
16351 lnum = get_tv_lnum(argvars);
16352 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16353 lnum = 0;
16354 else
16355 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16356 --lnum;
16357 rettv->vval.v_number = lnum;
16358}
16359
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016360/* This dummy va_list is here because:
16361 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16362 * - locally in the function results in a "used before set" warning
16363 * - using va_start() to initialize it gives "function with fixed args" error */
16364static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016365
Bram Moolenaar8c711452005-01-14 21:53:12 +000016366/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016367 * "printf()" function
16368 */
16369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016370f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016371{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016372 char_u buf[NUMBUFLEN];
16373 int len;
16374 char_u *s;
16375 int saved_did_emsg = did_emsg;
16376 char *fmt;
16377
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016378 rettv->v_type = VAR_STRING;
16379 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016380
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016381 /* Get the required length, allocate the buffer and do it for real. */
16382 did_emsg = FALSE;
16383 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16384 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16385 if (!did_emsg)
16386 {
16387 s = alloc(len + 1);
16388 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016389 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016390 rettv->vval.v_string = s;
16391 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016392 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016393 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016394 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016395}
16396
16397/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016398 * "pumvisible()" function
16399 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016401f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016402{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016403#ifdef FEAT_INS_EXPAND
16404 if (pum_visible())
16405 rettv->vval.v_number = 1;
16406#endif
16407}
16408
Bram Moolenaardb913952012-06-29 12:54:53 +020016409#ifdef FEAT_PYTHON3
16410/*
16411 * "py3eval()" function
16412 */
16413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016414f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016415{
16416 char_u *str;
16417 char_u buf[NUMBUFLEN];
16418
16419 str = get_tv_string_buf(&argvars[0], buf);
16420 do_py3eval(str, rettv);
16421}
16422#endif
16423
16424#ifdef FEAT_PYTHON
16425/*
16426 * "pyeval()" function
16427 */
16428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016429f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016430{
16431 char_u *str;
16432 char_u buf[NUMBUFLEN];
16433
16434 str = get_tv_string_buf(&argvars[0], buf);
16435 do_pyeval(str, rettv);
16436}
16437#endif
16438
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016439/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016440 * "range()" function
16441 */
16442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016443f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016444{
16445 long start;
16446 long end;
16447 long stride = 1;
16448 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016449 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016451 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016452 if (argvars[1].v_type == VAR_UNKNOWN)
16453 {
16454 end = start - 1;
16455 start = 0;
16456 }
16457 else
16458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016459 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016460 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016461 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016462 }
16463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016464 if (error)
16465 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016466 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016467 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016468 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016469 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016470 else
16471 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016472 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016473 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016474 if (list_append_number(rettv->vval.v_list,
16475 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016476 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016477 }
16478}
16479
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016480/*
16481 * "readfile()" function
16482 */
16483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016484f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016485{
16486 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016487 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016488 char_u *fname;
16489 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016490 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16491 int io_size = sizeof(buf);
16492 int readlen; /* size of last fread() */
16493 char_u *prev = NULL; /* previously read bytes, if any */
16494 long prevlen = 0; /* length of data in prev */
16495 long prevsize = 0; /* size of prev buffer */
16496 long maxline = MAXLNUM;
16497 long cnt = 0;
16498 char_u *p; /* position in buf */
16499 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016500
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016501 if (argvars[1].v_type != VAR_UNKNOWN)
16502 {
16503 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16504 binary = TRUE;
16505 if (argvars[2].v_type != VAR_UNKNOWN)
16506 maxline = get_tv_number(&argvars[2]);
16507 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016508
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016509 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016510 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016511
16512 /* Always open the file in binary mode, library functions have a mind of
16513 * their own about CR-LF conversion. */
16514 fname = get_tv_string(&argvars[0]);
16515 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16516 {
16517 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16518 return;
16519 }
16520
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016521 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016522 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016523 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016524
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016525 /* This for loop processes what was read, but is also entered at end
16526 * of file so that either:
16527 * - an incomplete line gets written
16528 * - a "binary" file gets an empty line at the end if it ends in a
16529 * newline. */
16530 for (p = buf, start = buf;
16531 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16532 ++p)
16533 {
16534 if (*p == '\n' || readlen <= 0)
16535 {
16536 listitem_T *li;
16537 char_u *s = NULL;
16538 long_u len = p - start;
16539
16540 /* Finished a line. Remove CRs before NL. */
16541 if (readlen > 0 && !binary)
16542 {
16543 while (len > 0 && start[len - 1] == '\r')
16544 --len;
16545 /* removal may cross back to the "prev" string */
16546 if (len == 0)
16547 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16548 --prevlen;
16549 }
16550 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016551 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016552 else
16553 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016554 /* Change "prev" buffer to be the right size. This way
16555 * the bytes are only copied once, and very long lines are
16556 * allocated only once. */
16557 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016558 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016559 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016560 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016561 prev = NULL; /* the list will own the string */
16562 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016563 }
16564 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016565 if (s == NULL)
16566 {
16567 do_outofmem_msg((long_u) prevlen + len + 1);
16568 failed = TRUE;
16569 break;
16570 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016571
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016572 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016573 {
16574 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016575 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016576 break;
16577 }
16578 li->li_tv.v_type = VAR_STRING;
16579 li->li_tv.v_lock = 0;
16580 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016581 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016582
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016583 start = p + 1; /* step over newline */
16584 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016585 break;
16586 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016587 else if (*p == NUL)
16588 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016589#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016590 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16591 * when finding the BF and check the previous two bytes. */
16592 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016593 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016594 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16595 * + 1, these may be in the "prev" string. */
16596 char_u back1 = p >= buf + 1 ? p[-1]
16597 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16598 char_u back2 = p >= buf + 2 ? p[-2]
16599 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16600 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016601
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016602 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016603 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016604 char_u *dest = p - 2;
16605
16606 /* Usually a BOM is at the beginning of a file, and so at
16607 * the beginning of a line; then we can just step over it.
16608 */
16609 if (start == dest)
16610 start = p + 1;
16611 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016612 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016613 /* have to shuffle buf to close gap */
16614 int adjust_prevlen = 0;
16615
16616 if (dest < buf)
16617 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016618 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016619 dest = buf;
16620 }
16621 if (readlen > p - buf + 1)
16622 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16623 readlen -= 3 - adjust_prevlen;
16624 prevlen -= adjust_prevlen;
16625 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016626 }
16627 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016628 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016629#endif
16630 } /* for */
16631
16632 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16633 break;
16634 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016635 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016636 /* There's part of a line in buf, store it in "prev". */
16637 if (p - start + prevlen >= prevsize)
16638 {
16639 /* need bigger "prev" buffer */
16640 char_u *newprev;
16641
16642 /* A common use case is ordinary text files and "prev" gets a
16643 * fragment of a line, so the first allocation is made
16644 * small, to avoid repeatedly 'allocing' large and
16645 * 'reallocing' small. */
16646 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016647 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016648 else
16649 {
16650 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016651 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016652 prevsize = grow50pc > growmin ? grow50pc : growmin;
16653 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016654 newprev = prev == NULL ? alloc(prevsize)
16655 : vim_realloc(prev, prevsize);
16656 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016657 {
16658 do_outofmem_msg((long_u)prevsize);
16659 failed = TRUE;
16660 break;
16661 }
16662 prev = newprev;
16663 }
16664 /* Add the line part to end of "prev". */
16665 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016666 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016667 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016668 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016669
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016670 /*
16671 * For a negative line count use only the lines at the end of the file,
16672 * free the rest.
16673 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016674 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016675 while (cnt > -maxline)
16676 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016677 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016678 --cnt;
16679 }
16680
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016681 if (failed)
16682 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016683 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016684 /* readfile doc says an empty list is returned on error */
16685 rettv->vval.v_list = list_alloc();
16686 }
16687
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016688 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016689 fclose(fd);
16690}
16691
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016692#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016693static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016694
16695/*
16696 * Convert a List to proftime_T.
16697 * Return FAIL when there is something wrong.
16698 */
16699 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016700list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016701{
16702 long n1, n2;
16703 int error = FALSE;
16704
16705 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16706 || arg->vval.v_list->lv_len != 2)
16707 return FAIL;
16708 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16709 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16710# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016711 tm->HighPart = n1;
16712 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016713# else
16714 tm->tv_sec = n1;
16715 tm->tv_usec = n2;
16716# endif
16717 return error ? FAIL : OK;
16718}
16719#endif /* FEAT_RELTIME */
16720
16721/*
16722 * "reltime()" function
16723 */
16724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016725f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016726{
16727#ifdef FEAT_RELTIME
16728 proftime_T res;
16729 proftime_T start;
16730
16731 if (argvars[0].v_type == VAR_UNKNOWN)
16732 {
16733 /* No arguments: get current time. */
16734 profile_start(&res);
16735 }
16736 else if (argvars[1].v_type == VAR_UNKNOWN)
16737 {
16738 if (list2proftime(&argvars[0], &res) == FAIL)
16739 return;
16740 profile_end(&res);
16741 }
16742 else
16743 {
16744 /* Two arguments: compute the difference. */
16745 if (list2proftime(&argvars[0], &start) == FAIL
16746 || list2proftime(&argvars[1], &res) == FAIL)
16747 return;
16748 profile_sub(&res, &start);
16749 }
16750
16751 if (rettv_list_alloc(rettv) == OK)
16752 {
16753 long n1, n2;
16754
16755# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016756 n1 = res.HighPart;
16757 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016758# else
16759 n1 = res.tv_sec;
16760 n2 = res.tv_usec;
16761# endif
16762 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16763 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16764 }
16765#endif
16766}
16767
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016768#ifdef FEAT_FLOAT
16769/*
16770 * "reltimefloat()" function
16771 */
16772 static void
16773f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16774{
16775# ifdef FEAT_RELTIME
16776 proftime_T tm;
16777# endif
16778
16779 rettv->v_type = VAR_FLOAT;
16780 rettv->vval.v_float = 0;
16781# ifdef FEAT_RELTIME
16782 if (list2proftime(&argvars[0], &tm) == OK)
16783 rettv->vval.v_float = profile_float(&tm);
16784# endif
16785}
16786#endif
16787
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016788/*
16789 * "reltimestr()" function
16790 */
16791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016792f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016793{
16794#ifdef FEAT_RELTIME
16795 proftime_T tm;
16796#endif
16797
16798 rettv->v_type = VAR_STRING;
16799 rettv->vval.v_string = NULL;
16800#ifdef FEAT_RELTIME
16801 if (list2proftime(&argvars[0], &tm) == OK)
16802 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16803#endif
16804}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016805
Bram Moolenaar0d660222005-01-07 21:51:51 +000016806#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016807static void make_connection(void);
16808static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016809
16810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016811make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016812{
16813 if (X_DISPLAY == NULL
16814# ifdef FEAT_GUI
16815 && !gui.in_use
16816# endif
16817 )
16818 {
16819 x_force_connect = TRUE;
16820 setup_term_clip();
16821 x_force_connect = FALSE;
16822 }
16823}
16824
16825 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016826check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016827{
16828 make_connection();
16829 if (X_DISPLAY == NULL)
16830 {
16831 EMSG(_("E240: No connection to Vim server"));
16832 return FAIL;
16833 }
16834 return OK;
16835}
16836#endif
16837
16838#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016840remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016841{
16842 char_u *server_name;
16843 char_u *keys;
16844 char_u *r = NULL;
16845 char_u buf[NUMBUFLEN];
16846# ifdef WIN32
16847 HWND w;
16848# else
16849 Window w;
16850# endif
16851
16852 if (check_restricted() || check_secure())
16853 return;
16854
16855# ifdef FEAT_X11
16856 if (check_connection() == FAIL)
16857 return;
16858# endif
16859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016860 server_name = get_tv_string_chk(&argvars[0]);
16861 if (server_name == NULL)
16862 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016863 keys = get_tv_string_buf(&argvars[1], buf);
16864# ifdef WIN32
16865 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16866# else
16867 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16868 < 0)
16869# endif
16870 {
16871 if (r != NULL)
16872 EMSG(r); /* sending worked but evaluation failed */
16873 else
16874 EMSG2(_("E241: Unable to send to %s"), server_name);
16875 return;
16876 }
16877
16878 rettv->vval.v_string = r;
16879
16880 if (argvars[2].v_type != VAR_UNKNOWN)
16881 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016882 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016883 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016884 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016885
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016886 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016887 v.di_tv.v_type = VAR_STRING;
16888 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016889 idvar = get_tv_string_chk(&argvars[2]);
16890 if (idvar != NULL)
16891 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016892 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016893 }
16894}
16895#endif
16896
16897/*
16898 * "remote_expr()" function
16899 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016901f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016902{
16903 rettv->v_type = VAR_STRING;
16904 rettv->vval.v_string = NULL;
16905#ifdef FEAT_CLIENTSERVER
16906 remote_common(argvars, rettv, TRUE);
16907#endif
16908}
16909
16910/*
16911 * "remote_foreground()" function
16912 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016914f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016915{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016916#ifdef FEAT_CLIENTSERVER
16917# ifdef WIN32
16918 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016919 {
16920 char_u *server_name = get_tv_string_chk(&argvars[0]);
16921
16922 if (server_name != NULL)
16923 serverForeground(server_name);
16924 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925# else
16926 /* Send a foreground() expression to the server. */
16927 argvars[1].v_type = VAR_STRING;
16928 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16929 argvars[2].v_type = VAR_UNKNOWN;
16930 remote_common(argvars, rettv, TRUE);
16931 vim_free(argvars[1].vval.v_string);
16932# endif
16933#endif
16934}
16935
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016937f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938{
16939#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016940 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016941 char_u *s = NULL;
16942# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016943 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016945 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946
16947 if (check_restricted() || check_secure())
16948 {
16949 rettv->vval.v_number = -1;
16950 return;
16951 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016952 serverid = get_tv_string_chk(&argvars[0]);
16953 if (serverid == NULL)
16954 {
16955 rettv->vval.v_number = -1;
16956 return; /* type error; errmsg already given */
16957 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016959 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 if (n == 0)
16961 rettv->vval.v_number = -1;
16962 else
16963 {
16964 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16965 rettv->vval.v_number = (s != NULL);
16966 }
16967# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968 if (check_connection() == FAIL)
16969 return;
16970
16971 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016972 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973# endif
16974
16975 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016977 char_u *retvar;
16978
Bram Moolenaar33570922005-01-25 22:26:29 +000016979 v.di_tv.v_type = VAR_STRING;
16980 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016981 retvar = get_tv_string_chk(&argvars[1]);
16982 if (retvar != NULL)
16983 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016984 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985 }
16986#else
16987 rettv->vval.v_number = -1;
16988#endif
16989}
16990
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016992f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993{
16994 char_u *r = NULL;
16995
16996#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016997 char_u *serverid = get_tv_string_chk(&argvars[0]);
16998
16999 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 {
17001# ifdef WIN32
17002 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017003 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017005 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006 if (n != 0)
17007 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17008 if (r == NULL)
17009# else
17010 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017011 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012# endif
17013 EMSG(_("E277: Unable to read a server reply"));
17014 }
17015#endif
17016 rettv->v_type = VAR_STRING;
17017 rettv->vval.v_string = r;
17018}
17019
17020/*
17021 * "remote_send()" function
17022 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017024f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017025{
17026 rettv->v_type = VAR_STRING;
17027 rettv->vval.v_string = NULL;
17028#ifdef FEAT_CLIENTSERVER
17029 remote_common(argvars, rettv, FALSE);
17030#endif
17031}
17032
17033/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017034 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017035 */
17036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017037f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017038{
Bram Moolenaar33570922005-01-25 22:26:29 +000017039 list_T *l;
17040 listitem_T *item, *item2;
17041 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017042 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017043 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017044 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017045 dict_T *d;
17046 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017047 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017048
Bram Moolenaar8c711452005-01-14 21:53:12 +000017049 if (argvars[0].v_type == VAR_DICT)
17050 {
17051 if (argvars[2].v_type != VAR_UNKNOWN)
17052 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017053 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017054 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017056 key = get_tv_string_chk(&argvars[1]);
17057 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017059 di = dict_find(d, key, -1);
17060 if (di == NULL)
17061 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017062 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17063 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017064 {
17065 *rettv = di->di_tv;
17066 init_tv(&di->di_tv);
17067 dictitem_remove(d, di);
17068 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017069 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017070 }
17071 }
17072 else if (argvars[0].v_type != VAR_LIST)
17073 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017074 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017075 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017077 int error = FALSE;
17078
17079 idx = get_tv_number_chk(&argvars[1], &error);
17080 if (error)
17081 ; /* type error: do nothing, errmsg already given */
17082 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017083 EMSGN(_(e_listidx), idx);
17084 else
17085 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017086 if (argvars[2].v_type == VAR_UNKNOWN)
17087 {
17088 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017089 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017090 *rettv = item->li_tv;
17091 vim_free(item);
17092 }
17093 else
17094 {
17095 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017096 end = get_tv_number_chk(&argvars[2], &error);
17097 if (error)
17098 ; /* type error: do nothing */
17099 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017100 EMSGN(_(e_listidx), end);
17101 else
17102 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017103 int cnt = 0;
17104
17105 for (li = item; li != NULL; li = li->li_next)
17106 {
17107 ++cnt;
17108 if (li == item2)
17109 break;
17110 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017111 if (li == NULL) /* didn't find "item2" after "item" */
17112 EMSG(_(e_invrange));
17113 else
17114 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017115 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017116 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017117 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017118 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017119 l->lv_first = item;
17120 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017121 item->li_prev = NULL;
17122 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017123 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017124 }
17125 }
17126 }
17127 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017128 }
17129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130}
17131
17132/*
17133 * "rename({from}, {to})" function
17134 */
17135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017136f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137{
17138 char_u buf[NUMBUFLEN];
17139
17140 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017141 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017143 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17144 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145}
17146
17147/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017148 * "repeat()" function
17149 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017151f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017152{
17153 char_u *p;
17154 int n;
17155 int slen;
17156 int len;
17157 char_u *r;
17158 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017159
17160 n = get_tv_number(&argvars[1]);
17161 if (argvars[0].v_type == VAR_LIST)
17162 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017163 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017164 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017165 if (list_extend(rettv->vval.v_list,
17166 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017167 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017168 }
17169 else
17170 {
17171 p = get_tv_string(&argvars[0]);
17172 rettv->v_type = VAR_STRING;
17173 rettv->vval.v_string = NULL;
17174
17175 slen = (int)STRLEN(p);
17176 len = slen * n;
17177 if (len <= 0)
17178 return;
17179
17180 r = alloc(len + 1);
17181 if (r != NULL)
17182 {
17183 for (i = 0; i < n; i++)
17184 mch_memmove(r + i * slen, p, (size_t)slen);
17185 r[len] = NUL;
17186 }
17187
17188 rettv->vval.v_string = r;
17189 }
17190}
17191
17192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 * "resolve()" function
17194 */
17195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017196f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197{
17198 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017199#ifdef HAVE_READLINK
17200 char_u *buf = NULL;
17201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017203 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204#ifdef FEAT_SHORTCUT
17205 {
17206 char_u *v = NULL;
17207
17208 v = mch_resolve_shortcut(p);
17209 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017210 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017212 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 }
17214#else
17215# ifdef HAVE_READLINK
17216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217 char_u *cpy;
17218 int len;
17219 char_u *remain = NULL;
17220 char_u *q;
17221 int is_relative_to_current = FALSE;
17222 int has_trailing_pathsep = FALSE;
17223 int limit = 100;
17224
17225 p = vim_strsave(p);
17226
17227 if (p[0] == '.' && (vim_ispathsep(p[1])
17228 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17229 is_relative_to_current = TRUE;
17230
17231 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017232 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017235 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237
17238 q = getnextcomp(p);
17239 if (*q != NUL)
17240 {
17241 /* Separate the first path component in "p", and keep the
17242 * remainder (beginning with the path separator). */
17243 remain = vim_strsave(q - 1);
17244 q[-1] = NUL;
17245 }
17246
Bram Moolenaard9462e32011-04-11 21:35:11 +020017247 buf = alloc(MAXPATHL + 1);
17248 if (buf == NULL)
17249 goto fail;
17250
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 for (;;)
17252 {
17253 for (;;)
17254 {
17255 len = readlink((char *)p, (char *)buf, MAXPATHL);
17256 if (len <= 0)
17257 break;
17258 buf[len] = NUL;
17259
17260 if (limit-- == 0)
17261 {
17262 vim_free(p);
17263 vim_free(remain);
17264 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017265 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 goto fail;
17267 }
17268
17269 /* Ensure that the result will have a trailing path separator
17270 * if the argument has one. */
17271 if (remain == NULL && has_trailing_pathsep)
17272 add_pathsep(buf);
17273
17274 /* Separate the first path component in the link value and
17275 * concatenate the remainders. */
17276 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17277 if (*q != NUL)
17278 {
17279 if (remain == NULL)
17280 remain = vim_strsave(q - 1);
17281 else
17282 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017283 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284 if (cpy != NULL)
17285 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 vim_free(remain);
17287 remain = cpy;
17288 }
17289 }
17290 q[-1] = NUL;
17291 }
17292
17293 q = gettail(p);
17294 if (q > p && *q == NUL)
17295 {
17296 /* Ignore trailing path separator. */
17297 q[-1] = NUL;
17298 q = gettail(p);
17299 }
17300 if (q > p && !mch_isFullName(buf))
17301 {
17302 /* symlink is relative to directory of argument */
17303 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17304 if (cpy != NULL)
17305 {
17306 STRCPY(cpy, p);
17307 STRCPY(gettail(cpy), buf);
17308 vim_free(p);
17309 p = cpy;
17310 }
17311 }
17312 else
17313 {
17314 vim_free(p);
17315 p = vim_strsave(buf);
17316 }
17317 }
17318
17319 if (remain == NULL)
17320 break;
17321
17322 /* Append the first path component of "remain" to "p". */
17323 q = getnextcomp(remain + 1);
17324 len = q - remain - (*q != NUL);
17325 cpy = vim_strnsave(p, STRLEN(p) + len);
17326 if (cpy != NULL)
17327 {
17328 STRNCAT(cpy, remain, len);
17329 vim_free(p);
17330 p = cpy;
17331 }
17332 /* Shorten "remain". */
17333 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017334 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335 else
17336 {
17337 vim_free(remain);
17338 remain = NULL;
17339 }
17340 }
17341
17342 /* If the result is a relative path name, make it explicitly relative to
17343 * the current directory if and only if the argument had this form. */
17344 if (!vim_ispathsep(*p))
17345 {
17346 if (is_relative_to_current
17347 && *p != NUL
17348 && !(p[0] == '.'
17349 && (p[1] == NUL
17350 || vim_ispathsep(p[1])
17351 || (p[1] == '.'
17352 && (p[2] == NUL
17353 || vim_ispathsep(p[2]))))))
17354 {
17355 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017356 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 if (cpy != NULL)
17358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 vim_free(p);
17360 p = cpy;
17361 }
17362 }
17363 else if (!is_relative_to_current)
17364 {
17365 /* Strip leading "./". */
17366 q = p;
17367 while (q[0] == '.' && vim_ispathsep(q[1]))
17368 q += 2;
17369 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017370 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 }
17372 }
17373
17374 /* Ensure that the result will have no trailing path separator
17375 * if the argument had none. But keep "/" or "//". */
17376 if (!has_trailing_pathsep)
17377 {
17378 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017379 if (after_pathsep(p, q))
17380 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 }
17382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017383 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 }
17385# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017386 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387# endif
17388#endif
17389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017390 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391
17392#ifdef HAVE_READLINK
17393fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017394 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017396 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397}
17398
17399/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017400 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 */
17402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017403f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404{
Bram Moolenaar33570922005-01-25 22:26:29 +000017405 list_T *l;
17406 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407
Bram Moolenaar0d660222005-01-07 21:51:51 +000017408 if (argvars[0].v_type != VAR_LIST)
17409 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017410 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017411 && !tv_check_lock(l->lv_lock,
17412 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017413 {
17414 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017415 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017416 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017417 while (li != NULL)
17418 {
17419 ni = li->li_prev;
17420 list_append(l, li);
17421 li = ni;
17422 }
17423 rettv->vval.v_list = l;
17424 rettv->v_type = VAR_LIST;
17425 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017426 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428}
17429
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017430#define SP_NOMOVE 0x01 /* don't move cursor */
17431#define SP_REPEAT 0x02 /* repeat to find outer pair */
17432#define SP_RETCOUNT 0x04 /* return matchcount */
17433#define SP_SETPCMARK 0x08 /* set previous context mark */
17434#define SP_START 0x10 /* accept match at start position */
17435#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17436#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017437#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017438
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017439static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017440
17441/*
17442 * Get flags for a search function.
17443 * Possibly sets "p_ws".
17444 * Returns BACKWARD, FORWARD or zero (for an error).
17445 */
17446 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017447get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017448{
17449 int dir = FORWARD;
17450 char_u *flags;
17451 char_u nbuf[NUMBUFLEN];
17452 int mask;
17453
17454 if (varp->v_type != VAR_UNKNOWN)
17455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017456 flags = get_tv_string_buf_chk(varp, nbuf);
17457 if (flags == NULL)
17458 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017459 while (*flags != NUL)
17460 {
17461 switch (*flags)
17462 {
17463 case 'b': dir = BACKWARD; break;
17464 case 'w': p_ws = TRUE; break;
17465 case 'W': p_ws = FALSE; break;
17466 default: mask = 0;
17467 if (flagsp != NULL)
17468 switch (*flags)
17469 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017470 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017471 case 'e': mask = SP_END; break;
17472 case 'm': mask = SP_RETCOUNT; break;
17473 case 'n': mask = SP_NOMOVE; break;
17474 case 'p': mask = SP_SUBPAT; break;
17475 case 'r': mask = SP_REPEAT; break;
17476 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017477 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017478 }
17479 if (mask == 0)
17480 {
17481 EMSG2(_(e_invarg2), flags);
17482 dir = 0;
17483 }
17484 else
17485 *flagsp |= mask;
17486 }
17487 if (dir == 0)
17488 break;
17489 ++flags;
17490 }
17491 }
17492 return dir;
17493}
17494
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017496 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017498 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017499search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017501 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 char_u *pat;
17503 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017504 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 int save_p_ws = p_ws;
17506 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017507 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017508 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017509 proftime_T tm;
17510#ifdef FEAT_RELTIME
17511 long time_limit = 0;
17512#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017513 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017514 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017516 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017517 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017518 if (dir == 0)
17519 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017520 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017521 if (flags & SP_START)
17522 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017523 if (flags & SP_END)
17524 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017525 if (flags & SP_COLUMN)
17526 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017527
Bram Moolenaar76929292008-01-06 19:07:36 +000017528 /* Optional arguments: line number to stop searching and timeout. */
17529 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017530 {
17531 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17532 if (lnum_stop < 0)
17533 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017534#ifdef FEAT_RELTIME
17535 if (argvars[3].v_type != VAR_UNKNOWN)
17536 {
17537 time_limit = get_tv_number_chk(&argvars[3], NULL);
17538 if (time_limit < 0)
17539 goto theend;
17540 }
17541#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017542 }
17543
Bram Moolenaar76929292008-01-06 19:07:36 +000017544#ifdef FEAT_RELTIME
17545 /* Set the time limit, if there is one. */
17546 profile_setlimit(time_limit, &tm);
17547#endif
17548
Bram Moolenaar231334e2005-07-25 20:46:57 +000017549 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017550 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017551 * Check to make sure only those flags are set.
17552 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17553 * flags cannot be set. Check for that condition also.
17554 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017555 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017556 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017558 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017559 goto theend;
17560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017562 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017563 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017564 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017565 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017567 if (flags & SP_SUBPAT)
17568 retval = subpatnum;
17569 else
17570 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017571 if (flags & SP_SETPCMARK)
17572 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017574 if (match_pos != NULL)
17575 {
17576 /* Store the match cursor position */
17577 match_pos->lnum = pos.lnum;
17578 match_pos->col = pos.col + 1;
17579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 /* "/$" will put the cursor after the end of the line, may need to
17581 * correct that here */
17582 check_cursor();
17583 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017584
17585 /* If 'n' flag is used: restore cursor position. */
17586 if (flags & SP_NOMOVE)
17587 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017588 else
17589 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017590theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017592
17593 return retval;
17594}
17595
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017596#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017597
17598/*
17599 * round() is not in C90, use ceil() or floor() instead.
17600 */
17601 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017602vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017603{
17604 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17605}
17606
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017607/*
17608 * "round({float})" function
17609 */
17610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017611f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017612{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017613 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017614
17615 rettv->v_type = VAR_FLOAT;
17616 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017617 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017618 else
17619 rettv->vval.v_float = 0.0;
17620}
17621#endif
17622
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017623/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017624 * "screenattr()" function
17625 */
17626 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017627f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017628{
17629 int row;
17630 int col;
17631 int c;
17632
17633 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17634 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17635 if (row < 0 || row >= screen_Rows
17636 || col < 0 || col >= screen_Columns)
17637 c = -1;
17638 else
17639 c = ScreenAttrs[LineOffset[row] + col];
17640 rettv->vval.v_number = c;
17641}
17642
17643/*
17644 * "screenchar()" function
17645 */
17646 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017647f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017648{
17649 int row;
17650 int col;
17651 int off;
17652 int c;
17653
17654 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17655 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17656 if (row < 0 || row >= screen_Rows
17657 || col < 0 || col >= screen_Columns)
17658 c = -1;
17659 else
17660 {
17661 off = LineOffset[row] + col;
17662#ifdef FEAT_MBYTE
17663 if (enc_utf8 && ScreenLinesUC[off] != 0)
17664 c = ScreenLinesUC[off];
17665 else
17666#endif
17667 c = ScreenLines[off];
17668 }
17669 rettv->vval.v_number = c;
17670}
17671
17672/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017673 * "screencol()" function
17674 *
17675 * First column is 1 to be consistent with virtcol().
17676 */
17677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017678f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017679{
17680 rettv->vval.v_number = screen_screencol() + 1;
17681}
17682
17683/*
17684 * "screenrow()" function
17685 */
17686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017687f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017688{
17689 rettv->vval.v_number = screen_screenrow() + 1;
17690}
17691
17692/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017693 * "search()" function
17694 */
17695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017696f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017697{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017698 int flags = 0;
17699
17700 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701}
17702
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017704 * "searchdecl()" function
17705 */
17706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017707f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017708{
17709 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017710 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017711 int error = FALSE;
17712 char_u *name;
17713
17714 rettv->vval.v_number = 1; /* default: FAIL */
17715
17716 name = get_tv_string_chk(&argvars[0]);
17717 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017718 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017719 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017720 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17721 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17722 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017723 if (!error && name != NULL)
17724 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017725 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017726}
17727
17728/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017729 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017731 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017732searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733{
17734 char_u *spat, *mpat, *epat;
17735 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 int dir;
17738 int flags = 0;
17739 char_u nbuf1[NUMBUFLEN];
17740 char_u nbuf2[NUMBUFLEN];
17741 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017742 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017743 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017744 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017747 spat = get_tv_string_chk(&argvars[0]);
17748 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17749 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17750 if (spat == NULL || mpat == NULL || epat == NULL)
17751 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753 /* Handle the optional fourth argument: flags */
17754 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017755 if (dir == 0)
17756 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017757
17758 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017759 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17760 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017761 if ((flags & (SP_END | SP_SUBPAT)) != 0
17762 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017763 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017764 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017765 goto theend;
17766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017768 /* Using 'r' implies 'W', otherwise it doesn't work. */
17769 if (flags & SP_REPEAT)
17770 p_ws = FALSE;
17771
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017772 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017773 if (argvars[3].v_type == VAR_UNKNOWN
17774 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775 skip = (char_u *)"";
17776 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017778 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017779 if (argvars[5].v_type != VAR_UNKNOWN)
17780 {
17781 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17782 if (lnum_stop < 0)
17783 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017784#ifdef FEAT_RELTIME
17785 if (argvars[6].v_type != VAR_UNKNOWN)
17786 {
17787 time_limit = get_tv_number_chk(&argvars[6], NULL);
17788 if (time_limit < 0)
17789 goto theend;
17790 }
17791#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017792 }
17793 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017794 if (skip == NULL)
17795 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017797 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017798 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017799
17800theend:
17801 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017802
17803 return retval;
17804}
17805
17806/*
17807 * "searchpair()" function
17808 */
17809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017810f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017811{
17812 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17813}
17814
17815/*
17816 * "searchpairpos()" function
17817 */
17818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017819f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017820{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017821 pos_T match_pos;
17822 int lnum = 0;
17823 int col = 0;
17824
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017825 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017826 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017827
17828 if (searchpair_cmn(argvars, &match_pos) > 0)
17829 {
17830 lnum = match_pos.lnum;
17831 col = match_pos.col;
17832 }
17833
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017834 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17835 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017836}
17837
17838/*
17839 * Search for a start/middle/end thing.
17840 * Used by searchpair(), see its documentation for the details.
17841 * Returns 0 or -1 for no match,
17842 */
17843 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017844do_searchpair(
17845 char_u *spat, /* start pattern */
17846 char_u *mpat, /* middle pattern */
17847 char_u *epat, /* end pattern */
17848 int dir, /* BACKWARD or FORWARD */
17849 char_u *skip, /* skip expression */
17850 int flags, /* SP_SETPCMARK and other SP_ values */
17851 pos_T *match_pos,
17852 linenr_T lnum_stop, /* stop at this line if not zero */
17853 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017854{
17855 char_u *save_cpo;
17856 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17857 long retval = 0;
17858 pos_T pos;
17859 pos_T firstpos;
17860 pos_T foundpos;
17861 pos_T save_cursor;
17862 pos_T save_pos;
17863 int n;
17864 int r;
17865 int nest = 1;
17866 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017867 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017868 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017869
17870 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17871 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017872 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017873
Bram Moolenaar76929292008-01-06 19:07:36 +000017874#ifdef FEAT_RELTIME
17875 /* Set the time limit, if there is one. */
17876 profile_setlimit(time_limit, &tm);
17877#endif
17878
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017879 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17880 * start/middle/end (pat3, for the top pair). */
17881 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17882 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17883 if (pat2 == NULL || pat3 == NULL)
17884 goto theend;
17885 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17886 if (*mpat == NUL)
17887 STRCPY(pat3, pat2);
17888 else
17889 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17890 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017891 if (flags & SP_START)
17892 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017893
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894 save_cursor = curwin->w_cursor;
17895 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017896 clearpos(&firstpos);
17897 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 pat = pat3;
17899 for (;;)
17900 {
17901 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017902 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17904 /* didn't find it or found the first match again: FAIL */
17905 break;
17906
17907 if (firstpos.lnum == 0)
17908 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017909 if (equalpos(pos, foundpos))
17910 {
17911 /* Found the same position again. Can happen with a pattern that
17912 * has "\zs" at the end and searching backwards. Advance one
17913 * character and try again. */
17914 if (dir == BACKWARD)
17915 decl(&pos);
17916 else
17917 incl(&pos);
17918 }
17919 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017921 /* clear the start flag to avoid getting stuck here */
17922 options &= ~SEARCH_START;
17923
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 /* If the skip pattern matches, ignore this match. */
17925 if (*skip != NUL)
17926 {
17927 save_pos = curwin->w_cursor;
17928 curwin->w_cursor = pos;
17929 r = eval_to_bool(skip, &err, NULL, FALSE);
17930 curwin->w_cursor = save_pos;
17931 if (err)
17932 {
17933 /* Evaluating {skip} caused an error, break here. */
17934 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017935 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 break;
17937 }
17938 if (r)
17939 continue;
17940 }
17941
17942 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17943 {
17944 /* Found end when searching backwards or start when searching
17945 * forward: nested pair. */
17946 ++nest;
17947 pat = pat2; /* nested, don't search for middle */
17948 }
17949 else
17950 {
17951 /* Found end when searching forward or start when searching
17952 * backward: end of (nested) pair; or found middle in outer pair. */
17953 if (--nest == 1)
17954 pat = pat3; /* outer level, search for middle */
17955 }
17956
17957 if (nest == 0)
17958 {
17959 /* Found the match: return matchcount or line number. */
17960 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017961 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017963 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017964 if (flags & SP_SETPCMARK)
17965 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966 curwin->w_cursor = pos;
17967 if (!(flags & SP_REPEAT))
17968 break;
17969 nest = 1; /* search for next unmatched */
17970 }
17971 }
17972
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017973 if (match_pos != NULL)
17974 {
17975 /* Store the match cursor position */
17976 match_pos->lnum = curwin->w_cursor.lnum;
17977 match_pos->col = curwin->w_cursor.col + 1;
17978 }
17979
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017981 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982 curwin->w_cursor = save_cursor;
17983
17984theend:
17985 vim_free(pat2);
17986 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017987 if (p_cpo == empty_option)
17988 p_cpo = save_cpo;
17989 else
17990 /* Darn, evaluating the {skip} expression changed the value. */
17991 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017992
17993 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994}
17995
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017996/*
17997 * "searchpos()" function
17998 */
17999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018000f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018001{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018002 pos_T match_pos;
18003 int lnum = 0;
18004 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018005 int n;
18006 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018007
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018008 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018009 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018010
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018011 n = search_cmn(argvars, &match_pos, &flags);
18012 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018013 {
18014 lnum = match_pos.lnum;
18015 col = match_pos.col;
18016 }
18017
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018018 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18019 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018020 if (flags & SP_SUBPAT)
18021 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018022}
18023
Bram Moolenaar0d660222005-01-07 21:51:51 +000018024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018025f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018027#ifdef FEAT_CLIENTSERVER
18028 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018029 char_u *server = get_tv_string_chk(&argvars[0]);
18030 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031
Bram Moolenaar0d660222005-01-07 21:51:51 +000018032 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018033 if (server == NULL || reply == NULL)
18034 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018035 if (check_restricted() || check_secure())
18036 return;
18037# ifdef FEAT_X11
18038 if (check_connection() == FAIL)
18039 return;
18040# endif
18041
18042 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018044 EMSG(_("E258: Unable to send to client"));
18045 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018047 rettv->vval.v_number = 0;
18048#else
18049 rettv->vval.v_number = -1;
18050#endif
18051}
18052
Bram Moolenaar0d660222005-01-07 21:51:51 +000018053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018054f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018055{
18056 char_u *r = NULL;
18057
18058#ifdef FEAT_CLIENTSERVER
18059# ifdef WIN32
18060 r = serverGetVimNames();
18061# else
18062 make_connection();
18063 if (X_DISPLAY != NULL)
18064 r = serverGetVimNames(X_DISPLAY);
18065# endif
18066#endif
18067 rettv->v_type = VAR_STRING;
18068 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069}
18070
18071/*
18072 * "setbufvar()" function
18073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018075f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076{
18077 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018080 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081 char_u nbuf[NUMBUFLEN];
18082
18083 if (check_restricted() || check_secure())
18084 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018085 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18086 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018087 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 varp = &argvars[2];
18089
18090 if (buf != NULL && varname != NULL && varp != NULL)
18091 {
18092 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094
18095 if (*varname == '&')
18096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018097 long numval;
18098 char_u *strval;
18099 int error = FALSE;
18100
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018102 numval = get_tv_number_chk(varp, &error);
18103 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018104 if (!error && strval != NULL)
18105 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 }
18107 else
18108 {
18109 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18110 if (bufvarname != NULL)
18111 {
18112 STRCPY(bufvarname, "b:");
18113 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018114 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 vim_free(bufvarname);
18116 }
18117 }
18118
18119 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122}
18123
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018125f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018126{
18127 dict_T *d;
18128 dictitem_T *di;
18129 char_u *csearch;
18130
18131 if (argvars[0].v_type != VAR_DICT)
18132 {
18133 EMSG(_(e_dictreq));
18134 return;
18135 }
18136
18137 if ((d = argvars[0].vval.v_dict) != NULL)
18138 {
18139 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18140 if (csearch != NULL)
18141 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018142#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018143 if (enc_utf8)
18144 {
18145 int pcc[MAX_MCO];
18146 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018147
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018148 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18149 }
18150 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018151#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018152 set_last_csearch(PTR2CHAR(csearch),
18153 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018154 }
18155
18156 di = dict_find(d, (char_u *)"forward", -1);
18157 if (di != NULL)
18158 set_csearch_direction(get_tv_number(&di->di_tv)
18159 ? FORWARD : BACKWARD);
18160
18161 di = dict_find(d, (char_u *)"until", -1);
18162 if (di != NULL)
18163 set_csearch_until(!!get_tv_number(&di->di_tv));
18164 }
18165}
18166
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167/*
18168 * "setcmdpos()" function
18169 */
18170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018171f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018173 int pos = (int)get_tv_number(&argvars[0]) - 1;
18174
18175 if (pos >= 0)
18176 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177}
18178
18179/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018180 * "setfperm({fname}, {mode})" function
18181 */
18182 static void
18183f_setfperm(typval_T *argvars, typval_T *rettv)
18184{
18185 char_u *fname;
18186 char_u modebuf[NUMBUFLEN];
18187 char_u *mode_str;
18188 int i;
18189 int mask;
18190 int mode = 0;
18191
18192 rettv->vval.v_number = 0;
18193 fname = get_tv_string_chk(&argvars[0]);
18194 if (fname == NULL)
18195 return;
18196 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18197 if (mode_str == NULL)
18198 return;
18199 if (STRLEN(mode_str) != 9)
18200 {
18201 EMSG2(_(e_invarg2), mode_str);
18202 return;
18203 }
18204
18205 mask = 1;
18206 for (i = 8; i >= 0; --i)
18207 {
18208 if (mode_str[i] != '-')
18209 mode |= mask;
18210 mask = mask << 1;
18211 }
18212 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18213}
18214
18215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 * "setline()" function
18217 */
18218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018219f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220{
18221 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018222 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018223 list_T *l = NULL;
18224 listitem_T *li = NULL;
18225 long added = 0;
18226 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018228 lnum = get_tv_lnum(&argvars[0]);
18229 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018231 l = argvars[1].vval.v_list;
18232 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018234 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018235 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018236
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018237 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018238 for (;;)
18239 {
18240 if (l != NULL)
18241 {
18242 /* list argument, get next string */
18243 if (li == NULL)
18244 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018245 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018246 li = li->li_next;
18247 }
18248
18249 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018250 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018251 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018252
18253 /* When coming here from Insert mode, sync undo, so that this can be
18254 * undone separately from what was previously inserted. */
18255 if (u_sync_once == 2)
18256 {
18257 u_sync_once = 1; /* notify that u_sync() was called */
18258 u_sync(TRUE);
18259 }
18260
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018261 if (lnum <= curbuf->b_ml.ml_line_count)
18262 {
18263 /* existing line, replace it */
18264 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18265 {
18266 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018267 if (lnum == curwin->w_cursor.lnum)
18268 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018269 rettv->vval.v_number = 0; /* OK */
18270 }
18271 }
18272 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18273 {
18274 /* lnum is one past the last line, append the line */
18275 ++added;
18276 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18277 rettv->vval.v_number = 0; /* OK */
18278 }
18279
18280 if (l == NULL) /* only one string argument */
18281 break;
18282 ++lnum;
18283 }
18284
18285 if (added > 0)
18286 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287}
18288
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018289static 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 +000018290
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018292 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018293 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018295set_qf_ll_list(
18296 win_T *wp UNUSED,
18297 typval_T *list_arg UNUSED,
18298 typval_T *action_arg UNUSED,
18299 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018300{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018301#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018302 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018303 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018304 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018305#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018306
Bram Moolenaar2641f772005-03-25 21:58:17 +000018307 rettv->vval.v_number = -1;
18308
18309#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018310 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018311 EMSG(_(e_listreq));
18312 else
18313 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018314 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018315
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018316 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018317 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018318 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018319 if (act == NULL)
18320 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018321 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018322 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018323 else
18324 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018325 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018326 else if (action_arg->v_type == VAR_UNKNOWN)
18327 action = ' ';
18328 else
18329 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018330
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018331 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018332 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018333 rettv->vval.v_number = 0;
18334 }
18335#endif
18336}
18337
18338/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018339 * "setloclist()" function
18340 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018342f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018343{
18344 win_T *win;
18345
18346 rettv->vval.v_number = -1;
18347
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018348 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018349 if (win != NULL)
18350 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18351}
18352
18353/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018354 * "setmatches()" function
18355 */
18356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018357f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018358{
18359#ifdef FEAT_SEARCH_EXTRA
18360 list_T *l;
18361 listitem_T *li;
18362 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018363 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018364
18365 rettv->vval.v_number = -1;
18366 if (argvars[0].v_type != VAR_LIST)
18367 {
18368 EMSG(_(e_listreq));
18369 return;
18370 }
18371 if ((l = argvars[0].vval.v_list) != NULL)
18372 {
18373
18374 /* To some extent make sure that we are dealing with a list from
18375 * "getmatches()". */
18376 li = l->lv_first;
18377 while (li != NULL)
18378 {
18379 if (li->li_tv.v_type != VAR_DICT
18380 || (d = li->li_tv.vval.v_dict) == NULL)
18381 {
18382 EMSG(_(e_invarg));
18383 return;
18384 }
18385 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018386 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18387 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018388 && dict_find(d, (char_u *)"priority", -1) != NULL
18389 && dict_find(d, (char_u *)"id", -1) != NULL))
18390 {
18391 EMSG(_(e_invarg));
18392 return;
18393 }
18394 li = li->li_next;
18395 }
18396
18397 clear_matches(curwin);
18398 li = l->lv_first;
18399 while (li != NULL)
18400 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018401 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018402 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018403 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018404 char_u *group;
18405 int priority;
18406 int id;
18407 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018408
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018409 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018410 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18411 {
18412 if (s == NULL)
18413 {
18414 s = list_alloc();
18415 if (s == NULL)
18416 return;
18417 }
18418
18419 /* match from matchaddpos() */
18420 for (i = 1; i < 9; i++)
18421 {
18422 sprintf((char *)buf, (char *)"pos%d", i);
18423 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18424 {
18425 if (di->di_tv.v_type != VAR_LIST)
18426 return;
18427
18428 list_append_tv(s, &di->di_tv);
18429 s->lv_refcount++;
18430 }
18431 else
18432 break;
18433 }
18434 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018435
18436 group = get_dict_string(d, (char_u *)"group", FALSE);
18437 priority = (int)get_dict_number(d, (char_u *)"priority");
18438 id = (int)get_dict_number(d, (char_u *)"id");
18439 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18440 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18441 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018442 if (i == 0)
18443 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018444 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018445 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018446 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018447 }
18448 else
18449 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018450 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018451 list_unref(s);
18452 s = NULL;
18453 }
18454
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018455 li = li->li_next;
18456 }
18457 rettv->vval.v_number = 0;
18458 }
18459#endif
18460}
18461
18462/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018463 * "setpos()" function
18464 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018466f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018467{
18468 pos_T pos;
18469 int fnum;
18470 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018471 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018472
Bram Moolenaar08250432008-02-13 11:42:46 +000018473 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018474 name = get_tv_string_chk(argvars);
18475 if (name != NULL)
18476 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018477 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018478 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018479 if (--pos.col < 0)
18480 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018481 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018482 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018483 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018484 if (fnum == curbuf->b_fnum)
18485 {
18486 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018487 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018488 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018489 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018490 curwin->w_set_curswant = FALSE;
18491 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018492 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018493 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018494 }
18495 else
18496 EMSG(_(e_invarg));
18497 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018498 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18499 {
18500 /* set mark */
18501 if (setmark_pos(name[1], &pos, fnum) == OK)
18502 rettv->vval.v_number = 0;
18503 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018504 else
18505 EMSG(_(e_invarg));
18506 }
18507 }
18508}
18509
18510/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018511 * "setqflist()" function
18512 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018514f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018515{
18516 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18517}
18518
18519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 * "setreg()" function
18521 */
18522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018523f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524{
18525 int regname;
18526 char_u *strregname;
18527 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018528 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 int append;
18530 char_u yank_type;
18531 long block_len;
18532
18533 block_len = -1;
18534 yank_type = MAUTO;
18535 append = FALSE;
18536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018537 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018538 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018540 if (strregname == NULL)
18541 return; /* type error; errmsg already given */
18542 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543 if (regname == 0 || regname == '@')
18544 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018546 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018548 stropt = get_tv_string_chk(&argvars[2]);
18549 if (stropt == NULL)
18550 return; /* type error */
18551 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 switch (*stropt)
18553 {
18554 case 'a': case 'A': /* append */
18555 append = TRUE;
18556 break;
18557 case 'v': case 'c': /* character-wise selection */
18558 yank_type = MCHAR;
18559 break;
18560 case 'V': case 'l': /* line-wise selection */
18561 yank_type = MLINE;
18562 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 case 'b': case Ctrl_V: /* block-wise selection */
18564 yank_type = MBLOCK;
18565 if (VIM_ISDIGIT(stropt[1]))
18566 {
18567 ++stropt;
18568 block_len = getdigits(&stropt) - 1;
18569 --stropt;
18570 }
18571 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 }
18573 }
18574
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018575 if (argvars[1].v_type == VAR_LIST)
18576 {
18577 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018578 char_u **allocval;
18579 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018580 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018581 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018582 int len = argvars[1].vval.v_list->lv_len;
18583 listitem_T *li;
18584
Bram Moolenaar7d647822014-04-05 21:28:56 +020018585 /* First half: use for pointers to result lines; second half: use for
18586 * pointers to allocated copies. */
18587 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018588 if (lstval == NULL)
18589 return;
18590 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018591 allocval = lstval + len + 2;
18592 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018593
18594 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18595 li = li->li_next)
18596 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018597 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018598 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018599 goto free_lstval;
18600 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018601 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018602 /* Need to make a copy, next get_tv_string_buf_chk() will
18603 * overwrite the string. */
18604 strval = vim_strsave(buf);
18605 if (strval == NULL)
18606 goto free_lstval;
18607 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018608 }
18609 *curval++ = strval;
18610 }
18611 *curval++ = NULL;
18612
18613 write_reg_contents_lst(regname, lstval, -1,
18614 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018615free_lstval:
18616 while (curallocval > allocval)
18617 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018618 vim_free(lstval);
18619 }
18620 else
18621 {
18622 strval = get_tv_string_chk(&argvars[1]);
18623 if (strval == NULL)
18624 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018625 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018628 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629}
18630
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018631/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018632 * "settabvar()" function
18633 */
18634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018635f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018636{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018637#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018638 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018639 tabpage_T *tp;
18640#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018641 char_u *varname, *tabvarname;
18642 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018643
18644 rettv->vval.v_number = 0;
18645
18646 if (check_restricted() || check_secure())
18647 return;
18648
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018649#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018650 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018651#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018652 varname = get_tv_string_chk(&argvars[1]);
18653 varp = &argvars[2];
18654
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018655 if (varname != NULL && varp != NULL
18656#ifdef FEAT_WINDOWS
18657 && tp != NULL
18658#endif
18659 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018660 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018661#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018662 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018663 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018664#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018665
18666 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18667 if (tabvarname != NULL)
18668 {
18669 STRCPY(tabvarname, "t:");
18670 STRCPY(tabvarname + 2, varname);
18671 set_var(tabvarname, varp, TRUE);
18672 vim_free(tabvarname);
18673 }
18674
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018675#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018676 /* Restore current tabpage */
18677 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018678 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018679#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018680 }
18681}
18682
18683/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018684 * "settabwinvar()" function
18685 */
18686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018687f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018688{
18689 setwinvar(argvars, rettv, 1);
18690}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691
18692/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018693 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018696f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018698 setwinvar(argvars, rettv, 0);
18699}
18700
18701/*
18702 * "setwinvar()" and "settabwinvar()" functions
18703 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018704
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018706setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018707{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 win_T *win;
18709#ifdef FEAT_WINDOWS
18710 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018711 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018712 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713#endif
18714 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018715 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018717 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718
18719 if (check_restricted() || check_secure())
18720 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018721
18722#ifdef FEAT_WINDOWS
18723 if (off == 1)
18724 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18725 else
18726 tp = curtab;
18727#endif
18728 win = find_win_by_nr(&argvars[off], tp);
18729 varname = get_tv_string_chk(&argvars[off + 1]);
18730 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731
18732 if (win != NULL && varname != NULL && varp != NULL)
18733 {
18734#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018735 need_switch_win = !(tp == curtab && win == curwin);
18736 if (!need_switch_win
18737 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018740 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018742 long numval;
18743 char_u *strval;
18744 int error = FALSE;
18745
18746 ++varname;
18747 numval = get_tv_number_chk(varp, &error);
18748 strval = get_tv_string_buf_chk(varp, nbuf);
18749 if (!error && strval != NULL)
18750 set_option_value(varname, numval, strval, OPT_LOCAL);
18751 }
18752 else
18753 {
18754 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18755 if (winvarname != NULL)
18756 {
18757 STRCPY(winvarname, "w:");
18758 STRCPY(winvarname + 2, varname);
18759 set_var(winvarname, varp, TRUE);
18760 vim_free(winvarname);
18761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 }
18763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018765 if (need_switch_win)
18766 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767#endif
18768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769}
18770
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018771#ifdef FEAT_CRYPT
18772/*
18773 * "sha256({string})" function
18774 */
18775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018776f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018777{
18778 char_u *p;
18779
18780 p = get_tv_string(&argvars[0]);
18781 rettv->vval.v_string = vim_strsave(
18782 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18783 rettv->v_type = VAR_STRING;
18784}
18785#endif /* FEAT_CRYPT */
18786
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018788 * "shellescape({string})" function
18789 */
18790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018791f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018792{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018793 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018794 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018795 rettv->v_type = VAR_STRING;
18796}
18797
18798/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018799 * shiftwidth() function
18800 */
18801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018802f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018803{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018804 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018805}
18806
18807/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018808 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 */
18810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018811f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018813 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814
Bram Moolenaar0d660222005-01-07 21:51:51 +000018815 p = get_tv_string(&argvars[0]);
18816 rettv->vval.v_string = vim_strsave(p);
18817 simplify_filename(rettv->vval.v_string); /* simplify in place */
18818 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819}
18820
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018821#ifdef FEAT_FLOAT
18822/*
18823 * "sin()" function
18824 */
18825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018826f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018827{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018828 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018829
18830 rettv->v_type = VAR_FLOAT;
18831 if (get_float_arg(argvars, &f) == OK)
18832 rettv->vval.v_float = sin(f);
18833 else
18834 rettv->vval.v_float = 0.0;
18835}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018836
18837/*
18838 * "sinh()" function
18839 */
18840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018841f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018842{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018843 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018844
18845 rettv->v_type = VAR_FLOAT;
18846 if (get_float_arg(argvars, &f) == OK)
18847 rettv->vval.v_float = sinh(f);
18848 else
18849 rettv->vval.v_float = 0.0;
18850}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018851#endif
18852
Bram Moolenaar0d660222005-01-07 21:51:51 +000018853static int
18854#ifdef __BORLANDC__
18855 _RTLENTRYF
18856#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018857 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018858static int
18859#ifdef __BORLANDC__
18860 _RTLENTRYF
18861#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018862 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018863
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018864/* struct used in the array that's given to qsort() */
18865typedef struct
18866{
18867 listitem_T *item;
18868 int idx;
18869} sortItem_T;
18870
Bram Moolenaar0b962472016-02-22 22:51:33 +010018871/* struct storing information about current sort */
18872typedef struct
18873{
18874 int item_compare_ic;
18875 int item_compare_numeric;
18876 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018877#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018878 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018879#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018880 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018881 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018882 dict_T *item_compare_selfdict;
18883 int item_compare_func_err;
18884 int item_compare_keep_zero;
18885} sortinfo_T;
18886static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018887static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018888#define ITEM_COMPARE_FAIL 999
18889
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018891 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018893 static int
18894#ifdef __BORLANDC__
18895_RTLENTRYF
18896#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018897item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018899 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018900 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018901 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018902 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018903 int res;
18904 char_u numbuf1[NUMBUFLEN];
18905 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018907 si1 = (sortItem_T *)s1;
18908 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018909 tv1 = &si1->item->li_tv;
18910 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018911
Bram Moolenaar0b962472016-02-22 22:51:33 +010018912 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018913 {
18914 long v1 = get_tv_number(tv1);
18915 long v2 = get_tv_number(tv2);
18916
18917 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18918 }
18919
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018920#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018921 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018922 {
18923 float_T v1 = get_tv_float(tv1);
18924 float_T v2 = get_tv_float(tv2);
18925
18926 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18927 }
18928#endif
18929
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018930 /* tv2string() puts quotes around a string and allocates memory. Don't do
18931 * that for string variables. Use a single quote when comparing with a
18932 * non-string to do what the docs promise. */
18933 if (tv1->v_type == VAR_STRING)
18934 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018935 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018936 p1 = (char_u *)"'";
18937 else
18938 p1 = tv1->vval.v_string;
18939 }
18940 else
18941 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18942 if (tv2->v_type == VAR_STRING)
18943 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018944 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018945 p2 = (char_u *)"'";
18946 else
18947 p2 = tv2->vval.v_string;
18948 }
18949 else
18950 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018951 if (p1 == NULL)
18952 p1 = (char_u *)"";
18953 if (p2 == NULL)
18954 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018955 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018956 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018957 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018958 res = STRICMP(p1, p2);
18959 else
18960 res = STRCMP(p1, p2);
18961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018963 {
18964 double n1, n2;
18965 n1 = strtod((char *)p1, (char **)&p1);
18966 n2 = strtod((char *)p2, (char **)&p2);
18967 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18968 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018969
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018970 /* When the result would be zero, compare the item indexes. Makes the
18971 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018972 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018973 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018974
Bram Moolenaar0d660222005-01-07 21:51:51 +000018975 vim_free(tofree1);
18976 vim_free(tofree2);
18977 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978}
18979
18980 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018981#ifdef __BORLANDC__
18982_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018984item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018985{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018986 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018987 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018988 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018989 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018990 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018991 char_u *func_name;
18992 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018994 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018995 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018996 return 0;
18997
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018998 si1 = (sortItem_T *)s1;
18999 si2 = (sortItem_T *)s2;
19000
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019001 if (partial == NULL)
19002 func_name = sortinfo->item_compare_func;
19003 else
19004 func_name = partial->pt_name;
19005
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019006 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019007 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019008 copy_tv(&si1->item->li_tv, &argv[0]);
19009 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019010
19011 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019012 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019013 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019014 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019015 clear_tv(&argv[0]);
19016 clear_tv(&argv[1]);
19017
19018 if (res == FAIL)
19019 res = ITEM_COMPARE_FAIL;
19020 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019021 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19022 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019023 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019024 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019025
19026 /* When the result would be zero, compare the pointers themselves. Makes
19027 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019028 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019029 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019030
Bram Moolenaar0d660222005-01-07 21:51:51 +000019031 return res;
19032}
19033
19034/*
19035 * "sort({list})" function
19036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019038do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039{
Bram Moolenaar33570922005-01-25 22:26:29 +000019040 list_T *l;
19041 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019042 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019043 sortinfo_T *old_sortinfo;
19044 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019045 long len;
19046 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047
Bram Moolenaar0b962472016-02-22 22:51:33 +010019048 /* Pointer to current info struct used in compare function. Save and
19049 * restore the current one for nested calls. */
19050 old_sortinfo = sortinfo;
19051 sortinfo = &info;
19052
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019054 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 else
19056 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019057 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019058 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019059 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19060 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019061 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019062 rettv->vval.v_list = l;
19063 rettv->v_type = VAR_LIST;
19064 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065
Bram Moolenaar0d660222005-01-07 21:51:51 +000019066 len = list_len(l);
19067 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019068 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069
Bram Moolenaar0b962472016-02-22 22:51:33 +010019070 info.item_compare_ic = FALSE;
19071 info.item_compare_numeric = FALSE;
19072 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019073#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019074 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019075#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019076 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019077 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019078 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019079 if (argvars[1].v_type != VAR_UNKNOWN)
19080 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019081 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019082 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019083 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019084 else if (argvars[1].v_type == VAR_PARTIAL)
19085 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019086 else
19087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019088 int error = FALSE;
19089
19090 i = get_tv_number_chk(&argvars[1], &error);
19091 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019092 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019093 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019094 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019095 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019096 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019097 else if (i != 0)
19098 {
19099 EMSG(_(e_invarg));
19100 goto theend;
19101 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019102 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019103 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019104 if (*info.item_compare_func == NUL)
19105 {
19106 /* empty string means default sort */
19107 info.item_compare_func = NULL;
19108 }
19109 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019110 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019111 info.item_compare_func = NULL;
19112 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019113 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019115 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019116 info.item_compare_func = NULL;
19117 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019118 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019119#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019120 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019121 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019122 info.item_compare_func = NULL;
19123 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019124 }
19125#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019126 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019127 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019128 info.item_compare_func = NULL;
19129 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019130 }
19131 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019132 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019133
19134 if (argvars[2].v_type != VAR_UNKNOWN)
19135 {
19136 /* optional third argument: {dict} */
19137 if (argvars[2].v_type != VAR_DICT)
19138 {
19139 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019140 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019141 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019142 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019143 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145
Bram Moolenaar0d660222005-01-07 21:51:51 +000019146 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019147 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019148 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019149 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150
Bram Moolenaar327aa022014-03-25 18:24:23 +010019151 i = 0;
19152 if (sort)
19153 {
19154 /* sort(): ptrs will be the list to sort */
19155 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019156 {
19157 ptrs[i].item = li;
19158 ptrs[i].idx = i;
19159 ++i;
19160 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019161
Bram Moolenaar0b962472016-02-22 22:51:33 +010019162 info.item_compare_func_err = FALSE;
19163 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019164 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019165 if ((info.item_compare_func != NULL
19166 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019167 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019168 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019169 EMSG(_("E702: Sort compare function failed"));
19170 else
19171 {
19172 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019173 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019174 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019175 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019176 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019177
Bram Moolenaar0b962472016-02-22 22:51:33 +010019178 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019179 {
19180 /* Clear the List and append the items in sorted order. */
19181 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19182 l->lv_len = 0;
19183 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019184 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019185 }
19186 }
19187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019189 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019190 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019191
19192 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019193 info.item_compare_func_err = FALSE;
19194 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019195 item_compare_func_ptr = info.item_compare_func != NULL
19196 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019197 ? item_compare2 : item_compare;
19198
19199 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19200 li = li->li_next)
19201 {
19202 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19203 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019204 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019205 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019206 {
19207 EMSG(_("E882: Uniq compare function failed"));
19208 break;
19209 }
19210 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019211
Bram Moolenaar0b962472016-02-22 22:51:33 +010019212 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019213 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019214 while (--i >= 0)
19215 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019216 li = ptrs[i].item->li_next;
19217 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019218 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019219 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019220 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019221 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019222 list_fix_watch(l, li);
19223 listitem_free(li);
19224 l->lv_len--;
19225 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019226 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019227 }
19228
19229 vim_free(ptrs);
19230 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019231theend:
19232 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019233}
19234
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019235/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019236 * "sort({list})" function
19237 */
19238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019239f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019240{
19241 do_sort_uniq(argvars, rettv, TRUE);
19242}
19243
19244/*
19245 * "uniq({list})" function
19246 */
19247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019248f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019249{
19250 do_sort_uniq(argvars, rettv, FALSE);
19251}
19252
19253/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019254 * "soundfold({word})" function
19255 */
19256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019257f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019258{
19259 char_u *s;
19260
19261 rettv->v_type = VAR_STRING;
19262 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019263#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019264 rettv->vval.v_string = eval_soundfold(s);
19265#else
19266 rettv->vval.v_string = vim_strsave(s);
19267#endif
19268}
19269
19270/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019271 * "spellbadword()" function
19272 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019274f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019275{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019276 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019277 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019278 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019279
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019280 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019281 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019282
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019283#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019284 if (argvars[0].v_type == VAR_UNKNOWN)
19285 {
19286 /* Find the start and length of the badly spelled word. */
19287 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19288 if (len != 0)
19289 word = ml_get_cursor();
19290 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019291 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019292 {
19293 char_u *str = get_tv_string_chk(&argvars[0]);
19294 int capcol = -1;
19295
19296 if (str != NULL)
19297 {
19298 /* Check the argument for spelling. */
19299 while (*str != NUL)
19300 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019301 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019302 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019303 {
19304 word = str;
19305 break;
19306 }
19307 str += len;
19308 }
19309 }
19310 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019311#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019312
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019313 list_append_string(rettv->vval.v_list, word, len);
19314 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019315 attr == HLF_SPB ? "bad" :
19316 attr == HLF_SPR ? "rare" :
19317 attr == HLF_SPL ? "local" :
19318 attr == HLF_SPC ? "caps" :
19319 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019320}
19321
19322/*
19323 * "spellsuggest()" function
19324 */
19325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019326f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019327{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019328#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019329 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019330 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019331 int maxcount;
19332 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019333 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019334 listitem_T *li;
19335 int need_capital = FALSE;
19336#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019337
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019338 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019339 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019340
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019341#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019342 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019343 {
19344 str = get_tv_string(&argvars[0]);
19345 if (argvars[1].v_type != VAR_UNKNOWN)
19346 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019347 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019348 if (maxcount <= 0)
19349 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019350 if (argvars[2].v_type != VAR_UNKNOWN)
19351 {
19352 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19353 if (typeerr)
19354 return;
19355 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019356 }
19357 else
19358 maxcount = 25;
19359
Bram Moolenaar4770d092006-01-12 23:22:24 +000019360 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019361
19362 for (i = 0; i < ga.ga_len; ++i)
19363 {
19364 str = ((char_u **)ga.ga_data)[i];
19365
19366 li = listitem_alloc();
19367 if (li == NULL)
19368 vim_free(str);
19369 else
19370 {
19371 li->li_tv.v_type = VAR_STRING;
19372 li->li_tv.v_lock = 0;
19373 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019374 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019375 }
19376 }
19377 ga_clear(&ga);
19378 }
19379#endif
19380}
19381
Bram Moolenaar0d660222005-01-07 21:51:51 +000019382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019383f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019384{
19385 char_u *str;
19386 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019387 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019388 regmatch_T regmatch;
19389 char_u patbuf[NUMBUFLEN];
19390 char_u *save_cpo;
19391 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019392 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019393 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019394 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019395
19396 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19397 save_cpo = p_cpo;
19398 p_cpo = (char_u *)"";
19399
19400 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019401 if (argvars[1].v_type != VAR_UNKNOWN)
19402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019403 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19404 if (pat == NULL)
19405 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019406 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019407 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019408 }
19409 if (pat == NULL || *pat == NUL)
19410 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019411
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019412 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019414 if (typeerr)
19415 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416
Bram Moolenaar0d660222005-01-07 21:51:51 +000019417 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19418 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019420 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019421 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019422 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019423 if (*str == NUL)
19424 match = FALSE; /* empty item at the end */
19425 else
19426 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019427 if (match)
19428 end = regmatch.startp[0];
19429 else
19430 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019431 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19432 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019433 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019434 if (list_append_string(rettv->vval.v_list, str,
19435 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019436 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019437 }
19438 if (!match)
19439 break;
19440 /* Advance to just after the match. */
19441 if (regmatch.endp[0] > str)
19442 col = 0;
19443 else
19444 {
19445 /* Don't get stuck at the same match. */
19446#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019447 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019448#else
19449 col = 1;
19450#endif
19451 }
19452 str = regmatch.endp[0];
19453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454
Bram Moolenaar473de612013-06-08 18:19:48 +020019455 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457
Bram Moolenaar0d660222005-01-07 21:51:51 +000019458 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459}
19460
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019461#ifdef FEAT_FLOAT
19462/*
19463 * "sqrt()" function
19464 */
19465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019466f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019467{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019468 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019469
19470 rettv->v_type = VAR_FLOAT;
19471 if (get_float_arg(argvars, &f) == OK)
19472 rettv->vval.v_float = sqrt(f);
19473 else
19474 rettv->vval.v_float = 0.0;
19475}
19476
19477/*
19478 * "str2float()" function
19479 */
19480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019481f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019482{
19483 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19484
19485 if (*p == '+')
19486 p = skipwhite(p + 1);
19487 (void)string2float(p, &rettv->vval.v_float);
19488 rettv->v_type = VAR_FLOAT;
19489}
19490#endif
19491
Bram Moolenaar2c932302006-03-18 21:42:09 +000019492/*
19493 * "str2nr()" function
19494 */
19495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019496f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019497{
19498 int base = 10;
19499 char_u *p;
19500 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019501 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019502
19503 if (argvars[1].v_type != VAR_UNKNOWN)
19504 {
19505 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019506 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019507 {
19508 EMSG(_(e_invarg));
19509 return;
19510 }
19511 }
19512
19513 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019514 if (*p == '+')
19515 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019516 switch (base)
19517 {
19518 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19519 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19520 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19521 default: what = 0;
19522 }
19523 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019524 rettv->vval.v_number = n;
19525}
19526
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527#ifdef HAVE_STRFTIME
19528/*
19529 * "strftime({format}[, {time}])" function
19530 */
19531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019532f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533{
19534 char_u result_buf[256];
19535 struct tm *curtime;
19536 time_t seconds;
19537 char_u *p;
19538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019541 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019542 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 seconds = time(NULL);
19544 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019545 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546 curtime = localtime(&seconds);
19547 /* MSVC returns NULL for an invalid value of seconds. */
19548 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019549 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 else
19551 {
19552# ifdef FEAT_MBYTE
19553 vimconv_T conv;
19554 char_u *enc;
19555
19556 conv.vc_type = CONV_NONE;
19557 enc = enc_locale();
19558 convert_setup(&conv, p_enc, enc);
19559 if (conv.vc_type != CONV_NONE)
19560 p = string_convert(&conv, p, NULL);
19561# endif
19562 if (p != NULL)
19563 (void)strftime((char *)result_buf, sizeof(result_buf),
19564 (char *)p, curtime);
19565 else
19566 result_buf[0] = NUL;
19567
19568# ifdef FEAT_MBYTE
19569 if (conv.vc_type != CONV_NONE)
19570 vim_free(p);
19571 convert_setup(&conv, enc, p_enc);
19572 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019573 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 else
19575# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019576 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577
19578# ifdef FEAT_MBYTE
19579 /* Release conversion descriptors */
19580 convert_setup(&conv, NULL, NULL);
19581 vim_free(enc);
19582# endif
19583 }
19584}
19585#endif
19586
19587/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019588 * "strgetchar()" function
19589 */
19590 static void
19591f_strgetchar(typval_T *argvars, typval_T *rettv)
19592{
19593 char_u *str;
19594 int len;
19595 int error = FALSE;
19596 int charidx;
19597
19598 rettv->vval.v_number = -1;
19599 str = get_tv_string_chk(&argvars[0]);
19600 if (str == NULL)
19601 return;
19602 len = (int)STRLEN(str);
19603 charidx = get_tv_number_chk(&argvars[1], &error);
19604 if (error)
19605 return;
19606#ifdef FEAT_MBYTE
19607 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019608 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019609
19610 while (charidx >= 0 && byteidx < len)
19611 {
19612 if (charidx == 0)
19613 {
19614 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19615 break;
19616 }
19617 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019618 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019619 }
19620 }
19621#else
19622 if (charidx < len)
19623 rettv->vval.v_number = str[charidx];
19624#endif
19625}
19626
19627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 * "stridx()" function
19629 */
19630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019631f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632{
19633 char_u buf[NUMBUFLEN];
19634 char_u *needle;
19635 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019638 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019640 needle = get_tv_string_chk(&argvars[1]);
19641 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019642 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019643 if (needle == NULL || haystack == NULL)
19644 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645
Bram Moolenaar33570922005-01-25 22:26:29 +000019646 if (argvars[2].v_type != VAR_UNKNOWN)
19647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019648 int error = FALSE;
19649
19650 start_idx = get_tv_number_chk(&argvars[2], &error);
19651 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019653 if (start_idx >= 0)
19654 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019655 }
19656
19657 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19658 if (pos != NULL)
19659 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660}
19661
19662/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019663 * "string()" function
19664 */
19665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019666f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019667{
19668 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019669 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019671 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019672 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19673 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019674 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019675 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019676 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677}
19678
19679/*
19680 * "strlen()" function
19681 */
19682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019683f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019685 rettv->vval.v_number = (varnumber_T)(STRLEN(
19686 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687}
19688
19689/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019690 * "strchars()" function
19691 */
19692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019693f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019694{
19695 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019696 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019697#ifdef FEAT_MBYTE
19698 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019699 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019700#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019701
19702 if (argvars[1].v_type != VAR_UNKNOWN)
19703 skipcc = get_tv_number_chk(&argvars[1], NULL);
19704 if (skipcc < 0 || skipcc > 1)
19705 EMSG(_(e_invarg));
19706 else
19707 {
19708#ifdef FEAT_MBYTE
19709 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19710 while (*s != NUL)
19711 {
19712 func_mb_ptr2char_adv(&s);
19713 ++len;
19714 }
19715 rettv->vval.v_number = len;
19716#else
19717 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19718#endif
19719 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019720}
19721
19722/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019723 * "strdisplaywidth()" function
19724 */
19725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019726f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019727{
19728 char_u *s = get_tv_string(&argvars[0]);
19729 int col = 0;
19730
19731 if (argvars[1].v_type != VAR_UNKNOWN)
19732 col = get_tv_number(&argvars[1]);
19733
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019734 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019735}
19736
19737/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019738 * "strwidth()" function
19739 */
19740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019741f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019742{
19743 char_u *s = get_tv_string(&argvars[0]);
19744
19745 rettv->vval.v_number = (varnumber_T)(
19746#ifdef FEAT_MBYTE
19747 mb_string2cells(s, -1)
19748#else
19749 STRLEN(s)
19750#endif
19751 );
19752}
19753
19754/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019755 * "strcharpart()" function
19756 */
19757 static void
19758f_strcharpart(typval_T *argvars, typval_T *rettv)
19759{
19760#ifdef FEAT_MBYTE
19761 char_u *p;
19762 int nchar;
19763 int nbyte = 0;
19764 int charlen;
19765 int len = 0;
19766 int slen;
19767 int error = FALSE;
19768
19769 p = get_tv_string(&argvars[0]);
19770 slen = (int)STRLEN(p);
19771
19772 nchar = get_tv_number_chk(&argvars[1], &error);
19773 if (!error)
19774 {
19775 if (nchar > 0)
19776 while (nchar > 0 && nbyte < slen)
19777 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019778 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019779 --nchar;
19780 }
19781 else
19782 nbyte = nchar;
19783 if (argvars[2].v_type != VAR_UNKNOWN)
19784 {
19785 charlen = get_tv_number(&argvars[2]);
19786 while (charlen > 0 && nbyte + len < slen)
19787 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019788 int off = nbyte + len;
19789
19790 if (off < 0)
19791 len += 1;
19792 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019793 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019794 --charlen;
19795 }
19796 }
19797 else
19798 len = slen - nbyte; /* default: all bytes that are available. */
19799 }
19800
19801 /*
19802 * Only return the overlap between the specified part and the actual
19803 * string.
19804 */
19805 if (nbyte < 0)
19806 {
19807 len += nbyte;
19808 nbyte = 0;
19809 }
19810 else if (nbyte > slen)
19811 nbyte = slen;
19812 if (len < 0)
19813 len = 0;
19814 else if (nbyte + len > slen)
19815 len = slen - nbyte;
19816
19817 rettv->v_type = VAR_STRING;
19818 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19819#else
19820 f_strpart(argvars, rettv);
19821#endif
19822}
19823
19824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825 * "strpart()" function
19826 */
19827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019828f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829{
19830 char_u *p;
19831 int n;
19832 int len;
19833 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019834 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 slen = (int)STRLEN(p);
19838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019839 n = get_tv_number_chk(&argvars[1], &error);
19840 if (error)
19841 len = 0;
19842 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019843 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844 else
19845 len = slen - n; /* default len: all bytes that are available. */
19846
19847 /*
19848 * Only return the overlap between the specified part and the actual
19849 * string.
19850 */
19851 if (n < 0)
19852 {
19853 len += n;
19854 n = 0;
19855 }
19856 else if (n > slen)
19857 n = slen;
19858 if (len < 0)
19859 len = 0;
19860 else if (n + len > slen)
19861 len = slen - n;
19862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019863 rettv->v_type = VAR_STRING;
19864 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865}
19866
19867/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019868 * "strridx()" function
19869 */
19870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019871f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019872{
19873 char_u buf[NUMBUFLEN];
19874 char_u *needle;
19875 char_u *haystack;
19876 char_u *rest;
19877 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019878 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019880 needle = get_tv_string_chk(&argvars[1]);
19881 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019882
19883 rettv->vval.v_number = -1;
19884 if (needle == NULL || haystack == NULL)
19885 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019886
19887 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019888 if (argvars[2].v_type != VAR_UNKNOWN)
19889 {
19890 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019891 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019892 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019893 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019894 }
19895 else
19896 end_idx = haystack_len;
19897
Bram Moolenaar0d660222005-01-07 21:51:51 +000019898 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019899 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019900 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019901 lastmatch = haystack + end_idx;
19902 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019903 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019904 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019905 for (rest = haystack; *rest != '\0'; ++rest)
19906 {
19907 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019908 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019909 break;
19910 lastmatch = rest;
19911 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019912 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019913
19914 if (lastmatch == NULL)
19915 rettv->vval.v_number = -1;
19916 else
19917 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19918}
19919
19920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 * "strtrans()" function
19922 */
19923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019924f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019926 rettv->v_type = VAR_STRING;
19927 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928}
19929
19930/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019931 * "submatch()" function
19932 */
19933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019934f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019935{
Bram Moolenaar41571762014-04-02 19:00:58 +020019936 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019937 int no;
19938 int retList = 0;
19939
19940 no = (int)get_tv_number_chk(&argvars[0], &error);
19941 if (error)
19942 return;
19943 error = FALSE;
19944 if (argvars[1].v_type != VAR_UNKNOWN)
19945 retList = get_tv_number_chk(&argvars[1], &error);
19946 if (error)
19947 return;
19948
19949 if (retList == 0)
19950 {
19951 rettv->v_type = VAR_STRING;
19952 rettv->vval.v_string = reg_submatch(no);
19953 }
19954 else
19955 {
19956 rettv->v_type = VAR_LIST;
19957 rettv->vval.v_list = reg_submatch_list(no);
19958 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019959}
19960
19961/*
19962 * "substitute()" function
19963 */
19964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019965f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019966{
19967 char_u patbuf[NUMBUFLEN];
19968 char_u subbuf[NUMBUFLEN];
19969 char_u flagsbuf[NUMBUFLEN];
19970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019971 char_u *str = get_tv_string_chk(&argvars[0]);
19972 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19973 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19974 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19975
Bram Moolenaar0d660222005-01-07 21:51:51 +000019976 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019977 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19978 rettv->vval.v_string = NULL;
19979 else
19980 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019981}
19982
19983/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019984 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019987f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988{
19989 int id = 0;
19990#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019991 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 long col;
19993 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019994 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019996 lnum = get_tv_lnum(argvars); /* -1 on type error */
19997 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19998 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020000 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020001 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020002 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003#endif
20004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020005 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006}
20007
20008/*
20009 * "synIDattr(id, what [, mode])" function
20010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020012f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013{
20014 char_u *p = NULL;
20015#ifdef FEAT_SYN_HL
20016 int id;
20017 char_u *what;
20018 char_u *mode;
20019 char_u modebuf[NUMBUFLEN];
20020 int modec;
20021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020022 id = get_tv_number(&argvars[0]);
20023 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020024 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020026 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020028 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 modec = 0; /* replace invalid with current */
20030 }
20031 else
20032 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020033#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020034 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 modec = 'g';
20036 else
20037#endif
20038 if (t_colors > 1)
20039 modec = 'c';
20040 else
20041 modec = 't';
20042 }
20043
20044
20045 switch (TOLOWER_ASC(what[0]))
20046 {
20047 case 'b':
20048 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20049 p = highlight_color(id, what, modec);
20050 else /* bold */
20051 p = highlight_has_attr(id, HL_BOLD, modec);
20052 break;
20053
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020054 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 p = highlight_color(id, what, modec);
20056 break;
20057
20058 case 'i':
20059 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20060 p = highlight_has_attr(id, HL_INVERSE, modec);
20061 else /* italic */
20062 p = highlight_has_attr(id, HL_ITALIC, modec);
20063 break;
20064
20065 case 'n': /* name */
20066 p = get_highlight_name(NULL, id - 1);
20067 break;
20068
20069 case 'r': /* reverse */
20070 p = highlight_has_attr(id, HL_INVERSE, modec);
20071 break;
20072
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020073 case 's':
20074 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20075 p = highlight_color(id, what, modec);
20076 else /* standout */
20077 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078 break;
20079
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020080 case 'u':
20081 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20082 /* underline */
20083 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20084 else
20085 /* undercurl */
20086 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087 break;
20088 }
20089
20090 if (p != NULL)
20091 p = vim_strsave(p);
20092#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020093 rettv->v_type = VAR_STRING;
20094 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095}
20096
20097/*
20098 * "synIDtrans(id)" function
20099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020101f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102{
20103 int id;
20104
20105#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020106 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107
20108 if (id > 0)
20109 id = syn_get_final_id(id);
20110 else
20111#endif
20112 id = 0;
20113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020114 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115}
20116
20117/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020118 * "synconcealed(lnum, col)" function
20119 */
20120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020121f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020122{
20123#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20124 long lnum;
20125 long col;
20126 int syntax_flags = 0;
20127 int cchar;
20128 int matchid = 0;
20129 char_u str[NUMBUFLEN];
20130#endif
20131
20132 rettv->v_type = VAR_LIST;
20133 rettv->vval.v_list = NULL;
20134
20135#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20136 lnum = get_tv_lnum(argvars); /* -1 on type error */
20137 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20138
20139 vim_memset(str, NUL, sizeof(str));
20140
20141 if (rettv_list_alloc(rettv) != FAIL)
20142 {
20143 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20144 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20145 && curwin->w_p_cole > 0)
20146 {
20147 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20148 syntax_flags = get_syntax_info(&matchid);
20149
20150 /* get the conceal character */
20151 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20152 {
20153 cchar = syn_get_sub_char();
20154 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20155 cchar = lcs_conceal;
20156 if (cchar != NUL)
20157 {
20158# ifdef FEAT_MBYTE
20159 if (has_mbyte)
20160 (*mb_char2bytes)(cchar, str);
20161 else
20162# endif
20163 str[0] = cchar;
20164 }
20165 }
20166 }
20167
20168 list_append_number(rettv->vval.v_list,
20169 (syntax_flags & HL_CONCEAL) != 0);
20170 /* -1 to auto-determine strlen */
20171 list_append_string(rettv->vval.v_list, str, -1);
20172 list_append_number(rettv->vval.v_list, matchid);
20173 }
20174#endif
20175}
20176
20177/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020178 * "synstack(lnum, col)" function
20179 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020181f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020182{
20183#ifdef FEAT_SYN_HL
20184 long lnum;
20185 long col;
20186 int i;
20187 int id;
20188#endif
20189
20190 rettv->v_type = VAR_LIST;
20191 rettv->vval.v_list = NULL;
20192
20193#ifdef FEAT_SYN_HL
20194 lnum = get_tv_lnum(argvars); /* -1 on type error */
20195 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20196
20197 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020198 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020199 && rettv_list_alloc(rettv) != FAIL)
20200 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020201 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020202 for (i = 0; ; ++i)
20203 {
20204 id = syn_get_stack_item(i);
20205 if (id < 0)
20206 break;
20207 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20208 break;
20209 }
20210 }
20211#endif
20212}
20213
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020215get_cmd_output_as_rettv(
20216 typval_T *argvars,
20217 typval_T *rettv,
20218 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020220 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020222 char_u *infile = NULL;
20223 char_u buf[NUMBUFLEN];
20224 int err = FALSE;
20225 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020226 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020227 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020229 rettv->v_type = VAR_STRING;
20230 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020231 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020232 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020233
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020234 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020235 {
20236 /*
20237 * Write the string to a temp file, to be used for input of the shell
20238 * command.
20239 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020240 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020241 {
20242 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020243 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020244 }
20245
20246 fd = mch_fopen((char *)infile, WRITEBIN);
20247 if (fd == NULL)
20248 {
20249 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020250 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020251 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020252 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020253 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020254 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20255 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020256 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020257 else
20258 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020259 size_t len;
20260
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020261 p = get_tv_string_buf_chk(&argvars[1], buf);
20262 if (p == NULL)
20263 {
20264 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020265 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020266 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020267 len = STRLEN(p);
20268 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020269 err = TRUE;
20270 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020271 if (fclose(fd) != 0)
20272 err = TRUE;
20273 if (err)
20274 {
20275 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020276 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020277 }
20278 }
20279
Bram Moolenaar52a72462014-08-29 15:53:52 +020020280 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20281 * echoes typeahead, that messes up the display. */
20282 if (!msg_silent)
20283 flags += SHELL_COOKED;
20284
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020285 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020287 int len;
20288 listitem_T *li;
20289 char_u *s = NULL;
20290 char_u *start;
20291 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020292 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293
Bram Moolenaar52a72462014-08-29 15:53:52 +020020294 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020295 if (res == NULL)
20296 goto errret;
20297
20298 list = list_alloc();
20299 if (list == NULL)
20300 goto errret;
20301
20302 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020304 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020305 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020306 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020307 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020308
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020309 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020310 if (s == NULL)
20311 goto errret;
20312
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020313 for (p = s; start < end; ++p, ++start)
20314 *p = *start == NUL ? NL : *start;
20315 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020316
20317 li = listitem_alloc();
20318 if (li == NULL)
20319 {
20320 vim_free(s);
20321 goto errret;
20322 }
20323 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020324 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020325 li->li_tv.vval.v_string = s;
20326 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020328
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020329 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020330 rettv->v_type = VAR_LIST;
20331 rettv->vval.v_list = list;
20332 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020334 else
20335 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020336 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020337#ifdef USE_CR
20338 /* translate <CR> into <NL> */
20339 if (res != NULL)
20340 {
20341 char_u *s;
20342
20343 for (s = res; *s; ++s)
20344 {
20345 if (*s == CAR)
20346 *s = NL;
20347 }
20348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349#else
20350# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020351 /* translate <CR><NL> into <NL> */
20352 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020354 char_u *s, *d;
20355
20356 d = res;
20357 for (s = res; *s; ++s)
20358 {
20359 if (s[0] == CAR && s[1] == NL)
20360 ++s;
20361 *d++ = *s;
20362 }
20363 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365# endif
20366#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020367 rettv->vval.v_string = res;
20368 res = NULL;
20369 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020370
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020371errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020372 if (infile != NULL)
20373 {
20374 mch_remove(infile);
20375 vim_free(infile);
20376 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020377 if (res != NULL)
20378 vim_free(res);
20379 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020380 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020381}
20382
20383/*
20384 * "system()" function
20385 */
20386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020387f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020388{
20389 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20390}
20391
20392/*
20393 * "systemlist()" function
20394 */
20395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020396f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020397{
20398 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399}
20400
20401/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020402 * "tabpagebuflist()" function
20403 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020405f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020406{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020407#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020408 tabpage_T *tp;
20409 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020410
20411 if (argvars[0].v_type == VAR_UNKNOWN)
20412 wp = firstwin;
20413 else
20414 {
20415 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20416 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020417 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020418 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020419 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020420 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020421 for (; wp != NULL; wp = wp->w_next)
20422 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020423 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020424 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020425 }
20426#endif
20427}
20428
20429
20430/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020431 * "tabpagenr()" function
20432 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020434f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020435{
20436 int nr = 1;
20437#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020438 char_u *arg;
20439
20440 if (argvars[0].v_type != VAR_UNKNOWN)
20441 {
20442 arg = get_tv_string_chk(&argvars[0]);
20443 nr = 0;
20444 if (arg != NULL)
20445 {
20446 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020447 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020448 else
20449 EMSG2(_(e_invexpr2), arg);
20450 }
20451 }
20452 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020453 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020454#endif
20455 rettv->vval.v_number = nr;
20456}
20457
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020458
20459#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020460static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020461
20462/*
20463 * Common code for tabpagewinnr() and winnr().
20464 */
20465 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020466get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020467{
20468 win_T *twin;
20469 int nr = 1;
20470 win_T *wp;
20471 char_u *arg;
20472
20473 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20474 if (argvar->v_type != VAR_UNKNOWN)
20475 {
20476 arg = get_tv_string_chk(argvar);
20477 if (arg == NULL)
20478 nr = 0; /* type error; errmsg already given */
20479 else if (STRCMP(arg, "$") == 0)
20480 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20481 else if (STRCMP(arg, "#") == 0)
20482 {
20483 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20484 if (twin == NULL)
20485 nr = 0;
20486 }
20487 else
20488 {
20489 EMSG2(_(e_invexpr2), arg);
20490 nr = 0;
20491 }
20492 }
20493
20494 if (nr > 0)
20495 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20496 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020497 {
20498 if (wp == NULL)
20499 {
20500 /* didn't find it in this tabpage */
20501 nr = 0;
20502 break;
20503 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020504 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020505 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020506 return nr;
20507}
20508#endif
20509
20510/*
20511 * "tabpagewinnr()" function
20512 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020514f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020515{
20516 int nr = 1;
20517#ifdef FEAT_WINDOWS
20518 tabpage_T *tp;
20519
20520 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20521 if (tp == NULL)
20522 nr = 0;
20523 else
20524 nr = get_winnr(tp, &argvars[1]);
20525#endif
20526 rettv->vval.v_number = nr;
20527}
20528
20529
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020530/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020531 * "tagfiles()" function
20532 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020534f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020535{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020536 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020537 tagname_T tn;
20538 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020539
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020540 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020541 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020542 fname = alloc(MAXPATHL);
20543 if (fname == NULL)
20544 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020545
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020546 for (first = TRUE; ; first = FALSE)
20547 if (get_tagfname(&tn, first, fname) == FAIL
20548 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020549 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020550 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020551 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020552}
20553
20554/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020555 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020556 */
20557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020558f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020559{
20560 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020561
20562 tag_pattern = get_tv_string(&argvars[0]);
20563
20564 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020565 if (*tag_pattern == NUL)
20566 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020567
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020568 if (rettv_list_alloc(rettv) == OK)
20569 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020570}
20571
20572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 * "tempname()" function
20574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020576f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577{
20578 static int x = 'A';
20579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020580 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020581 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582
20583 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20584 * names. Skip 'I' and 'O', they are used for shell redirection. */
20585 do
20586 {
20587 if (x == 'Z')
20588 x = '0';
20589 else if (x == '9')
20590 x = 'A';
20591 else
20592 {
20593#ifdef EBCDIC
20594 if (x == 'I')
20595 x = 'J';
20596 else if (x == 'R')
20597 x = 'S';
20598 else
20599#endif
20600 ++x;
20601 }
20602 } while (x == 'I' || x == 'O');
20603}
20604
20605/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020606 * "test(list)" function: Just checking the walls...
20607 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020609f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020610{
20611 /* Used for unit testing. Change the code below to your liking. */
20612#if 0
20613 listitem_T *li;
20614 list_T *l;
20615 char_u *bad, *good;
20616
20617 if (argvars[0].v_type != VAR_LIST)
20618 return;
20619 l = argvars[0].vval.v_list;
20620 if (l == NULL)
20621 return;
20622 li = l->lv_first;
20623 if (li == NULL)
20624 return;
20625 bad = get_tv_string(&li->li_tv);
20626 li = li->li_next;
20627 if (li == NULL)
20628 return;
20629 good = get_tv_string(&li->li_tv);
20630 rettv->vval.v_number = test_edit_score(bad, good);
20631#endif
20632}
20633
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020634#ifdef FEAT_FLOAT
20635/*
20636 * "tan()" function
20637 */
20638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020639f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020640{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020641 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020642
20643 rettv->v_type = VAR_FLOAT;
20644 if (get_float_arg(argvars, &f) == OK)
20645 rettv->vval.v_float = tan(f);
20646 else
20647 rettv->vval.v_float = 0.0;
20648}
20649
20650/*
20651 * "tanh()" function
20652 */
20653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020654f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020655{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020656 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020657
20658 rettv->v_type = VAR_FLOAT;
20659 if (get_float_arg(argvars, &f) == OK)
20660 rettv->vval.v_float = tanh(f);
20661 else
20662 rettv->vval.v_float = 0.0;
20663}
20664#endif
20665
Bram Moolenaar975b5272016-03-15 23:10:59 +010020666#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20667/*
20668 * Get a callback from "arg". It can be a Funcref or a function name.
20669 * When "arg" is zero return an empty string.
20670 * Return NULL for an invalid argument.
20671 */
20672 char_u *
20673get_callback(typval_T *arg, partial_T **pp)
20674{
20675 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20676 {
20677 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020678 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020679 return (*pp)->pt_name;
20680 }
20681 *pp = NULL;
20682 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20683 return arg->vval.v_string;
20684 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20685 return (char_u *)"";
20686 EMSG(_("E921: Invalid callback argument"));
20687 return NULL;
20688}
20689#endif
20690
20691#ifdef FEAT_TIMERS
20692/*
20693 * "timer_start(time, callback [, options])" function
20694 */
20695 static void
20696f_timer_start(typval_T *argvars, typval_T *rettv)
20697{
20698 long msec = get_tv_number(&argvars[0]);
20699 timer_T *timer;
20700 int repeat = 0;
20701 char_u *callback;
20702 dict_T *dict;
20703
Bram Moolenaar38499922016-04-22 20:46:52 +020020704 if (check_secure())
20705 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020706 if (argvars[2].v_type != VAR_UNKNOWN)
20707 {
20708 if (argvars[2].v_type != VAR_DICT
20709 || (dict = argvars[2].vval.v_dict) == NULL)
20710 {
20711 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20712 return;
20713 }
20714 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20715 repeat = get_dict_number(dict, (char_u *)"repeat");
20716 }
20717
20718 timer = create_timer(msec, repeat);
20719 callback = get_callback(&argvars[1], &timer->tr_partial);
20720 if (callback == NULL)
20721 {
20722 stop_timer(timer);
20723 rettv->vval.v_number = -1;
20724 }
20725 else
20726 {
20727 timer->tr_callback = vim_strsave(callback);
20728 rettv->vval.v_number = timer->tr_id;
20729 }
20730}
20731
20732/*
20733 * "timer_stop(timer)" function
20734 */
20735 static void
20736f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20737{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020738 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020739
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020740 if (argvars[0].v_type != VAR_NUMBER)
20741 {
20742 EMSG(_(e_number_exp));
20743 return;
20744 }
20745 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020746 if (timer != NULL)
20747 stop_timer(timer);
20748}
20749#endif
20750
Bram Moolenaard52d9742005-08-21 22:20:28 +000020751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 * "tolower(string)" function
20753 */
20754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020755f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756{
20757 char_u *p;
20758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020759 p = vim_strsave(get_tv_string(&argvars[0]));
20760 rettv->v_type = VAR_STRING;
20761 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762
20763 if (p != NULL)
20764 while (*p != NUL)
20765 {
20766#ifdef FEAT_MBYTE
20767 int l;
20768
20769 if (enc_utf8)
20770 {
20771 int c, lc;
20772
20773 c = utf_ptr2char(p);
20774 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020775 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 /* TODO: reallocate string when byte count changes. */
20777 if (utf_char2len(lc) == l)
20778 utf_char2bytes(lc, p);
20779 p += l;
20780 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020781 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 p += l; /* skip multi-byte character */
20783 else
20784#endif
20785 {
20786 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20787 ++p;
20788 }
20789 }
20790}
20791
20792/*
20793 * "toupper(string)" function
20794 */
20795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020796f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020798 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020799 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800}
20801
20802/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020803 * "tr(string, fromstr, tostr)" function
20804 */
20805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020806f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020807{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020808 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020809 char_u *fromstr;
20810 char_u *tostr;
20811 char_u *p;
20812#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020813 int inlen;
20814 int fromlen;
20815 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020816 int idx;
20817 char_u *cpstr;
20818 int cplen;
20819 int first = TRUE;
20820#endif
20821 char_u buf[NUMBUFLEN];
20822 char_u buf2[NUMBUFLEN];
20823 garray_T ga;
20824
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020825 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020826 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20827 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020828
20829 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020830 rettv->v_type = VAR_STRING;
20831 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020832 if (fromstr == NULL || tostr == NULL)
20833 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020834 ga_init2(&ga, (int)sizeof(char), 80);
20835
20836#ifdef FEAT_MBYTE
20837 if (!has_mbyte)
20838#endif
20839 /* not multi-byte: fromstr and tostr must be the same length */
20840 if (STRLEN(fromstr) != STRLEN(tostr))
20841 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020842#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020843error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020844#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020845 EMSG2(_(e_invarg2), fromstr);
20846 ga_clear(&ga);
20847 return;
20848 }
20849
20850 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020851 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020852 {
20853#ifdef FEAT_MBYTE
20854 if (has_mbyte)
20855 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020856 inlen = (*mb_ptr2len)(in_str);
20857 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020858 cplen = inlen;
20859 idx = 0;
20860 for (p = fromstr; *p != NUL; p += fromlen)
20861 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020862 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020863 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020864 {
20865 for (p = tostr; *p != NUL; p += tolen)
20866 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020867 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020868 if (idx-- == 0)
20869 {
20870 cplen = tolen;
20871 cpstr = p;
20872 break;
20873 }
20874 }
20875 if (*p == NUL) /* tostr is shorter than fromstr */
20876 goto error;
20877 break;
20878 }
20879 ++idx;
20880 }
20881
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020882 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020883 {
20884 /* Check that fromstr and tostr have the same number of
20885 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020886 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020887 first = FALSE;
20888 for (p = tostr; *p != NUL; p += tolen)
20889 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020890 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020891 --idx;
20892 }
20893 if (idx != 0)
20894 goto error;
20895 }
20896
Bram Moolenaarcde88542015-08-11 19:14:00 +020020897 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020898 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020899 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020900
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020901 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020902 }
20903 else
20904#endif
20905 {
20906 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020907 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020908 if (p != NULL)
20909 ga_append(&ga, tostr[p - fromstr]);
20910 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020911 ga_append(&ga, *in_str);
20912 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020913 }
20914 }
20915
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020916 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020917 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020918 ga_append(&ga, NUL);
20919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020920 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020921}
20922
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020923#ifdef FEAT_FLOAT
20924/*
20925 * "trunc({float})" function
20926 */
20927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020928f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020929{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020930 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020931
20932 rettv->v_type = VAR_FLOAT;
20933 if (get_float_arg(argvars, &f) == OK)
20934 /* trunc() is not in C90, use floor() or ceil() instead. */
20935 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20936 else
20937 rettv->vval.v_float = 0.0;
20938}
20939#endif
20940
Bram Moolenaar8299df92004-07-10 09:47:34 +000020941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 * "type(expr)" function
20943 */
20944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020945f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020947 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020948
20949 switch (argvars[0].v_type)
20950 {
20951 case VAR_NUMBER: n = 0; break;
20952 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020953 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020954 case VAR_FUNC: n = 2; break;
20955 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020956 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020957 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020958 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020959 if (argvars[0].vval.v_number == VVAL_FALSE
20960 || argvars[0].vval.v_number == VVAL_TRUE)
20961 n = 6;
20962 else
20963 n = 7;
20964 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020965 case VAR_JOB: n = 8; break;
20966 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020967 case VAR_UNKNOWN:
20968 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20969 n = -1;
20970 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020971 }
20972 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973}
20974
20975/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020976 * "undofile(name)" function
20977 */
20978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020979f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020980{
20981 rettv->v_type = VAR_STRING;
20982#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020983 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020984 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020985
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020986 if (*fname == NUL)
20987 {
20988 /* If there is no file name there will be no undo file. */
20989 rettv->vval.v_string = NULL;
20990 }
20991 else
20992 {
20993 char_u *ffname = FullName_save(fname, FALSE);
20994
20995 if (ffname != NULL)
20996 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20997 vim_free(ffname);
20998 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020999 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021000#else
21001 rettv->vval.v_string = NULL;
21002#endif
21003}
21004
21005/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021006 * "undotree()" function
21007 */
21008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021009f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021010{
21011 if (rettv_dict_alloc(rettv) == OK)
21012 {
21013 dict_T *dict = rettv->vval.v_dict;
21014 list_T *list;
21015
Bram Moolenaar730cde92010-06-27 05:18:54 +020021016 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021017 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021018 dict_add_nr_str(dict, "save_last",
21019 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021020 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21021 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021022 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021023
21024 list = list_alloc();
21025 if (list != NULL)
21026 {
21027 u_eval_tree(curbuf->b_u_oldhead, list);
21028 dict_add_list(dict, "entries", list);
21029 }
21030 }
21031}
21032
21033/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021034 * "values(dict)" function
21035 */
21036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021037f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021038{
21039 dict_list(argvars, rettv, 1);
21040}
21041
21042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043 * "virtcol(string)" function
21044 */
21045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021046f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047{
21048 colnr_T vcol = 0;
21049 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021050 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021052 fp = var2fpos(&argvars[0], FALSE, &fnum);
21053 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21054 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 {
21056 getvvcol(curwin, fp, NULL, NULL, &vcol);
21057 ++vcol;
21058 }
21059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021060 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061}
21062
21063/*
21064 * "visualmode()" function
21065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021067f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069 char_u str[2];
21070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021071 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072 str[0] = curbuf->b_visual_mode_eval;
21073 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021074 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075
21076 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021077 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079}
21080
21081/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021082 * "wildmenumode()" function
21083 */
21084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021085f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021086{
21087#ifdef FEAT_WILDMENU
21088 if (wild_menu_showing)
21089 rettv->vval.v_number = 1;
21090#endif
21091}
21092
21093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 * "winbufnr(nr)" function
21095 */
21096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021097f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098{
21099 win_T *wp;
21100
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021101 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021103 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021105 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106}
21107
21108/*
21109 * "wincol()" function
21110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021112f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113{
21114 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021115 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116}
21117
21118/*
21119 * "winheight(nr)" function
21120 */
21121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021122f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123{
21124 win_T *wp;
21125
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021126 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021128 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021130 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131}
21132
21133/*
21134 * "winline()" function
21135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021137f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138{
21139 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021140 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141}
21142
21143/*
21144 * "winnr()" function
21145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021147f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148{
21149 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021150
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021152 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021154 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155}
21156
21157/*
21158 * "winrestcmd()" function
21159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021161f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162{
21163#ifdef FEAT_WINDOWS
21164 win_T *wp;
21165 int winnr = 1;
21166 garray_T ga;
21167 char_u buf[50];
21168
21169 ga_init2(&ga, (int)sizeof(char), 70);
21170 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21171 {
21172 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21173 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21175 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 ++winnr;
21177 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021178 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021180 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021182 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021184 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185}
21186
21187/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021188 * "winrestview()" function
21189 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021191f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021192{
21193 dict_T *dict;
21194
21195 if (argvars[0].v_type != VAR_DICT
21196 || (dict = argvars[0].vval.v_dict) == NULL)
21197 EMSG(_(e_invarg));
21198 else
21199 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021200 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21201 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21202 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21203 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021204#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021205 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21206 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021207#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021208 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21209 {
21210 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21211 curwin->w_set_curswant = FALSE;
21212 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021213
Bram Moolenaar82c25852014-05-28 16:47:16 +020021214 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21215 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021216#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021217 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21218 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021219#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021220 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21221 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21222 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21223 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021224
21225 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021226 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021227# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021228 win_new_width(curwin, W_WIDTH(curwin));
21229# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021230 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021231
Bram Moolenaarb851a962014-10-31 15:45:52 +010021232 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021233 curwin->w_topline = 1;
21234 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21235 curwin->w_topline = curbuf->b_ml.ml_line_count;
21236#ifdef FEAT_DIFF
21237 check_topfill(curwin, TRUE);
21238#endif
21239 }
21240}
21241
21242/*
21243 * "winsaveview()" function
21244 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021246f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021247{
21248 dict_T *dict;
21249
Bram Moolenaara800b422010-06-27 01:15:55 +020021250 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021251 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021252 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021253
21254 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21255 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21256#ifdef FEAT_VIRTUALEDIT
21257 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21258#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021259 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021260 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21261
21262 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21263#ifdef FEAT_DIFF
21264 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21265#endif
21266 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21267 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21268}
21269
21270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 * "winwidth(nr)" function
21272 */
21273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021274f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275{
21276 win_T *wp;
21277
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021278 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021280 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021282#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021283 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021285 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286#endif
21287}
21288
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021290 * "wordcount()" function
21291 */
21292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021293f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021294{
21295 if (rettv_dict_alloc(rettv) == FAIL)
21296 return;
21297 cursor_pos_info(rettv->vval.v_dict);
21298}
21299
21300/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021301 * Write list of strings to file
21302 */
21303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021304write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021305{
21306 listitem_T *li;
21307 int c;
21308 int ret = OK;
21309 char_u *s;
21310
21311 for (li = list->lv_first; li != NULL; li = li->li_next)
21312 {
21313 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21314 {
21315 if (*s == '\n')
21316 c = putc(NUL, fd);
21317 else
21318 c = putc(*s, fd);
21319 if (c == EOF)
21320 {
21321 ret = FAIL;
21322 break;
21323 }
21324 }
21325 if (!binary || li->li_next != NULL)
21326 if (putc('\n', fd) == EOF)
21327 {
21328 ret = FAIL;
21329 break;
21330 }
21331 if (ret == FAIL)
21332 {
21333 EMSG(_(e_write));
21334 break;
21335 }
21336 }
21337 return ret;
21338}
21339
21340/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021341 * "writefile()" function
21342 */
21343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021344f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021345{
21346 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021347 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021348 char_u *fname;
21349 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021350 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021351
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021352 if (check_restricted() || check_secure())
21353 return;
21354
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021355 if (argvars[0].v_type != VAR_LIST)
21356 {
21357 EMSG2(_(e_listarg), "writefile()");
21358 return;
21359 }
21360 if (argvars[0].vval.v_list == NULL)
21361 return;
21362
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021363 if (argvars[2].v_type != VAR_UNKNOWN)
21364 {
21365 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21366 binary = TRUE;
21367 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21368 append = TRUE;
21369 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021370
21371 /* Always open the file in binary mode, library functions have a mind of
21372 * their own about CR-LF conversion. */
21373 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021374 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21375 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021376 {
21377 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21378 ret = -1;
21379 }
21380 else
21381 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021382 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21383 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021384 fclose(fd);
21385 }
21386
21387 rettv->vval.v_number = ret;
21388}
21389
21390/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021391 * "xor(expr, expr)" function
21392 */
21393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021394f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021395{
21396 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21397 ^ get_tv_number_chk(&argvars[1], NULL);
21398}
21399
21400
21401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021403 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 */
21405 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021406var2fpos(
21407 typval_T *varp,
21408 int dollar_lnum, /* TRUE when $ is last line */
21409 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021411 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021413 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414
Bram Moolenaara5525202006-03-02 22:52:09 +000021415 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021416 if (varp->v_type == VAR_LIST)
21417 {
21418 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021419 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021420 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021421 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021422
21423 l = varp->vval.v_list;
21424 if (l == NULL)
21425 return NULL;
21426
21427 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021428 pos.lnum = list_find_nr(l, 0L, &error);
21429 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021430 return NULL; /* invalid line number */
21431
21432 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021433 pos.col = list_find_nr(l, 1L, &error);
21434 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021435 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021436 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021437
21438 /* We accept "$" for the column number: last column. */
21439 li = list_find(l, 1L);
21440 if (li != NULL && li->li_tv.v_type == VAR_STRING
21441 && li->li_tv.vval.v_string != NULL
21442 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21443 pos.col = len + 1;
21444
Bram Moolenaara5525202006-03-02 22:52:09 +000021445 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021446 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021447 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021448 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021449
Bram Moolenaara5525202006-03-02 22:52:09 +000021450#ifdef FEAT_VIRTUALEDIT
21451 /* Get the virtual offset. Defaults to zero. */
21452 pos.coladd = list_find_nr(l, 2L, &error);
21453 if (error)
21454 pos.coladd = 0;
21455#endif
21456
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021457 return &pos;
21458 }
21459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021460 name = get_tv_string_chk(varp);
21461 if (name == NULL)
21462 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021463 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021465 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21466 {
21467 if (VIsual_active)
21468 return &VIsual;
21469 return &curwin->w_cursor;
21470 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021471 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021473 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21475 return NULL;
21476 return pp;
21477 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021478
21479#ifdef FEAT_VIRTUALEDIT
21480 pos.coladd = 0;
21481#endif
21482
Bram Moolenaar477933c2007-07-17 14:32:23 +000021483 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021484 {
21485 pos.col = 0;
21486 if (name[1] == '0') /* "w0": first visible line */
21487 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021488 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021489 pos.lnum = curwin->w_topline;
21490 return &pos;
21491 }
21492 else if (name[1] == '$') /* "w$": last visible line */
21493 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021494 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021495 pos.lnum = curwin->w_botline - 1;
21496 return &pos;
21497 }
21498 }
21499 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021501 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 {
21503 pos.lnum = curbuf->b_ml.ml_line_count;
21504 pos.col = 0;
21505 }
21506 else
21507 {
21508 pos.lnum = curwin->w_cursor.lnum;
21509 pos.col = (colnr_T)STRLEN(ml_get_curline());
21510 }
21511 return &pos;
21512 }
21513 return NULL;
21514}
21515
21516/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021517 * Convert list in "arg" into a position and optional file number.
21518 * When "fnump" is NULL there is no file number, only 3 items.
21519 * Note that the column is passed on as-is, the caller may want to decrement
21520 * it to use 1 for the first column.
21521 * Return FAIL when conversion is not possible, doesn't check the position for
21522 * validity.
21523 */
21524 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021525list2fpos(
21526 typval_T *arg,
21527 pos_T *posp,
21528 int *fnump,
21529 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021530{
21531 list_T *l = arg->vval.v_list;
21532 long i = 0;
21533 long n;
21534
Bram Moolenaar493c1782014-05-28 14:34:46 +020021535 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21536 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021537 if (arg->v_type != VAR_LIST
21538 || l == NULL
21539 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021540 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021541 return FAIL;
21542
21543 if (fnump != NULL)
21544 {
21545 n = list_find_nr(l, i++, NULL); /* fnum */
21546 if (n < 0)
21547 return FAIL;
21548 if (n == 0)
21549 n = curbuf->b_fnum; /* current buffer */
21550 *fnump = n;
21551 }
21552
21553 n = list_find_nr(l, i++, NULL); /* lnum */
21554 if (n < 0)
21555 return FAIL;
21556 posp->lnum = n;
21557
21558 n = list_find_nr(l, i++, NULL); /* col */
21559 if (n < 0)
21560 return FAIL;
21561 posp->col = n;
21562
21563#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021564 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021565 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021566 posp->coladd = 0;
21567 else
21568 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021569#endif
21570
Bram Moolenaar493c1782014-05-28 14:34:46 +020021571 if (curswantp != NULL)
21572 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21573
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021574 return OK;
21575}
21576
21577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 * Get the length of an environment variable name.
21579 * Advance "arg" to the first character after the name.
21580 * Return 0 for error.
21581 */
21582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021583get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584{
21585 char_u *p;
21586 int len;
21587
21588 for (p = *arg; vim_isIDc(*p); ++p)
21589 ;
21590 if (p == *arg) /* no name found */
21591 return 0;
21592
21593 len = (int)(p - *arg);
21594 *arg = p;
21595 return len;
21596}
21597
21598/*
21599 * Get the length of the name of a function or internal variable.
21600 * "arg" is advanced to the first non-white character after the name.
21601 * Return 0 if something is wrong.
21602 */
21603 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021604get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605{
21606 char_u *p;
21607 int len;
21608
21609 /* Find the end of the name. */
21610 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021611 {
21612 if (*p == ':')
21613 {
21614 /* "s:" is start of "s:var", but "n:" is not and can be used in
21615 * slice "[n:]". Also "xx:" is not a namespace. */
21616 len = (int)(p - *arg);
21617 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21618 || len > 1)
21619 break;
21620 }
21621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 if (p == *arg) /* no name found */
21623 return 0;
21624
21625 len = (int)(p - *arg);
21626 *arg = skipwhite(p);
21627
21628 return len;
21629}
21630
21631/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021632 * Get the length of the name of a variable or function.
21633 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021635 * Return -1 if curly braces expansion failed.
21636 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 * If the name contains 'magic' {}'s, expand them and return the
21638 * expanded name in an allocated string via 'alias' - caller must free.
21639 */
21640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021641get_name_len(
21642 char_u **arg,
21643 char_u **alias,
21644 int evaluate,
21645 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646{
21647 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 char_u *p;
21649 char_u *expr_start;
21650 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651
21652 *alias = NULL; /* default to no alias */
21653
21654 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21655 && (*arg)[2] == (int)KE_SNR)
21656 {
21657 /* hard coded <SNR>, already translated */
21658 *arg += 3;
21659 return get_id_len(arg) + 3;
21660 }
21661 len = eval_fname_script(*arg);
21662 if (len > 0)
21663 {
21664 /* literal "<SID>", "s:" or "<SNR>" */
21665 *arg += len;
21666 }
21667
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021669 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021671 p = find_name_end(*arg, &expr_start, &expr_end,
21672 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673 if (expr_start != NULL)
21674 {
21675 char_u *temp_string;
21676
21677 if (!evaluate)
21678 {
21679 len += (int)(p - *arg);
21680 *arg = skipwhite(p);
21681 return len;
21682 }
21683
21684 /*
21685 * Include any <SID> etc in the expanded string:
21686 * Thus the -len here.
21687 */
21688 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21689 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021690 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 *alias = temp_string;
21692 *arg = skipwhite(p);
21693 return (int)STRLEN(temp_string);
21694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695
21696 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021697 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698 EMSG2(_(e_invexpr2), *arg);
21699
21700 return len;
21701}
21702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021703/*
21704 * Find the end of a variable or function name, taking care of magic braces.
21705 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21706 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021707 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021708 * Return a pointer to just after the name. Equal to "arg" if there is no
21709 * valid name.
21710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021712find_name_end(
21713 char_u *arg,
21714 char_u **expr_start,
21715 char_u **expr_end,
21716 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021718 int mb_nest = 0;
21719 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021720 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021721 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021723 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021725 *expr_start = NULL;
21726 *expr_end = NULL;
21727 }
21728
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021729 /* Quick check for valid starting character. */
21730 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21731 return arg;
21732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021733 for (p = arg; *p != NUL
21734 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021735 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021736 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021737 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021738 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021739 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021740 if (*p == '\'')
21741 {
21742 /* skip over 'string' to avoid counting [ and ] inside it. */
21743 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21744 ;
21745 if (*p == NUL)
21746 break;
21747 }
21748 else if (*p == '"')
21749 {
21750 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21751 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21752 if (*p == '\\' && p[1] != NUL)
21753 ++p;
21754 if (*p == NUL)
21755 break;
21756 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021757 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21758 {
21759 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021760 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021761 len = (int)(p - arg);
21762 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021763 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021764 break;
21765 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021767 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021769 if (*p == '[')
21770 ++br_nest;
21771 else if (*p == ']')
21772 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021775 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021777 if (*p == '{')
21778 {
21779 mb_nest++;
21780 if (expr_start != NULL && *expr_start == NULL)
21781 *expr_start = p;
21782 }
21783 else if (*p == '}')
21784 {
21785 mb_nest--;
21786 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21787 *expr_end = p;
21788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 }
21791
21792 return p;
21793}
21794
21795/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021796 * Expands out the 'magic' {}'s in a variable/function name.
21797 * Note that this can call itself recursively, to deal with
21798 * constructs like foo{bar}{baz}{bam}
21799 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21800 * "in_start" ^
21801 * "expr_start" ^
21802 * "expr_end" ^
21803 * "in_end" ^
21804 *
21805 * Returns a new allocated string, which the caller must free.
21806 * Returns NULL for failure.
21807 */
21808 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021809make_expanded_name(
21810 char_u *in_start,
21811 char_u *expr_start,
21812 char_u *expr_end,
21813 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021814{
21815 char_u c1;
21816 char_u *retval = NULL;
21817 char_u *temp_result;
21818 char_u *nextcmd = NULL;
21819
21820 if (expr_end == NULL || in_end == NULL)
21821 return NULL;
21822 *expr_start = NUL;
21823 *expr_end = NUL;
21824 c1 = *in_end;
21825 *in_end = NUL;
21826
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021827 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021828 if (temp_result != NULL && nextcmd == NULL)
21829 {
21830 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21831 + (in_end - expr_end) + 1));
21832 if (retval != NULL)
21833 {
21834 STRCPY(retval, in_start);
21835 STRCAT(retval, temp_result);
21836 STRCAT(retval, expr_end + 1);
21837 }
21838 }
21839 vim_free(temp_result);
21840
21841 *in_end = c1; /* put char back for error messages */
21842 *expr_start = '{';
21843 *expr_end = '}';
21844
21845 if (retval != NULL)
21846 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021847 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021848 if (expr_start != NULL)
21849 {
21850 /* Further expansion! */
21851 temp_result = make_expanded_name(retval, expr_start,
21852 expr_end, temp_result);
21853 vim_free(retval);
21854 retval = temp_result;
21855 }
21856 }
21857
21858 return retval;
21859}
21860
21861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021863 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 */
21865 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021866eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021868 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21869}
21870
21871/*
21872 * Return TRUE if character "c" can be used as the first character in a
21873 * variable or function name (excluding '{' and '}').
21874 */
21875 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021876eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021877{
21878 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879}
21880
21881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 * Set number v: variable to "val".
21883 */
21884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021885set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021887 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888}
21889
21890/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021891 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 */
21893 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021894get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021896 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897}
21898
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021899/*
21900 * Get string v: variable value. Uses a static buffer, can only be used once.
21901 */
21902 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021903get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021904{
21905 return get_tv_string(&vimvars[idx].vv_tv);
21906}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021907
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021909 * Get List v: variable value. Caller must take care of reference count when
21910 * needed.
21911 */
21912 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021913get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021914{
21915 return vimvars[idx].vv_list;
21916}
21917
21918/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021919 * Set v:char to character "c".
21920 */
21921 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021922set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021923{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021924 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021925
21926#ifdef FEAT_MBYTE
21927 if (has_mbyte)
21928 buf[(*mb_char2bytes)(c, buf)] = NUL;
21929 else
21930#endif
21931 {
21932 buf[0] = c;
21933 buf[1] = NUL;
21934 }
21935 set_vim_var_string(VV_CHAR, buf, -1);
21936}
21937
21938/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021939 * Set v:count to "count" and v:count1 to "count1".
21940 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941 */
21942 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021943set_vcount(
21944 long count,
21945 long count1,
21946 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021948 if (set_prevcount)
21949 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021950 vimvars[VV_COUNT].vv_nr = count;
21951 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952}
21953
21954/*
21955 * Set string v: variable to a copy of "val".
21956 */
21957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021958set_vim_var_string(
21959 int idx,
21960 char_u *val,
21961 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962{
Bram Moolenaara542c682016-01-31 16:28:04 +010021963 clear_tv(&vimvars[idx].vv_di.di_tv);
21964 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021965 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021966 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021968 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021970 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971}
21972
21973/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021974 * Set List v: variable to "val".
21975 */
21976 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021977set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021978{
Bram Moolenaara542c682016-01-31 16:28:04 +010021979 clear_tv(&vimvars[idx].vv_di.di_tv);
21980 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021981 vimvars[idx].vv_list = val;
21982 if (val != NULL)
21983 ++val->lv_refcount;
21984}
21985
21986/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021987 * Set Dictionary v: variable to "val".
21988 */
21989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021990set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021991{
21992 int todo;
21993 hashitem_T *hi;
21994
Bram Moolenaara542c682016-01-31 16:28:04 +010021995 clear_tv(&vimvars[idx].vv_di.di_tv);
21996 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021997 vimvars[idx].vv_dict = val;
21998 if (val != NULL)
21999 {
22000 ++val->dv_refcount;
22001
22002 /* Set readonly */
22003 todo = (int)val->dv_hashtab.ht_used;
22004 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22005 {
22006 if (HASHITEM_EMPTY(hi))
22007 continue;
22008 --todo;
22009 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22010 }
22011 }
22012}
22013
22014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015 * Set v:register if needed.
22016 */
22017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022018set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019{
22020 char_u regname;
22021
22022 if (c == 0 || c == ' ')
22023 regname = '"';
22024 else
22025 regname = c;
22026 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022027 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 set_vim_var_string(VV_REG, &regname, 1);
22029}
22030
22031/*
22032 * Get or set v:exception. If "oldval" == NULL, return the current value.
22033 * Otherwise, restore the value to "oldval" and return NULL.
22034 * Must always be called in pairs to save and restore v:exception! Does not
22035 * take care of memory allocations.
22036 */
22037 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022038v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039{
22040 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022041 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042
Bram Moolenaare9a41262005-01-15 22:18:47 +000022043 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 return NULL;
22045}
22046
22047/*
22048 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22049 * Otherwise, restore the value to "oldval" and return NULL.
22050 * Must always be called in pairs to save and restore v:throwpoint! Does not
22051 * take care of memory allocations.
22052 */
22053 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022054v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055{
22056 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022057 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058
Bram Moolenaare9a41262005-01-15 22:18:47 +000022059 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 return NULL;
22061}
22062
22063#if defined(FEAT_AUTOCMD) || defined(PROTO)
22064/*
22065 * Set v:cmdarg.
22066 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22067 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22068 * Must always be called in pairs!
22069 */
22070 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022071set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072{
22073 char_u *oldval;
22074 char_u *newval;
22075 unsigned len;
22076
Bram Moolenaare9a41262005-01-15 22:18:47 +000022077 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022078 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022080 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022081 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022082 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 }
22084
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022085 if (eap->force_bin == FORCE_BIN)
22086 len = 6;
22087 else if (eap->force_bin == FORCE_NOBIN)
22088 len = 8;
22089 else
22090 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022091
22092 if (eap->read_edit)
22093 len += 7;
22094
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022095 if (eap->force_ff != 0)
22096 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22097# ifdef FEAT_MBYTE
22098 if (eap->force_enc != 0)
22099 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022100 if (eap->bad_char != 0)
22101 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022102# endif
22103
22104 newval = alloc(len + 1);
22105 if (newval == NULL)
22106 return NULL;
22107
22108 if (eap->force_bin == FORCE_BIN)
22109 sprintf((char *)newval, " ++bin");
22110 else if (eap->force_bin == FORCE_NOBIN)
22111 sprintf((char *)newval, " ++nobin");
22112 else
22113 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022114
22115 if (eap->read_edit)
22116 STRCAT(newval, " ++edit");
22117
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022118 if (eap->force_ff != 0)
22119 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22120 eap->cmd + eap->force_ff);
22121# ifdef FEAT_MBYTE
22122 if (eap->force_enc != 0)
22123 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22124 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022125 if (eap->bad_char == BAD_KEEP)
22126 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22127 else if (eap->bad_char == BAD_DROP)
22128 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22129 else if (eap->bad_char != 0)
22130 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022131# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022132 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022133 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134}
22135#endif
22136
22137/*
22138 * Get the value of internal variable "name".
22139 * Return OK or FAIL.
22140 */
22141 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022142get_var_tv(
22143 char_u *name,
22144 int len, /* length of "name" */
22145 typval_T *rettv, /* NULL when only checking existence */
22146 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22147 int verbose, /* may give error message */
22148 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149{
22150 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022151 typval_T *tv = NULL;
22152 typval_T atv;
22153 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155
22156 /* truncate the name, so that we can use strcmp() */
22157 cc = name[len];
22158 name[len] = NUL;
22159
22160 /*
22161 * Check for "b:changedtick".
22162 */
22163 if (STRCMP(name, "b:changedtick") == 0)
22164 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022165 atv.v_type = VAR_NUMBER;
22166 atv.vval.v_number = curbuf->b_changedtick;
22167 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 }
22169
22170 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 * Check for user-defined variables.
22172 */
22173 else
22174 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022175 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022177 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022178 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022179 if (dip != NULL)
22180 *dip = v;
22181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 }
22183
Bram Moolenaare9a41262005-01-15 22:18:47 +000022184 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022186 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022187 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 ret = FAIL;
22189 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022190 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022191 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192
22193 name[len] = cc;
22194
22195 return ret;
22196}
22197
22198/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022199 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22200 * Also handle function call with Funcref variable: func(expr)
22201 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22202 */
22203 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022204handle_subscript(
22205 char_u **arg,
22206 typval_T *rettv,
22207 int evaluate, /* do more than finding the end */
22208 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022209{
22210 int ret = OK;
22211 dict_T *selfdict = NULL;
22212 char_u *s;
22213 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022214 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022215
22216 while (ret == OK
22217 && (**arg == '['
22218 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022219 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22220 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022221 && !vim_iswhite(*(*arg - 1)))
22222 {
22223 if (**arg == '(')
22224 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022225 partial_T *pt = NULL;
22226
Bram Moolenaard9fba312005-06-26 22:34:35 +000022227 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022228 if (evaluate)
22229 {
22230 functv = *rettv;
22231 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022232
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022233 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022234 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022235 {
22236 pt = functv.vval.v_partial;
22237 s = pt->pt_name;
22238 }
22239 else
22240 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022241 }
22242 else
22243 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022244 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022245 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022246 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022247
22248 /* Clear the funcref afterwards, so that deleting it while
22249 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022250 if (evaluate)
22251 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022252
22253 /* Stop the expression evaluation when immediately aborting on
22254 * error, or when an interrupt occurred or an exception was thrown
22255 * but not caught. */
22256 if (aborting())
22257 {
22258 if (ret == OK)
22259 clear_tv(rettv);
22260 ret = FAIL;
22261 }
22262 dict_unref(selfdict);
22263 selfdict = NULL;
22264 }
22265 else /* **arg == '[' || **arg == '.' */
22266 {
22267 dict_unref(selfdict);
22268 if (rettv->v_type == VAR_DICT)
22269 {
22270 selfdict = rettv->vval.v_dict;
22271 if (selfdict != NULL)
22272 ++selfdict->dv_refcount;
22273 }
22274 else
22275 selfdict = NULL;
22276 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22277 {
22278 clear_tv(rettv);
22279 ret = FAIL;
22280 }
22281 }
22282 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022283
Bram Moolenaar1d429612016-05-24 15:44:17 +020022284 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22285 * Don't do this when "Func" is already a partial that was bound
22286 * explicitly (pt_auto is FALSE). */
22287 if (selfdict != NULL
22288 && (rettv->v_type == VAR_FUNC
22289 || (rettv->v_type == VAR_PARTIAL
22290 && (rettv->vval.v_partial->pt_auto
22291 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022292 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022293 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22294 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022295 char_u *tofree = NULL;
22296 ufunc_T *fp;
22297 char_u fname_buf[FLEN_FIXED + 1];
22298 int error;
22299
22300 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022301 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022302 fp = find_func(fname);
22303 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022304
Bram Moolenaar65639032016-03-16 21:40:30 +010022305 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022306 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022307 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22308
22309 if (pt != NULL)
22310 {
22311 pt->pt_refcount = 1;
22312 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022313 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022314 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022315 if (rettv->v_type == VAR_FUNC)
22316 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022317 /* Just a function: Take over the function name and use
22318 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022319 pt->pt_name = rettv->vval.v_string;
22320 }
22321 else
22322 {
22323 partial_T *ret_pt = rettv->vval.v_partial;
22324 int i;
22325
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022326 /* Partial: copy the function name, use selfdict and copy
22327 * args. Can't take over name or args, the partial might
22328 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022329 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022330 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022331 if (ret_pt->pt_argc > 0)
22332 {
22333 pt->pt_argv = (typval_T *)alloc(
22334 sizeof(typval_T) * ret_pt->pt_argc);
22335 if (pt->pt_argv == NULL)
22336 /* out of memory: drop the arguments */
22337 pt->pt_argc = 0;
22338 else
22339 {
22340 pt->pt_argc = ret_pt->pt_argc;
22341 for (i = 0; i < pt->pt_argc; i++)
22342 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22343 }
22344 }
22345 partial_unref(ret_pt);
22346 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022347 rettv->v_type = VAR_PARTIAL;
22348 rettv->vval.v_partial = pt;
22349 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022350 }
22351 }
22352
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022353 dict_unref(selfdict);
22354 return ret;
22355}
22356
22357/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022358 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022359 * value).
22360 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022361 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022362alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022363{
Bram Moolenaar33570922005-01-25 22:26:29 +000022364 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022365}
22366
22367/*
22368 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 * The string "s" must have been allocated, it is consumed.
22370 * Return NULL for out of memory, the variable otherwise.
22371 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022372 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022373alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374{
Bram Moolenaar33570922005-01-25 22:26:29 +000022375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022377 rettv = alloc_tv();
22378 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022380 rettv->v_type = VAR_STRING;
22381 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 }
22383 else
22384 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022385 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386}
22387
22388/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022389 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022392free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393{
22394 if (varp != NULL)
22395 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022396 switch (varp->v_type)
22397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022398 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022399 func_unref(varp->vval.v_string);
22400 /*FALLTHROUGH*/
22401 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022402 vim_free(varp->vval.v_string);
22403 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022404 case VAR_PARTIAL:
22405 partial_unref(varp->vval.v_partial);
22406 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022407 case VAR_LIST:
22408 list_unref(varp->vval.v_list);
22409 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022410 case VAR_DICT:
22411 dict_unref(varp->vval.v_dict);
22412 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022413 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022414#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022415 job_unref(varp->vval.v_job);
22416 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022417#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022418 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022419#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022420 channel_unref(varp->vval.v_channel);
22421 break;
22422#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022423 case VAR_NUMBER:
22424 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022425 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022426 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022427 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 vim_free(varp);
22430 }
22431}
22432
22433/*
22434 * Free the memory for a variable value and set the value to NULL or 0.
22435 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022437clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438{
22439 if (varp != NULL)
22440 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022441 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022443 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022444 func_unref(varp->vval.v_string);
22445 /*FALLTHROUGH*/
22446 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022447 vim_free(varp->vval.v_string);
22448 varp->vval.v_string = NULL;
22449 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022450 case VAR_PARTIAL:
22451 partial_unref(varp->vval.v_partial);
22452 varp->vval.v_partial = NULL;
22453 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022454 case VAR_LIST:
22455 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022456 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022457 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022458 case VAR_DICT:
22459 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022460 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022461 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022462 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022463 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022464 varp->vval.v_number = 0;
22465 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022466 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022467#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022468 varp->vval.v_float = 0.0;
22469 break;
22470#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022471 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022472#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022473 job_unref(varp->vval.v_job);
22474 varp->vval.v_job = NULL;
22475#endif
22476 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022477 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022478#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022479 channel_unref(varp->vval.v_channel);
22480 varp->vval.v_channel = NULL;
22481#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022482 case VAR_UNKNOWN:
22483 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022485 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 }
22487}
22488
22489/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022490 * Set the value of a variable to NULL without freeing items.
22491 */
22492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022493init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022494{
22495 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022496 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022497}
22498
22499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 * Get the number value of a variable.
22501 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022502 * For incompatible types, return 0.
22503 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22504 * caller of incompatible types: it sets *denote to TRUE if "denote"
22505 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022507 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022508get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022510 int error = FALSE;
22511
22512 return get_tv_number_chk(varp, &error); /* return 0L on error */
22513}
22514
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022515 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022516get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022517{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022518 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022520 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022522 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022523 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022524 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022525#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022526 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022527 break;
22528#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022529 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022530 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022531 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022532 break;
22533 case VAR_STRING:
22534 if (varp->vval.v_string != NULL)
22535 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022536 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022537 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022538 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022539 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022540 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022541 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022542 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022543 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022544 case VAR_SPECIAL:
22545 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22546 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022547 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022548#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022549 EMSG(_("E910: Using a Job as a Number"));
22550 break;
22551#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022552 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022553#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022554 EMSG(_("E913: Using a Channel as a Number"));
22555 break;
22556#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022557 case VAR_UNKNOWN:
22558 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022559 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022561 if (denote == NULL) /* useful for values that must be unsigned */
22562 n = -1;
22563 else
22564 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022565 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566}
22567
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022568#ifdef FEAT_FLOAT
22569 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022570get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022571{
22572 switch (varp->v_type)
22573 {
22574 case VAR_NUMBER:
22575 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022576 case VAR_FLOAT:
22577 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022578 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022579 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022580 EMSG(_("E891: Using a Funcref as a Float"));
22581 break;
22582 case VAR_STRING:
22583 EMSG(_("E892: Using a String as a Float"));
22584 break;
22585 case VAR_LIST:
22586 EMSG(_("E893: Using a List as a Float"));
22587 break;
22588 case VAR_DICT:
22589 EMSG(_("E894: Using a Dictionary as a Float"));
22590 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022591 case VAR_SPECIAL:
22592 EMSG(_("E907: Using a special value as a Float"));
22593 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022594 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022595# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022596 EMSG(_("E911: Using a Job as a Float"));
22597 break;
22598# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022599 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022600# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022601 EMSG(_("E914: Using a Channel as a Float"));
22602 break;
22603# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022604 case VAR_UNKNOWN:
22605 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022606 break;
22607 }
22608 return 0;
22609}
22610#endif
22611
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022613 * Get the lnum from the first argument.
22614 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022615 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 */
22617 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022618get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619{
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621 linenr_T lnum;
22622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022623 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 if (lnum == 0) /* no valid number, try using line() */
22625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022626 rettv.v_type = VAR_NUMBER;
22627 f_line(argvars, &rettv);
22628 lnum = rettv.vval.v_number;
22629 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 }
22631 return lnum;
22632}
22633
22634/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022635 * Get the lnum from the first argument.
22636 * Also accepts "$", then "buf" is used.
22637 * Returns 0 on error.
22638 */
22639 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022640get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022641{
22642 if (argvars[0].v_type == VAR_STRING
22643 && argvars[0].vval.v_string != NULL
22644 && argvars[0].vval.v_string[0] == '$'
22645 && buf != NULL)
22646 return buf->b_ml.ml_line_count;
22647 return get_tv_number_chk(&argvars[0], NULL);
22648}
22649
22650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 * Get the string value of a variable.
22652 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022653 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22654 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 * If the String variable has never been set, return an empty string.
22656 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022657 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22658 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022660 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022661get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662{
22663 static char_u mybuf[NUMBUFLEN];
22664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022665 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666}
22667
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022668 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022669get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022671 char_u *res = get_tv_string_buf_chk(varp, buf);
22672
22673 return res != NULL ? res : (char_u *)"";
22674}
22675
Bram Moolenaar7d647822014-04-05 21:28:56 +020022676/*
22677 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22678 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022679 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022680get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022681{
22682 static char_u mybuf[NUMBUFLEN];
22683
22684 return get_tv_string_buf_chk(varp, mybuf);
22685}
22686
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022687 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022688get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022689{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022690 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022692 case VAR_NUMBER:
22693 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22694 return buf;
22695 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022696 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022697 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022698 break;
22699 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022700 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022701 break;
22702 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022703 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022704 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022705 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022706#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022707 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022708 break;
22709#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022710 case VAR_STRING:
22711 if (varp->vval.v_string != NULL)
22712 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022713 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022714 case VAR_SPECIAL:
22715 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22716 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022717 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022718#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022719 {
22720 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022721 char *status;
22722
22723 if (job == NULL)
22724 return (char_u *)"no process";
22725 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022726 : job->jv_status == JOB_ENDED ? "dead"
22727 : "run";
22728# ifdef UNIX
22729 vim_snprintf((char *)buf, NUMBUFLEN,
22730 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022731# elif defined(WIN32)
22732 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022733 "process %ld %s",
22734 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022735 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022736# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022737 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022738 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22739# endif
22740 return buf;
22741 }
22742#endif
22743 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022744 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022745#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022746 {
22747 channel_T *channel = varp->vval.v_channel;
22748 char *status = channel_status(channel);
22749
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022750 if (channel == NULL)
22751 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22752 else
22753 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022754 "channel %d %s", channel->ch_id, status);
22755 return buf;
22756 }
22757#endif
22758 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022759 case VAR_UNKNOWN:
22760 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022761 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022763 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764}
22765
22766/*
22767 * Find variable "name" in the list of variables.
22768 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022769 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022770 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022771 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022773 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022774find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022777 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778
Bram Moolenaara7043832005-01-21 11:56:39 +000022779 ht = find_var_ht(name, &varname);
22780 if (htp != NULL)
22781 *htp = ht;
22782 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022784 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022785}
22786
22787/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022788 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022789 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022791 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022792find_var_in_ht(
22793 hashtab_T *ht,
22794 int htname,
22795 char_u *varname,
22796 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022797{
Bram Moolenaar33570922005-01-25 22:26:29 +000022798 hashitem_T *hi;
22799
22800 if (*varname == NUL)
22801 {
22802 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022803 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022804 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022805 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022806 case 'g': return &globvars_var;
22807 case 'v': return &vimvars_var;
22808 case 'b': return &curbuf->b_bufvar;
22809 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022810#ifdef FEAT_WINDOWS
22811 case 't': return &curtab->tp_winvar;
22812#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022813 case 'l': return current_funccal == NULL
22814 ? NULL : &current_funccal->l_vars_var;
22815 case 'a': return current_funccal == NULL
22816 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022817 }
22818 return NULL;
22819 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022820
22821 hi = hash_find(ht, varname);
22822 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022823 {
22824 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022825 * worked find the variable again. Don't auto-load a script if it was
22826 * loaded already, otherwise it would be loaded every time when
22827 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022828 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022829 {
22830 /* Note: script_autoload() may make "hi" invalid. It must either
22831 * be obtained again or not used. */
22832 if (!script_autoload(varname, FALSE) || aborting())
22833 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022834 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022835 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022836 if (HASHITEM_EMPTY(hi))
22837 return NULL;
22838 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022839 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022840}
22841
22842/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022843 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022844 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022845 * Set "varname" to the start of name without ':'.
22846 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022847 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022848find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022850 hashitem_T *hi;
22851
Bram Moolenaar73627d02015-08-11 15:46:09 +020022852 if (name[0] == NUL)
22853 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 if (name[1] != ':')
22855 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022856 /* The name must not start with a colon or #. */
22857 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858 return NULL;
22859 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022860
22861 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022862 hi = hash_find(&compat_hashtab, name);
22863 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022864 return &compat_hashtab;
22865
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022867 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022868 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 }
22870 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022871 if (*name == 'g') /* global variable */
22872 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022873 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22874 */
22875 if (vim_strchr(name + 2, ':') != NULL
22876 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022877 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022879 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022881 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022882#ifdef FEAT_WINDOWS
22883 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022884 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022885#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022886 if (*name == 'v') /* v: variable */
22887 return &vimvarht;
22888 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022889 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022890 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022891 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 if (*name == 's' /* script variable */
22893 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22894 return &SCRIPT_VARS(current_SID);
22895 return NULL;
22896}
22897
22898/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022899 * Get function call environment based on bactrace debug level
22900 */
22901 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022902get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022903{
22904 int i;
22905 funccall_T *funccal;
22906 funccall_T *temp_funccal;
22907
22908 funccal = current_funccal;
22909 if (debug_backtrace_level > 0)
22910 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022911 for (i = 0; i < debug_backtrace_level; i++)
22912 {
22913 temp_funccal = funccal->caller;
22914 if (temp_funccal)
22915 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022916 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022917 /* backtrace level overflow. reset to max */
22918 debug_backtrace_level = i;
22919 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022920 }
22921 return funccal;
22922}
22923
22924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022926 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 * Returns NULL when it doesn't exist.
22928 */
22929 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022930get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931{
Bram Moolenaar33570922005-01-25 22:26:29 +000022932 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022934 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 if (v == NULL)
22936 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022937 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938}
22939
22940/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022941 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 * sourcing this script and when executing functions defined in the script.
22943 */
22944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022945new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946{
Bram Moolenaara7043832005-01-21 11:56:39 +000022947 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022948 hashtab_T *ht;
22949 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022950
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22952 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022953 /* Re-allocating ga_data means that an ht_array pointing to
22954 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022955 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022956 for (i = 1; i <= ga_scripts.ga_len; ++i)
22957 {
22958 ht = &SCRIPT_VARS(i);
22959 if (ht->ht_mask == HT_INIT_SIZE - 1)
22960 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022961 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022962 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022963 }
22964
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 while (ga_scripts.ga_len < id)
22966 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022967 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022968 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022969 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 }
22972 }
22973}
22974
22975/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022976 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22977 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 */
22979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022980init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981{
Bram Moolenaar33570922005-01-25 22:26:29 +000022982 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022983 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022984 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022985 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022986 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022987 dict_var->di_tv.vval.v_dict = dict;
22988 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022989 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022990 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22991 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992}
22993
22994/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022995 * Unreference a dictionary initialized by init_var_dict().
22996 */
22997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022998unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022999{
23000 /* Now the dict needs to be freed if no one else is using it, go back to
23001 * normal reference counting. */
23002 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23003 dict_unref(dict);
23004}
23005
23006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023008 * Frees all allocated variables and the value they contain.
23009 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 */
23011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023012vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023013{
23014 vars_clear_ext(ht, TRUE);
23015}
23016
23017/*
23018 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23019 */
23020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023021vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022{
Bram Moolenaara7043832005-01-21 11:56:39 +000023023 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 hashitem_T *hi;
23025 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026
Bram Moolenaar33570922005-01-25 22:26:29 +000023027 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023028 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023029 for (hi = ht->ht_array; todo > 0; ++hi)
23030 {
23031 if (!HASHITEM_EMPTY(hi))
23032 {
23033 --todo;
23034
Bram Moolenaar33570922005-01-25 22:26:29 +000023035 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023036 * ht_array might change then. hash_clear() takes care of it
23037 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023038 v = HI2DI(hi);
23039 if (free_val)
23040 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023041 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023042 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023043 }
23044 }
23045 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023046 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047}
23048
Bram Moolenaara7043832005-01-21 11:56:39 +000023049/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023050 * Delete a variable from hashtab "ht" at item "hi".
23051 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023054delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055{
Bram Moolenaar33570922005-01-25 22:26:29 +000023056 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023057
23058 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023059 clear_tv(&di->di_tv);
23060 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061}
23062
23063/*
23064 * List the value of one internal variable.
23065 */
23066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023067list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023069 char_u *tofree;
23070 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023071 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023072
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023073 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023074 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023075 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023076 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077}
23078
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023080list_one_var_a(
23081 char_u *prefix,
23082 char_u *name,
23083 int type,
23084 char_u *string,
23085 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086{
Bram Moolenaar31859182007-08-14 20:41:13 +000023087 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23088 msg_start();
23089 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 if (name != NULL) /* "a:" vars don't have a name stored */
23091 msg_puts(name);
23092 msg_putchar(' ');
23093 msg_advance(22);
23094 if (type == VAR_NUMBER)
23095 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023096 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023097 msg_putchar('*');
23098 else if (type == VAR_LIST)
23099 {
23100 msg_putchar('[');
23101 if (*string == '[')
23102 ++string;
23103 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023104 else if (type == VAR_DICT)
23105 {
23106 msg_putchar('{');
23107 if (*string == '{')
23108 ++string;
23109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 else
23111 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023112
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023114
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023115 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023116 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023117 if (*first)
23118 {
23119 msg_clr_eos();
23120 *first = FALSE;
23121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122}
23123
23124/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023125 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 * If the variable already exists, the value is updated.
23127 * Otherwise the variable is created.
23128 */
23129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023130set_var(
23131 char_u *name,
23132 typval_T *tv,
23133 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134{
Bram Moolenaar33570922005-01-25 22:26:29 +000023135 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023137 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023139 ht = find_var_ht(name, &varname);
23140 if (ht == NULL || *varname == NUL)
23141 {
23142 EMSG2(_(e_illvar), name);
23143 return;
23144 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023145 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023146
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023147 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23148 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023149 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023150
Bram Moolenaar33570922005-01-25 22:26:29 +000023151 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023153 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023154 if (var_check_ro(v->di_flags, name, FALSE)
23155 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023156 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023157
23158 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023159 * Handle setting internal v: variables separately where needed to
23160 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023161 */
23162 if (ht == &vimvarht)
23163 {
23164 if (v->di_tv.v_type == VAR_STRING)
23165 {
23166 vim_free(v->di_tv.vval.v_string);
23167 if (copy || tv->v_type != VAR_STRING)
23168 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23169 else
23170 {
23171 /* Take over the string to avoid an extra alloc/free. */
23172 v->di_tv.vval.v_string = tv->vval.v_string;
23173 tv->vval.v_string = NULL;
23174 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023175 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023176 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023177 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023178 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023179 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023180 if (STRCMP(varname, "searchforward") == 0)
23181 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023182#ifdef FEAT_SEARCH_EXTRA
23183 else if (STRCMP(varname, "hlsearch") == 0)
23184 {
23185 no_hlsearch = !v->di_tv.vval.v_number;
23186 redraw_all_later(SOME_VALID);
23187 }
23188#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023189 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023190 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023191 else if (v->di_tv.v_type != tv->v_type)
23192 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023193 }
23194
23195 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 }
23197 else /* add a new variable */
23198 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023199 /* Can't add "v:" variable. */
23200 if (ht == &vimvarht)
23201 {
23202 EMSG2(_(e_illvar), name);
23203 return;
23204 }
23205
Bram Moolenaar92124a32005-06-17 22:03:40 +000023206 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023207 if (!valid_varname(varname))
23208 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023209
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023210 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23211 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023212 if (v == NULL)
23213 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023214 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023215 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023217 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 return;
23219 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023220 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023222
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023223 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023224 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023225 else
23226 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023227 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023228 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023229 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231}
23232
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023233/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023234 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023235 * Also give an error message.
23236 */
23237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023238var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023239{
23240 if (flags & DI_FLAGS_RO)
23241 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023242 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023243 return TRUE;
23244 }
23245 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23246 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023247 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023248 return TRUE;
23249 }
23250 return FALSE;
23251}
23252
23253/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023254 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23255 * Also give an error message.
23256 */
23257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023258var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023259{
23260 if (flags & DI_FLAGS_FIX)
23261 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023262 EMSG2(_("E795: Cannot delete variable %s"),
23263 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023264 return TRUE;
23265 }
23266 return FALSE;
23267}
23268
23269/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023270 * Check if a funcref is assigned to a valid variable name.
23271 * Return TRUE and give an error if not.
23272 */
23273 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023274var_check_func_name(
23275 char_u *name, /* points to start of variable name */
23276 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023277{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023278 /* Allow for w: b: s: and t:. */
23279 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023280 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23281 ? name[2] : name[0]))
23282 {
23283 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23284 name);
23285 return TRUE;
23286 }
23287 /* Don't allow hiding a function. When "v" is not NULL we might be
23288 * assigning another function to the same var, the type is checked
23289 * below. */
23290 if (new_var && function_exists(name))
23291 {
23292 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23293 name);
23294 return TRUE;
23295 }
23296 return FALSE;
23297}
23298
23299/*
23300 * Check if a variable name is valid.
23301 * Return FALSE and give an error if not.
23302 */
23303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023304valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023305{
23306 char_u *p;
23307
23308 for (p = varname; *p != NUL; ++p)
23309 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23310 && *p != AUTOLOAD_CHAR)
23311 {
23312 EMSG2(_(e_illvar), varname);
23313 return FALSE;
23314 }
23315 return TRUE;
23316}
23317
23318/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023319 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023320 * Also give an error message, using "name" or _("name") when use_gettext is
23321 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023322 */
23323 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023324tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023325{
23326 if (lock & VAR_LOCKED)
23327 {
23328 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023329 name == NULL ? (char_u *)_("Unknown")
23330 : use_gettext ? (char_u *)_(name)
23331 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023332 return TRUE;
23333 }
23334 if (lock & VAR_FIXED)
23335 {
23336 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023337 name == NULL ? (char_u *)_("Unknown")
23338 : use_gettext ? (char_u *)_(name)
23339 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023340 return TRUE;
23341 }
23342 return FALSE;
23343}
23344
23345/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023346 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023347 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023348 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023349 * It is OK for "from" and "to" to point to the same item. This is used to
23350 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023351 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023352 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023353copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023355 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023356 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023357 switch (from->v_type)
23358 {
23359 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023360 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023361 to->vval.v_number = from->vval.v_number;
23362 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023363 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023364#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023365 to->vval.v_float = from->vval.v_float;
23366 break;
23367#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023368 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023369#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023370 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023371 if (to->vval.v_job != NULL)
23372 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023373 break;
23374#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023375 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023376#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023377 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023378 if (to->vval.v_channel != NULL)
23379 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023380 break;
23381#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023382 case VAR_STRING:
23383 case VAR_FUNC:
23384 if (from->vval.v_string == NULL)
23385 to->vval.v_string = NULL;
23386 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023387 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023388 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023389 if (from->v_type == VAR_FUNC)
23390 func_ref(to->vval.v_string);
23391 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023392 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023393 case VAR_PARTIAL:
23394 if (from->vval.v_partial == NULL)
23395 to->vval.v_partial = NULL;
23396 else
23397 {
23398 to->vval.v_partial = from->vval.v_partial;
23399 ++to->vval.v_partial->pt_refcount;
23400 }
23401 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023402 case VAR_LIST:
23403 if (from->vval.v_list == NULL)
23404 to->vval.v_list = NULL;
23405 else
23406 {
23407 to->vval.v_list = from->vval.v_list;
23408 ++to->vval.v_list->lv_refcount;
23409 }
23410 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023411 case VAR_DICT:
23412 if (from->vval.v_dict == NULL)
23413 to->vval.v_dict = NULL;
23414 else
23415 {
23416 to->vval.v_dict = from->vval.v_dict;
23417 ++to->vval.v_dict->dv_refcount;
23418 }
23419 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023420 case VAR_UNKNOWN:
23421 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023422 break;
23423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424}
23425
23426/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023427 * Make a copy of an item.
23428 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023429 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23430 * reference to an already copied list/dict can be used.
23431 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023432 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023434item_copy(
23435 typval_T *from,
23436 typval_T *to,
23437 int deep,
23438 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023439{
23440 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023441 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023442
Bram Moolenaar33570922005-01-25 22:26:29 +000023443 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023444 {
23445 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023446 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023447 }
23448 ++recurse;
23449
23450 switch (from->v_type)
23451 {
23452 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023453 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023454 case VAR_STRING:
23455 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023456 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023457 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023458 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023459 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023460 copy_tv(from, to);
23461 break;
23462 case VAR_LIST:
23463 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023464 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023465 if (from->vval.v_list == NULL)
23466 to->vval.v_list = NULL;
23467 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23468 {
23469 /* use the copy made earlier */
23470 to->vval.v_list = from->vval.v_list->lv_copylist;
23471 ++to->vval.v_list->lv_refcount;
23472 }
23473 else
23474 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23475 if (to->vval.v_list == NULL)
23476 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023477 break;
23478 case VAR_DICT:
23479 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023480 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023481 if (from->vval.v_dict == NULL)
23482 to->vval.v_dict = NULL;
23483 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23484 {
23485 /* use the copy made earlier */
23486 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23487 ++to->vval.v_dict->dv_refcount;
23488 }
23489 else
23490 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23491 if (to->vval.v_dict == NULL)
23492 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023493 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023494 case VAR_UNKNOWN:
23495 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023496 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023497 }
23498 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023499 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023500}
23501
23502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503 * ":echo expr1 ..." print each argument separated with a space, add a
23504 * newline at the end.
23505 * ":echon expr1 ..." print each argument plain.
23506 */
23507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023508ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509{
23510 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023511 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023512 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 char_u *p;
23514 int needclr = TRUE;
23515 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023516 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517
23518 if (eap->skip)
23519 ++emsg_skip;
23520 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23521 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023522 /* If eval1() causes an error message the text from the command may
23523 * still need to be cleared. E.g., "echo 22,44". */
23524 need_clr_eos = needclr;
23525
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023527 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 {
23529 /*
23530 * Report the invalid expression unless the expression evaluation
23531 * has been cancelled due to an aborting error, an interrupt, or an
23532 * exception.
23533 */
23534 if (!aborting())
23535 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023536 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 break;
23538 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023539 need_clr_eos = FALSE;
23540
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 if (!eap->skip)
23542 {
23543 if (atstart)
23544 {
23545 atstart = FALSE;
23546 /* Call msg_start() after eval1(), evaluating the expression
23547 * may cause a message to appear. */
23548 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023549 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023550 /* Mark the saved text as finishing the line, so that what
23551 * follows is displayed on a new line when scrolling back
23552 * at the more prompt. */
23553 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556 }
23557 else if (eap->cmdidx == CMD_echo)
23558 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023559 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023560 if (p != NULL)
23561 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023563 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023565 if (*p != TAB && needclr)
23566 {
23567 /* remove any text still there from the command */
23568 msg_clr_eos();
23569 needclr = FALSE;
23570 }
23571 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572 }
23573 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023574 {
23575#ifdef FEAT_MBYTE
23576 if (has_mbyte)
23577 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023578 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023579
23580 (void)msg_outtrans_len_attr(p, i, echo_attr);
23581 p += i - 1;
23582 }
23583 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023585 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023588 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023590 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591 arg = skipwhite(arg);
23592 }
23593 eap->nextcmd = check_nextcmd(arg);
23594
23595 if (eap->skip)
23596 --emsg_skip;
23597 else
23598 {
23599 /* remove text that may still be there from the command */
23600 if (needclr)
23601 msg_clr_eos();
23602 if (eap->cmdidx == CMD_echo)
23603 msg_end();
23604 }
23605}
23606
23607/*
23608 * ":echohl {name}".
23609 */
23610 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023611ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612{
23613 int id;
23614
23615 id = syn_name2id(eap->arg);
23616 if (id == 0)
23617 echo_attr = 0;
23618 else
23619 echo_attr = syn_id2attr(id);
23620}
23621
23622/*
23623 * ":execute expr1 ..." execute the result of an expression.
23624 * ":echomsg expr1 ..." Print a message
23625 * ":echoerr expr1 ..." Print an error
23626 * Each gets spaces around each argument and a newline at the end for
23627 * echo commands
23628 */
23629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023630ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631{
23632 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023633 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 int ret = OK;
23635 char_u *p;
23636 garray_T ga;
23637 int len;
23638 int save_did_emsg;
23639
23640 ga_init2(&ga, 1, 80);
23641
23642 if (eap->skip)
23643 ++emsg_skip;
23644 while (*arg != NUL && *arg != '|' && *arg != '\n')
23645 {
23646 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023647 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 {
23649 /*
23650 * Report the invalid expression unless the expression evaluation
23651 * has been cancelled due to an aborting error, an interrupt, or an
23652 * exception.
23653 */
23654 if (!aborting())
23655 EMSG2(_(e_invexpr2), p);
23656 ret = FAIL;
23657 break;
23658 }
23659
23660 if (!eap->skip)
23661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023662 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663 len = (int)STRLEN(p);
23664 if (ga_grow(&ga, len + 2) == FAIL)
23665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023666 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 ret = FAIL;
23668 break;
23669 }
23670 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 ga.ga_len += len;
23674 }
23675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023676 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023677 arg = skipwhite(arg);
23678 }
23679
23680 if (ret != FAIL && ga.ga_data != NULL)
23681 {
23682 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023683 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023685 out_flush();
23686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023687 else if (eap->cmdidx == CMD_echoerr)
23688 {
23689 /* We don't want to abort following commands, restore did_emsg. */
23690 save_did_emsg = did_emsg;
23691 EMSG((char_u *)ga.ga_data);
23692 if (!force_abort)
23693 did_emsg = save_did_emsg;
23694 }
23695 else if (eap->cmdidx == CMD_execute)
23696 do_cmdline((char_u *)ga.ga_data,
23697 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23698 }
23699
23700 ga_clear(&ga);
23701
23702 if (eap->skip)
23703 --emsg_skip;
23704
23705 eap->nextcmd = check_nextcmd(arg);
23706}
23707
23708/*
23709 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23710 * "arg" points to the "&" or '+' when called, to "option" when returning.
23711 * Returns NULL when no option name found. Otherwise pointer to the char
23712 * after the option name.
23713 */
23714 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023715find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716{
23717 char_u *p = *arg;
23718
23719 ++p;
23720 if (*p == 'g' && p[1] == ':')
23721 {
23722 *opt_flags = OPT_GLOBAL;
23723 p += 2;
23724 }
23725 else if (*p == 'l' && p[1] == ':')
23726 {
23727 *opt_flags = OPT_LOCAL;
23728 p += 2;
23729 }
23730 else
23731 *opt_flags = 0;
23732
23733 if (!ASCII_ISALPHA(*p))
23734 return NULL;
23735 *arg = p;
23736
23737 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23738 p += 4; /* termcap option */
23739 else
23740 while (ASCII_ISALPHA(*p))
23741 ++p;
23742 return p;
23743}
23744
23745/*
23746 * ":function"
23747 */
23748 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023749ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023750{
23751 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023752 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753 int j;
23754 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023756 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 char_u *name = NULL;
23758 char_u *p;
23759 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023760 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023761 garray_T newargs;
23762 garray_T newlines;
23763 int varargs = FALSE;
23764 int mustend = FALSE;
23765 int flags = 0;
23766 ufunc_T *fp;
23767 int indent;
23768 int nesting;
23769 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023770 dictitem_T *v;
23771 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023772 static int func_nr = 0; /* number for nameless function */
23773 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023774 hashtab_T *ht;
23775 int todo;
23776 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023777 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023778
23779 /*
23780 * ":function" without argument: list functions.
23781 */
23782 if (ends_excmd(*eap->arg))
23783 {
23784 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023785 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023786 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023787 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023788 {
23789 if (!HASHITEM_EMPTY(hi))
23790 {
23791 --todo;
23792 fp = HI2UF(hi);
23793 if (!isdigit(*fp->uf_name))
23794 list_func_head(fp, FALSE);
23795 }
23796 }
23797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 eap->nextcmd = check_nextcmd(eap->arg);
23799 return;
23800 }
23801
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023802 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023803 * ":function /pat": list functions matching pattern.
23804 */
23805 if (*eap->arg == '/')
23806 {
23807 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23808 if (!eap->skip)
23809 {
23810 regmatch_T regmatch;
23811
23812 c = *p;
23813 *p = NUL;
23814 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23815 *p = c;
23816 if (regmatch.regprog != NULL)
23817 {
23818 regmatch.rm_ic = p_ic;
23819
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023820 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023821 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23822 {
23823 if (!HASHITEM_EMPTY(hi))
23824 {
23825 --todo;
23826 fp = HI2UF(hi);
23827 if (!isdigit(*fp->uf_name)
23828 && vim_regexec(&regmatch, fp->uf_name, 0))
23829 list_func_head(fp, FALSE);
23830 }
23831 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023832 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023833 }
23834 }
23835 if (*p == '/')
23836 ++p;
23837 eap->nextcmd = check_nextcmd(p);
23838 return;
23839 }
23840
23841 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023842 * Get the function name. There are these situations:
23843 * func normal function name
23844 * "name" == func, "fudi.fd_dict" == NULL
23845 * dict.func new dictionary entry
23846 * "name" == NULL, "fudi.fd_dict" set,
23847 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23848 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023849 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023850 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23851 * dict.func existing dict entry that's not a Funcref
23852 * "name" == NULL, "fudi.fd_dict" set,
23853 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023854 * s:func script-local function name
23855 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023858 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023859 paren = (vim_strchr(p, '(') != NULL);
23860 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861 {
23862 /*
23863 * Return on an invalid expression in braces, unless the expression
23864 * evaluation has been cancelled due to an aborting error, an
23865 * interrupt, or an exception.
23866 */
23867 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023868 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023869 if (!eap->skip && fudi.fd_newkey != NULL)
23870 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023871 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 else
23875 eap->skip = TRUE;
23876 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023877
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878 /* An error in a function call during evaluation of an expression in magic
23879 * braces should not cause the function not to be defined. */
23880 saved_did_emsg = did_emsg;
23881 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882
23883 /*
23884 * ":function func" with only function name: list function.
23885 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023886 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 {
23888 if (!ends_excmd(*skipwhite(p)))
23889 {
23890 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023891 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 }
23893 eap->nextcmd = check_nextcmd(p);
23894 if (eap->nextcmd != NULL)
23895 *p = NUL;
23896 if (!eap->skip && !got_int)
23897 {
23898 fp = find_func(name);
23899 if (fp != NULL)
23900 {
23901 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023902 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023904 if (FUNCLINE(fp, j) == NULL)
23905 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 msg_putchar('\n');
23907 msg_outnum((long)(j + 1));
23908 if (j < 9)
23909 msg_putchar(' ');
23910 if (j < 99)
23911 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023912 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023913 out_flush(); /* show a line at a time */
23914 ui_breakcheck();
23915 }
23916 if (!got_int)
23917 {
23918 msg_putchar('\n');
23919 msg_puts((char_u *)" endfunction");
23920 }
23921 }
23922 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023923 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023924 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023925 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 }
23927
23928 /*
23929 * ":function name(arg1, arg2)" Define function.
23930 */
23931 p = skipwhite(p);
23932 if (*p != '(')
23933 {
23934 if (!eap->skip)
23935 {
23936 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023937 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 }
23939 /* attempt to continue by skipping some text */
23940 if (vim_strchr(p, '(') != NULL)
23941 p = vim_strchr(p, '(');
23942 }
23943 p = skipwhite(p + 1);
23944
23945 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23946 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23947
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023948 if (!eap->skip)
23949 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023950 /* Check the name of the function. Unless it's a dictionary function
23951 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023952 if (name != NULL)
23953 arg = name;
23954 else
23955 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023956 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023957 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23958 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023959 {
23960 if (*arg == K_SPECIAL)
23961 j = 3;
23962 else
23963 j = 0;
23964 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23965 : eval_isnamec(arg[j])))
23966 ++j;
23967 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023968 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023969 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023970 /* Disallow using the g: dict. */
23971 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23972 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023973 }
23974
Bram Moolenaar071d4272004-06-13 20:20:40 +000023975 /*
23976 * Isolate the arguments: "arg1, arg2, ...)"
23977 */
23978 while (*p != ')')
23979 {
23980 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23981 {
23982 varargs = TRUE;
23983 p += 3;
23984 mustend = TRUE;
23985 }
23986 else
23987 {
23988 arg = p;
23989 while (ASCII_ISALNUM(*p) || *p == '_')
23990 ++p;
23991 if (arg == p || isdigit(*arg)
23992 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23993 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23994 {
23995 if (!eap->skip)
23996 EMSG2(_("E125: Illegal argument: %s"), arg);
23997 break;
23998 }
23999 if (ga_grow(&newargs, 1) == FAIL)
24000 goto erret;
24001 c = *p;
24002 *p = NUL;
24003 arg = vim_strsave(arg);
24004 if (arg == NULL)
24005 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024006
24007 /* Check for duplicate argument name. */
24008 for (i = 0; i < newargs.ga_len; ++i)
24009 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24010 {
24011 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024012 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024013 goto erret;
24014 }
24015
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24017 *p = c;
24018 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 if (*p == ',')
24020 ++p;
24021 else
24022 mustend = TRUE;
24023 }
24024 p = skipwhite(p);
24025 if (mustend && *p != ')')
24026 {
24027 if (!eap->skip)
24028 EMSG2(_(e_invarg2), eap->arg);
24029 break;
24030 }
24031 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024032 if (*p != ')')
24033 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034 ++p; /* skip the ')' */
24035
Bram Moolenaare9a41262005-01-15 22:18:47 +000024036 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 for (;;)
24038 {
24039 p = skipwhite(p);
24040 if (STRNCMP(p, "range", 5) == 0)
24041 {
24042 flags |= FC_RANGE;
24043 p += 5;
24044 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024045 else if (STRNCMP(p, "dict", 4) == 0)
24046 {
24047 flags |= FC_DICT;
24048 p += 4;
24049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 else if (STRNCMP(p, "abort", 5) == 0)
24051 {
24052 flags |= FC_ABORT;
24053 p += 5;
24054 }
24055 else
24056 break;
24057 }
24058
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024059 /* When there is a line break use what follows for the function body.
24060 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24061 if (*p == '\n')
24062 line_arg = p + 1;
24063 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 EMSG(_(e_trailing));
24065
24066 /*
24067 * Read the body of the function, until ":endfunction" is found.
24068 */
24069 if (KeyTyped)
24070 {
24071 /* Check if the function already exists, don't let the user type the
24072 * whole function before telling him it doesn't work! For a script we
24073 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024074 if (!eap->skip && !eap->forceit)
24075 {
24076 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24077 EMSG(_(e_funcdict));
24078 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024079 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024082 if (!eap->skip && did_emsg)
24083 goto erret;
24084
Bram Moolenaar071d4272004-06-13 20:20:40 +000024085 msg_putchar('\n'); /* don't overwrite the function name */
24086 cmdline_row = msg_row;
24087 }
24088
24089 indent = 2;
24090 nesting = 0;
24091 for (;;)
24092 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024093 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024094 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024095 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024096 saved_wait_return = FALSE;
24097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024099 sourcing_lnum_off = sourcing_lnum;
24100
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024101 if (line_arg != NULL)
24102 {
24103 /* Use eap->arg, split up in parts by line breaks. */
24104 theline = line_arg;
24105 p = vim_strchr(theline, '\n');
24106 if (p == NULL)
24107 line_arg += STRLEN(line_arg);
24108 else
24109 {
24110 *p = NUL;
24111 line_arg = p + 1;
24112 }
24113 }
24114 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 theline = getcmdline(':', 0L, indent);
24116 else
24117 theline = eap->getline(':', eap->cookie, indent);
24118 if (KeyTyped)
24119 lines_left = Rows - 1;
24120 if (theline == NULL)
24121 {
24122 EMSG(_("E126: Missing :endfunction"));
24123 goto erret;
24124 }
24125
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024126 /* Detect line continuation: sourcing_lnum increased more than one. */
24127 if (sourcing_lnum > sourcing_lnum_off + 1)
24128 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24129 else
24130 sourcing_lnum_off = 0;
24131
Bram Moolenaar071d4272004-06-13 20:20:40 +000024132 if (skip_until != NULL)
24133 {
24134 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24135 * don't check for ":endfunc". */
24136 if (STRCMP(theline, skip_until) == 0)
24137 {
24138 vim_free(skip_until);
24139 skip_until = NULL;
24140 }
24141 }
24142 else
24143 {
24144 /* skip ':' and blanks*/
24145 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24146 ;
24147
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024148 /* Check for "endfunction". */
24149 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024151 if (line_arg == NULL)
24152 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 break;
24154 }
24155
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024156 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 * at "end". */
24158 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24159 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024160 else if (STRNCMP(p, "if", 2) == 0
24161 || STRNCMP(p, "wh", 2) == 0
24162 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163 || STRNCMP(p, "try", 3) == 0)
24164 indent += 2;
24165
24166 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024167 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024168 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024169 if (*p == '!')
24170 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024172 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024173 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024175 ++nesting;
24176 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024177 }
24178 }
24179
24180 /* Check for ":append" or ":insert". */
24181 p = skip_range(p, NULL);
24182 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24183 || (p[0] == 'i'
24184 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24185 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24186 skip_until = vim_strsave((char_u *)".");
24187
24188 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24189 arg = skipwhite(skiptowhite(p));
24190 if (arg[0] == '<' && arg[1] =='<'
24191 && ((p[0] == 'p' && p[1] == 'y'
24192 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24193 || (p[0] == 'p' && p[1] == 'e'
24194 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24195 || (p[0] == 't' && p[1] == 'c'
24196 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024197 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24198 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24200 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024201 || (p[0] == 'm' && p[1] == 'z'
24202 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 ))
24204 {
24205 /* ":python <<" continues until a dot, like ":append" */
24206 p = skipwhite(arg + 2);
24207 if (*p == NUL)
24208 skip_until = vim_strsave((char_u *)".");
24209 else
24210 skip_until = vim_strsave(p);
24211 }
24212 }
24213
24214 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024215 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024216 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024217 if (line_arg == NULL)
24218 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024219 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024220 }
24221
24222 /* Copy the line to newly allocated memory. get_one_sourceline()
24223 * allocates 250 bytes per line, this saves 80% on average. The cost
24224 * is an extra alloc/free. */
24225 p = vim_strsave(theline);
24226 if (p != NULL)
24227 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024228 if (line_arg == NULL)
24229 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024230 theline = p;
24231 }
24232
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024233 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24234
24235 /* Add NULL lines for continuation lines, so that the line count is
24236 * equal to the index in the growarray. */
24237 while (sourcing_lnum_off-- > 0)
24238 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024239
24240 /* Check for end of eap->arg. */
24241 if (line_arg != NULL && *line_arg == NUL)
24242 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243 }
24244
24245 /* Don't define the function when skipping commands or when an error was
24246 * detected. */
24247 if (eap->skip || did_emsg)
24248 goto erret;
24249
24250 /*
24251 * If there are no errors, add the function
24252 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024253 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024254 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024255 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024256 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024257 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024258 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024259 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024260 goto erret;
24261 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024262
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024263 fp = find_func(name);
24264 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024266 if (!eap->forceit)
24267 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024268 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024269 goto erret;
24270 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024271 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024272 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024273 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024274 name);
24275 goto erret;
24276 }
24277 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024278 ga_clear_strings(&(fp->uf_args));
24279 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024280 vim_free(name);
24281 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283 }
24284 else
24285 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024286 char numbuf[20];
24287
24288 fp = NULL;
24289 if (fudi.fd_newkey == NULL && !eap->forceit)
24290 {
24291 EMSG(_(e_funcdict));
24292 goto erret;
24293 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024294 if (fudi.fd_di == NULL)
24295 {
24296 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024297 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024298 goto erret;
24299 }
24300 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024301 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024302 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024303
24304 /* Give the function a sequential number. Can only be used with a
24305 * Funcref! */
24306 vim_free(name);
24307 sprintf(numbuf, "%d", ++func_nr);
24308 name = vim_strsave((char_u *)numbuf);
24309 if (name == NULL)
24310 goto erret;
24311 }
24312
24313 if (fp == NULL)
24314 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024315 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024316 {
24317 int slen, plen;
24318 char_u *scriptname;
24319
24320 /* Check that the autoload name matches the script name. */
24321 j = FAIL;
24322 if (sourcing_name != NULL)
24323 {
24324 scriptname = autoload_name(name);
24325 if (scriptname != NULL)
24326 {
24327 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024328 plen = (int)STRLEN(p);
24329 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024330 if (slen > plen && fnamecmp(p,
24331 sourcing_name + slen - plen) == 0)
24332 j = OK;
24333 vim_free(scriptname);
24334 }
24335 }
24336 if (j == FAIL)
24337 {
24338 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24339 goto erret;
24340 }
24341 }
24342
24343 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344 if (fp == NULL)
24345 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024346
24347 if (fudi.fd_dict != NULL)
24348 {
24349 if (fudi.fd_di == NULL)
24350 {
24351 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024352 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024353 if (fudi.fd_di == NULL)
24354 {
24355 vim_free(fp);
24356 goto erret;
24357 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024358 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24359 {
24360 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024361 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024362 goto erret;
24363 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024364 }
24365 else
24366 /* overwrite existing dict entry */
24367 clear_tv(&fudi.fd_di->di_tv);
24368 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024369 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024370 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024371 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024372
24373 /* behave like "dict" was used */
24374 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024375 }
24376
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024378 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024379 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24380 {
24381 vim_free(fp);
24382 goto erret;
24383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024384 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024385 fp->uf_args = newargs;
24386 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024387#ifdef FEAT_PROFILE
24388 fp->uf_tml_count = NULL;
24389 fp->uf_tml_total = NULL;
24390 fp->uf_tml_self = NULL;
24391 fp->uf_profiling = FALSE;
24392 if (prof_def_func())
24393 func_do_profile(fp);
24394#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024395 fp->uf_varargs = varargs;
24396 fp->uf_flags = flags;
24397 fp->uf_calls = 0;
24398 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024399 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400
24401erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024402 ga_clear_strings(&newargs);
24403 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024404ret_free:
24405 vim_free(skip_until);
24406 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024407 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024408 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024409 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410}
24411
24412/*
24413 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024414 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024415 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024416 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024417 * TFN_INT: internal function name OK
24418 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024419 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024420 * Advances "pp" to just after the function name (if no error).
24421 */
24422 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024423trans_function_name(
24424 char_u **pp,
24425 int skip, /* only find the end, don't evaluate */
24426 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024427 funcdict_T *fdp, /* return: info about dictionary used */
24428 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024429{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024430 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024431 char_u *start;
24432 char_u *end;
24433 int lead;
24434 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024435 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024436 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024437
24438 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024439 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024441
24442 /* Check for hard coded <SNR>: already translated function ID (from a user
24443 * command). */
24444 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24445 && (*pp)[2] == (int)KE_SNR)
24446 {
24447 *pp += 3;
24448 len = get_id_len(pp) + 3;
24449 return vim_strnsave(start, len);
24450 }
24451
24452 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24453 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024455 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024456 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024457
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024458 /* Note that TFN_ flags use the same values as GLV_ flags. */
24459 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024460 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024461 if (end == start)
24462 {
24463 if (!skip)
24464 EMSG(_("E129: Function name required"));
24465 goto theend;
24466 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024467 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024468 {
24469 /*
24470 * Report an invalid expression in braces, unless the expression
24471 * evaluation has been cancelled due to an aborting error, an
24472 * interrupt, or an exception.
24473 */
24474 if (!aborting())
24475 {
24476 if (end != NULL)
24477 EMSG2(_(e_invarg2), start);
24478 }
24479 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024480 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024481 goto theend;
24482 }
24483
24484 if (lv.ll_tv != NULL)
24485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024486 if (fdp != NULL)
24487 {
24488 fdp->fd_dict = lv.ll_dict;
24489 fdp->fd_newkey = lv.ll_newkey;
24490 lv.ll_newkey = NULL;
24491 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024492 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024493 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24494 {
24495 name = vim_strsave(lv.ll_tv->vval.v_string);
24496 *pp = end;
24497 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024498 else if (lv.ll_tv->v_type == VAR_PARTIAL
24499 && lv.ll_tv->vval.v_partial != NULL)
24500 {
24501 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24502 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024503 if (partial != NULL)
24504 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024506 else
24507 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024508 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24509 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024510 EMSG(_(e_funcref));
24511 else
24512 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024513 name = NULL;
24514 }
24515 goto theend;
24516 }
24517
24518 if (lv.ll_name == NULL)
24519 {
24520 /* Error found, but continue after the function name. */
24521 *pp = end;
24522 goto theend;
24523 }
24524
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024525 /* Check if the name is a Funcref. If so, use the value. */
24526 if (lv.ll_exp_name != NULL)
24527 {
24528 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024529 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024530 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024531 if (name == lv.ll_exp_name)
24532 name = NULL;
24533 }
24534 else
24535 {
24536 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024537 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024538 if (name == *pp)
24539 name = NULL;
24540 }
24541 if (name != NULL)
24542 {
24543 name = vim_strsave(name);
24544 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024545 if (STRNCMP(name, "<SNR>", 5) == 0)
24546 {
24547 /* Change "<SNR>" to the byte sequence. */
24548 name[0] = K_SPECIAL;
24549 name[1] = KS_EXTRA;
24550 name[2] = (int)KE_SNR;
24551 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24552 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024553 goto theend;
24554 }
24555
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024556 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024557 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024558 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024559 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24560 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24561 {
24562 /* When there was "s:" already or the name expanded to get a
24563 * leading "s:" then remove it. */
24564 lv.ll_name += 2;
24565 len -= 2;
24566 lead = 2;
24567 }
24568 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024569 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024570 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024571 /* skip over "s:" and "g:" */
24572 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024573 lv.ll_name += 2;
24574 len = (int)(end - lv.ll_name);
24575 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024576
24577 /*
24578 * Copy the function name to allocated memory.
24579 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24580 * Accept <SNR>123_name() outside a script.
24581 */
24582 if (skip)
24583 lead = 0; /* do nothing */
24584 else if (lead > 0)
24585 {
24586 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024587 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24588 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024589 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024590 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024591 if (current_SID <= 0)
24592 {
24593 EMSG(_(e_usingsid));
24594 goto theend;
24595 }
24596 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24597 lead += (int)STRLEN(sid_buf);
24598 }
24599 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024600 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024601 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024602 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024603 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024604 goto theend;
24605 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024606 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024607 {
24608 char_u *cp = vim_strchr(lv.ll_name, ':');
24609
24610 if (cp != NULL && cp < end)
24611 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024612 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024613 goto theend;
24614 }
24615 }
24616
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024617 name = alloc((unsigned)(len + lead + 1));
24618 if (name != NULL)
24619 {
24620 if (lead > 0)
24621 {
24622 name[0] = K_SPECIAL;
24623 name[1] = KS_EXTRA;
24624 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024625 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024626 STRCPY(name + 3, sid_buf);
24627 }
24628 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024629 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024630 }
24631 *pp = end;
24632
24633theend:
24634 clear_lval(&lv);
24635 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636}
24637
24638/*
24639 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24640 * Return 2 if "p" starts with "s:".
24641 * Return 0 otherwise.
24642 */
24643 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024644eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024646 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24647 * the standard library function. */
24648 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24649 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 return 5;
24651 if (p[0] == 's' && p[1] == ':')
24652 return 2;
24653 return 0;
24654}
24655
24656/*
24657 * Return TRUE if "p" starts with "<SID>" or "s:".
24658 * Only works if eval_fname_script() returned non-zero for "p"!
24659 */
24660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024661eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024662{
24663 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24664}
24665
24666/*
24667 * List the head of the function: "name(arg1, arg2)".
24668 */
24669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024670list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024671{
24672 int j;
24673
24674 msg_start();
24675 if (indent)
24676 MSG_PUTS(" ");
24677 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024678 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679 {
24680 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024681 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682 }
24683 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024684 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024686 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024687 {
24688 if (j)
24689 MSG_PUTS(", ");
24690 msg_puts(FUNCARG(fp, j));
24691 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024692 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 {
24694 if (j)
24695 MSG_PUTS(", ");
24696 MSG_PUTS("...");
24697 }
24698 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024699 if (fp->uf_flags & FC_ABORT)
24700 MSG_PUTS(" abort");
24701 if (fp->uf_flags & FC_RANGE)
24702 MSG_PUTS(" range");
24703 if (fp->uf_flags & FC_DICT)
24704 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024705 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024706 if (p_verbose > 0)
24707 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708}
24709
24710/*
24711 * Find a function by name, return pointer to it in ufuncs.
24712 * Return NULL for unknown function.
24713 */
24714 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024715find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024716{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024717 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024718
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024719 hi = hash_find(&func_hashtab, name);
24720 if (!HASHITEM_EMPTY(hi))
24721 return HI2UF(hi);
24722 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723}
24724
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024725#if defined(EXITFREE) || defined(PROTO)
24726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024727free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024728{
24729 hashitem_T *hi;
24730
24731 /* Need to start all over every time, because func_free() may change the
24732 * hash table. */
24733 while (func_hashtab.ht_used > 0)
24734 for (hi = func_hashtab.ht_array; ; ++hi)
24735 if (!HASHITEM_EMPTY(hi))
24736 {
24737 func_free(HI2UF(hi));
24738 break;
24739 }
24740}
24741#endif
24742
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024743 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024744translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024745{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024746 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024747 return find_internal_func(name) >= 0;
24748 return find_func(name) != NULL;
24749}
24750
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024751/*
24752 * Return TRUE if a function "name" exists.
24753 */
24754 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024755function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024756{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024757 char_u *nm = name;
24758 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024759 int n = FALSE;
24760
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024761 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024762 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024763 nm = skipwhite(nm);
24764
24765 /* Only accept "funcname", "funcname ", "funcname (..." and
24766 * "funcname(...", not "funcname!...". */
24767 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024768 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024769 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024770 return n;
24771}
24772
Bram Moolenaara1544c02013-05-30 12:35:52 +020024773 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024774get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024775{
24776 char_u *nm = name;
24777 char_u *p;
24778
Bram Moolenaar65639032016-03-16 21:40:30 +010024779 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024780
24781 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024782 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024783 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024784
Bram Moolenaara1544c02013-05-30 12:35:52 +020024785 vim_free(p);
24786 return NULL;
24787}
24788
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024789/*
24790 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024791 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24792 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024793 */
24794 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024795builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024796{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024797 char_u *p;
24798
24799 if (!ASCII_ISLOWER(name[0]))
24800 return FALSE;
24801 p = vim_strchr(name, AUTOLOAD_CHAR);
24802 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024803}
24804
Bram Moolenaar05159a02005-02-26 23:04:13 +000024805#if defined(FEAT_PROFILE) || defined(PROTO)
24806/*
24807 * Start profiling function "fp".
24808 */
24809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024810func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024811{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024812 int len = fp->uf_lines.ga_len;
24813
24814 if (len == 0)
24815 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024816 fp->uf_tm_count = 0;
24817 profile_zero(&fp->uf_tm_self);
24818 profile_zero(&fp->uf_tm_total);
24819 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024820 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024821 if (fp->uf_tml_total == NULL)
24822 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024823 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024824 if (fp->uf_tml_self == NULL)
24825 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024826 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024827 fp->uf_tml_idx = -1;
24828 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24829 || fp->uf_tml_self == NULL)
24830 return; /* out of memory */
24831
24832 fp->uf_profiling = TRUE;
24833}
24834
24835/*
24836 * Dump the profiling results for all functions in file "fd".
24837 */
24838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024839func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024840{
24841 hashitem_T *hi;
24842 int todo;
24843 ufunc_T *fp;
24844 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024845 ufunc_T **sorttab;
24846 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024847
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024848 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024849 if (todo == 0)
24850 return; /* nothing to dump */
24851
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024852 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024853
Bram Moolenaar05159a02005-02-26 23:04:13 +000024854 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24855 {
24856 if (!HASHITEM_EMPTY(hi))
24857 {
24858 --todo;
24859 fp = HI2UF(hi);
24860 if (fp->uf_profiling)
24861 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024862 if (sorttab != NULL)
24863 sorttab[st_len++] = fp;
24864
Bram Moolenaar05159a02005-02-26 23:04:13 +000024865 if (fp->uf_name[0] == K_SPECIAL)
24866 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24867 else
24868 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24869 if (fp->uf_tm_count == 1)
24870 fprintf(fd, "Called 1 time\n");
24871 else
24872 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24873 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24874 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24875 fprintf(fd, "\n");
24876 fprintf(fd, "count total (s) self (s)\n");
24877
24878 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24879 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024880 if (FUNCLINE(fp, i) == NULL)
24881 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024882 prof_func_line(fd, fp->uf_tml_count[i],
24883 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024884 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24885 }
24886 fprintf(fd, "\n");
24887 }
24888 }
24889 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024890
24891 if (sorttab != NULL && st_len > 0)
24892 {
24893 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24894 prof_total_cmp);
24895 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24896 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24897 prof_self_cmp);
24898 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24899 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024900
24901 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024902}
Bram Moolenaar73830342005-02-28 22:48:19 +000024903
24904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024905prof_sort_list(
24906 FILE *fd,
24907 ufunc_T **sorttab,
24908 int st_len,
24909 char *title,
24910 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024911{
24912 int i;
24913 ufunc_T *fp;
24914
24915 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24916 fprintf(fd, "count total (s) self (s) function\n");
24917 for (i = 0; i < 20 && i < st_len; ++i)
24918 {
24919 fp = sorttab[i];
24920 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24921 prefer_self);
24922 if (fp->uf_name[0] == K_SPECIAL)
24923 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24924 else
24925 fprintf(fd, " %s()\n", fp->uf_name);
24926 }
24927 fprintf(fd, "\n");
24928}
24929
24930/*
24931 * Print the count and times for one function or function line.
24932 */
24933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024934prof_func_line(
24935 FILE *fd,
24936 int count,
24937 proftime_T *total,
24938 proftime_T *self,
24939 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024940{
24941 if (count > 0)
24942 {
24943 fprintf(fd, "%5d ", count);
24944 if (prefer_self && profile_equal(total, self))
24945 fprintf(fd, " ");
24946 else
24947 fprintf(fd, "%s ", profile_msg(total));
24948 if (!prefer_self && profile_equal(total, self))
24949 fprintf(fd, " ");
24950 else
24951 fprintf(fd, "%s ", profile_msg(self));
24952 }
24953 else
24954 fprintf(fd, " ");
24955}
24956
24957/*
24958 * Compare function for total time sorting.
24959 */
24960 static int
24961#ifdef __BORLANDC__
24962_RTLENTRYF
24963#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024964prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024965{
24966 ufunc_T *p1, *p2;
24967
24968 p1 = *(ufunc_T **)s1;
24969 p2 = *(ufunc_T **)s2;
24970 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24971}
24972
24973/*
24974 * Compare function for self time sorting.
24975 */
24976 static int
24977#ifdef __BORLANDC__
24978_RTLENTRYF
24979#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024980prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024981{
24982 ufunc_T *p1, *p2;
24983
24984 p1 = *(ufunc_T **)s1;
24985 p2 = *(ufunc_T **)s2;
24986 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24987}
24988
Bram Moolenaar05159a02005-02-26 23:04:13 +000024989#endif
24990
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024991/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024992 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024993 * Return TRUE if a package was loaded.
24994 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024995 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024996script_autoload(
24997 char_u *name,
24998 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024999{
25000 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025001 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025002 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025003 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025004
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025005 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025006 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025007 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025008 return FALSE;
25009
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025010 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025011
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025012 /* Find the name in the list of previously loaded package names. Skip
25013 * "autoload/", it's always the same. */
25014 for (i = 0; i < ga_loaded.ga_len; ++i)
25015 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25016 break;
25017 if (!reload && i < ga_loaded.ga_len)
25018 ret = FALSE; /* was loaded already */
25019 else
25020 {
25021 /* Remember the name if it wasn't loaded already. */
25022 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25023 {
25024 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25025 tofree = NULL;
25026 }
25027
25028 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025029 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025030 ret = TRUE;
25031 }
25032
25033 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025034 return ret;
25035}
25036
25037/*
25038 * Return the autoload script name for a function or variable name.
25039 * Returns NULL when out of memory.
25040 */
25041 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025042autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025043{
25044 char_u *p;
25045 char_u *scriptname;
25046
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025047 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025048 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25049 if (scriptname == NULL)
25050 return FALSE;
25051 STRCPY(scriptname, "autoload/");
25052 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025053 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025054 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025055 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025056 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025057 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025058}
25059
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25061
25062/*
25063 * Function given to ExpandGeneric() to obtain the list of user defined
25064 * function names.
25065 */
25066 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025067get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025069 static long_u done;
25070 static hashitem_T *hi;
25071 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072
25073 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025075 done = 0;
25076 hi = func_hashtab.ht_array;
25077 }
25078 if (done < func_hashtab.ht_used)
25079 {
25080 if (done++ > 0)
25081 ++hi;
25082 while (HASHITEM_EMPTY(hi))
25083 ++hi;
25084 fp = HI2UF(hi);
25085
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025086 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025087 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025088
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025089 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25090 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025091
25092 cat_func_name(IObuff, fp);
25093 if (xp->xp_context != EXPAND_USER_FUNC)
25094 {
25095 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025096 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025097 STRCAT(IObuff, ")");
25098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025099 return IObuff;
25100 }
25101 return NULL;
25102}
25103
25104#endif /* FEAT_CMDL_COMPL */
25105
25106/*
25107 * Copy the function name of "fp" to buffer "buf".
25108 * "buf" must be able to hold the function name plus three bytes.
25109 * Takes care of script-local function names.
25110 */
25111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025112cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025114 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115 {
25116 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025117 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025118 }
25119 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025120 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025121}
25122
25123/*
25124 * ":delfunction {name}"
25125 */
25126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025127ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025129 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130 char_u *p;
25131 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025132 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025133
25134 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025135 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025136 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025137 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025138 {
25139 if (fudi.fd_dict != NULL && !eap->skip)
25140 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025141 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025143 if (!ends_excmd(*skipwhite(p)))
25144 {
25145 vim_free(name);
25146 EMSG(_(e_trailing));
25147 return;
25148 }
25149 eap->nextcmd = check_nextcmd(p);
25150 if (eap->nextcmd != NULL)
25151 *p = NUL;
25152
25153 if (!eap->skip)
25154 fp = find_func(name);
25155 vim_free(name);
25156
25157 if (!eap->skip)
25158 {
25159 if (fp == NULL)
25160 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025161 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162 return;
25163 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025164 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025165 {
25166 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25167 return;
25168 }
25169
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025170 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025171 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025172 /* Delete the dict item that refers to the function, it will
25173 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025174 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025176 else
25177 func_free(fp);
25178 }
25179}
25180
25181/*
25182 * Free a function and remove it from the list of functions.
25183 */
25184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025185func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025186{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025187 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025188
25189 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025190 ga_clear_strings(&(fp->uf_args));
25191 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025192#ifdef FEAT_PROFILE
25193 vim_free(fp->uf_tml_count);
25194 vim_free(fp->uf_tml_total);
25195 vim_free(fp->uf_tml_self);
25196#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025197
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025198 /* remove the function from the function hashtable */
25199 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25200 if (HASHITEM_EMPTY(hi))
25201 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025202 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025203 hash_remove(&func_hashtab, hi);
25204
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025205 vim_free(fp);
25206}
25207
25208/*
25209 * Unreference a Function: decrement the reference count and free it when it
25210 * becomes zero. Only for numbered functions.
25211 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025213func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025214{
25215 ufunc_T *fp;
25216
25217 if (name != NULL && isdigit(*name))
25218 {
25219 fp = find_func(name);
25220 if (fp == NULL)
25221 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025222 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025223 {
25224 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025225 * when "uf_calls" becomes zero. */
25226 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025227 func_free(fp);
25228 }
25229 }
25230}
25231
25232/*
25233 * Count a reference to a Function.
25234 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025236func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025237{
25238 ufunc_T *fp;
25239
25240 if (name != NULL && isdigit(*name))
25241 {
25242 fp = find_func(name);
25243 if (fp == NULL)
25244 EMSG2(_(e_intern2), "func_ref()");
25245 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025246 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025247 }
25248}
25249
25250/*
25251 * Call a user function.
25252 */
25253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025254call_user_func(
25255 ufunc_T *fp, /* pointer to function */
25256 int argcount, /* nr of args */
25257 typval_T *argvars, /* arguments */
25258 typval_T *rettv, /* return value */
25259 linenr_T firstline, /* first line of range */
25260 linenr_T lastline, /* last line of range */
25261 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262{
Bram Moolenaar33570922005-01-25 22:26:29 +000025263 char_u *save_sourcing_name;
25264 linenr_T save_sourcing_lnum;
25265 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025266 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025267 int save_did_emsg;
25268 static int depth = 0;
25269 dictitem_T *v;
25270 int fixvar_idx = 0; /* index in fixvar[] */
25271 int i;
25272 int ai;
25273 char_u numbuf[NUMBUFLEN];
25274 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025275 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025276#ifdef FEAT_PROFILE
25277 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025278 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280
25281 /* If depth of calling is getting too high, don't execute the function */
25282 if (depth >= p_mfd)
25283 {
25284 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025285 rettv->v_type = VAR_NUMBER;
25286 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287 return;
25288 }
25289 ++depth;
25290
25291 line_breakcheck(); /* check for CTRL-C hit */
25292
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025293 fc = (funccall_T *)alloc(sizeof(funccall_T));
25294 fc->caller = current_funccal;
25295 current_funccal = fc;
25296 fc->func = fp;
25297 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025298 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025299 fc->linenr = 0;
25300 fc->returned = FALSE;
25301 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025303 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25304 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025305
Bram Moolenaar33570922005-01-25 22:26:29 +000025306 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025307 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025308 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25309 * each argument variable and saves a lot of time.
25310 */
25311 /*
25312 * Init l: variables.
25313 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025314 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025315 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025316 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025317 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25318 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025319 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025320 name = v->di_key;
25321 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025322 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025323 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025324 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025325 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025326 v->di_tv.vval.v_dict = selfdict;
25327 ++selfdict->dv_refcount;
25328 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025329
Bram Moolenaar33570922005-01-25 22:26:29 +000025330 /*
25331 * Init a: variables.
25332 * Set a:0 to "argcount".
25333 * Set a:000 to a list with room for the "..." arguments.
25334 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025335 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025336 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025337 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025338 /* Use "name" to avoid a warning from some compiler that checks the
25339 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025340 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025341 name = v->di_key;
25342 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025343 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025344 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025345 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025346 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025347 v->di_tv.vval.v_list = &fc->l_varlist;
25348 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25349 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25350 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025351
25352 /*
25353 * Set a:firstline to "firstline" and a:lastline to "lastline".
25354 * Set a:name to named arguments.
25355 * Set a:N to the "..." arguments.
25356 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025357 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025358 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025359 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025360 (varnumber_T)lastline);
25361 for (i = 0; i < argcount; ++i)
25362 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025363 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025364 if (ai < 0)
25365 /* named argument a:name */
25366 name = FUNCARG(fp, i);
25367 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025368 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025369 /* "..." argument a:1, a:2, etc. */
25370 sprintf((char *)numbuf, "%d", ai + 1);
25371 name = numbuf;
25372 }
25373 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25374 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025375 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025376 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25377 }
25378 else
25379 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025380 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25381 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025382 if (v == NULL)
25383 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025384 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025385 }
25386 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025387 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025388
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025389 /* Note: the values are copied directly to avoid alloc/free.
25390 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025391 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025392 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025393
25394 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25395 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025396 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25397 fc->l_listitems[ai].li_tv = argvars[i];
25398 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025399 }
25400 }
25401
Bram Moolenaar071d4272004-06-13 20:20:40 +000025402 /* Don't redraw while executing the function. */
25403 ++RedrawingDisabled;
25404 save_sourcing_name = sourcing_name;
25405 save_sourcing_lnum = sourcing_lnum;
25406 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025407 /* need space for function name + ("function " + 3) or "[number]" */
25408 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25409 + STRLEN(fp->uf_name) + 20;
25410 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025411 if (sourcing_name != NULL)
25412 {
25413 if (save_sourcing_name != NULL
25414 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025415 sprintf((char *)sourcing_name, "%s[%d]..",
25416 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417 else
25418 STRCPY(sourcing_name, "function ");
25419 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25420
25421 if (p_verbose >= 12)
25422 {
25423 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025424 verbose_enter_scroll();
25425
Bram Moolenaar555b2802005-05-19 21:08:39 +000025426 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025427 if (p_verbose >= 14)
25428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025430 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025431 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025432 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433
25434 msg_puts((char_u *)"(");
25435 for (i = 0; i < argcount; ++i)
25436 {
25437 if (i > 0)
25438 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025439 if (argvars[i].v_type == VAR_NUMBER)
25440 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441 else
25442 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025443 /* Do not want errors such as E724 here. */
25444 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025445 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025446 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025447 if (s != NULL)
25448 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025449 if (vim_strsize(s) > MSG_BUF_CLEN)
25450 {
25451 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25452 s = buf;
25453 }
25454 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025455 vim_free(tofree);
25456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457 }
25458 }
25459 msg_puts((char_u *)")");
25460 }
25461 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025462
25463 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464 --no_wait_return;
25465 }
25466 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025467#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025468 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025469 {
25470 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25471 func_do_profile(fp);
25472 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025473 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025474 {
25475 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025476 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025477 profile_zero(&fp->uf_tm_children);
25478 }
25479 script_prof_save(&wait_start);
25480 }
25481#endif
25482
Bram Moolenaar071d4272004-06-13 20:20:40 +000025483 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025484 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485 save_did_emsg = did_emsg;
25486 did_emsg = FALSE;
25487
25488 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025489 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25491
25492 --RedrawingDisabled;
25493
25494 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025495 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025497 clear_tv(rettv);
25498 rettv->v_type = VAR_NUMBER;
25499 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500 }
25501
Bram Moolenaar05159a02005-02-26 23:04:13 +000025502#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025503 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025504 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025505 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025506 profile_end(&call_start);
25507 profile_sub_wait(&wait_start, &call_start);
25508 profile_add(&fp->uf_tm_total, &call_start);
25509 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025510 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025511 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025512 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25513 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025514 }
25515 }
25516#endif
25517
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 /* when being verbose, mention the return value */
25519 if (p_verbose >= 12)
25520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025522 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523
Bram Moolenaar071d4272004-06-13 20:20:40 +000025524 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025525 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025526 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025527 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025528 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025531 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025532 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025533 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025534 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025535
Bram Moolenaar555b2802005-05-19 21:08:39 +000025536 /* The value may be very long. Skip the middle part, so that we
25537 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025538 * truncate it at the end. Don't want errors such as E724 here. */
25539 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025540 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025541 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025542 if (s != NULL)
25543 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025544 if (vim_strsize(s) > MSG_BUF_CLEN)
25545 {
25546 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25547 s = buf;
25548 }
25549 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025550 vim_free(tofree);
25551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025552 }
25553 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025554
25555 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556 --no_wait_return;
25557 }
25558
25559 vim_free(sourcing_name);
25560 sourcing_name = save_sourcing_name;
25561 sourcing_lnum = save_sourcing_lnum;
25562 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025563#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025564 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025565 script_prof_restore(&wait_start);
25566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567
25568 if (p_verbose >= 12 && sourcing_name != NULL)
25569 {
25570 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025571 verbose_enter_scroll();
25572
Bram Moolenaar555b2802005-05-19 21:08:39 +000025573 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025575
25576 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577 --no_wait_return;
25578 }
25579
25580 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025581 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025582 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025583
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025584 /* If the a:000 list and the l: and a: dicts are not referenced we can
25585 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025586 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25587 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25588 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25589 {
25590 free_funccal(fc, FALSE);
25591 }
25592 else
25593 {
25594 hashitem_T *hi;
25595 listitem_T *li;
25596 int todo;
25597
25598 /* "fc" is still in use. This can happen when returning "a:000" or
25599 * assigning "l:" to a global variable.
25600 * Link "fc" in the list for garbage collection later. */
25601 fc->caller = previous_funccal;
25602 previous_funccal = fc;
25603
25604 /* Make a copy of the a: variables, since we didn't do that above. */
25605 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25606 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25607 {
25608 if (!HASHITEM_EMPTY(hi))
25609 {
25610 --todo;
25611 v = HI2DI(hi);
25612 copy_tv(&v->di_tv, &v->di_tv);
25613 }
25614 }
25615
25616 /* Make a copy of the a:000 items, since we didn't do that above. */
25617 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25618 copy_tv(&li->li_tv, &li->li_tv);
25619 }
25620}
25621
25622/*
25623 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025624 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025625 */
25626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025627can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025628{
25629 return (fc->l_varlist.lv_copyID != copyID
25630 && fc->l_vars.dv_copyID != copyID
25631 && fc->l_avars.dv_copyID != copyID);
25632}
25633
25634/*
25635 * Free "fc" and what it contains.
25636 */
25637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025638free_funccal(
25639 funccall_T *fc,
25640 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025641{
25642 listitem_T *li;
25643
25644 /* The a: variables typevals may not have been allocated, only free the
25645 * allocated variables. */
25646 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25647
25648 /* free all l: variables */
25649 vars_clear(&fc->l_vars.dv_hashtab);
25650
25651 /* Free the a:000 variables if they were allocated. */
25652 if (free_val)
25653 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25654 clear_tv(&li->li_tv);
25655
25656 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657}
25658
25659/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025660 * Add a number variable "name" to dict "dp" with value "nr".
25661 */
25662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025663add_nr_var(
25664 dict_T *dp,
25665 dictitem_T *v,
25666 char *name,
25667 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025668{
25669 STRCPY(v->di_key, name);
25670 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25671 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25672 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025673 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025674 v->di_tv.vval.v_number = nr;
25675}
25676
25677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025678 * ":return [expr]"
25679 */
25680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025681ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682{
25683 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025684 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025685 int returning = FALSE;
25686
25687 if (current_funccal == NULL)
25688 {
25689 EMSG(_("E133: :return not inside a function"));
25690 return;
25691 }
25692
25693 if (eap->skip)
25694 ++emsg_skip;
25695
25696 eap->nextcmd = NULL;
25697 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025698 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025699 {
25700 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025701 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025702 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025703 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704 }
25705 /* It's safer to return also on error. */
25706 else if (!eap->skip)
25707 {
25708 /*
25709 * Return unless the expression evaluation has been cancelled due to an
25710 * aborting error, an interrupt, or an exception.
25711 */
25712 if (!aborting())
25713 returning = do_return(eap, FALSE, TRUE, NULL);
25714 }
25715
25716 /* When skipping or the return gets pending, advance to the next command
25717 * in this line (!returning). Otherwise, ignore the rest of the line.
25718 * Following lines will be ignored by get_func_line(). */
25719 if (returning)
25720 eap->nextcmd = NULL;
25721 else if (eap->nextcmd == NULL) /* no argument */
25722 eap->nextcmd = check_nextcmd(arg);
25723
25724 if (eap->skip)
25725 --emsg_skip;
25726}
25727
25728/*
25729 * Return from a function. Possibly makes the return pending. Also called
25730 * for a pending return at the ":endtry" or after returning from an extra
25731 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025732 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025733 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025734 * FALSE when the return gets pending.
25735 */
25736 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025737do_return(
25738 exarg_T *eap,
25739 int reanimate,
25740 int is_cmd,
25741 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025742{
25743 int idx;
25744 struct condstack *cstack = eap->cstack;
25745
25746 if (reanimate)
25747 /* Undo the return. */
25748 current_funccal->returned = FALSE;
25749
25750 /*
25751 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25752 * not in its finally clause (which then is to be executed next) is found.
25753 * In this case, make the ":return" pending for execution at the ":endtry".
25754 * Otherwise, return normally.
25755 */
25756 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25757 if (idx >= 0)
25758 {
25759 cstack->cs_pending[idx] = CSTP_RETURN;
25760
25761 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025762 /* A pending return again gets pending. "rettv" points to an
25763 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025764 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025765 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766 else
25767 {
25768 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025769 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025770 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025771 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025773 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 {
25775 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025776 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025777 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778 else
25779 EMSG(_(e_outofmem));
25780 }
25781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025782 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783
25784 if (reanimate)
25785 {
25786 /* The pending return value could be overwritten by a ":return"
25787 * without argument in a finally clause; reset the default
25788 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025789 current_funccal->rettv->v_type = VAR_NUMBER;
25790 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025791 }
25792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025793 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025794 }
25795 else
25796 {
25797 current_funccal->returned = TRUE;
25798
25799 /* If the return is carried out now, store the return value. For
25800 * a return immediately after reanimation, the value is already
25801 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025802 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025804 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025805 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025807 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808 }
25809 }
25810
25811 return idx < 0;
25812}
25813
25814/*
25815 * Free the variable with a pending return value.
25816 */
25817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025818discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025819{
Bram Moolenaar33570922005-01-25 22:26:29 +000025820 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025821}
25822
25823/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025824 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825 * is an allocated string. Used by report_pending() for verbose messages.
25826 */
25827 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025828get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025830 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025831 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025832 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025833
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025834 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025835 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025836 if (s == NULL)
25837 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025838
25839 STRCPY(IObuff, ":return ");
25840 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25841 if (STRLEN(s) + 8 >= IOSIZE)
25842 STRCPY(IObuff + IOSIZE - 4, "...");
25843 vim_free(tofree);
25844 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025845}
25846
25847/*
25848 * Get next function line.
25849 * Called by do_cmdline() to get the next line.
25850 * Returns allocated string, or NULL for end of function.
25851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025853get_func_line(
25854 int c UNUSED,
25855 void *cookie,
25856 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025857{
Bram Moolenaar33570922005-01-25 22:26:29 +000025858 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025859 ufunc_T *fp = fcp->func;
25860 char_u *retval;
25861 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025862
25863 /* If breakpoints have been added/deleted need to check for it. */
25864 if (fcp->dbg_tick != debug_tick)
25865 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025866 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025867 sourcing_lnum);
25868 fcp->dbg_tick = debug_tick;
25869 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025870#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025871 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025872 func_line_end(cookie);
25873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025874
Bram Moolenaar05159a02005-02-26 23:04:13 +000025875 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025876 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25877 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878 retval = NULL;
25879 else
25880 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025881 /* Skip NULL lines (continuation lines). */
25882 while (fcp->linenr < gap->ga_len
25883 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25884 ++fcp->linenr;
25885 if (fcp->linenr >= gap->ga_len)
25886 retval = NULL;
25887 else
25888 {
25889 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25890 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025891#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025892 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025893 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025894#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025896 }
25897
25898 /* Did we encounter a breakpoint? */
25899 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25900 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025901 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025903 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025904 sourcing_lnum);
25905 fcp->dbg_tick = debug_tick;
25906 }
25907
25908 return retval;
25909}
25910
Bram Moolenaar05159a02005-02-26 23:04:13 +000025911#if defined(FEAT_PROFILE) || defined(PROTO)
25912/*
25913 * Called when starting to read a function line.
25914 * "sourcing_lnum" must be correct!
25915 * When skipping lines it may not actually be executed, but we won't find out
25916 * until later and we need to store the time now.
25917 */
25918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025919func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025920{
25921 funccall_T *fcp = (funccall_T *)cookie;
25922 ufunc_T *fp = fcp->func;
25923
25924 if (fp->uf_profiling && sourcing_lnum >= 1
25925 && sourcing_lnum <= fp->uf_lines.ga_len)
25926 {
25927 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025928 /* Skip continuation lines. */
25929 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25930 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025931 fp->uf_tml_execed = FALSE;
25932 profile_start(&fp->uf_tml_start);
25933 profile_zero(&fp->uf_tml_children);
25934 profile_get_wait(&fp->uf_tml_wait);
25935 }
25936}
25937
25938/*
25939 * Called when actually executing a function line.
25940 */
25941 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025942func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025943{
25944 funccall_T *fcp = (funccall_T *)cookie;
25945 ufunc_T *fp = fcp->func;
25946
25947 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25948 fp->uf_tml_execed = TRUE;
25949}
25950
25951/*
25952 * Called when done with a function line.
25953 */
25954 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025955func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025956{
25957 funccall_T *fcp = (funccall_T *)cookie;
25958 ufunc_T *fp = fcp->func;
25959
25960 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25961 {
25962 if (fp->uf_tml_execed)
25963 {
25964 ++fp->uf_tml_count[fp->uf_tml_idx];
25965 profile_end(&fp->uf_tml_start);
25966 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025967 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025968 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25969 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025970 }
25971 fp->uf_tml_idx = -1;
25972 }
25973}
25974#endif
25975
Bram Moolenaar071d4272004-06-13 20:20:40 +000025976/*
25977 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025978 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025979 */
25980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025981func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025982{
Bram Moolenaar33570922005-01-25 22:26:29 +000025983 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025984
25985 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25986 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025987 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025988 || fcp->returned);
25989}
25990
25991/*
25992 * return TRUE if cookie indicates a function which "abort"s on errors.
25993 */
25994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025995func_has_abort(
25996 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025998 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999}
26000
26001#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26002typedef enum
26003{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026004 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26005 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26006 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007} var_flavour_T;
26008
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026009static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026010
26011 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026012var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013{
26014 char_u *p = varname;
26015
26016 if (ASCII_ISUPPER(*p))
26017 {
26018 while (*(++p))
26019 if (ASCII_ISLOWER(*p))
26020 return VAR_FLAVOUR_SESSION;
26021 return VAR_FLAVOUR_VIMINFO;
26022 }
26023 else
26024 return VAR_FLAVOUR_DEFAULT;
26025}
26026#endif
26027
26028#if defined(FEAT_VIMINFO) || defined(PROTO)
26029/*
26030 * Restore global vars that start with a capital from the viminfo file
26031 */
26032 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026033read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026034{
26035 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026036 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026037 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026038 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026039
26040 if (!writing && (find_viminfo_parameter('!') != NULL))
26041 {
26042 tab = vim_strchr(virp->vir_line + 1, '\t');
26043 if (tab != NULL)
26044 {
26045 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026046 switch (*tab)
26047 {
26048 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026049#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026050 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026051#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026052 case 'D': type = VAR_DICT; break;
26053 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026054 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026056
26057 tab = vim_strchr(tab, '\t');
26058 if (tab != NULL)
26059 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026060 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026061 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026062 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026063 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026064#ifdef FEAT_FLOAT
26065 else if (type == VAR_FLOAT)
26066 (void)string2float(tab + 1, &tv.vval.v_float);
26067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026069 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026070 if (type == VAR_DICT || type == VAR_LIST)
26071 {
26072 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26073
26074 if (etv == NULL)
26075 /* Failed to parse back the dict or list, use it as a
26076 * string. */
26077 tv.v_type = VAR_STRING;
26078 else
26079 {
26080 vim_free(tv.vval.v_string);
26081 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026082 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026083 }
26084 }
26085
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026086 /* when in a function use global variables */
26087 save_funccal = current_funccal;
26088 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026089 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026090 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026091
26092 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026093 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026094 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26095 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026096 }
26097 }
26098 }
26099
26100 return viminfo_readline(virp);
26101}
26102
26103/*
26104 * Write global vars that start with a capital to the viminfo file
26105 */
26106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026107write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026108{
Bram Moolenaar33570922005-01-25 22:26:29 +000026109 hashitem_T *hi;
26110 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026111 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026112 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026113 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026114 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026115 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026116
26117 if (find_viminfo_parameter('!') == NULL)
26118 return;
26119
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026120 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026121
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026122 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026123 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026124 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026125 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026126 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026127 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026128 this_var = HI2DI(hi);
26129 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026130 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026131 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026132 {
26133 case VAR_STRING: s = "STR"; break;
26134 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026135 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026136 case VAR_DICT: s = "DIC"; break;
26137 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026138 case VAR_SPECIAL: s = "XPL"; break;
26139
26140 case VAR_UNKNOWN:
26141 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026142 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026143 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026144 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026145 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026146 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026147 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026148 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026149 if (p != NULL)
26150 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026151 vim_free(tofree);
26152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026153 }
26154 }
26155}
26156#endif
26157
26158#if defined(FEAT_SESSION) || defined(PROTO)
26159 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026160store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026161{
Bram Moolenaar33570922005-01-25 22:26:29 +000026162 hashitem_T *hi;
26163 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026164 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026165 char_u *p, *t;
26166
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026167 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026168 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026170 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026172 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026173 this_var = HI2DI(hi);
26174 if ((this_var->di_tv.v_type == VAR_NUMBER
26175 || this_var->di_tv.v_type == VAR_STRING)
26176 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026177 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026178 /* Escape special characters with a backslash. Turn a LF and
26179 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026180 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026181 (char_u *)"\\\"\n\r");
26182 if (p == NULL) /* out of memory */
26183 break;
26184 for (t = p; *t != NUL; ++t)
26185 if (*t == '\n')
26186 *t = 'n';
26187 else if (*t == '\r')
26188 *t = 'r';
26189 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026190 this_var->di_key,
26191 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26192 : ' ',
26193 p,
26194 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26195 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026196 || put_eol(fd) == FAIL)
26197 {
26198 vim_free(p);
26199 return FAIL;
26200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026201 vim_free(p);
26202 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026203#ifdef FEAT_FLOAT
26204 else if (this_var->di_tv.v_type == VAR_FLOAT
26205 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26206 {
26207 float_T f = this_var->di_tv.vval.v_float;
26208 int sign = ' ';
26209
26210 if (f < 0)
26211 {
26212 f = -f;
26213 sign = '-';
26214 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026215 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026216 this_var->di_key, sign, f) < 0)
26217 || put_eol(fd) == FAIL)
26218 return FAIL;
26219 }
26220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026221 }
26222 }
26223 return OK;
26224}
26225#endif
26226
Bram Moolenaar661b1822005-07-28 22:36:45 +000026227/*
26228 * Display script name where an item was last set.
26229 * Should only be invoked when 'verbose' is non-zero.
26230 */
26231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026232last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026233{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026234 char_u *p;
26235
Bram Moolenaar661b1822005-07-28 22:36:45 +000026236 if (scriptID != 0)
26237 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026238 p = home_replace_save(NULL, get_scriptname(scriptID));
26239 if (p != NULL)
26240 {
26241 verbose_enter();
26242 MSG_PUTS(_("\n\tLast set from "));
26243 MSG_PUTS(p);
26244 vim_free(p);
26245 verbose_leave();
26246 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026247 }
26248}
26249
Bram Moolenaard812df62008-11-09 12:46:09 +000026250/*
26251 * List v:oldfiles in a nice way.
26252 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026253 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026254ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026255{
26256 list_T *l = vimvars[VV_OLDFILES].vv_list;
26257 listitem_T *li;
26258 int nr = 0;
26259
26260 if (l == NULL)
26261 msg((char_u *)_("No old files"));
26262 else
26263 {
26264 msg_start();
26265 msg_scroll = TRUE;
26266 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26267 {
26268 msg_outnum((long)++nr);
26269 MSG_PUTS(": ");
26270 msg_outtrans(get_tv_string(&li->li_tv));
26271 msg_putchar('\n');
26272 out_flush(); /* output one line at a time */
26273 ui_breakcheck();
26274 }
26275 /* Assume "got_int" was set to truncate the listing. */
26276 got_int = FALSE;
26277
26278#ifdef FEAT_BROWSE_CMD
26279 if (cmdmod.browse)
26280 {
26281 quit_more = FALSE;
26282 nr = prompt_for_number(FALSE);
26283 msg_starthere();
26284 if (nr > 0)
26285 {
26286 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26287 (long)nr);
26288
26289 if (p != NULL)
26290 {
26291 p = expand_env_save(p);
26292 eap->arg = p;
26293 eap->cmdidx = CMD_edit;
26294 cmdmod.browse = FALSE;
26295 do_exedit(eap, NULL);
26296 vim_free(p);
26297 }
26298 }
26299 }
26300#endif
26301 }
26302}
26303
Bram Moolenaar53744302015-07-17 17:38:22 +020026304/* reset v:option_new, v:option_old and v:option_type */
26305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026306reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026307{
26308 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26309 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26310 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26311}
26312
26313
Bram Moolenaar071d4272004-06-13 20:20:40 +000026314#endif /* FEAT_EVAL */
26315
Bram Moolenaar071d4272004-06-13 20:20:40 +000026316
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026317#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026318
26319#ifdef WIN3264
26320/*
26321 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26322 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026323static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26324static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26325static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026326
26327/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026328 * Get the short path (8.3) for the filename in "fnamep".
26329 * Only works for a valid file name.
26330 * When the path gets longer "fnamep" is changed and the allocated buffer
26331 * is put in "bufp".
26332 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26333 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026334 */
26335 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026336get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026337{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026338 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026339 char_u *newbuf;
26340
26341 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026342 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026343 if (l > len - 1)
26344 {
26345 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026346 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026347 newbuf = vim_strnsave(*fnamep, l);
26348 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026349 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026350
26351 vim_free(*bufp);
26352 *fnamep = *bufp = newbuf;
26353
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026354 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026355 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026356 }
26357
26358 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026359 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360}
26361
26362/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026363 * Get the short path (8.3) for the filename in "fname". The converted
26364 * path is returned in "bufp".
26365 *
26366 * Some of the directories specified in "fname" may not exist. This function
26367 * will shorten the existing directories at the beginning of the path and then
26368 * append the remaining non-existing path.
26369 *
26370 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026371 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026372 * bufp - Pointer to an allocated buffer for the filename.
26373 * fnamelen - Length of the filename pointed to by fname
26374 *
26375 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026376 */
26377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026378shortpath_for_invalid_fname(
26379 char_u **fname,
26380 char_u **bufp,
26381 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026382{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026383 char_u *short_fname, *save_fname, *pbuf_unused;
26384 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026385 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026386 int old_len, len;
26387 int new_len, sfx_len;
26388 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026389
26390 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026391 old_len = *fnamelen;
26392 save_fname = vim_strnsave(*fname, old_len);
26393 pbuf_unused = NULL;
26394 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026395
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026396 endp = save_fname + old_len - 1; /* Find the end of the copy */
26397 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026398
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026399 /*
26400 * Try shortening the supplied path till it succeeds by removing one
26401 * directory at a time from the tail of the path.
26402 */
26403 len = 0;
26404 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026405 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026406 /* go back one path-separator */
26407 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26408 --endp;
26409 if (endp <= save_fname)
26410 break; /* processed the complete path */
26411
26412 /*
26413 * Replace the path separator with a NUL and try to shorten the
26414 * resulting path.
26415 */
26416 ch = *endp;
26417 *endp = 0;
26418 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026419 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026420 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26421 {
26422 retval = FAIL;
26423 goto theend;
26424 }
26425 *endp = ch; /* preserve the string */
26426
26427 if (len > 0)
26428 break; /* successfully shortened the path */
26429
26430 /* failed to shorten the path. Skip the path separator */
26431 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026432 }
26433
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026434 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026435 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026436 /*
26437 * Succeeded in shortening the path. Now concatenate the shortened
26438 * path with the remaining path at the tail.
26439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026440
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026441 /* Compute the length of the new path. */
26442 sfx_len = (int)(save_endp - endp) + 1;
26443 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026444
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026445 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026446 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026447 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026448 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026449 /* There is not enough space in the currently allocated string,
26450 * copy it to a buffer big enough. */
26451 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026453 {
26454 retval = FAIL;
26455 goto theend;
26456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026457 }
26458 else
26459 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026460 /* Transfer short_fname to the main buffer (it's big enough),
26461 * unless get_short_pathname() did its work in-place. */
26462 *fname = *bufp = save_fname;
26463 if (short_fname != save_fname)
26464 vim_strncpy(save_fname, short_fname, len);
26465 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026466 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026467
26468 /* concat the not-shortened part of the path */
26469 vim_strncpy(*fname + len, endp, sfx_len);
26470 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026471 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026472
26473theend:
26474 vim_free(pbuf_unused);
26475 vim_free(save_fname);
26476
26477 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026478}
26479
26480/*
26481 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026482 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026483 */
26484 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026485shortpath_for_partial(
26486 char_u **fnamep,
26487 char_u **bufp,
26488 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026489{
26490 int sepcount, len, tflen;
26491 char_u *p;
26492 char_u *pbuf, *tfname;
26493 int hasTilde;
26494
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026495 /* Count up the path separators from the RHS.. so we know which part
26496 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026497 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026498 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026499 if (vim_ispathsep(*p))
26500 ++sepcount;
26501
26502 /* Need full path first (use expand_env() to remove a "~/") */
26503 hasTilde = (**fnamep == '~');
26504 if (hasTilde)
26505 pbuf = tfname = expand_env_save(*fnamep);
26506 else
26507 pbuf = tfname = FullName_save(*fnamep, FALSE);
26508
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026509 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026510
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026511 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26512 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026513
26514 if (len == 0)
26515 {
26516 /* Don't have a valid filename, so shorten the rest of the
26517 * path if we can. This CAN give us invalid 8.3 filenames, but
26518 * there's not a lot of point in guessing what it might be.
26519 */
26520 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026521 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26522 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026523 }
26524
26525 /* Count the paths backward to find the beginning of the desired string. */
26526 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026527 {
26528#ifdef FEAT_MBYTE
26529 if (has_mbyte)
26530 p -= mb_head_off(tfname, p);
26531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532 if (vim_ispathsep(*p))
26533 {
26534 if (sepcount == 0 || (hasTilde && sepcount == 1))
26535 break;
26536 else
26537 sepcount --;
26538 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026540 if (hasTilde)
26541 {
26542 --p;
26543 if (p >= tfname)
26544 *p = '~';
26545 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026546 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026547 }
26548 else
26549 ++p;
26550
26551 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26552 vim_free(*bufp);
26553 *fnamelen = (int)STRLEN(p);
26554 *bufp = pbuf;
26555 *fnamep = p;
26556
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026557 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026558}
26559#endif /* WIN3264 */
26560
26561/*
26562 * Adjust a filename, according to a string of modifiers.
26563 * *fnamep must be NUL terminated when called. When returning, the length is
26564 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026565 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026566 * When there is an error, *fnamep is set to NULL.
26567 */
26568 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026569modify_fname(
26570 char_u *src, /* string with modifiers */
26571 int *usedlen, /* characters after src that are used */
26572 char_u **fnamep, /* file name so far */
26573 char_u **bufp, /* buffer for allocated file name or NULL */
26574 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026575{
26576 int valid = 0;
26577 char_u *tail;
26578 char_u *s, *p, *pbuf;
26579 char_u dirname[MAXPATHL];
26580 int c;
26581 int has_fullname = 0;
26582#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026583 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026584 int has_shortname = 0;
26585#endif
26586
26587repeat:
26588 /* ":p" - full path/file_name */
26589 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26590 {
26591 has_fullname = 1;
26592
26593 valid |= VALID_PATH;
26594 *usedlen += 2;
26595
26596 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26597 if ((*fnamep)[0] == '~'
26598#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26599 && ((*fnamep)[1] == '/'
26600# ifdef BACKSLASH_IN_FILENAME
26601 || (*fnamep)[1] == '\\'
26602# endif
26603 || (*fnamep)[1] == NUL)
26604
26605#endif
26606 )
26607 {
26608 *fnamep = expand_env_save(*fnamep);
26609 vim_free(*bufp); /* free any allocated file name */
26610 *bufp = *fnamep;
26611 if (*fnamep == NULL)
26612 return -1;
26613 }
26614
26615 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026616 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026617 {
26618 if (vim_ispathsep(*p)
26619 && p[1] == '.'
26620 && (p[2] == NUL
26621 || vim_ispathsep(p[2])
26622 || (p[2] == '.'
26623 && (p[3] == NUL || vim_ispathsep(p[3])))))
26624 break;
26625 }
26626
26627 /* FullName_save() is slow, don't use it when not needed. */
26628 if (*p != NUL || !vim_isAbsName(*fnamep))
26629 {
26630 *fnamep = FullName_save(*fnamep, *p != NUL);
26631 vim_free(*bufp); /* free any allocated file name */
26632 *bufp = *fnamep;
26633 if (*fnamep == NULL)
26634 return -1;
26635 }
26636
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026637#ifdef WIN3264
26638# if _WIN32_WINNT >= 0x0500
26639 if (vim_strchr(*fnamep, '~') != NULL)
26640 {
26641 /* Expand 8.3 filename to full path. Needed to make sure the same
26642 * file does not have two different names.
26643 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26644 p = alloc(_MAX_PATH + 1);
26645 if (p != NULL)
26646 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026647 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026648 {
26649 vim_free(*bufp);
26650 *bufp = *fnamep = p;
26651 }
26652 else
26653 vim_free(p);
26654 }
26655 }
26656# endif
26657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026658 /* Append a path separator to a directory. */
26659 if (mch_isdir(*fnamep))
26660 {
26661 /* Make room for one or two extra characters. */
26662 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26663 vim_free(*bufp); /* free any allocated file name */
26664 *bufp = *fnamep;
26665 if (*fnamep == NULL)
26666 return -1;
26667 add_pathsep(*fnamep);
26668 }
26669 }
26670
26671 /* ":." - path relative to the current directory */
26672 /* ":~" - path relative to the home directory */
26673 /* ":8" - shortname path - postponed till after */
26674 while (src[*usedlen] == ':'
26675 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26676 {
26677 *usedlen += 2;
26678 if (c == '8')
26679 {
26680#ifdef WIN3264
26681 has_shortname = 1; /* Postpone this. */
26682#endif
26683 continue;
26684 }
26685 pbuf = NULL;
26686 /* Need full path first (use expand_env() to remove a "~/") */
26687 if (!has_fullname)
26688 {
26689 if (c == '.' && **fnamep == '~')
26690 p = pbuf = expand_env_save(*fnamep);
26691 else
26692 p = pbuf = FullName_save(*fnamep, FALSE);
26693 }
26694 else
26695 p = *fnamep;
26696
26697 has_fullname = 0;
26698
26699 if (p != NULL)
26700 {
26701 if (c == '.')
26702 {
26703 mch_dirname(dirname, MAXPATHL);
26704 s = shorten_fname(p, dirname);
26705 if (s != NULL)
26706 {
26707 *fnamep = s;
26708 if (pbuf != NULL)
26709 {
26710 vim_free(*bufp); /* free any allocated file name */
26711 *bufp = pbuf;
26712 pbuf = NULL;
26713 }
26714 }
26715 }
26716 else
26717 {
26718 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26719 /* Only replace it when it starts with '~' */
26720 if (*dirname == '~')
26721 {
26722 s = vim_strsave(dirname);
26723 if (s != NULL)
26724 {
26725 *fnamep = s;
26726 vim_free(*bufp);
26727 *bufp = s;
26728 }
26729 }
26730 }
26731 vim_free(pbuf);
26732 }
26733 }
26734
26735 tail = gettail(*fnamep);
26736 *fnamelen = (int)STRLEN(*fnamep);
26737
26738 /* ":h" - head, remove "/file_name", can be repeated */
26739 /* Don't remove the first "/" or "c:\" */
26740 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26741 {
26742 valid |= VALID_HEAD;
26743 *usedlen += 2;
26744 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026745 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026746 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026747 *fnamelen = (int)(tail - *fnamep);
26748#ifdef VMS
26749 if (*fnamelen > 0)
26750 *fnamelen += 1; /* the path separator is part of the path */
26751#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026752 if (*fnamelen == 0)
26753 {
26754 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26755 p = vim_strsave((char_u *)".");
26756 if (p == NULL)
26757 return -1;
26758 vim_free(*bufp);
26759 *bufp = *fnamep = tail = p;
26760 *fnamelen = 1;
26761 }
26762 else
26763 {
26764 while (tail > s && !after_pathsep(s, tail))
26765 mb_ptr_back(*fnamep, tail);
26766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026767 }
26768
26769 /* ":8" - shortname */
26770 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26771 {
26772 *usedlen += 2;
26773#ifdef WIN3264
26774 has_shortname = 1;
26775#endif
26776 }
26777
26778#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026779 /*
26780 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026781 */
26782 if (has_shortname)
26783 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026784 /* Copy the string if it is shortened by :h and when it wasn't copied
26785 * yet, because we are going to change it in place. Avoids changing
26786 * the buffer name for "%:8". */
26787 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026788 {
26789 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026790 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026791 return -1;
26792 vim_free(*bufp);
26793 *bufp = *fnamep = p;
26794 }
26795
26796 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026797 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026798 if (!has_fullname && !vim_isAbsName(*fnamep))
26799 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026800 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026801 return -1;
26802 }
26803 else
26804 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026805 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026806
Bram Moolenaardc935552011-08-17 15:23:23 +020026807 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026808 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026809 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026810 return -1;
26811
26812 if (l == 0)
26813 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026814 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026815 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026816 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026817 return -1;
26818 }
26819 *fnamelen = l;
26820 }
26821 }
26822#endif /* WIN3264 */
26823
26824 /* ":t" - tail, just the basename */
26825 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26826 {
26827 *usedlen += 2;
26828 *fnamelen -= (int)(tail - *fnamep);
26829 *fnamep = tail;
26830 }
26831
26832 /* ":e" - extension, can be repeated */
26833 /* ":r" - root, without extension, can be repeated */
26834 while (src[*usedlen] == ':'
26835 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26836 {
26837 /* find a '.' in the tail:
26838 * - for second :e: before the current fname
26839 * - otherwise: The last '.'
26840 */
26841 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26842 s = *fnamep - 2;
26843 else
26844 s = *fnamep + *fnamelen - 1;
26845 for ( ; s > tail; --s)
26846 if (s[0] == '.')
26847 break;
26848 if (src[*usedlen + 1] == 'e') /* :e */
26849 {
26850 if (s > tail)
26851 {
26852 *fnamelen += (int)(*fnamep - (s + 1));
26853 *fnamep = s + 1;
26854#ifdef VMS
26855 /* cut version from the extension */
26856 s = *fnamep + *fnamelen - 1;
26857 for ( ; s > *fnamep; --s)
26858 if (s[0] == ';')
26859 break;
26860 if (s > *fnamep)
26861 *fnamelen = s - *fnamep;
26862#endif
26863 }
26864 else if (*fnamep <= tail)
26865 *fnamelen = 0;
26866 }
26867 else /* :r */
26868 {
26869 if (s > tail) /* remove one extension */
26870 *fnamelen = (int)(s - *fnamep);
26871 }
26872 *usedlen += 2;
26873 }
26874
26875 /* ":s?pat?foo?" - substitute */
26876 /* ":gs?pat?foo?" - global substitute */
26877 if (src[*usedlen] == ':'
26878 && (src[*usedlen + 1] == 's'
26879 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26880 {
26881 char_u *str;
26882 char_u *pat;
26883 char_u *sub;
26884 int sep;
26885 char_u *flags;
26886 int didit = FALSE;
26887
26888 flags = (char_u *)"";
26889 s = src + *usedlen + 2;
26890 if (src[*usedlen + 1] == 'g')
26891 {
26892 flags = (char_u *)"g";
26893 ++s;
26894 }
26895
26896 sep = *s++;
26897 if (sep)
26898 {
26899 /* find end of pattern */
26900 p = vim_strchr(s, sep);
26901 if (p != NULL)
26902 {
26903 pat = vim_strnsave(s, (int)(p - s));
26904 if (pat != NULL)
26905 {
26906 s = p + 1;
26907 /* find end of substitution */
26908 p = vim_strchr(s, sep);
26909 if (p != NULL)
26910 {
26911 sub = vim_strnsave(s, (int)(p - s));
26912 str = vim_strnsave(*fnamep, *fnamelen);
26913 if (sub != NULL && str != NULL)
26914 {
26915 *usedlen = (int)(p + 1 - src);
26916 s = do_string_sub(str, pat, sub, flags);
26917 if (s != NULL)
26918 {
26919 *fnamep = s;
26920 *fnamelen = (int)STRLEN(s);
26921 vim_free(*bufp);
26922 *bufp = s;
26923 didit = TRUE;
26924 }
26925 }
26926 vim_free(sub);
26927 vim_free(str);
26928 }
26929 vim_free(pat);
26930 }
26931 }
26932 /* after using ":s", repeat all the modifiers */
26933 if (didit)
26934 goto repeat;
26935 }
26936 }
26937
Bram Moolenaar26df0922014-02-23 23:39:13 +010026938 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26939 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026940 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026941 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026942 if (c != NUL)
26943 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026944 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026945 if (c != NUL)
26946 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026947 if (p == NULL)
26948 return -1;
26949 vim_free(*bufp);
26950 *bufp = *fnamep = p;
26951 *fnamelen = (int)STRLEN(p);
26952 *usedlen += 2;
26953 }
26954
Bram Moolenaar071d4272004-06-13 20:20:40 +000026955 return valid;
26956}
26957
26958/*
26959 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26960 * "flags" can be "g" to do a global substitute.
26961 * Returns an allocated string, NULL for error.
26962 */
26963 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026964do_string_sub(
26965 char_u *str,
26966 char_u *pat,
26967 char_u *sub,
26968 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026969{
26970 int sublen;
26971 regmatch_T regmatch;
26972 int i;
26973 int do_all;
26974 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026975 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026976 garray_T ga;
26977 char_u *ret;
26978 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026979 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026980
26981 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26982 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026983 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026984
26985 ga_init2(&ga, 1, 200);
26986
26987 do_all = (flags[0] == 'g');
26988
26989 regmatch.rm_ic = p_ic;
26990 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26991 if (regmatch.regprog != NULL)
26992 {
26993 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026994 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026995 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26996 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026997 /* Skip empty match except for first match. */
26998 if (regmatch.startp[0] == regmatch.endp[0])
26999 {
27000 if (zero_width == regmatch.startp[0])
27001 {
27002 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027003 i = MB_PTR2LEN(tail);
27004 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27005 (size_t)i);
27006 ga.ga_len += i;
27007 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027008 continue;
27009 }
27010 zero_width = regmatch.startp[0];
27011 }
27012
Bram Moolenaar071d4272004-06-13 20:20:40 +000027013 /*
27014 * Get some space for a temporary buffer to do the substitution
27015 * into. It will contain:
27016 * - The text up to where the match is.
27017 * - The substituted text.
27018 * - The text after the match.
27019 */
27020 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027021 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027022 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27023 {
27024 ga_clear(&ga);
27025 break;
27026 }
27027
27028 /* copy the text up to where the match is */
27029 i = (int)(regmatch.startp[0] - tail);
27030 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27031 /* add the substituted text */
27032 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27033 + ga.ga_len + i, TRUE, TRUE, FALSE);
27034 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027035 tail = regmatch.endp[0];
27036 if (*tail == NUL)
27037 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027038 if (!do_all)
27039 break;
27040 }
27041
27042 if (ga.ga_data != NULL)
27043 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27044
Bram Moolenaar473de612013-06-08 18:19:48 +020027045 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027046 }
27047
27048 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27049 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027050 if (p_cpo == empty_option)
27051 p_cpo = save_cpo;
27052 else
27053 /* Darn, evaluating {sub} expression changed the value. */
27054 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027055
27056 return ret;
27057}
27058
27059#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */