blob: b3ebbd7276415abea17766709a77eb64d01b2640 [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;
3295 if (tv.v_type != VAR_LIST || l == NULL)
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 Moolenaar3d60ec22005-01-05 22:19:46 +00003300 else
3301 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003302 /* No need to increment the refcount, it's already set for the
3303 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 fi->fi_list = l;
3305 list_add_watch(l, &fi->fi_lw);
3306 fi->fi_lw.lw_item = l->lv_first;
3307 }
3308 }
3309 }
3310 if (skip)
3311 --emsg_skip;
3312
3313 return fi;
3314}
3315
3316/*
3317 * Use the first item in a ":for" list. Advance to the next.
3318 * Assign the values to the variable (list). "arg" points to the first one.
3319 * Return TRUE when a valid item was found, FALSE when at end of list or
3320 * something wrong.
3321 */
3322 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003323next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003325 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328
3329 item = fi->fi_lw.lw_item;
3330 if (item == NULL)
3331 result = FALSE;
3332 else
3333 {
3334 fi->fi_lw.lw_item = item->li_next;
3335 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3336 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3337 }
3338 return result;
3339}
3340
3341/*
3342 * Free the structure used to store info used by ":for".
3343 */
3344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003345free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346{
Bram Moolenaar33570922005-01-25 22:26:29 +00003347 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003349 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003350 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003352 list_unref(fi->fi_list);
3353 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003354 vim_free(fi);
3355}
3356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3358
3359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003360set_context_for_expression(
3361 expand_T *xp,
3362 char_u *arg,
3363 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 int got_eq = FALSE;
3366 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003367 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003369 if (cmdidx == CMD_let)
3370 {
3371 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003372 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003373 {
3374 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003375 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003376 {
3377 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003378 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003379 if (vim_iswhite(*p))
3380 break;
3381 }
3382 return;
3383 }
3384 }
3385 else
3386 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3387 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 while ((xp->xp_pattern = vim_strpbrk(arg,
3389 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3390 {
3391 c = *xp->xp_pattern;
3392 if (c == '&')
3393 {
3394 c = xp->xp_pattern[1];
3395 if (c == '&')
3396 {
3397 ++xp->xp_pattern;
3398 xp->xp_context = cmdidx != CMD_let || got_eq
3399 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3400 }
3401 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003404 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3405 xp->xp_pattern += 2;
3406
3407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
3409 else if (c == '$')
3410 {
3411 /* environment variable */
3412 xp->xp_context = EXPAND_ENV_VARS;
3413 }
3414 else if (c == '=')
3415 {
3416 got_eq = TRUE;
3417 xp->xp_context = EXPAND_EXPRESSION;
3418 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003419 else if (c == '#'
3420 && xp->xp_context == EXPAND_EXPRESSION)
3421 {
3422 /* Autoload function/variable contains '#'. */
3423 break;
3424 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003425 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 && xp->xp_context == EXPAND_FUNCTIONS
3427 && vim_strchr(xp->xp_pattern, '(') == NULL)
3428 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003429 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 break;
3431 }
3432 else if (cmdidx != CMD_let || got_eq)
3433 {
3434 if (c == '"') /* string */
3435 {
3436 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3437 if (c == '\\' && xp->xp_pattern[1] != NUL)
3438 ++xp->xp_pattern;
3439 xp->xp_context = EXPAND_NOTHING;
3440 }
3441 else if (c == '\'') /* literal string */
3442 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003443 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3445 /* skip */ ;
3446 xp->xp_context = EXPAND_NOTHING;
3447 }
3448 else if (c == '|')
3449 {
3450 if (xp->xp_pattern[1] == '|')
3451 {
3452 ++xp->xp_pattern;
3453 xp->xp_context = EXPAND_EXPRESSION;
3454 }
3455 else
3456 xp->xp_context = EXPAND_COMMANDS;
3457 }
3458 else
3459 xp->xp_context = EXPAND_EXPRESSION;
3460 }
3461 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003462 /* Doesn't look like something valid, expand as an expression
3463 * anyway. */
3464 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 arg = xp->xp_pattern;
3466 if (*arg != NUL)
3467 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3468 /* skip */ ;
3469 }
3470 xp->xp_pattern = arg;
3471}
3472
3473#endif /* FEAT_CMDL_COMPL */
3474
3475/*
3476 * ":1,25call func(arg1, arg2)" function call.
3477 */
3478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003479ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481 char_u *arg = eap->arg;
3482 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003484 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003486 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 linenr_T lnum;
3488 int doesrange;
3489 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003490 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003491 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003493 if (eap->skip)
3494 {
3495 /* trans_function_name() doesn't work well when skipping, use eval0()
3496 * instead to skip to any following command, e.g. for:
3497 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003498 ++emsg_skip;
3499 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3500 clear_tv(&rettv);
3501 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003502 return;
3503 }
3504
Bram Moolenaar65639032016-03-16 21:40:30 +01003505 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003506 if (fudi.fd_newkey != NULL)
3507 {
3508 /* Still need to give an error message for missing key. */
3509 EMSG2(_(e_dictkey), fudi.fd_newkey);
3510 vim_free(fudi.fd_newkey);
3511 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003512 if (tofree == NULL)
3513 return;
3514
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003515 /* Increase refcount on dictionary, it could get deleted when evaluating
3516 * the arguments. */
3517 if (fudi.fd_dict != NULL)
3518 ++fudi.fd_dict->dv_refcount;
3519
Bram Moolenaar65639032016-03-16 21:40:30 +01003520 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3521 * contents. For VAR_PARTIAL get its partial, unless we already have one
3522 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003523 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003524 name = deref_func_name(tofree, &len,
3525 partial != NULL ? NULL : &partial, FALSE);
3526
Bram Moolenaar532c7802005-01-27 14:44:31 +00003527 /* Skip white space to allow ":call func ()". Not good, but required for
3528 * backward compatibility. */
3529 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003530 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
3532 if (*startarg != '(')
3533 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003534 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 goto end;
3536 }
3537
3538 /*
3539 * When skipping, evaluate the function once, to find the end of the
3540 * arguments.
3541 * When the function takes a range, this is discovered after the first
3542 * call, and the loop is broken.
3543 */
3544 if (eap->skip)
3545 {
3546 ++emsg_skip;
3547 lnum = eap->line2; /* do it once, also with an invalid range */
3548 }
3549 else
3550 lnum = eap->line1;
3551 for ( ; lnum <= eap->line2; ++lnum)
3552 {
3553 if (!eap->skip && eap->addr_count > 0)
3554 {
3555 curwin->w_cursor.lnum = lnum;
3556 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003557#ifdef FEAT_VIRTUALEDIT
3558 curwin->w_cursor.coladd = 0;
3559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
3561 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003562 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003563 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003564 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
3566 failed = TRUE;
3567 break;
3568 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003569
3570 /* Handle a function returning a Funcref, Dictionary or List. */
3571 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3572 {
3573 failed = TRUE;
3574 break;
3575 }
3576
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003577 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (doesrange || eap->skip)
3579 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003580
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003582 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003583 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003584 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (aborting())
3586 break;
3587 }
3588 if (eap->skip)
3589 --emsg_skip;
3590
3591 if (!failed)
3592 {
3593 /* Check for trailing illegal characters and a following command. */
3594 if (!ends_excmd(*arg))
3595 {
3596 emsg_severe = TRUE;
3597 EMSG(_(e_trailing));
3598 }
3599 else
3600 eap->nextcmd = check_nextcmd(arg);
3601 }
3602
3603end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003604 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003605 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606}
3607
3608/*
3609 * ":unlet[!] var1 ... " command.
3610 */
3611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003612ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003614 ex_unletlock(eap, eap->arg, 0);
3615}
3616
3617/*
3618 * ":lockvar" and ":unlockvar" commands
3619 */
3620 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003621ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003622{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624 int deep = 2;
3625
3626 if (eap->forceit)
3627 deep = -1;
3628 else if (vim_isdigit(*arg))
3629 {
3630 deep = getdigits(&arg);
3631 arg = skipwhite(arg);
3632 }
3633
3634 ex_unletlock(eap, arg, deep);
3635}
3636
3637/*
3638 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3639 */
3640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003641ex_unletlock(
3642 exarg_T *eap,
3643 char_u *argstart,
3644 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003645{
3646 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003649 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 do
3652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003654 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003655 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 if (lv.ll_name == NULL)
3657 error = TRUE; /* error but continue parsing */
3658 if (name_end == NULL || (!vim_iswhite(*name_end)
3659 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 if (name_end != NULL)
3662 {
3663 emsg_severe = TRUE;
3664 EMSG(_(e_trailing));
3665 }
3666 if (!(eap->skip || error))
3667 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 break;
3669 }
3670
3671 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 {
3673 if (eap->cmdidx == CMD_unlet)
3674 {
3675 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3676 error = TRUE;
3677 }
3678 else
3679 {
3680 if (do_lock_var(&lv, name_end, deep,
3681 eap->cmdidx == CMD_lockvar) == FAIL)
3682 error = TRUE;
3683 }
3684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 if (!eap->skip)
3687 clear_lval(&lv);
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 arg = skipwhite(name_end);
3690 } while (!ends_excmd(*arg));
3691
3692 eap->nextcmd = check_nextcmd(arg);
3693}
3694
Bram Moolenaar8c711452005-01-14 21:53:12 +00003695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003696do_unlet_var(
3697 lval_T *lp,
3698 char_u *name_end,
3699 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003700{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 int ret = OK;
3702 int cc;
3703
3704 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 cc = *name_end;
3707 *name_end = NUL;
3708
3709 /* Normal name or expanded name. */
3710 if (check_changedtick(lp->ll_name))
3711 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003712 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003715 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003716 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003717 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003718 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003719 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003720 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 else if (lp->ll_range)
3722 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003724 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003725 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003726
3727 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3728 {
3729 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003730 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003731 return FAIL;
3732 ll_li = li;
3733 ++ll_n1;
3734 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735
3736 /* Delete a range of List items. */
3737 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3738 {
3739 li = lp->ll_li->li_next;
3740 listitem_remove(lp->ll_list, lp->ll_li);
3741 lp->ll_li = li;
3742 ++lp->ll_n1;
3743 }
3744 }
3745 else
3746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003747 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003748 /* unlet a List item. */
3749 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003750 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003751 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003752 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003753 }
3754
3755 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003756}
3757
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758/*
3759 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 */
3762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003763do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
Bram Moolenaar33570922005-01-25 22:26:29 +00003765 hashtab_T *ht;
3766 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003767 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003768 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003769 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
Bram Moolenaar33570922005-01-25 22:26:29 +00003771 ht = find_var_ht(name, &varname);
3772 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003774 if (ht == &globvarht)
3775 d = &globvardict;
3776 else if (current_funccal != NULL
3777 && ht == &current_funccal->l_vars.dv_hashtab)
3778 d = &current_funccal->l_vars;
3779 else if (ht == &compat_hashtab)
3780 d = &vimvardict;
3781 else
3782 {
3783 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3784 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3785 }
3786 if (d == NULL)
3787 {
3788 EMSG2(_(e_intern2), "do_unlet()");
3789 return FAIL;
3790 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003791 hi = hash_find(ht, varname);
3792 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003793 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003794 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003795 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003796 || var_check_ro(di->di_flags, name, FALSE)
3797 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003798 return FAIL;
3799
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800 delete_var(ht, hi);
3801 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003804 if (forceit)
3805 return OK;
3806 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 return FAIL;
3808}
3809
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003810/*
3811 * Lock or unlock variable indicated by "lp".
3812 * "deep" is the levels to go (-1 for unlimited);
3813 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3814 */
3815 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003816do_lock_var(
3817 lval_T *lp,
3818 char_u *name_end,
3819 int deep,
3820 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003821{
3822 int ret = OK;
3823 int cc;
3824 dictitem_T *di;
3825
3826 if (deep == 0) /* nothing to do */
3827 return OK;
3828
3829 if (lp->ll_tv == NULL)
3830 {
3831 cc = *name_end;
3832 *name_end = NUL;
3833
3834 /* Normal name or expanded name. */
3835 if (check_changedtick(lp->ll_name))
3836 ret = FAIL;
3837 else
3838 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003839 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003840 if (di == NULL)
3841 ret = FAIL;
3842 else
3843 {
3844 if (lock)
3845 di->di_flags |= DI_FLAGS_LOCK;
3846 else
3847 di->di_flags &= ~DI_FLAGS_LOCK;
3848 item_lock(&di->di_tv, deep, lock);
3849 }
3850 }
3851 *name_end = cc;
3852 }
3853 else if (lp->ll_range)
3854 {
3855 listitem_T *li = lp->ll_li;
3856
3857 /* (un)lock a range of List items. */
3858 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3859 {
3860 item_lock(&li->li_tv, deep, lock);
3861 li = li->li_next;
3862 ++lp->ll_n1;
3863 }
3864 }
3865 else if (lp->ll_list != NULL)
3866 /* (un)lock a List item. */
3867 item_lock(&lp->ll_li->li_tv, deep, lock);
3868 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003869 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003870 item_lock(&lp->ll_di->di_tv, deep, lock);
3871
3872 return ret;
3873}
3874
3875/*
3876 * Lock or unlock an item. "deep" is nr of levels to go.
3877 */
3878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003879item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003880{
3881 static int recurse = 0;
3882 list_T *l;
3883 listitem_T *li;
3884 dict_T *d;
3885 hashitem_T *hi;
3886 int todo;
3887
3888 if (recurse >= DICT_MAXNEST)
3889 {
3890 EMSG(_("E743: variable nested too deep for (un)lock"));
3891 return;
3892 }
3893 if (deep == 0)
3894 return;
3895 ++recurse;
3896
3897 /* lock/unlock the item itself */
3898 if (lock)
3899 tv->v_lock |= VAR_LOCKED;
3900 else
3901 tv->v_lock &= ~VAR_LOCKED;
3902
3903 switch (tv->v_type)
3904 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003905 case VAR_UNKNOWN:
3906 case VAR_NUMBER:
3907 case VAR_STRING:
3908 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003909 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003910 case VAR_FLOAT:
3911 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003912 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003913 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003914 break;
3915
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003916 case VAR_LIST:
3917 if ((l = tv->vval.v_list) != NULL)
3918 {
3919 if (lock)
3920 l->lv_lock |= VAR_LOCKED;
3921 else
3922 l->lv_lock &= ~VAR_LOCKED;
3923 if (deep < 0 || deep > 1)
3924 /* recursive: lock/unlock the items the List contains */
3925 for (li = l->lv_first; li != NULL; li = li->li_next)
3926 item_lock(&li->li_tv, deep - 1, lock);
3927 }
3928 break;
3929 case VAR_DICT:
3930 if ((d = tv->vval.v_dict) != NULL)
3931 {
3932 if (lock)
3933 d->dv_lock |= VAR_LOCKED;
3934 else
3935 d->dv_lock &= ~VAR_LOCKED;
3936 if (deep < 0 || deep > 1)
3937 {
3938 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003939 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003940 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3941 {
3942 if (!HASHITEM_EMPTY(hi))
3943 {
3944 --todo;
3945 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3946 }
3947 }
3948 }
3949 }
3950 }
3951 --recurse;
3952}
3953
Bram Moolenaara40058a2005-07-11 22:42:07 +00003954/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003955 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3956 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003957 */
3958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003959tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003960{
3961 return (tv->v_lock & VAR_LOCKED)
3962 || (tv->v_type == VAR_LIST
3963 && tv->vval.v_list != NULL
3964 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3965 || (tv->v_type == VAR_DICT
3966 && tv->vval.v_dict != NULL
3967 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3968}
3969
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3971/*
3972 * Delete all "menutrans_" variables.
3973 */
3974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003975del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
Bram Moolenaar33570922005-01-25 22:26:29 +00003977 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003978 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003981 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003982 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003983 {
3984 if (!HASHITEM_EMPTY(hi))
3985 {
3986 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003987 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3988 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003989 }
3990 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003991 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992}
3993#endif
3994
3995#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3996
3997/*
3998 * Local string buffer for the next two functions to store a variable name
3999 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4000 * get_user_var_name().
4001 */
4002
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004003static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
4005static char_u *varnamebuf = NULL;
4006static int varnamebuflen = 0;
4007
4008/*
4009 * Function to concatenate a prefix and a variable name.
4010 */
4011 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004012cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
4014 int len;
4015
4016 len = (int)STRLEN(name) + 3;
4017 if (len > varnamebuflen)
4018 {
4019 vim_free(varnamebuf);
4020 len += 10; /* some additional space */
4021 varnamebuf = alloc(len);
4022 if (varnamebuf == NULL)
4023 {
4024 varnamebuflen = 0;
4025 return NULL;
4026 }
4027 varnamebuflen = len;
4028 }
4029 *varnamebuf = prefix;
4030 varnamebuf[1] = ':';
4031 STRCPY(varnamebuf + 2, name);
4032 return varnamebuf;
4033}
4034
4035/*
4036 * Function given to ExpandGeneric() to obtain the list of user defined
4037 * (global/buffer/window/built-in) variable names.
4038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004040get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004042 static long_u gdone;
4043 static long_u bdone;
4044 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004045#ifdef FEAT_WINDOWS
4046 static long_u tdone;
4047#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004048 static int vidx;
4049 static hashitem_T *hi;
4050 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004053 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004054 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004055#ifdef FEAT_WINDOWS
4056 tdone = 0;
4057#endif
4058 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004059
4060 /* Global variables */
4061 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004063 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004064 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004065 else
4066 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004067 while (HASHITEM_EMPTY(hi))
4068 ++hi;
4069 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4070 return cat_prefix_varname('g', hi->hi_key);
4071 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004073
4074 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004075 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004078 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004080 else
4081 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004082 while (HASHITEM_EMPTY(hi))
4083 ++hi;
4084 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004088 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 return (char_u *)"b:changedtick";
4090 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004091
4092 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004093 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004094 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004096 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004097 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004098 else
4099 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004100 while (HASHITEM_EMPTY(hi))
4101 ++hi;
4102 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004104
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004105#ifdef FEAT_WINDOWS
4106 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004107 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004108 if (tdone < ht->ht_used)
4109 {
4110 if (tdone++ == 0)
4111 hi = ht->ht_array;
4112 else
4113 ++hi;
4114 while (HASHITEM_EMPTY(hi))
4115 ++hi;
4116 return cat_prefix_varname('t', hi->hi_key);
4117 }
4118#endif
4119
Bram Moolenaar33570922005-01-25 22:26:29 +00004120 /* v: variables */
4121 if (vidx < VV_LEN)
4122 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
4124 vim_free(varnamebuf);
4125 varnamebuf = NULL;
4126 varnamebuflen = 0;
4127 return NULL;
4128}
4129
4130#endif /* FEAT_CMDL_COMPL */
4131
4132/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004133 * Return TRUE if "pat" matches "text".
4134 * Does not use 'cpo' and always uses 'magic'.
4135 */
4136 static int
4137pattern_match(char_u *pat, char_u *text, int ic)
4138{
4139 int matches = FALSE;
4140 char_u *save_cpo;
4141 regmatch_T regmatch;
4142
4143 /* avoid 'l' flag in 'cpoptions' */
4144 save_cpo = p_cpo;
4145 p_cpo = (char_u *)"";
4146 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4147 if (regmatch.regprog != NULL)
4148 {
4149 regmatch.rm_ic = ic;
4150 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4151 vim_regfree(regmatch.regprog);
4152 }
4153 p_cpo = save_cpo;
4154 return matches;
4155}
4156
4157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 * types for expressions.
4159 */
4160typedef enum
4161{
4162 TYPE_UNKNOWN = 0
4163 , TYPE_EQUAL /* == */
4164 , TYPE_NEQUAL /* != */
4165 , TYPE_GREATER /* > */
4166 , TYPE_GEQUAL /* >= */
4167 , TYPE_SMALLER /* < */
4168 , TYPE_SEQUAL /* <= */
4169 , TYPE_MATCH /* =~ */
4170 , TYPE_NOMATCH /* !~ */
4171} exptype_T;
4172
4173/*
4174 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4177 */
4178
4179/*
4180 * Handle zero level expression.
4181 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004183 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 * Return OK or FAIL.
4185 */
4186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004187eval0(
4188 char_u *arg,
4189 typval_T *rettv,
4190 char_u **nextcmd,
4191 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 int ret;
4194 char_u *p;
4195
4196 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 if (ret == FAIL || !ends_excmd(*p))
4199 {
4200 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 /*
4203 * Report the invalid expression unless the expression evaluation has
4204 * been cancelled due to an aborting error, an interrupt, or an
4205 * exception.
4206 */
4207 if (!aborting())
4208 EMSG2(_(e_invexpr2), arg);
4209 ret = FAIL;
4210 }
4211 if (nextcmd != NULL)
4212 *nextcmd = check_nextcmd(p);
4213
4214 return ret;
4215}
4216
4217/*
4218 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004219 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 *
4221 * "arg" must point to the first non-white of the expression.
4222 * "arg" is advanced to the next non-white after the recognized expression.
4223 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004224 * Note: "rettv.v_lock" is not set.
4225 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 * Return OK or FAIL.
4227 */
4228 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004229eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
4231 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004232 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234 /*
4235 * Get the first variable.
4236 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return FAIL;
4239
4240 if ((*arg)[0] == '?')
4241 {
4242 result = FALSE;
4243 if (evaluate)
4244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 int error = FALSE;
4246
4247 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 if (error)
4251 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253
4254 /*
4255 * Get the second variable.
4256 */
4257 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 return FAIL;
4260
4261 /*
4262 * Check for the ":".
4263 */
4264 if ((*arg)[0] != ':')
4265 {
4266 EMSG(_("E109: Missing ':' after '?'"));
4267 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 return FAIL;
4270 }
4271
4272 /*
4273 * Get the third variable.
4274 */
4275 *arg = skipwhite(*arg + 1);
4276 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4277 {
4278 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 return FAIL;
4281 }
4282 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285
4286 return OK;
4287}
4288
4289/*
4290 * Handle first level expression:
4291 * expr2 || expr2 || expr2 logical OR
4292 *
4293 * "arg" must point to the first non-white of the expression.
4294 * "arg" is advanced to the next non-white after the recognized expression.
4295 *
4296 * Return OK or FAIL.
4297 */
4298 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004299eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
Bram Moolenaar33570922005-01-25 22:26:29 +00004301 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 long result;
4303 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 /*
4307 * Get the first variable.
4308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return FAIL;
4311
4312 /*
4313 * Repeat until there is no following "||".
4314 */
4315 first = TRUE;
4316 result = FALSE;
4317 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4318 {
4319 if (evaluate && first)
4320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 if (error)
4325 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 first = FALSE;
4327 }
4328
4329 /*
4330 * Get the second variable.
4331 */
4332 *arg = skipwhite(*arg + 2);
4333 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4334 return FAIL;
4335
4336 /*
4337 * Compute the result.
4338 */
4339 if (evaluate && !result)
4340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 if (error)
4345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 if (evaluate)
4348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 rettv->v_type = VAR_NUMBER;
4350 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 }
4353
4354 return OK;
4355}
4356
4357/*
4358 * Handle second level expression:
4359 * expr3 && expr3 && expr3 logical AND
4360 *
4361 * "arg" must point to the first non-white of the expression.
4362 * "arg" is advanced to the next non-white after the recognized expression.
4363 *
4364 * Return OK or FAIL.
4365 */
4366 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004367eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
Bram Moolenaar33570922005-01-25 22:26:29 +00004369 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 long result;
4371 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004372 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374 /*
4375 * Get the first variable.
4376 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 return FAIL;
4379
4380 /*
4381 * Repeat until there is no following "&&".
4382 */
4383 first = TRUE;
4384 result = TRUE;
4385 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4386 {
4387 if (evaluate && first)
4388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004389 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004392 if (error)
4393 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 first = FALSE;
4395 }
4396
4397 /*
4398 * Get the second variable.
4399 */
4400 *arg = skipwhite(*arg + 2);
4401 if (eval4(arg, &var2, evaluate && result) == FAIL)
4402 return FAIL;
4403
4404 /*
4405 * Compute the result.
4406 */
4407 if (evaluate && result)
4408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004411 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412 if (error)
4413 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415 if (evaluate)
4416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004417 rettv->v_type = VAR_NUMBER;
4418 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 }
4421
4422 return OK;
4423}
4424
4425/*
4426 * Handle third level expression:
4427 * var1 == var2
4428 * var1 =~ var2
4429 * var1 != var2
4430 * var1 !~ var2
4431 * var1 > var2
4432 * var1 >= var2
4433 * var1 < var2
4434 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 * var1 is var2
4436 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 *
4438 * "arg" must point to the first non-white of the expression.
4439 * "arg" is advanced to the next non-white after the recognized expression.
4440 *
4441 * Return OK or FAIL.
4442 */
4443 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004444eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445{
Bram Moolenaar33570922005-01-25 22:26:29 +00004446 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 char_u *p;
4448 int i;
4449 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004450 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 int len = 2;
4452 long n1, n2;
4453 char_u *s1, *s2;
4454 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
4457 /*
4458 * Get the first variable.
4459 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004460 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 return FAIL;
4462
4463 p = *arg;
4464 switch (p[0])
4465 {
4466 case '=': if (p[1] == '=')
4467 type = TYPE_EQUAL;
4468 else if (p[1] == '~')
4469 type = TYPE_MATCH;
4470 break;
4471 case '!': if (p[1] == '=')
4472 type = TYPE_NEQUAL;
4473 else if (p[1] == '~')
4474 type = TYPE_NOMATCH;
4475 break;
4476 case '>': if (p[1] != '=')
4477 {
4478 type = TYPE_GREATER;
4479 len = 1;
4480 }
4481 else
4482 type = TYPE_GEQUAL;
4483 break;
4484 case '<': if (p[1] != '=')
4485 {
4486 type = TYPE_SMALLER;
4487 len = 1;
4488 }
4489 else
4490 type = TYPE_SEQUAL;
4491 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004492 case 'i': if (p[1] == 's')
4493 {
4494 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4495 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004496 i = p[len];
4497 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004498 {
4499 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4500 type_is = TRUE;
4501 }
4502 }
4503 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505
4506 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004507 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 */
4509 if (type != TYPE_UNKNOWN)
4510 {
4511 /* extra question mark appended: ignore case */
4512 if (p[len] == '?')
4513 {
4514 ic = TRUE;
4515 ++len;
4516 }
4517 /* extra '#' appended: match case */
4518 else if (p[len] == '#')
4519 {
4520 ic = FALSE;
4521 ++len;
4522 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004523 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 else
4525 ic = p_ic;
4526
4527 /*
4528 * Get the second variable.
4529 */
4530 *arg = skipwhite(p + len);
4531 if (eval5(arg, &var2, evaluate) == FAIL)
4532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004533 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 return FAIL;
4535 }
4536
4537 if (evaluate)
4538 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004539 if (type_is && rettv->v_type != var2.v_type)
4540 {
4541 /* For "is" a different type always means FALSE, for "notis"
4542 * it means TRUE. */
4543 n1 = (type == TYPE_NEQUAL);
4544 }
4545 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4546 {
4547 if (type_is)
4548 {
4549 n1 = (rettv->v_type == var2.v_type
4550 && rettv->vval.v_list == var2.vval.v_list);
4551 if (type == TYPE_NEQUAL)
4552 n1 = !n1;
4553 }
4554 else if (rettv->v_type != var2.v_type
4555 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4556 {
4557 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004558 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004560 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 clear_tv(rettv);
4562 clear_tv(&var2);
4563 return FAIL;
4564 }
4565 else
4566 {
4567 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004568 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4569 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004570 if (type == TYPE_NEQUAL)
4571 n1 = !n1;
4572 }
4573 }
4574
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004575 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4576 {
4577 if (type_is)
4578 {
4579 n1 = (rettv->v_type == var2.v_type
4580 && rettv->vval.v_dict == var2.vval.v_dict);
4581 if (type == TYPE_NEQUAL)
4582 n1 = !n1;
4583 }
4584 else if (rettv->v_type != var2.v_type
4585 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4586 {
4587 if (rettv->v_type != var2.v_type)
4588 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4589 else
4590 EMSG(_("E736: Invalid operation for Dictionary"));
4591 clear_tv(rettv);
4592 clear_tv(&var2);
4593 return FAIL;
4594 }
4595 else
4596 {
4597 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004598 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4599 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004600 if (type == TYPE_NEQUAL)
4601 n1 = !n1;
4602 }
4603 }
4604
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004605 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4606 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004607 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004608 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004609 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004610 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004611 clear_tv(rettv);
4612 clear_tv(&var2);
4613 return FAIL;
4614 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004615 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004616 if (type == TYPE_NEQUAL)
4617 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004618 }
4619
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004620#ifdef FEAT_FLOAT
4621 /*
4622 * If one of the two variables is a float, compare as a float.
4623 * When using "=~" or "!~", always compare as string.
4624 */
4625 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4626 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4627 {
4628 float_T f1, f2;
4629
4630 if (rettv->v_type == VAR_FLOAT)
4631 f1 = rettv->vval.v_float;
4632 else
4633 f1 = get_tv_number(rettv);
4634 if (var2.v_type == VAR_FLOAT)
4635 f2 = var2.vval.v_float;
4636 else
4637 f2 = get_tv_number(&var2);
4638 n1 = FALSE;
4639 switch (type)
4640 {
4641 case TYPE_EQUAL: n1 = (f1 == f2); break;
4642 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4643 case TYPE_GREATER: n1 = (f1 > f2); break;
4644 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4645 case TYPE_SMALLER: n1 = (f1 < f2); break;
4646 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4647 case TYPE_UNKNOWN:
4648 case TYPE_MATCH:
4649 case TYPE_NOMATCH: break; /* avoid gcc warning */
4650 }
4651 }
4652#endif
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 /*
4655 * If one of the two variables is a number, compare as a number.
4656 * When using "=~" or "!~", always compare as string.
4657 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004658 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 n1 = get_tv_number(rettv);
4662 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 switch (type)
4664 {
4665 case TYPE_EQUAL: n1 = (n1 == n2); break;
4666 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4667 case TYPE_GREATER: n1 = (n1 > n2); break;
4668 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4669 case TYPE_SMALLER: n1 = (n1 < n2); break;
4670 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4671 case TYPE_UNKNOWN:
4672 case TYPE_MATCH:
4673 case TYPE_NOMATCH: break; /* avoid gcc warning */
4674 }
4675 }
4676 else
4677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004678 s1 = get_tv_string_buf(rettv, buf1);
4679 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4681 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4682 else
4683 i = 0;
4684 n1 = FALSE;
4685 switch (type)
4686 {
4687 case TYPE_EQUAL: n1 = (i == 0); break;
4688 case TYPE_NEQUAL: n1 = (i != 0); break;
4689 case TYPE_GREATER: n1 = (i > 0); break;
4690 case TYPE_GEQUAL: n1 = (i >= 0); break;
4691 case TYPE_SMALLER: n1 = (i < 0); break;
4692 case TYPE_SEQUAL: n1 = (i <= 0); break;
4693
4694 case TYPE_MATCH:
4695 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004696 n1 = pattern_match(s2, s1, ic);
4697 if (type == TYPE_NOMATCH)
4698 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 break;
4700
4701 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4702 }
4703 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004704 clear_tv(rettv);
4705 clear_tv(&var2);
4706 rettv->v_type = VAR_NUMBER;
4707 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709 }
4710
4711 return OK;
4712}
4713
4714/*
4715 * Handle fourth level expression:
4716 * + number addition
4717 * - number subtraction
4718 * . string concatenation
4719 *
4720 * "arg" must point to the first non-white of the expression.
4721 * "arg" is advanced to the next non-white after the recognized expression.
4722 *
4723 * Return OK or FAIL.
4724 */
4725 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004726eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727{
Bram Moolenaar33570922005-01-25 22:26:29 +00004728 typval_T var2;
4729 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 int op;
4731 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004732#ifdef FEAT_FLOAT
4733 float_T f1 = 0, f2 = 0;
4734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 char_u *s1, *s2;
4736 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4737 char_u *p;
4738
4739 /*
4740 * Get the first variable.
4741 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004742 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 return FAIL;
4744
4745 /*
4746 * Repeat computing, until no '+', '-' or '.' is following.
4747 */
4748 for (;;)
4749 {
4750 op = **arg;
4751 if (op != '+' && op != '-' && op != '.')
4752 break;
4753
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004754 if ((op != '+' || rettv->v_type != VAR_LIST)
4755#ifdef FEAT_FLOAT
4756 && (op == '.' || rettv->v_type != VAR_FLOAT)
4757#endif
4758 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 {
4760 /* For "list + ...", an illegal use of the first operand as
4761 * a number cannot be determined before evaluating the 2nd
4762 * operand: if this is also a list, all is ok.
4763 * For "something . ...", "something - ..." or "non-list + ...",
4764 * we know that the first operand needs to be a string or number
4765 * without evaluating the 2nd operand. So check before to avoid
4766 * side effects after an error. */
4767 if (evaluate && get_tv_string_chk(rettv) == NULL)
4768 {
4769 clear_tv(rettv);
4770 return FAIL;
4771 }
4772 }
4773
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 /*
4775 * Get the second variable.
4776 */
4777 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004778 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 return FAIL;
4782 }
4783
4784 if (evaluate)
4785 {
4786 /*
4787 * Compute the result.
4788 */
4789 if (op == '.')
4790 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4792 s2 = get_tv_string_buf_chk(&var2, buf2);
4793 if (s2 == NULL) /* type error ? */
4794 {
4795 clear_tv(rettv);
4796 clear_tv(&var2);
4797 return FAIL;
4798 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004799 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004800 clear_tv(rettv);
4801 rettv->v_type = VAR_STRING;
4802 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004804 else if (op == '+' && rettv->v_type == VAR_LIST
4805 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004806 {
4807 /* concatenate Lists */
4808 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4809 &var3) == FAIL)
4810 {
4811 clear_tv(rettv);
4812 clear_tv(&var2);
4813 return FAIL;
4814 }
4815 clear_tv(rettv);
4816 *rettv = var3;
4817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 else
4819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004820 int error = FALSE;
4821
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004822#ifdef FEAT_FLOAT
4823 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004824 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004825 f1 = rettv->vval.v_float;
4826 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828 else
4829#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831 n1 = get_tv_number_chk(rettv, &error);
4832 if (error)
4833 {
4834 /* This can only happen for "list + non-list". For
4835 * "non-list + ..." or "something - ...", we returned
4836 * before evaluating the 2nd operand. */
4837 clear_tv(rettv);
4838 return FAIL;
4839 }
4840#ifdef FEAT_FLOAT
4841 if (var2.v_type == VAR_FLOAT)
4842 f1 = n1;
4843#endif
4844 }
4845#ifdef FEAT_FLOAT
4846 if (var2.v_type == VAR_FLOAT)
4847 {
4848 f2 = var2.vval.v_float;
4849 n2 = 0;
4850 }
4851 else
4852#endif
4853 {
4854 n2 = get_tv_number_chk(&var2, &error);
4855 if (error)
4856 {
4857 clear_tv(rettv);
4858 clear_tv(&var2);
4859 return FAIL;
4860 }
4861#ifdef FEAT_FLOAT
4862 if (rettv->v_type == VAR_FLOAT)
4863 f2 = n2;
4864#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004865 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004866 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867
4868#ifdef FEAT_FLOAT
4869 /* If there is a float on either side the result is a float. */
4870 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4871 {
4872 if (op == '+')
4873 f1 = f1 + f2;
4874 else
4875 f1 = f1 - f2;
4876 rettv->v_type = VAR_FLOAT;
4877 rettv->vval.v_float = f1;
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880#endif
4881 {
4882 if (op == '+')
4883 n1 = n1 + n2;
4884 else
4885 n1 = n1 - n2;
4886 rettv->v_type = VAR_NUMBER;
4887 rettv->vval.v_number = n1;
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004890 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892 }
4893 return OK;
4894}
4895
4896/*
4897 * Handle fifth level expression:
4898 * * number multiplication
4899 * / number division
4900 * % number modulo
4901 *
4902 * "arg" must point to the first non-white of the expression.
4903 * "arg" is advanced to the next non-white after the recognized expression.
4904 *
4905 * Return OK or FAIL.
4906 */
4907 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004908eval6(
4909 char_u **arg,
4910 typval_T *rettv,
4911 int evaluate,
4912 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913{
Bram Moolenaar33570922005-01-25 22:26:29 +00004914 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 int op;
4916 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004917#ifdef FEAT_FLOAT
4918 int use_float = FALSE;
4919 float_T f1 = 0, f2;
4920#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004921 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 /*
4924 * Get the first variable.
4925 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004926 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 return FAIL;
4928
4929 /*
4930 * Repeat computing, until no '*', '/' or '%' is following.
4931 */
4932 for (;;)
4933 {
4934 op = **arg;
4935 if (op != '*' && op != '/' && op != '%')
4936 break;
4937
4938 if (evaluate)
4939 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004940#ifdef FEAT_FLOAT
4941 if (rettv->v_type == VAR_FLOAT)
4942 {
4943 f1 = rettv->vval.v_float;
4944 use_float = TRUE;
4945 n1 = 0;
4946 }
4947 else
4948#endif
4949 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004950 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004951 if (error)
4952 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954 else
4955 n1 = 0;
4956
4957 /*
4958 * Get the second variable.
4959 */
4960 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004961 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 return FAIL;
4963
4964 if (evaluate)
4965 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966#ifdef FEAT_FLOAT
4967 if (var2.v_type == VAR_FLOAT)
4968 {
4969 if (!use_float)
4970 {
4971 f1 = n1;
4972 use_float = TRUE;
4973 }
4974 f2 = var2.vval.v_float;
4975 n2 = 0;
4976 }
4977 else
4978#endif
4979 {
4980 n2 = get_tv_number_chk(&var2, &error);
4981 clear_tv(&var2);
4982 if (error)
4983 return FAIL;
4984#ifdef FEAT_FLOAT
4985 if (use_float)
4986 f2 = n2;
4987#endif
4988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
4990 /*
4991 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994#ifdef FEAT_FLOAT
4995 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 if (op == '*')
4998 f1 = f1 * f2;
4999 else if (op == '/')
5000 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005001# ifdef VMS
5002 /* VMS crashes on divide by zero, work around it */
5003 if (f2 == 0.0)
5004 {
5005 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005006 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005007 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005008 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005009 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005010 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005011 }
5012 else
5013 f1 = f1 / f2;
5014# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005015 /* We rely on the floating point library to handle divide
5016 * by zero to result in "inf" and not a crash. */
5017 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005018# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005021 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005022 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023 return FAIL;
5024 }
5025 rettv->v_type = VAR_FLOAT;
5026 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005031 if (op == '*')
5032 n1 = n1 * n2;
5033 else if (op == '/')
5034 {
5035 if (n2 == 0) /* give an error message? */
5036 {
5037 if (n1 == 0)
5038 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5039 else if (n1 < 0)
5040 n1 = -0x7fffffffL;
5041 else
5042 n1 = 0x7fffffffL;
5043 }
5044 else
5045 n1 = n1 / n2;
5046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005048 {
5049 if (n2 == 0) /* give an error message? */
5050 n1 = 0;
5051 else
5052 n1 = n1 % n2;
5053 }
5054 rettv->v_type = VAR_NUMBER;
5055 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058 }
5059
5060 return OK;
5061}
5062
5063/*
5064 * Handle sixth level expression:
5065 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005066 * "string" string constant
5067 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 * &option-name option value
5069 * @r register contents
5070 * identifier variable value
5071 * function() function call
5072 * $VAR environment variable
5073 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005074 * [expr, expr] List
5075 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 *
5077 * Also handle:
5078 * ! in front logical NOT
5079 * - in front unary minus
5080 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 * trailing [] subscript in String or List
5082 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 *
5084 * "arg" must point to the first non-white of the expression.
5085 * "arg" is advanced to the next non-white after the recognized expression.
5086 *
5087 * Return OK or FAIL.
5088 */
5089 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005090eval7(
5091 char_u **arg,
5092 typval_T *rettv,
5093 int evaluate,
5094 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 long n;
5097 int len;
5098 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 char_u *start_leader, *end_leader;
5100 int ret = OK;
5101 char_u *alias;
5102
5103 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005104 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005105 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005107 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
5109 /*
5110 * Skip '!' and '-' characters. They are handled later.
5111 */
5112 start_leader = *arg;
5113 while (**arg == '!' || **arg == '-' || **arg == '+')
5114 *arg = skipwhite(*arg + 1);
5115 end_leader = *arg;
5116
5117 switch (**arg)
5118 {
5119 /*
5120 * Number constant.
5121 */
5122 case '0':
5123 case '1':
5124 case '2':
5125 case '3':
5126 case '4':
5127 case '5':
5128 case '6':
5129 case '7':
5130 case '8':
5131 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005132 {
5133#ifdef FEAT_FLOAT
5134 char_u *p = skipdigits(*arg + 1);
5135 int get_float = FALSE;
5136
5137 /* We accept a float when the format matches
5138 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005139 * strict to avoid backwards compatibility problems.
5140 * Don't look for a float after the "." operator, so that
5141 * ":let vers = 1.2.3" doesn't fail. */
5142 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005144 get_float = TRUE;
5145 p = skipdigits(p + 2);
5146 if (*p == 'e' || *p == 'E')
5147 {
5148 ++p;
5149 if (*p == '-' || *p == '+')
5150 ++p;
5151 if (!vim_isdigit(*p))
5152 get_float = FALSE;
5153 else
5154 p = skipdigits(p + 1);
5155 }
5156 if (ASCII_ISALPHA(*p) || *p == '.')
5157 get_float = FALSE;
5158 }
5159 if (get_float)
5160 {
5161 float_T f;
5162
5163 *arg += string2float(*arg, &f);
5164 if (evaluate)
5165 {
5166 rettv->v_type = VAR_FLOAT;
5167 rettv->vval.v_float = f;
5168 }
5169 }
5170 else
5171#endif
5172 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005173 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005174 *arg += len;
5175 if (evaluate)
5176 {
5177 rettv->v_type = VAR_NUMBER;
5178 rettv->vval.v_number = n;
5179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
5181 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183
5184 /*
5185 * String constant: "string".
5186 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189
5190 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005191 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005193 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194 break;
5195
5196 /*
5197 * List: [expr, expr]
5198 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 break;
5201
5202 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 * Dictionary: {key: val, key: val}
5204 */
5205 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5206 break;
5207
5208 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005209 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005211 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 break;
5213
5214 /*
5215 * Environment variable: $VAR.
5216 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005217 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 break;
5219
5220 /*
5221 * Register contents: @r.
5222 */
5223 case '@': ++*arg;
5224 if (evaluate)
5225 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005226 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005227 rettv->vval.v_string = get_reg_contents(**arg,
5228 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230 if (**arg != NUL)
5231 ++*arg;
5232 break;
5233
5234 /*
5235 * nested expression: (expression).
5236 */
5237 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005238 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 if (**arg == ')')
5240 ++*arg;
5241 else if (ret == OK)
5242 {
5243 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005244 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 ret = FAIL;
5246 }
5247 break;
5248
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 break;
5251 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252
5253 if (ret == NOTDONE)
5254 {
5255 /*
5256 * Must be a variable or function name.
5257 * Can also be a curly-braces kind of name: {expr}.
5258 */
5259 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 if (alias != NULL)
5262 s = alias;
5263
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005264 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 ret = FAIL;
5266 else
5267 {
5268 if (**arg == '(') /* recursive! */
5269 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005270 partial_T *partial;
5271
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 /* If "s" is the name of a variable of type VAR_FUNC
5273 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005274 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275
5276 /* Invoke the function. */
5277 ret = get_func_tv(s, len, rettv, arg,
5278 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005279 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005280
5281 /* If evaluate is FALSE rettv->v_type was not set in
5282 * get_func_tv, but it's needed in handle_subscript() to parse
5283 * what follows. So set it here. */
5284 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5285 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005286 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005287 rettv->v_type = VAR_FUNC;
5288 }
5289
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 /* Stop the expression evaluation when immediately
5291 * aborting on error, or when an interrupt occurred or
5292 * an exception was thrown but not caught. */
5293 if (aborting())
5294 {
5295 if (ret == OK)
5296 clear_tv(rettv);
5297 ret = FAIL;
5298 }
5299 }
5300 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005301 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005302 else
5303 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005305 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 }
5307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 *arg = skipwhite(*arg);
5309
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005310 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5311 * expr(expr). */
5312 if (ret == OK)
5313 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
5315 /*
5316 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5317 */
5318 if (ret == OK && evaluate && end_leader > start_leader)
5319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321 int val = 0;
5322#ifdef FEAT_FLOAT
5323 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005325 if (rettv->v_type == VAR_FLOAT)
5326 f = rettv->vval.v_float;
5327 else
5328#endif
5329 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005330 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 clear_tv(rettv);
5333 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 else
5336 {
5337 while (end_leader > start_leader)
5338 {
5339 --end_leader;
5340 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005341 {
5342#ifdef FEAT_FLOAT
5343 if (rettv->v_type == VAR_FLOAT)
5344 f = !f;
5345 else
5346#endif
5347 val = !val;
5348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005350 {
5351#ifdef FEAT_FLOAT
5352 if (rettv->v_type == VAR_FLOAT)
5353 f = -f;
5354 else
5355#endif
5356 val = -val;
5357 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005358 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005359#ifdef FEAT_FLOAT
5360 if (rettv->v_type == VAR_FLOAT)
5361 {
5362 clear_tv(rettv);
5363 rettv->vval.v_float = f;
5364 }
5365 else
5366#endif
5367 {
5368 clear_tv(rettv);
5369 rettv->v_type = VAR_NUMBER;
5370 rettv->vval.v_number = val;
5371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
5374
5375 return ret;
5376}
5377
5378/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005379 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5380 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5382 */
5383 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005384eval_index(
5385 char_u **arg,
5386 typval_T *rettv,
5387 int evaluate,
5388 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389{
5390 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005391 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 long len = -1;
5394 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397
Bram Moolenaara03f2332016-02-06 18:09:59 +01005398 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005400 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005401 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005402 if (verbose)
5403 EMSG(_("E695: Cannot index a Funcref"));
5404 return FAIL;
5405 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005406#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005407 if (verbose)
5408 EMSG(_(e_float_as_string));
5409 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005410#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005411 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005412 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005413 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005414 if (verbose)
5415 EMSG(_("E909: Cannot index a special variable"));
5416 return FAIL;
5417 case VAR_UNKNOWN:
5418 if (evaluate)
5419 return FAIL;
5420 /* FALLTHROUGH */
5421
5422 case VAR_STRING:
5423 case VAR_NUMBER:
5424 case VAR_LIST:
5425 case VAR_DICT:
5426 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005427 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005429 init_tv(&var1);
5430 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005433 /*
5434 * dict.name
5435 */
5436 key = *arg + 1;
5437 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5438 ;
5439 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 }
5443 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 /*
5446 * something[idx]
5447 *
5448 * Get the (first) variable from inside the [].
5449 */
5450 *arg = skipwhite(*arg + 1);
5451 if (**arg == ':')
5452 empty1 = TRUE;
5453 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5454 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005455 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5456 {
5457 /* not a number or string */
5458 clear_tv(&var1);
5459 return FAIL;
5460 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461
5462 /*
5463 * Get the second variable from inside the [:].
5464 */
5465 if (**arg == ':')
5466 {
5467 range = TRUE;
5468 *arg = skipwhite(*arg + 1);
5469 if (**arg == ']')
5470 empty2 = TRUE;
5471 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005473 if (!empty1)
5474 clear_tv(&var1);
5475 return FAIL;
5476 }
5477 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5478 {
5479 /* not a number or string */
5480 if (!empty1)
5481 clear_tv(&var1);
5482 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005483 return FAIL;
5484 }
5485 }
5486
5487 /* Check for the ']'. */
5488 if (**arg != ']')
5489 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005490 if (verbose)
5491 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492 clear_tv(&var1);
5493 if (range)
5494 clear_tv(&var2);
5495 return FAIL;
5496 }
5497 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498 }
5499
5500 if (evaluate)
5501 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 n1 = 0;
5503 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 n1 = get_tv_number(&var1);
5506 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 }
5508 if (range)
5509 {
5510 if (empty2)
5511 n2 = -1;
5512 else
5513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514 n2 = get_tv_number(&var2);
5515 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 }
5517 }
5518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005521 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005522 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005523 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005524 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005525 case VAR_SPECIAL:
5526 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005527 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005528 break; /* not evaluating, skipping over subscript */
5529
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 case VAR_NUMBER:
5531 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 len = (long)STRLEN(s);
5534 if (range)
5535 {
5536 /* The resulting variable is a substring. If the indexes
5537 * are out of range the result is empty. */
5538 if (n1 < 0)
5539 {
5540 n1 = len + n1;
5541 if (n1 < 0)
5542 n1 = 0;
5543 }
5544 if (n2 < 0)
5545 n2 = len + n2;
5546 else if (n2 >= len)
5547 n2 = len;
5548 if (n1 >= len || n2 < 0 || n1 > n2)
5549 s = NULL;
5550 else
5551 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5552 }
5553 else
5554 {
5555 /* The resulting variable is a string of a single
5556 * character. If the index is too big or negative the
5557 * result is empty. */
5558 if (n1 >= len || n1 < 0)
5559 s = NULL;
5560 else
5561 s = vim_strnsave(s + n1, 1);
5562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 clear_tv(rettv);
5564 rettv->v_type = VAR_STRING;
5565 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 break;
5567
5568 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 if (n1 < 0)
5571 n1 = len + n1;
5572 if (!empty1 && (n1 < 0 || n1 >= len))
5573 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005574 /* For a range we allow invalid values and return an empty
5575 * list. A list index out of range is an error. */
5576 if (!range)
5577 {
5578 if (verbose)
5579 EMSGN(_(e_listidx), n1);
5580 return FAIL;
5581 }
5582 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583 }
5584 if (range)
5585 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 list_T *l;
5587 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588
5589 if (n2 < 0)
5590 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005591 else if (n2 >= len)
5592 n2 = len - 1;
5593 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005594 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 l = list_alloc();
5596 if (l == NULL)
5597 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 n1 <= n2; ++n1)
5600 {
5601 if (list_append_tv(l, &item->li_tv) == FAIL)
5602 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005603 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 return FAIL;
5605 }
5606 item = item->li_next;
5607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005608 clear_tv(rettv);
5609 rettv->v_type = VAR_LIST;
5610 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005611 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 }
5613 else
5614 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005615 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005616 clear_tv(rettv);
5617 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 }
5619 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005620
5621 case VAR_DICT:
5622 if (range)
5623 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005624 if (verbose)
5625 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 if (len == -1)
5627 clear_tv(&var1);
5628 return FAIL;
5629 }
5630 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005631 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005632
5633 if (len == -1)
5634 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005635 key = get_tv_string_chk(&var1);
5636 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638 clear_tv(&var1);
5639 return FAIL;
5640 }
5641 }
5642
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005643 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005645 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005646 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 if (len == -1)
5648 clear_tv(&var1);
5649 if (item == NULL)
5650 return FAIL;
5651
5652 copy_tv(&item->di_tv, &var1);
5653 clear_tv(rettv);
5654 *rettv = var1;
5655 }
5656 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005657 }
5658 }
5659
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005660 return OK;
5661}
5662
5663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 * Get an option value.
5665 * "arg" points to the '&' or '+' before the option name.
5666 * "arg" is advanced to character after the option name.
5667 * Return OK or FAIL.
5668 */
5669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005670get_option_tv(
5671 char_u **arg,
5672 typval_T *rettv, /* when NULL, only check if option exists */
5673 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
5675 char_u *option_end;
5676 long numval;
5677 char_u *stringval;
5678 int opt_type;
5679 int c;
5680 int working = (**arg == '+'); /* has("+option") */
5681 int ret = OK;
5682 int opt_flags;
5683
5684 /*
5685 * Isolate the option name and find its value.
5686 */
5687 option_end = find_option_end(arg, &opt_flags);
5688 if (option_end == NULL)
5689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 EMSG2(_("E112: Option name missing: %s"), *arg);
5692 return FAIL;
5693 }
5694
5695 if (!evaluate)
5696 {
5697 *arg = option_end;
5698 return OK;
5699 }
5700
5701 c = *option_end;
5702 *option_end = NUL;
5703 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705
5706 if (opt_type == -3) /* invalid name */
5707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005708 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 EMSG2(_("E113: Unknown option: %s"), *arg);
5710 ret = FAIL;
5711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005712 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
5714 if (opt_type == -2) /* hidden string option */
5715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005716 rettv->v_type = VAR_STRING;
5717 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
5719 else if (opt_type == -1) /* hidden number option */
5720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005721 rettv->v_type = VAR_NUMBER;
5722 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724 else if (opt_type == 1) /* number option */
5725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726 rettv->v_type = VAR_NUMBER;
5727 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729 else /* string option */
5730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005731 rettv->v_type = VAR_STRING;
5732 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734 }
5735 else if (working && (opt_type == -2 || opt_type == -1))
5736 ret = FAIL;
5737
5738 *option_end = c; /* put back for error messages */
5739 *arg = option_end;
5740
5741 return ret;
5742}
5743
5744/*
5745 * Allocate a variable for a string constant.
5746 * Return OK or FAIL.
5747 */
5748 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005749get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
5751 char_u *p;
5752 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 int extra = 0;
5754
5755 /*
5756 * Find the end of the string, skipping backslashed characters.
5757 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 {
5760 if (*p == '\\' && p[1] != NUL)
5761 {
5762 ++p;
5763 /* A "\<x>" form occupies at least 4 characters, and produces up
5764 * to 6 characters: reserve space for 2 extra */
5765 if (*p == '<')
5766 extra += 2;
5767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
5769
5770 if (*p != '"')
5771 {
5772 EMSG2(_("E114: Missing quote: %s"), *arg);
5773 return FAIL;
5774 }
5775
5776 /* If only parsing, set *arg and return here */
5777 if (!evaluate)
5778 {
5779 *arg = p + 1;
5780 return OK;
5781 }
5782
5783 /*
5784 * Copy the string into allocated memory, handling backslashed
5785 * characters.
5786 */
5787 name = alloc((unsigned)(p - *arg + extra));
5788 if (name == NULL)
5789 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 rettv->v_type = VAR_STRING;
5791 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
5795 if (*p == '\\')
5796 {
5797 switch (*++p)
5798 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 case 'b': *name++ = BS; ++p; break;
5800 case 'e': *name++ = ESC; ++p; break;
5801 case 'f': *name++ = FF; ++p; break;
5802 case 'n': *name++ = NL; ++p; break;
5803 case 'r': *name++ = CAR; ++p; break;
5804 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806 case 'X': /* hex: "\x1", "\x12" */
5807 case 'x':
5808 case 'u': /* Unicode: "\u0023" */
5809 case 'U':
5810 if (vim_isxdigit(p[1]))
5811 {
5812 int n, nr;
5813 int c = toupper(*p);
5814
5815 if (c == 'X')
5816 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005817 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005819 else
5820 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 nr = 0;
5822 while (--n >= 0 && vim_isxdigit(p[1]))
5823 {
5824 ++p;
5825 nr = (nr << 4) + hex2nr(*p);
5826 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828#ifdef FEAT_MBYTE
5829 /* For "\u" store the number according to
5830 * 'encoding'. */
5831 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 else
5834#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 break;
5838
5839 /* octal: "\1", "\12", "\123" */
5840 case '0':
5841 case '1':
5842 case '2':
5843 case '3':
5844 case '4':
5845 case '5':
5846 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 case '7': *name = *p++ - '0';
5848 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 *name = (*name << 3) + *p++ - '0';
5851 if (*p >= '0' && *p <= '7')
5852 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 break;
5856
5857 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 if (extra != 0)
5860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 break;
5863 }
5864 /* FALLTHROUGH */
5865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 break;
5868 }
5869 }
5870 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 *arg = p + 1;
5876
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 return OK;
5878}
5879
5880/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 * Return OK or FAIL.
5883 */
5884 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005885get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
5887 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 int reduce = 0;
5890
5891 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 */
5894 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5895 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 break;
5900 ++reduce;
5901 ++p;
5902 }
5903 }
5904
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 return FAIL;
5909 }
5910
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 if (!evaluate)
5913 {
5914 *arg = p + 1;
5915 return OK;
5916 }
5917
5918 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 */
5921 str = alloc((unsigned)((p - *arg) - reduce));
5922 if (str == NULL)
5923 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 rettv->v_type = VAR_STRING;
5925 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005932 break;
5933 ++p;
5934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005935 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005937 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005938 *arg = p + 1;
5939
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940 return OK;
5941}
5942
Bram Moolenaarddecc252016-04-06 22:59:37 +02005943 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005944partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005945{
5946 int i;
5947
5948 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005949 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005950 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005951 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005952 func_unref(pt->pt_name);
5953 vim_free(pt->pt_name);
5954 vim_free(pt);
5955}
5956
5957/*
5958 * Unreference a closure: decrement the reference count and free it when it
5959 * becomes zero.
5960 */
5961 void
5962partial_unref(partial_T *pt)
5963{
5964 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005966}
5967
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005968/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 * Allocate a variable for a List and fill it from "*arg".
5970 * Return OK or FAIL.
5971 */
5972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005973get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974{
Bram Moolenaar33570922005-01-25 22:26:29 +00005975 list_T *l = NULL;
5976 typval_T tv;
5977 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978
5979 if (evaluate)
5980 {
5981 l = list_alloc();
5982 if (l == NULL)
5983 return FAIL;
5984 }
5985
5986 *arg = skipwhite(*arg + 1);
5987 while (**arg != ']' && **arg != NUL)
5988 {
5989 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5990 goto failret;
5991 if (evaluate)
5992 {
5993 item = listitem_alloc();
5994 if (item != NULL)
5995 {
5996 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005997 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998 list_append(l, item);
5999 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006000 else
6001 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 }
6003
6004 if (**arg == ']')
6005 break;
6006 if (**arg != ',')
6007 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006008 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 goto failret;
6010 }
6011 *arg = skipwhite(*arg + 1);
6012 }
6013
6014 if (**arg != ']')
6015 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006016 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017failret:
6018 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006019 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020 return FAIL;
6021 }
6022
6023 *arg = skipwhite(*arg + 1);
6024 if (evaluate)
6025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006026 rettv->v_type = VAR_LIST;
6027 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 ++l->lv_refcount;
6029 }
6030
6031 return OK;
6032}
6033
6034/*
6035 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006036 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006038 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006039list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006041 list_T *l;
6042
6043 l = (list_T *)alloc_clear(sizeof(list_T));
6044 if (l != NULL)
6045 {
6046 /* Prepend the list to the list of lists for garbage collection. */
6047 if (first_list != NULL)
6048 first_list->lv_used_prev = l;
6049 l->lv_used_prev = NULL;
6050 l->lv_used_next = first_list;
6051 first_list = l;
6052 }
6053 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054}
6055
6056/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006057 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006058 * Returns OK or FAIL.
6059 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006060 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006061rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006062{
6063 list_T *l = list_alloc();
6064
6065 if (l == NULL)
6066 return FAIL;
6067
6068 rettv->vval.v_list = l;
6069 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006070 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006071 ++l->lv_refcount;
6072 return OK;
6073}
6074
6075/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 * Unreference a list: decrement the reference count and free it when it
6077 * becomes zero.
6078 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006082 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006083 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084}
6085
6086/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006087 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 * Ignores the reference count.
6089 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006090 static void
6091list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092{
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006095 for (item = l->lv_first; item != NULL; item = l->lv_first)
6096 {
6097 /* Remove the item before deleting it. */
6098 l->lv_first = item->li_next;
6099 clear_tv(&item->li_tv);
6100 vim_free(item);
6101 }
6102}
6103
6104 static void
6105list_free_list(list_T *l)
6106{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006107 /* Remove the list from the list of lists for garbage collection. */
6108 if (l->lv_used_prev == NULL)
6109 first_list = l->lv_used_next;
6110 else
6111 l->lv_used_prev->lv_used_next = l->lv_used_next;
6112 if (l->lv_used_next != NULL)
6113 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6114
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006115 vim_free(l);
6116}
6117
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006118 void
6119list_free(list_T *l)
6120{
6121 if (!in_free_unref_items)
6122 {
6123 list_free_contents(l);
6124 list_free_list(l);
6125 }
6126}
6127
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128/*
6129 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006130 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006131 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006132 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006133listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006134{
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136}
6137
6138/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006139 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006140 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006142listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006144 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 vim_free(item);
6146}
6147
6148/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006149 * Remove a list item from a List and free it. Also clears the value.
6150 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006152listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006153{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006154 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006155 listitem_free(item);
6156}
6157
6158/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 * Get the number of items in a list.
6160 */
6161 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006162list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 if (l == NULL)
6165 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006166 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006167}
6168
6169/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 * Return TRUE when two lists have exactly the same values.
6171 */
6172 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006173list_equal(
6174 list_T *l1,
6175 list_T *l2,
6176 int ic, /* ignore case for strings */
6177 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178{
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006181 if (l1 == NULL || l2 == NULL)
6182 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006183 if (l1 == l2)
6184 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006185 if (list_len(l1) != list_len(l2))
6186 return FALSE;
6187
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006188 for (item1 = l1->lv_first, item2 = l2->lv_first;
6189 item1 != NULL && item2 != NULL;
6190 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192 return FALSE;
6193 return item1 == NULL && item2 == NULL;
6194}
6195
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006196/*
6197 * Return the dictitem that an entry in a hashtable points to.
6198 */
6199 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006200dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006201{
6202 return HI2DI(hi);
6203}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006204
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006206 * Return TRUE when two dictionaries have exactly the same key/values.
6207 */
6208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006209dict_equal(
6210 dict_T *d1,
6211 dict_T *d2,
6212 int ic, /* ignore case for strings */
6213 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006214{
Bram Moolenaar33570922005-01-25 22:26:29 +00006215 hashitem_T *hi;
6216 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006217 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006218
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006219 if (d1 == NULL || d2 == NULL)
6220 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006221 if (d1 == d2)
6222 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006223 if (dict_len(d1) != dict_len(d2))
6224 return FALSE;
6225
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006226 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006227 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006228 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006229 if (!HASHITEM_EMPTY(hi))
6230 {
6231 item2 = dict_find(d2, hi->hi_key, -1);
6232 if (item2 == NULL)
6233 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006234 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006235 return FALSE;
6236 --todo;
6237 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006238 }
6239 return TRUE;
6240}
6241
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006242static int tv_equal_recurse_limit;
6243
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006244/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006246 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006247 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006248 */
6249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006250tv_equal(
6251 typval_T *tv1,
6252 typval_T *tv2,
6253 int ic, /* ignore case */
6254 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006255{
6256 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006257 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006258 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006259 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006260
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006261 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6262 if ((tv1->v_type == VAR_FUNC
6263 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6264 && (tv2->v_type == VAR_FUNC
6265 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6266 {
6267 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6268 : tv1->vval.v_partial->pt_name;
6269 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6270 : tv2->vval.v_partial->pt_name;
6271 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6272 }
6273
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006274 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006275 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006276
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006277 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006278 * recursiveness to a limit. We guess they are equal then.
6279 * A fixed limit has the problem of still taking an awful long time.
6280 * Reduce the limit every time running into it. That should work fine for
6281 * deeply linked structures that are not recursively linked and catch
6282 * recursiveness quickly. */
6283 if (!recursive)
6284 tv_equal_recurse_limit = 1000;
6285 if (recursive_cnt >= tv_equal_recurse_limit)
6286 {
6287 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006288 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006289 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006290
6291 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006292 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006293 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006294 ++recursive_cnt;
6295 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6296 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006297 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006298
6299 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006300 ++recursive_cnt;
6301 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6302 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006303 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006304
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006305 case VAR_NUMBER:
6306 return tv1->vval.v_number == tv2->vval.v_number;
6307
6308 case VAR_STRING:
6309 s1 = get_tv_string_buf(tv1, buf1);
6310 s2 = get_tv_string_buf(tv2, buf2);
6311 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006312
6313 case VAR_SPECIAL:
6314 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006315
6316 case VAR_FLOAT:
6317#ifdef FEAT_FLOAT
6318 return tv1->vval.v_float == tv2->vval.v_float;
6319#endif
6320 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006321#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006322 return tv1->vval.v_job == tv2->vval.v_job;
6323#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006324 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006325#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006326 return tv1->vval.v_channel == tv2->vval.v_channel;
6327#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006328 case VAR_FUNC:
6329 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006330 case VAR_UNKNOWN:
6331 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006332 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006333
Bram Moolenaara03f2332016-02-06 18:09:59 +01006334 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6335 * does not equal anything, not even itself. */
6336 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006337}
6338
6339/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340 * Locate item with index "n" in list "l" and return it.
6341 * A negative index is counted from the end; -1 is the last item.
6342 * Returns NULL when "n" is out of range.
6343 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006344 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006345list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346{
Bram Moolenaar33570922005-01-25 22:26:29 +00006347 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 long idx;
6349
6350 if (l == NULL)
6351 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006352
6353 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006355 n = l->lv_len + n;
6356
6357 /* Check for index out of range. */
6358 if (n < 0 || n >= l->lv_len)
6359 return NULL;
6360
6361 /* When there is a cached index may start search from there. */
6362 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006363 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006364 if (n < l->lv_idx / 2)
6365 {
6366 /* closest to the start of the list */
6367 item = l->lv_first;
6368 idx = 0;
6369 }
6370 else if (n > (l->lv_idx + l->lv_len) / 2)
6371 {
6372 /* closest to the end of the list */
6373 item = l->lv_last;
6374 idx = l->lv_len - 1;
6375 }
6376 else
6377 {
6378 /* closest to the cached index */
6379 item = l->lv_idx_item;
6380 idx = l->lv_idx;
6381 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382 }
6383 else
6384 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006385 if (n < l->lv_len / 2)
6386 {
6387 /* closest to the start of the list */
6388 item = l->lv_first;
6389 idx = 0;
6390 }
6391 else
6392 {
6393 /* closest to the end of the list */
6394 item = l->lv_last;
6395 idx = l->lv_len - 1;
6396 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006398
6399 while (n > idx)
6400 {
6401 /* search forward */
6402 item = item->li_next;
6403 ++idx;
6404 }
6405 while (n < idx)
6406 {
6407 /* search backward */
6408 item = item->li_prev;
6409 --idx;
6410 }
6411
6412 /* cache the used index */
6413 l->lv_idx = idx;
6414 l->lv_idx_item = item;
6415
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 return item;
6417}
6418
6419/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006420 * Get list item "l[idx]" as a number.
6421 */
6422 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006423list_find_nr(
6424 list_T *l,
6425 long idx,
6426 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006427{
6428 listitem_T *li;
6429
6430 li = list_find(l, idx);
6431 if (li == NULL)
6432 {
6433 if (errorp != NULL)
6434 *errorp = TRUE;
6435 return -1L;
6436 }
6437 return get_tv_number_chk(&li->li_tv, errorp);
6438}
6439
6440/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006441 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6442 */
6443 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006444list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006445{
6446 listitem_T *li;
6447
6448 li = list_find(l, idx - 1);
6449 if (li == NULL)
6450 {
6451 EMSGN(_(e_listidx), idx);
6452 return NULL;
6453 }
6454 return get_tv_string(&li->li_tv);
6455}
6456
6457/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006458 * Locate "item" list "l" and return its index.
6459 * Returns -1 when "item" is not in the list.
6460 */
6461 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006462list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006463{
6464 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006466
6467 if (l == NULL)
6468 return -1;
6469 idx = 0;
6470 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6471 ++idx;
6472 if (li == NULL)
6473 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006474 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006475}
6476
6477/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478 * Append item "item" to the end of list "l".
6479 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006481list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006482{
6483 if (l->lv_last == NULL)
6484 {
6485 /* empty list */
6486 l->lv_first = item;
6487 l->lv_last = item;
6488 item->li_prev = NULL;
6489 }
6490 else
6491 {
6492 l->lv_last->li_next = item;
6493 item->li_prev = l->lv_last;
6494 l->lv_last = item;
6495 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006496 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 item->li_next = NULL;
6498}
6499
6500/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006505list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006507 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508
Bram Moolenaar05159a02005-02-26 23:04:13 +00006509 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006511 copy_tv(tv, &li->li_tv);
6512 list_append(l, li);
6513 return OK;
6514}
6515
6516/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006517 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006518 * Return FAIL when out of memory.
6519 */
6520 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006521list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006522{
6523 listitem_T *li = listitem_alloc();
6524
6525 if (li == NULL)
6526 return FAIL;
6527 li->li_tv.v_type = VAR_DICT;
6528 li->li_tv.v_lock = 0;
6529 li->li_tv.vval.v_dict = dict;
6530 list_append(list, li);
6531 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 return OK;
6533}
6534
6535/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006536 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006537 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006538 * Returns FAIL when out of memory.
6539 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006541list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006542{
6543 listitem_T *li = listitem_alloc();
6544
6545 if (li == NULL)
6546 return FAIL;
6547 list_append(l, li);
6548 li->li_tv.v_type = VAR_STRING;
6549 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006550 if (str == NULL)
6551 li->li_tv.vval.v_string = NULL;
6552 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006553 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006554 return FAIL;
6555 return OK;
6556}
6557
6558/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006559 * Append "n" to list "l".
6560 * Returns FAIL when out of memory.
6561 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006563list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006564{
6565 listitem_T *li;
6566
6567 li = listitem_alloc();
6568 if (li == NULL)
6569 return FAIL;
6570 li->li_tv.v_type = VAR_NUMBER;
6571 li->li_tv.v_lock = 0;
6572 li->li_tv.vval.v_number = n;
6573 list_append(l, li);
6574 return OK;
6575}
6576
6577/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 * If "item" is NULL append at the end.
6580 * Return FAIL when out of memory.
6581 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006582 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006583list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584{
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586
6587 if (ni == NULL)
6588 return FAIL;
6589 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006590 list_insert(l, ni, item);
6591 return OK;
6592}
6593
6594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006595list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006596{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006597 if (item == NULL)
6598 /* Append new item at end of list. */
6599 list_append(l, ni);
6600 else
6601 {
6602 /* Insert new item before existing item. */
6603 ni->li_prev = item->li_prev;
6604 ni->li_next = item;
6605 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006606 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006607 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006608 ++l->lv_idx;
6609 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006610 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006611 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006612 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006613 l->lv_idx_item = NULL;
6614 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006615 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006616 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006617 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006618}
6619
6620/*
6621 * Extend "l1" with "l2".
6622 * If "bef" is NULL append at the end, otherwise insert before this item.
6623 * Returns FAIL when out of memory.
6624 */
6625 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006626list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006627{
Bram Moolenaar33570922005-01-25 22:26:29 +00006628 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006629 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006630
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006631 /* We also quit the loop when we have inserted the original item count of
6632 * the list, avoid a hang when we extend a list with itself. */
6633 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6635 return FAIL;
6636 return OK;
6637}
6638
6639/*
6640 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6641 * Return FAIL when out of memory.
6642 */
6643 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006644list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006645{
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006648 if (l1 == NULL || l2 == NULL)
6649 return FAIL;
6650
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006653 if (l == NULL)
6654 return FAIL;
6655 tv->v_type = VAR_LIST;
6656 tv->vval.v_list = l;
6657
6658 /* append all items from the second list */
6659 return list_extend(l, l2, NULL);
6660}
6661
6662/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006663 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006665 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666 * Returns NULL when out of memory.
6667 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006668 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006669list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670{
Bram Moolenaar33570922005-01-25 22:26:29 +00006671 list_T *copy;
6672 listitem_T *item;
6673 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674
6675 if (orig == NULL)
6676 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677
6678 copy = list_alloc();
6679 if (copy != NULL)
6680 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006681 if (copyID != 0)
6682 {
6683 /* Do this before adding the items, because one of the items may
6684 * refer back to this list. */
6685 orig->lv_copyID = copyID;
6686 orig->lv_copylist = copy;
6687 }
6688 for (item = orig->lv_first; item != NULL && !got_int;
6689 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690 {
6691 ni = listitem_alloc();
6692 if (ni == NULL)
6693 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006694 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006695 {
6696 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6697 {
6698 vim_free(ni);
6699 break;
6700 }
6701 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006702 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006703 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704 list_append(copy, ni);
6705 }
6706 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006707 if (item != NULL)
6708 {
6709 list_unref(copy);
6710 copy = NULL;
6711 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006712 }
6713
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006714 return copy;
6715}
6716
6717/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006718 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006719 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006720 * This used to be called list_remove, but that conflicts with a Sun header
6721 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006722 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006723 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006724vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006725{
Bram Moolenaar33570922005-01-25 22:26:29 +00006726 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006727
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006728 /* notify watchers */
6729 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006730 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006731 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732 list_fix_watch(l, ip);
6733 if (ip == item2)
6734 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006736
6737 if (item2->li_next == NULL)
6738 l->lv_last = item->li_prev;
6739 else
6740 item2->li_next->li_prev = item->li_prev;
6741 if (item->li_prev == NULL)
6742 l->lv_first = item2->li_next;
6743 else
6744 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006745 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006746}
6747
6748/*
6749 * Return an allocated string with the string representation of a list.
6750 * May return NULL.
6751 */
6752 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006753list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006754{
6755 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006756
6757 if (tv->vval.v_list == NULL)
6758 return NULL;
6759 ga_init2(&ga, (int)sizeof(char), 80);
6760 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006761 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006762 {
6763 vim_free(ga.ga_data);
6764 return NULL;
6765 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006766 ga_append(&ga, ']');
6767 ga_append(&ga, NUL);
6768 return (char_u *)ga.ga_data;
6769}
6770
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006771typedef struct join_S {
6772 char_u *s;
6773 char_u *tofree;
6774} join_T;
6775
6776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006777list_join_inner(
6778 garray_T *gap, /* to store the result in */
6779 list_T *l,
6780 char_u *sep,
6781 int echo_style,
6782 int copyID,
6783 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006784{
6785 int i;
6786 join_T *p;
6787 int len;
6788 int sumlen = 0;
6789 int first = TRUE;
6790 char_u *tofree;
6791 char_u numbuf[NUMBUFLEN];
6792 listitem_T *item;
6793 char_u *s;
6794
6795 /* Stringify each item in the list. */
6796 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6797 {
6798 if (echo_style)
6799 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6800 else
6801 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6802 if (s == NULL)
6803 return FAIL;
6804
6805 len = (int)STRLEN(s);
6806 sumlen += len;
6807
Bram Moolenaarcde88542015-08-11 19:14:00 +02006808 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006809 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6810 if (tofree != NULL || s != numbuf)
6811 {
6812 p->s = s;
6813 p->tofree = tofree;
6814 }
6815 else
6816 {
6817 p->s = vim_strnsave(s, len);
6818 p->tofree = p->s;
6819 }
6820
6821 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006822 if (did_echo_string_emsg) /* recursion error, bail out */
6823 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006824 }
6825
6826 /* Allocate result buffer with its total size, avoid re-allocation and
6827 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6828 if (join_gap->ga_len >= 2)
6829 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6830 if (ga_grow(gap, sumlen + 2) == FAIL)
6831 return FAIL;
6832
6833 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6834 {
6835 if (first)
6836 first = FALSE;
6837 else
6838 ga_concat(gap, sep);
6839 p = ((join_T *)join_gap->ga_data) + i;
6840
6841 if (p->s != NULL)
6842 ga_concat(gap, p->s);
6843 line_breakcheck();
6844 }
6845
6846 return OK;
6847}
6848
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006849/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006850 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006851 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006852 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006853 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006855list_join(
6856 garray_T *gap,
6857 list_T *l,
6858 char_u *sep,
6859 int echo_style,
6860 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006861{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006862 garray_T join_ga;
6863 int retval;
6864 join_T *p;
6865 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006866
Bram Moolenaard39a7512015-04-16 22:51:22 +02006867 if (l->lv_len < 1)
6868 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006869 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6870 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6871
6872 /* Dispose each item in join_ga. */
6873 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006874 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006875 p = (join_T *)join_ga.ga_data;
6876 for (i = 0; i < join_ga.ga_len; ++i)
6877 {
6878 vim_free(p->tofree);
6879 ++p;
6880 }
6881 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006882 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006883
6884 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006885}
6886
6887/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006888 * Return the next (unique) copy ID.
6889 * Used for serializing nested structures.
6890 */
6891 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006892get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006893{
6894 current_copyID += COPYID_INC;
6895 return current_copyID;
6896}
6897
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006898/* Used by get_func_tv() */
6899static garray_T funcargs = GA_EMPTY;
6900
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006901/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 * Garbage collection for lists and dictionaries.
6903 *
6904 * We use reference counts to be able to free most items right away when they
6905 * are no longer used. But for composite items it's possible that it becomes
6906 * unused while the reference count is > 0: When there is a recursive
6907 * reference. Example:
6908 * :let l = [1, 2, 3]
6909 * :let d = {9: l}
6910 * :let l[1] = d
6911 *
6912 * Since this is quite unusual we handle this with garbage collection: every
6913 * once in a while find out which lists and dicts are not referenced from any
6914 * variable.
6915 *
6916 * Here is a good reference text about garbage collection (refers to Python
6917 * but it applies to all reference-counting mechanisms):
6918 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006919 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006920
6921/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922 * Do garbage collection for lists and dicts.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006923 * When "testing" is TRUE this is called from garbagecollect_for_testing().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006925 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006927garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006928{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006929 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931 buf_T *buf;
6932 win_T *wp;
6933 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006934 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006935 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006936 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006937#ifdef FEAT_WINDOWS
6938 tabpage_T *tp;
6939#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006941 if (!testing)
6942 {
6943 /* Only do this once. */
6944 want_garbage_collect = FALSE;
6945 may_garbage_collect = FALSE;
6946 garbage_collect_at_exit = FALSE;
6947 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006948
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949 /* We advance by two because we add one for items referenced through
6950 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006951 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953 /*
6954 * 1. Go through all accessible variables and mark all lists and dicts
6955 * with copyID.
6956 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957
6958 /* Don't free variables in the previous_funccal list unless they are only
6959 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006960 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6962 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006963 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6964 NULL);
6965 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6966 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 }
6968
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 /* script-local variables */
6970 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006971 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972
6973 /* buffer-local variables */
6974 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006975 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6976 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977
6978 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006979 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006980 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6981 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006982#ifdef FEAT_AUTOCMD
6983 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006984 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6985 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006986#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006988#ifdef FEAT_WINDOWS
6989 /* tabpage-local variables */
6990 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006991 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6992 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006993#endif
6994
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997
6998 /* function-local variables */
6999 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7000 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007001 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7002 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 }
7004
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007005 /* function call arguments, if v:testing is set. */
7006 for (i = 0; i < funcargs.ga_len; ++i)
7007 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7008 copyID, NULL, NULL);
7009
Bram Moolenaard812df62008-11-09 12:46:09 +00007010 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007011 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007012
Bram Moolenaar1dced572012-04-05 16:54:08 +02007013#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007014 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007015#endif
7016
Bram Moolenaardb913952012-06-29 12:54:53 +02007017#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007018 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007019#endif
7020
7021#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007022 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007023#endif
7024
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007025#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007026 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007027#endif
7028
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007030 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 /*
7032 * 2. Free lists and dictionaries that are not referenced.
7033 */
7034 did_free = free_unref_items(copyID);
7035
7036 /*
7037 * 3. Check if any funccal can be freed now.
7038 */
7039 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007040 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 if (can_free_funccal(*pfc, copyID))
7042 {
7043 fc = *pfc;
7044 *pfc = fc->caller;
7045 free_funccal(fc, TRUE);
7046 did_free = TRUE;
7047 did_free_funccal = TRUE;
7048 }
7049 else
7050 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007051 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 if (did_free_funccal)
7053 /* When a funccal was freed some more items might be garbage
7054 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007055 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007056 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057 else if (p_verbose > 0)
7058 {
7059 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7060 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007061
7062 return did_free;
7063}
7064
7065/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007066 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007067 */
7068 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007069free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007070{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007071 dict_T *dd, *dd_next;
7072 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007073 int did_free = FALSE;
7074
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007075 /* Let all "free" functions know that we are here. This means no
7076 * dictionaries, lists, channels or jobs are to be freed, because we will
7077 * do that here. */
7078 in_free_unref_items = TRUE;
7079
7080 /*
7081 * PASS 1: free the contents of the items. We don't free the items
7082 * themselves yet, so that it is possible to decrement refcount counters
7083 */
7084
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007086 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007088 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007089 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007091 /* Free the Dictionary and ordinary items it contains, but don't
7092 * recurse into Lists and Dictionaries, they will be in the list
7093 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007094 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007097
7098 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007099 * Go through the list of lists and free items without the copyID.
7100 * But don't free a list that has a watcher (used in a for loop), these
7101 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007103 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7104 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7105 && ll->lv_watch == NULL)
7106 {
7107 /* Free the List and ordinary items it contains, but don't recurse
7108 * into Lists and Dictionaries, they will be in the list of dicts
7109 * or list of lists. */
7110 list_free_contents(ll);
7111 did_free = TRUE;
7112 }
7113
7114#ifdef FEAT_JOB_CHANNEL
7115 /* Go through the list of jobs and free items without the copyID. This
7116 * must happen before doing channels, because jobs refer to channels, but
7117 * the reference from the channel to the job isn't tracked. */
7118 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7119
7120 /* Go through the list of channels and free items without the copyID. */
7121 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7122#endif
7123
7124 /*
7125 * PASS 2: free the items themselves.
7126 */
7127 for (dd = first_dict; dd != NULL; dd = dd_next)
7128 {
7129 dd_next = dd->dv_used_next;
7130 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7131 dict_free_dict(dd);
7132 }
7133
7134 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007135 {
7136 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007137 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7138 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007139 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007140 /* Free the List and ordinary items it contains, but don't recurse
7141 * into Lists and Dictionaries, they will be in the list of dicts
7142 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007143 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007144 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007145 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007146
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007147#ifdef FEAT_JOB_CHANNEL
7148 /* Go through the list of jobs and free items without the copyID. This
7149 * must happen before doing channels, because jobs refer to channels, but
7150 * the reference from the channel to the job isn't tracked. */
7151 free_unused_jobs(copyID, COPYID_MASK);
7152
7153 /* Go through the list of channels and free items without the copyID. */
7154 free_unused_channels(copyID, COPYID_MASK);
7155#endif
7156
7157 in_free_unref_items = FALSE;
7158
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007159 return did_free;
7160}
7161
7162/*
7163 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007164 * "list_stack" is used to add lists to be marked. Can be NULL.
7165 *
7166 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007167 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007168 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007169set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007170{
7171 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007172 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007173 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007174 hashtab_T *cur_ht;
7175 ht_stack_T *ht_stack = NULL;
7176 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007177
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007178 cur_ht = ht;
7179 for (;;)
7180 {
7181 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007182 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007183 /* Mark each item in the hashtab. If the item contains a hashtab
7184 * it is added to ht_stack, if it contains a list it is added to
7185 * list_stack. */
7186 todo = (int)cur_ht->ht_used;
7187 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7188 if (!HASHITEM_EMPTY(hi))
7189 {
7190 --todo;
7191 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7192 &ht_stack, list_stack);
7193 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007194 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007195
7196 if (ht_stack == NULL)
7197 break;
7198
7199 /* take an item from the stack */
7200 cur_ht = ht_stack->ht;
7201 tempitem = ht_stack;
7202 ht_stack = ht_stack->prev;
7203 free(tempitem);
7204 }
7205
7206 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007207}
7208
7209/*
7210 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007211 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7212 *
7213 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007214 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007215 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007216set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007218 listitem_T *li;
7219 int abort = FALSE;
7220 list_T *cur_l;
7221 list_stack_T *list_stack = NULL;
7222 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007223
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007224 cur_l = l;
7225 for (;;)
7226 {
7227 if (!abort)
7228 /* Mark each item in the list. If the item contains a hashtab
7229 * it is added to ht_stack, if it contains a list it is added to
7230 * list_stack. */
7231 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7232 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7233 ht_stack, &list_stack);
7234 if (list_stack == NULL)
7235 break;
7236
7237 /* take an item from the stack */
7238 cur_l = list_stack->list;
7239 tempitem = list_stack;
7240 list_stack = list_stack->prev;
7241 free(tempitem);
7242 }
7243
7244 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007245}
7246
7247/*
7248 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007249 * "list_stack" is used to add lists to be marked. Can be NULL.
7250 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7251 *
7252 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007253 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007254 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007255set_ref_in_item(
7256 typval_T *tv,
7257 int copyID,
7258 ht_stack_T **ht_stack,
7259 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007260{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007261 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007262
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007263 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007264 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007265 dict_T *dd = tv->vval.v_dict;
7266
Bram Moolenaara03f2332016-02-06 18:09:59 +01007267 if (dd != NULL && dd->dv_copyID != copyID)
7268 {
7269 /* Didn't see this dict yet. */
7270 dd->dv_copyID = copyID;
7271 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007272 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007273 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7274 }
7275 else
7276 {
7277 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7278 if (newitem == NULL)
7279 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007280 else
7281 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007282 newitem->ht = &dd->dv_hashtab;
7283 newitem->prev = *ht_stack;
7284 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007285 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007286 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007287 }
7288 }
7289 else if (tv->v_type == VAR_LIST)
7290 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007291 list_T *ll = tv->vval.v_list;
7292
Bram Moolenaara03f2332016-02-06 18:09:59 +01007293 if (ll != NULL && ll->lv_copyID != copyID)
7294 {
7295 /* Didn't see this list yet. */
7296 ll->lv_copyID = copyID;
7297 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007298 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007299 abort = set_ref_in_list(ll, copyID, ht_stack);
7300 }
7301 else
7302 {
7303 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007304 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007305 if (newitem == NULL)
7306 abort = TRUE;
7307 else
7308 {
7309 newitem->list = ll;
7310 newitem->prev = *list_stack;
7311 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007312 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007313 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007314 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007315 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007316 else if (tv->v_type == VAR_PARTIAL)
7317 {
7318 partial_T *pt = tv->vval.v_partial;
7319 int i;
7320
7321 /* A partial does not have a copyID, because it cannot contain itself.
7322 */
7323 if (pt != NULL)
7324 {
7325 if (pt->pt_dict != NULL)
7326 {
7327 typval_T dtv;
7328
7329 dtv.v_type = VAR_DICT;
7330 dtv.vval.v_dict = pt->pt_dict;
7331 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7332 }
7333
7334 for (i = 0; i < pt->pt_argc; ++i)
7335 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7336 ht_stack, list_stack);
7337 }
7338 }
7339#ifdef FEAT_JOB_CHANNEL
7340 else if (tv->v_type == VAR_JOB)
7341 {
7342 job_T *job = tv->vval.v_job;
7343 typval_T dtv;
7344
7345 if (job != NULL && job->jv_copyID != copyID)
7346 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007347 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007348 if (job->jv_channel != NULL)
7349 {
7350 dtv.v_type = VAR_CHANNEL;
7351 dtv.vval.v_channel = job->jv_channel;
7352 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7353 }
7354 if (job->jv_exit_partial != NULL)
7355 {
7356 dtv.v_type = VAR_PARTIAL;
7357 dtv.vval.v_partial = job->jv_exit_partial;
7358 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7359 }
7360 }
7361 }
7362 else if (tv->v_type == VAR_CHANNEL)
7363 {
7364 channel_T *ch =tv->vval.v_channel;
7365 int part;
7366 typval_T dtv;
7367 jsonq_T *jq;
7368 cbq_T *cq;
7369
7370 if (ch != NULL && ch->ch_copyID != copyID)
7371 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007372 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007373 for (part = PART_SOCK; part <= PART_IN; ++part)
7374 {
7375 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7376 jq = jq->jq_next)
7377 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7378 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7379 cq = cq->cq_next)
7380 if (cq->cq_partial != NULL)
7381 {
7382 dtv.v_type = VAR_PARTIAL;
7383 dtv.vval.v_partial = cq->cq_partial;
7384 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7385 }
7386 if (ch->ch_part[part].ch_partial != NULL)
7387 {
7388 dtv.v_type = VAR_PARTIAL;
7389 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7390 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7391 }
7392 }
7393 if (ch->ch_partial != NULL)
7394 {
7395 dtv.v_type = VAR_PARTIAL;
7396 dtv.vval.v_partial = ch->ch_partial;
7397 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7398 }
7399 if (ch->ch_close_partial != NULL)
7400 {
7401 dtv.v_type = VAR_PARTIAL;
7402 dtv.vval.v_partial = ch->ch_close_partial;
7403 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7404 }
7405 }
7406 }
7407#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007408 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007409}
7410
7411/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 * Allocate an empty header for a dictionary.
7413 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007414 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007415dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416{
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007418
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007420 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007421 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007422 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007423 if (first_dict != NULL)
7424 first_dict->dv_used_prev = d;
7425 d->dv_used_next = first_dict;
7426 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007427 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007428
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007430 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007431 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007432 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007433 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007434 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007435 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436}
7437
7438/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007439 * Allocate an empty dict for a return value.
7440 * Returns OK or FAIL.
7441 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007442 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007443rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007444{
7445 dict_T *d = dict_alloc();
7446
7447 if (d == NULL)
7448 return FAIL;
7449
7450 rettv->vval.v_dict = d;
7451 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007452 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007453 ++d->dv_refcount;
7454 return OK;
7455}
7456
7457
7458/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007459 * Unreference a Dictionary: decrement the reference count and free it when it
7460 * becomes zero.
7461 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007462 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007463dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007465 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007466 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467}
7468
7469/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007470 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007471 * Ignores the reference count.
7472 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007473 static void
7474dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007476 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007477 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007478 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007480 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007481 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007482 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 if (!HASHITEM_EMPTY(hi))
7486 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007487 /* Remove the item before deleting it, just in case there is
7488 * something recursive causing trouble. */
7489 di = HI2DI(hi);
7490 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007491 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007492 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007493 --todo;
7494 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007497}
7498
7499 static void
7500dict_free_dict(dict_T *d)
7501{
7502 /* Remove the dict from the list of dicts for garbage collection. */
7503 if (d->dv_used_prev == NULL)
7504 first_dict = d->dv_used_next;
7505 else
7506 d->dv_used_prev->dv_used_next = d->dv_used_next;
7507 if (d->dv_used_next != NULL)
7508 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509 vim_free(d);
7510}
7511
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007512 void
7513dict_free(dict_T *d)
7514{
7515 if (!in_free_unref_items)
7516 {
7517 dict_free_contents(d);
7518 dict_free_dict(d);
7519 }
7520}
7521
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522/*
7523 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 * The "key" is copied to the new item.
7525 * Note that the value of the item "di_tv" still needs to be initialized!
7526 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007528 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007529dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530{
Bram Moolenaar33570922005-01-25 22:26:29 +00007531 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007533 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007534 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007535 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007536 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007537 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007538 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540}
7541
7542/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007543 * Make a copy of a Dictionary item.
7544 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007545 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007546dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007547{
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007549
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007550 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7551 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 if (di != NULL)
7553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007555 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007556 copy_tv(&org->di_tv, &di->di_tv);
7557 }
7558 return di;
7559}
7560
7561/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 * Remove item "item" from Dictionary "dict" and free it.
7563 */
7564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007565dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566{
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007568
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007570 if (HASHITEM_EMPTY(hi))
7571 EMSG2(_(e_intern2), "dictitem_remove()");
7572 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007573 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007574 dictitem_free(item);
7575}
7576
7577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 * Free a dict item. Also clears the value.
7579 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007580 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007581dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007584 if (item->di_flags & DI_FLAGS_ALLOC)
7585 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586}
7587
7588/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7590 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007591 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007592 * Returns NULL when out of memory.
7593 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007595dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007596{
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 dict_T *copy;
7598 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007599 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007600 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007601
7602 if (orig == NULL)
7603 return NULL;
7604
7605 copy = dict_alloc();
7606 if (copy != NULL)
7607 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007608 if (copyID != 0)
7609 {
7610 orig->dv_copyID = copyID;
7611 orig->dv_copydict = copy;
7612 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007613 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007614 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007616 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007618 --todo;
7619
7620 di = dictitem_alloc(hi->hi_key);
7621 if (di == NULL)
7622 break;
7623 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007624 {
7625 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7626 copyID) == FAIL)
7627 {
7628 vim_free(di);
7629 break;
7630 }
7631 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007632 else
7633 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7634 if (dict_add(copy, di) == FAIL)
7635 {
7636 dictitem_free(di);
7637 break;
7638 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007641
Bram Moolenaare9a41262005-01-15 22:18:47 +00007642 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007643 if (todo > 0)
7644 {
7645 dict_unref(copy);
7646 copy = NULL;
7647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 }
7649
7650 return copy;
7651}
7652
7653/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007655 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007657 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007658dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659{
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661}
7662
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007664 * Add a number or string entry to dictionary "d".
7665 * When "str" is NULL use number "nr", otherwise use "str".
7666 * Returns FAIL when out of memory and when key already exists.
7667 */
7668 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007669dict_add_nr_str(
7670 dict_T *d,
7671 char *key,
7672 long nr,
7673 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007674{
7675 dictitem_T *item;
7676
7677 item = dictitem_alloc((char_u *)key);
7678 if (item == NULL)
7679 return FAIL;
7680 item->di_tv.v_lock = 0;
7681 if (str == NULL)
7682 {
7683 item->di_tv.v_type = VAR_NUMBER;
7684 item->di_tv.vval.v_number = nr;
7685 }
7686 else
7687 {
7688 item->di_tv.v_type = VAR_STRING;
7689 item->di_tv.vval.v_string = vim_strsave(str);
7690 }
7691 if (dict_add(d, item) == FAIL)
7692 {
7693 dictitem_free(item);
7694 return FAIL;
7695 }
7696 return OK;
7697}
7698
7699/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007700 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007701 * Returns FAIL when out of memory and when key already exists.
7702 */
7703 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007704dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007705{
7706 dictitem_T *item;
7707
7708 item = dictitem_alloc((char_u *)key);
7709 if (item == NULL)
7710 return FAIL;
7711 item->di_tv.v_lock = 0;
7712 item->di_tv.v_type = VAR_LIST;
7713 item->di_tv.vval.v_list = list;
7714 if (dict_add(d, item) == FAIL)
7715 {
7716 dictitem_free(item);
7717 return FAIL;
7718 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007719 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007720 return OK;
7721}
7722
7723/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007724 * Get the number of items in a Dictionary.
7725 */
7726 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007727dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007728{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007729 if (d == NULL)
7730 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007731 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007732}
7733
7734/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 * Find item "key[len]" in Dictionary "d".
7736 * If "len" is negative use strlen(key).
7737 * Returns NULL when not found.
7738 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007739 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007740dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007741{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007742#define AKEYLEN 200
7743 char_u buf[AKEYLEN];
7744 char_u *akey;
7745 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007746 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007748 if (len < 0)
7749 akey = key;
7750 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007751 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007752 tofree = akey = vim_strnsave(key, len);
7753 if (akey == NULL)
7754 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007755 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007756 else
7757 {
7758 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007759 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007760 akey = buf;
7761 }
7762
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007764 vim_free(tofree);
7765 if (HASHITEM_EMPTY(hi))
7766 return NULL;
7767 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007768}
7769
7770/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007771 * Get a string item from a dictionary.
7772 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007773 * Returns NULL if the entry doesn't exist or out of memory.
7774 */
7775 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007776get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007777{
7778 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007779 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007780
7781 di = dict_find(d, key, -1);
7782 if (di == NULL)
7783 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007784 s = get_tv_string(&di->di_tv);
7785 if (save && s != NULL)
7786 s = vim_strsave(s);
7787 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007788}
7789
7790/*
7791 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007792 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007793 */
7794 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007795get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007796{
7797 dictitem_T *di;
7798
7799 di = dict_find(d, key, -1);
7800 if (di == NULL)
7801 return 0;
7802 return get_tv_number(&di->di_tv);
7803}
7804
7805/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007806 * Return an allocated string with the string representation of a Dictionary.
7807 * May return NULL.
7808 */
7809 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007810dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007811{
7812 garray_T ga;
7813 int first = TRUE;
7814 char_u *tofree;
7815 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007816 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007817 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007818 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007819 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007820
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007821 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007822 return NULL;
7823 ga_init2(&ga, (int)sizeof(char), 80);
7824 ga_append(&ga, '{');
7825
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007826 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007827 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007828 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007829 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007830 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007831 --todo;
7832
7833 if (first)
7834 first = FALSE;
7835 else
7836 ga_concat(&ga, (char_u *)", ");
7837
7838 tofree = string_quote(hi->hi_key, FALSE);
7839 if (tofree != NULL)
7840 {
7841 ga_concat(&ga, tofree);
7842 vim_free(tofree);
7843 }
7844 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007845 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007846 if (s != NULL)
7847 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007848 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007849 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007850 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007851 line_breakcheck();
7852
Bram Moolenaar8c711452005-01-14 21:53:12 +00007853 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007854 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007855 if (todo > 0)
7856 {
7857 vim_free(ga.ga_data);
7858 return NULL;
7859 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007860
7861 ga_append(&ga, '}');
7862 ga_append(&ga, NUL);
7863 return (char_u *)ga.ga_data;
7864}
7865
7866/*
7867 * Allocate a variable for a Dictionary and fill it from "*arg".
7868 * Return OK or FAIL. Returns NOTDONE for {expr}.
7869 */
7870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007871get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007872{
Bram Moolenaar33570922005-01-25 22:26:29 +00007873 dict_T *d = NULL;
7874 typval_T tvkey;
7875 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007876 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007877 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007878 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007879 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007880
7881 /*
7882 * First check if it's not a curly-braces thing: {expr}.
7883 * Must do this without evaluating, otherwise a function may be called
7884 * twice. Unfortunately this means we need to call eval1() twice for the
7885 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007886 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007887 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888 if (*start != '}')
7889 {
7890 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7891 return FAIL;
7892 if (*start == '}')
7893 return NOTDONE;
7894 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007895
7896 if (evaluate)
7897 {
7898 d = dict_alloc();
7899 if (d == NULL)
7900 return FAIL;
7901 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007902 tvkey.v_type = VAR_UNKNOWN;
7903 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007904
7905 *arg = skipwhite(*arg + 1);
7906 while (**arg != '}' && **arg != NUL)
7907 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007908 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007909 goto failret;
7910 if (**arg != ':')
7911 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007912 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007913 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007914 goto failret;
7915 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007916 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007917 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007918 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007919 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007920 {
7921 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007922 clear_tv(&tvkey);
7923 goto failret;
7924 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007925 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007926
7927 *arg = skipwhite(*arg + 1);
7928 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7929 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007930 if (evaluate)
7931 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 goto failret;
7933 }
7934 if (evaluate)
7935 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007936 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007937 if (item != NULL)
7938 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007939 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007940 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 clear_tv(&tv);
7942 goto failret;
7943 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007944 item = dictitem_alloc(key);
7945 clear_tv(&tvkey);
7946 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007949 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007950 if (dict_add(d, item) == FAIL)
7951 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007952 }
7953 }
7954
7955 if (**arg == '}')
7956 break;
7957 if (**arg != ',')
7958 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007959 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007960 goto failret;
7961 }
7962 *arg = skipwhite(*arg + 1);
7963 }
7964
7965 if (**arg != '}')
7966 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007967 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007968failret:
7969 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007970 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 return FAIL;
7972 }
7973
7974 *arg = skipwhite(*arg + 1);
7975 if (evaluate)
7976 {
7977 rettv->v_type = VAR_DICT;
7978 rettv->vval.v_dict = d;
7979 ++d->dv_refcount;
7980 }
7981
7982 return OK;
7983}
7984
Bram Moolenaar17a13432016-01-24 14:22:10 +01007985 static char *
7986get_var_special_name(int nr)
7987{
7988 switch (nr)
7989 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007990 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007991 case VVAL_TRUE: return "v:true";
7992 case VVAL_NONE: return "v:none";
7993 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007994 }
7995 EMSG2(_(e_intern2), "get_var_special_name()");
7996 return "42";
7997}
7998
Bram Moolenaar8c711452005-01-14 21:53:12 +00007999/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008000 * Return a string with the string representation of a variable.
8001 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008002 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008003 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008004 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008005 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008006 */
8007 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008008echo_string(
8009 typval_T *tv,
8010 char_u **tofree,
8011 char_u *numbuf,
8012 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008013{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 static int recurse = 0;
8015 char_u *r = NULL;
8016
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008018 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008019 if (!did_echo_string_emsg)
8020 {
8021 /* Only give this message once for a recursive call to avoid
8022 * flooding the user with errors. And stop iterating over lists
8023 * and dicts. */
8024 did_echo_string_emsg = TRUE;
8025 EMSG(_("E724: variable nested too deep for displaying"));
8026 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008027 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008028 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008029 }
8030 ++recurse;
8031
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008032 switch (tv->v_type)
8033 {
8034 case VAR_FUNC:
8035 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008036 r = tv->vval.v_string;
8037 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008038
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008039 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008040 {
8041 partial_T *pt = tv->vval.v_partial;
8042 char_u *fname = string_quote(pt == NULL ? NULL
8043 : pt->pt_name, FALSE);
8044 garray_T ga;
8045 int i;
8046 char_u *tf;
8047
8048 ga_init2(&ga, 1, 100);
8049 ga_concat(&ga, (char_u *)"function(");
8050 if (fname != NULL)
8051 {
8052 ga_concat(&ga, fname);
8053 vim_free(fname);
8054 }
8055 if (pt != NULL && pt->pt_argc > 0)
8056 {
8057 ga_concat(&ga, (char_u *)", [");
8058 for (i = 0; i < pt->pt_argc; ++i)
8059 {
8060 if (i > 0)
8061 ga_concat(&ga, (char_u *)", ");
8062 ga_concat(&ga,
8063 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8064 vim_free(tf);
8065 }
8066 ga_concat(&ga, (char_u *)"]");
8067 }
8068 if (pt != NULL && pt->pt_dict != NULL)
8069 {
8070 typval_T dtv;
8071
8072 ga_concat(&ga, (char_u *)", ");
8073 dtv.v_type = VAR_DICT;
8074 dtv.vval.v_dict = pt->pt_dict;
8075 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8076 vim_free(tf);
8077 }
8078 ga_concat(&ga, (char_u *)")");
8079
8080 *tofree = ga.ga_data;
8081 r = *tofree;
8082 break;
8083 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008084
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008085 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008086 if (tv->vval.v_list == NULL)
8087 {
8088 *tofree = NULL;
8089 r = NULL;
8090 }
8091 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8092 {
8093 *tofree = NULL;
8094 r = (char_u *)"[...]";
8095 }
8096 else
8097 {
8098 tv->vval.v_list->lv_copyID = copyID;
8099 *tofree = list2string(tv, copyID);
8100 r = *tofree;
8101 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008102 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008103
Bram Moolenaar8c711452005-01-14 21:53:12 +00008104 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008105 if (tv->vval.v_dict == NULL)
8106 {
8107 *tofree = NULL;
8108 r = NULL;
8109 }
8110 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8111 {
8112 *tofree = NULL;
8113 r = (char_u *)"{...}";
8114 }
8115 else
8116 {
8117 tv->vval.v_dict->dv_copyID = copyID;
8118 *tofree = dict2string(tv, copyID);
8119 r = *tofree;
8120 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008121 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008122
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008123 case VAR_STRING:
8124 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008125 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008126 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008127 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008128 *tofree = NULL;
8129 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008130 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008131
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008132 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008133#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008134 *tofree = NULL;
8135 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8136 r = numbuf;
8137 break;
8138#endif
8139
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008140 case VAR_SPECIAL:
8141 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008142 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008143 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008144 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008145
Bram Moolenaar8502c702014-06-17 12:51:16 +02008146 if (--recurse == 0)
8147 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008148 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008149}
8150
8151/*
8152 * Return a string with the string representation of a variable.
8153 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8154 * "numbuf" is used for a number.
8155 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008156 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008157 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008158 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008159tv2string(
8160 typval_T *tv,
8161 char_u **tofree,
8162 char_u *numbuf,
8163 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008164{
8165 switch (tv->v_type)
8166 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008167 case VAR_FUNC:
8168 *tofree = string_quote(tv->vval.v_string, TRUE);
8169 return *tofree;
8170 case VAR_STRING:
8171 *tofree = string_quote(tv->vval.v_string, FALSE);
8172 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008173 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008174#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008175 *tofree = NULL;
8176 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8177 return numbuf;
8178#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008179 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008180 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008181 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008182 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008183 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008184 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008185 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008186 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008187 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008188 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008189 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008190}
8191
8192/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008193 * Return string "str" in ' quotes, doubling ' characters.
8194 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008195 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008196 */
8197 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008198string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008199{
Bram Moolenaar33570922005-01-25 22:26:29 +00008200 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008201 char_u *p, *r, *s;
8202
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 len = (function ? 13 : 3);
8204 if (str != NULL)
8205 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008206 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008207 for (p = str; *p != NUL; mb_ptr_adv(p))
8208 if (*p == '\'')
8209 ++len;
8210 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008211 s = r = alloc(len);
8212 if (r != NULL)
8213 {
8214 if (function)
8215 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008216 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008217 r += 10;
8218 }
8219 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008220 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 if (str != NULL)
8222 for (p = str; *p != NUL; )
8223 {
8224 if (*p == '\'')
8225 *r++ = '\'';
8226 MB_COPY_CHAR(p, r);
8227 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008228 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008229 if (function)
8230 *r++ = ')';
8231 *r++ = NUL;
8232 }
8233 return s;
8234}
8235
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008236#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237/*
8238 * Convert the string "text" to a floating point number.
8239 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8240 * this always uses a decimal point.
8241 * Returns the length of the text that was consumed.
8242 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008243 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008244string2float(
8245 char_u *text,
8246 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008247{
8248 char *s = (char *)text;
8249 float_T f;
8250
8251 f = strtod(s, &s);
8252 *value = f;
8253 return (int)((char_u *)s - text);
8254}
8255#endif
8256
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 * Get the value of an environment variable.
8259 * "arg" is pointing to the '$'. It is advanced to after the name.
8260 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008261 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 */
8263 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008264get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266 char_u *string = NULL;
8267 int len;
8268 int cc;
8269 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008270 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271
8272 ++*arg;
8273 name = *arg;
8274 len = get_env_len(arg);
8275 if (evaluate)
8276 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008277 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008278 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008279
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008280 cc = name[len];
8281 name[len] = NUL;
8282 /* first try vim_getenv(), fast for normal environment vars */
8283 string = vim_getenv(name, &mustfree);
8284 if (string != NULL && *string != NUL)
8285 {
8286 if (!mustfree)
8287 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008289 else
8290 {
8291 if (mustfree)
8292 vim_free(string);
8293
8294 /* next try expanding things like $VIM and ${HOME} */
8295 string = expand_env_save(name - 1);
8296 if (string != NULL && *string == '$')
8297 {
8298 vim_free(string);
8299 string = NULL;
8300 }
8301 }
8302 name[len] = cc;
8303
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304 rettv->v_type = VAR_STRING;
8305 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
8307
8308 return OK;
8309}
8310
8311/*
8312 * Array with names and number of arguments of all internal functions
8313 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8314 */
8315static struct fst
8316{
8317 char *f_name; /* function name */
8318 char f_min_argc; /* minimal number of arguments */
8319 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008320 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008321 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322} functions[] =
8323{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008324#ifdef FEAT_FLOAT
8325 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008326 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008327#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008328 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008329 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008330 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {"append", 2, 2, f_append},
8332 {"argc", 0, 0, f_argc},
8333 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008334 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008335 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008336#ifdef FEAT_FLOAT
8337 {"asin", 1, 1, f_asin}, /* WJMc */
8338#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008339 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008340 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008341 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008342 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008343 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008344 {"assert_notequal", 2, 3, f_assert_notequal},
8345 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008346 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008347#ifdef FEAT_FLOAT
8348 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008349 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008352 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"bufexists", 1, 1, f_bufexists},
8354 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8355 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8356 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8357 {"buflisted", 1, 1, f_buflisted},
8358 {"bufloaded", 1, 1, f_bufloaded},
8359 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008360 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"bufwinnr", 1, 1, f_bufwinnr},
8362 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008363 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008364 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008365 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008366#ifdef FEAT_FLOAT
8367 {"ceil", 1, 1, f_ceil},
8368#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008369#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008370 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008371 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8372 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008373 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008374 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008375 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008376 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008377 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008378 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008379 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008380 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008381 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8382 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008383 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008384 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008385#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008386 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008387 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008389 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008391#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008392 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008393 {"complete_add", 1, 1, f_complete_add},
8394 {"complete_check", 0, 0, f_complete_check},
8395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008397 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008398#ifdef FEAT_FLOAT
8399 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008400 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008401#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008402 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008404 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008405 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008406 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008408 {"diff_filler", 1, 1, f_diff_filler},
8409 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008410 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008411 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008413 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {"eventhandler", 0, 0, f_eventhandler},
8415 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008416 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008418#ifdef FEAT_FLOAT
8419 {"exp", 1, 1, f_exp},
8420#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008421 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008422 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008423 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8425 {"filereadable", 1, 1, f_filereadable},
8426 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008427 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008428 {"finddir", 1, 3, f_finddir},
8429 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008430#ifdef FEAT_FLOAT
8431 {"float2nr", 1, 1, f_float2nr},
8432 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008433 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008434#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008435 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"fnamemodify", 2, 2, f_fnamemodify},
8437 {"foldclosed", 1, 1, f_foldclosed},
8438 {"foldclosedend", 1, 1, f_foldclosedend},
8439 {"foldlevel", 1, 1, f_foldlevel},
8440 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008441 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008443 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008444 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008445 {"garbagecollect_for_testing", 0, 0, f_garbagecollect_for_testing},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008446 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008447 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008448 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"getchar", 0, 1, f_getchar},
8450 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008451 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {"getcmdline", 0, 0, f_getcmdline},
8453 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008454 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008455 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008456 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008457 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008458 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008459 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {"getfsize", 1, 1, f_getfsize},
8461 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008462 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008463 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008464 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008465 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008466 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008467 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008468 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008469 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008471 {"gettabvar", 2, 3, f_gettabvar},
8472 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {"getwinposx", 0, 0, f_getwinposx},
8474 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008475 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008476 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008477 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008478 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008480 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008481 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008482 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8484 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8485 {"histadd", 2, 2, f_histadd},
8486 {"histdel", 1, 2, f_histdel},
8487 {"histget", 1, 2, f_histget},
8488 {"histnr", 1, 1, f_histnr},
8489 {"hlID", 1, 1, f_hlID},
8490 {"hlexists", 1, 1, f_hlexists},
8491 {"hostname", 0, 0, f_hostname},
8492 {"iconv", 3, 3, f_iconv},
8493 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008494 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008495 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008497 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {"inputrestore", 0, 0, f_inputrestore},
8499 {"inputsave", 0, 0, f_inputsave},
8500 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008502 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008504 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008505#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8506 {"isnan", 1, 1, f_isnan},
8507#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008508 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008509#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008510 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008511 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008512 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008513 {"job_start", 1, 2, f_job_start},
8514 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008515 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008516#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008517 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008518 {"js_decode", 1, 1, f_js_decode},
8519 {"js_encode", 1, 1, f_js_encode},
8520 {"json_decode", 1, 1, f_json_decode},
8521 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008522 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 {"libcall", 3, 3, f_libcall},
8526 {"libcallnr", 3, 3, f_libcallnr},
8527 {"line", 1, 1, f_line},
8528 {"line2byte", 1, 1, f_line2byte},
8529 {"lispindent", 1, 1, f_lispindent},
8530 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008531#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008532 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008533 {"log10", 1, 1, f_log10},
8534#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008535#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008536 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008537#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008538 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008539 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008540 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008541 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008542 {"matchadd", 2, 5, f_matchadd},
8543 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008544 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008545 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008546 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008547 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008548 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008549 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008550 {"max", 1, 1, f_max},
8551 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008552#ifdef vim_mkdir
8553 {"mkdir", 1, 3, f_mkdir},
8554#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008555 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008556#ifdef FEAT_MZSCHEME
8557 {"mzeval", 1, 1, f_mzeval},
8558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008560 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008561 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008562 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008563#ifdef FEAT_PERL
8564 {"perleval", 1, 1, f_perleval},
8565#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008566#ifdef FEAT_FLOAT
8567 {"pow", 2, 2, f_pow},
8568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008570 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008571 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008572#ifdef FEAT_PYTHON3
8573 {"py3eval", 1, 1, f_py3eval},
8574#endif
8575#ifdef FEAT_PYTHON
8576 {"pyeval", 1, 1, f_pyeval},
8577#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008578 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008579 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008580 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008581#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008582 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008583#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008584 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 {"remote_expr", 2, 3, f_remote_expr},
8586 {"remote_foreground", 1, 1, f_remote_foreground},
8587 {"remote_peek", 1, 2, f_remote_peek},
8588 {"remote_read", 1, 1, f_remote_read},
8589 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008590 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008592 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008594 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008595#ifdef FEAT_FLOAT
8596 {"round", 1, 1, f_round},
8597#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008598 {"screenattr", 2, 2, f_screenattr},
8599 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008600 {"screencol", 0, 0, f_screencol},
8601 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008602 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008603 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008604 {"searchpair", 3, 7, f_searchpair},
8605 {"searchpairpos", 3, 7, f_searchpairpos},
8606 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 {"server2client", 2, 2, f_server2client},
8608 {"serverlist", 0, 0, f_serverlist},
8609 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008610 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008612 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008614 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008615 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008616 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008617 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008619 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008620 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008622#ifdef FEAT_CRYPT
8623 {"sha256", 1, 1, f_sha256},
8624#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008625 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008626 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008628#ifdef FEAT_FLOAT
8629 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008630 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008631#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008632 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008633 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008634 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008635 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008636 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008637#ifdef FEAT_FLOAT
8638 {"sqrt", 1, 1, f_sqrt},
8639 {"str2float", 1, 1, f_str2float},
8640#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008641 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008642 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008643 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008644 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645#ifdef HAVE_STRFTIME
8646 {"strftime", 1, 2, f_strftime},
8647#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008648 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008649 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 {"strlen", 1, 1, f_strlen},
8652 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008653 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008655 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008656 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {"substitute", 4, 4, f_substitute},
8658 {"synID", 3, 3, f_synID},
8659 {"synIDattr", 2, 3, f_synIDattr},
8660 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008661 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008662 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008663 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008664 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008665 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008666 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008667 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008668 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008669 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008670#ifdef FEAT_FLOAT
8671 {"tan", 1, 1, f_tan},
8672 {"tanh", 1, 1, f_tanh},
8673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008675 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008676#ifdef FEAT_TIMERS
8677 {"timer_start", 2, 3, f_timer_start},
8678 {"timer_stop", 1, 1, f_timer_stop},
8679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 {"tolower", 1, 1, f_tolower},
8681 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008682 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008683#ifdef FEAT_FLOAT
8684 {"trunc", 1, 1, f_trunc},
8685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008687 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008688 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008689 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008690 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {"virtcol", 1, 1, f_virtcol},
8692 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008693 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008694 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008695 {"win_getid", 0, 2, f_win_getid},
8696 {"win_gotoid", 1, 1, f_win_gotoid},
8697 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8698 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 {"winbufnr", 1, 1, f_winbufnr},
8700 {"wincol", 0, 0, f_wincol},
8701 {"winheight", 1, 1, f_winheight},
8702 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008703 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008705 {"winrestview", 1, 1, f_winrestview},
8706 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008708 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008709 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008710 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711};
8712
8713#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8714
8715/*
8716 * Function given to ExpandGeneric() to obtain the list of internal
8717 * or user defined function names.
8718 */
8719 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008720get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721{
8722 static int intidx = -1;
8723 char_u *name;
8724
8725 if (idx == 0)
8726 intidx = -1;
8727 if (intidx < 0)
8728 {
8729 name = get_user_func_name(xp, idx);
8730 if (name != NULL)
8731 return name;
8732 }
8733 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8734 {
8735 STRCPY(IObuff, functions[intidx].f_name);
8736 STRCAT(IObuff, "(");
8737 if (functions[intidx].f_max_argc == 0)
8738 STRCAT(IObuff, ")");
8739 return IObuff;
8740 }
8741
8742 return NULL;
8743}
8744
8745/*
8746 * Function given to ExpandGeneric() to obtain the list of internal or
8747 * user defined variable or function names.
8748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008750get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751{
8752 static int intidx = -1;
8753 char_u *name;
8754
8755 if (idx == 0)
8756 intidx = -1;
8757 if (intidx < 0)
8758 {
8759 name = get_function_name(xp, idx);
8760 if (name != NULL)
8761 return name;
8762 }
8763 return get_user_var_name(xp, ++intidx);
8764}
8765
8766#endif /* FEAT_CMDL_COMPL */
8767
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008768#if defined(EBCDIC) || defined(PROTO)
8769/*
8770 * Compare struct fst by function name.
8771 */
8772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008773compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008774{
8775 struct fst *p1 = (struct fst *)s1;
8776 struct fst *p2 = (struct fst *)s2;
8777
8778 return STRCMP(p1->f_name, p2->f_name);
8779}
8780
8781/*
8782 * Sort the function table by function name.
8783 * The sorting of the table above is ASCII dependant.
8784 * On machines using EBCDIC we have to sort it.
8785 */
8786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008787sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008788{
8789 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8790
8791 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8792}
8793#endif
8794
8795
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796/*
8797 * Find internal function in table above.
8798 * Return index, or -1 if not found
8799 */
8800 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008801find_internal_func(
8802 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803{
8804 int first = 0;
8805 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8806 int cmp;
8807 int x;
8808
8809 /*
8810 * Find the function name in the table. Binary search.
8811 */
8812 while (first <= last)
8813 {
8814 x = first + ((unsigned)(last - first) >> 1);
8815 cmp = STRCMP(name, functions[x].f_name);
8816 if (cmp < 0)
8817 last = x - 1;
8818 else if (cmp > 0)
8819 first = x + 1;
8820 else
8821 return x;
8822 }
8823 return -1;
8824}
8825
8826/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008827 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8828 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008829 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8830 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008831 */
8832 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008833deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008834{
Bram Moolenaar33570922005-01-25 22:26:29 +00008835 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008836 int cc;
8837
Bram Moolenaar65639032016-03-16 21:40:30 +01008838 if (partialp != NULL)
8839 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008840
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008841 cc = name[*lenp];
8842 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008843 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008844 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008846 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008848 {
8849 *lenp = 0;
8850 return (char_u *)""; /* just in case */
8851 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008852 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008853 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008854 }
8855
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008856 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8857 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008858 partial_T *pt = v->di_tv.vval.v_partial;
8859
8860 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008861 {
8862 *lenp = 0;
8863 return (char_u *)""; /* just in case */
8864 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008865 if (partialp != NULL)
8866 *partialp = pt;
8867 *lenp = (int)STRLEN(pt->pt_name);
8868 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008869 }
8870
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008871 return name;
8872}
8873
8874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 * Allocate a variable for the result of a function.
8876 * Return OK or FAIL.
8877 */
8878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008879get_func_tv(
8880 char_u *name, /* name of the function */
8881 int len, /* length of "name" */
8882 typval_T *rettv,
8883 char_u **arg, /* argument, pointing to the '(' */
8884 linenr_T firstline, /* first line of range */
8885 linenr_T lastline, /* last line of range */
8886 int *doesrange, /* return: function handled range */
8887 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008888 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008889 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
8891 char_u *argp;
8892 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008893 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 int argcount = 0; /* number of arguments found */
8895
8896 /*
8897 * Get the arguments.
8898 */
8899 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008900 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
8902 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8903 if (*argp == ')' || *argp == ',' || *argp == NUL)
8904 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8906 {
8907 ret = FAIL;
8908 break;
8909 }
8910 ++argcount;
8911 if (*argp != ',')
8912 break;
8913 }
8914 if (*argp == ')')
8915 ++argp;
8916 else
8917 ret = FAIL;
8918
8919 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008920 {
8921 int i = 0;
8922
8923 if (get_vim_var_nr(VV_TESTING))
8924 {
8925 /* Prepare for calling garbagecollect_for_testing(), need to know
8926 * what variables are used on the call stack. */
8927 if (funcargs.ga_itemsize == 0)
8928 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8929 for (i = 0; i < argcount; ++i)
8930 if (ga_grow(&funcargs, 1) == OK)
8931 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8932 &argvars[i];
8933 }
8934
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008936 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008937
8938 funcargs.ga_len -= i;
8939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008941 {
8942 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008943 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008945 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
8948 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
8951 *arg = skipwhite(argp);
8952 return ret;
8953}
8954
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008955#define ERROR_UNKNOWN 0
8956#define ERROR_TOOMANY 1
8957#define ERROR_TOOFEW 2
8958#define ERROR_SCRIPT 3
8959#define ERROR_DICT 4
8960#define ERROR_NONE 5
8961#define ERROR_OTHER 6
8962#define FLEN_FIXED 40
8963
8964/*
8965 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8966 * Change <SNR>123_name() to K_SNR 123_name().
8967 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8968 * (slow).
8969 */
8970 static char_u *
8971fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8972{
8973 int llen;
8974 char_u *fname;
8975 int i;
8976
8977 llen = eval_fname_script(name);
8978 if (llen > 0)
8979 {
8980 fname_buf[0] = K_SPECIAL;
8981 fname_buf[1] = KS_EXTRA;
8982 fname_buf[2] = (int)KE_SNR;
8983 i = 3;
8984 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8985 {
8986 if (current_SID <= 0)
8987 *error = ERROR_SCRIPT;
8988 else
8989 {
8990 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8991 i = (int)STRLEN(fname_buf);
8992 }
8993 }
8994 if (i + STRLEN(name + llen) < FLEN_FIXED)
8995 {
8996 STRCPY(fname_buf + i, name + llen);
8997 fname = fname_buf;
8998 }
8999 else
9000 {
9001 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9002 if (fname == NULL)
9003 *error = ERROR_OTHER;
9004 else
9005 {
9006 *tofree = fname;
9007 mch_memmove(fname, fname_buf, (size_t)i);
9008 STRCPY(fname + i, name + llen);
9009 }
9010 }
9011 }
9012 else
9013 fname = name;
9014 return fname;
9015}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016
9017/*
9018 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009019 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009020 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009023call_func(
9024 char_u *funcname, /* name of the function */
9025 int len, /* length of "name" */
9026 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009027 int argcount_in, /* number of "argvars" */
9028 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009029 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009030 linenr_T firstline, /* first line of range */
9031 linenr_T lastline, /* last line of range */
9032 int *doesrange, /* return: function handled range */
9033 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009034 partial_T *partial, /* optional, can be NULL */
9035 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036{
9037 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 int error = ERROR_NONE;
9039 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009042 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009044 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009045 int argcount = argcount_in;
9046 typval_T *argvars = argvars_in;
9047 dict_T *selfdict = selfdict_in;
9048 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9049 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009050
9051 /* Make a copy of the name, if it comes from a funcref variable it could
9052 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009053 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009054 if (name == NULL)
9055 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009057 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058
9059 *doesrange = FALSE;
9060
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009061 if (partial != NULL)
9062 {
9063 if (partial->pt_dict != NULL)
9064 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009065 /* When the function has a partial with a dict and there is a dict
9066 * argument, use the dict argument. That is backwards compatible.
9067 */
9068 if (selfdict_in == NULL)
9069 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009070 }
9071 if (error == ERROR_NONE && partial->pt_argc > 0)
9072 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009073 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9074 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9075 for (i = 0; i < argcount_in; ++i)
9076 argv[i + argv_clear] = argvars_in[i];
9077 argvars = argv;
9078 argcount = partial->pt_argc + argcount_in;
9079 }
9080 }
9081
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082
9083 /* execute the function if no errors detected and executing */
9084 if (evaluate && error == ERROR_NONE)
9085 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009086 char_u *rfname = fname;
9087
9088 /* Ignore "g:" before a function name. */
9089 if (fname[0] == 'g' && fname[1] == ':')
9090 rfname = fname + 2;
9091
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009092 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9093 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 error = ERROR_UNKNOWN;
9095
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009096 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 {
9098 /*
9099 * User defined function.
9100 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009101 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009104 /* Trigger FuncUndefined event, may load the function. */
9105 if (fp == NULL
9106 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009107 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009108 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009110 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009111 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 }
9113#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009114 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009115 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009116 {
9117 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009118 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009119 }
9120
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 if (fp != NULL)
9122 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009123 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009125 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009127 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009129 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009130 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 else
9132 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009133 int did_save_redo = FALSE;
9134
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 /*
9136 * Call the user function.
9137 * Save and restore search patterns, script variables and
9138 * redo buffer.
9139 */
9140 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009141#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009142 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009143#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009144 {
9145 saveRedobuff();
9146 did_save_redo = TRUE;
9147 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009148 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009149 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009150 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009151 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9152 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9153 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009154 /* Function was unreferenced while being used, free it
9155 * now. */
9156 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009157 if (did_save_redo)
9158 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 restore_search_patterns();
9160 error = ERROR_NONE;
9161 }
9162 }
9163 }
9164 else
9165 {
9166 /*
9167 * Find the function name in the table, call its implementation.
9168 */
9169 i = find_internal_func(fname);
9170 if (i >= 0)
9171 {
9172 if (argcount < functions[i].f_min_argc)
9173 error = ERROR_TOOFEW;
9174 else if (argcount > functions[i].f_max_argc)
9175 error = ERROR_TOOMANY;
9176 else
9177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009178 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 error = ERROR_NONE;
9181 }
9182 }
9183 }
9184 /*
9185 * The function call (or "FuncUndefined" autocommand sequence) might
9186 * have been aborted by an error, an interrupt, or an explicitly thrown
9187 * exception that has not been caught so far. This situation can be
9188 * tested for by calling aborting(). For an error in an internal
9189 * function or for the "E132" error in call_user_func(), however, the
9190 * throw point at which the "force_abort" flag (temporarily reset by
9191 * emsg()) is normally updated has not been reached yet. We need to
9192 * update that flag first to make aborting() reliable.
9193 */
9194 update_force_abort();
9195 }
9196 if (error == ERROR_NONE)
9197 ret = OK;
9198
9199 /*
9200 * Report an error unless the argument evaluation or function call has been
9201 * cancelled due to an aborting error, an interrupt, or an exception.
9202 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009203 if (!aborting())
9204 {
9205 switch (error)
9206 {
9207 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009208 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009209 break;
9210 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009211 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009212 break;
9213 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009214 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009215 name);
9216 break;
9217 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009218 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009219 name);
9220 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009221 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009222 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009223 name);
9224 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009225 }
9226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009228 while (argv_clear > 0)
9229 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009230 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009231 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
9233 return ret;
9234}
9235
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009236/*
9237 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009238 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009239 */
9240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009241emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009242{
9243 char_u *p;
9244
9245 if (*name == K_SPECIAL)
9246 p = concat_str((char_u *)"<SNR>", name + 3);
9247 else
9248 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009249 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009250 if (p != name)
9251 vim_free(p);
9252}
9253
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009254/*
9255 * Return TRUE for a non-zero Number and a non-empty String.
9256 */
9257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009258non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009259{
9260 return ((argvars[0].v_type == VAR_NUMBER
9261 && argvars[0].vval.v_number != 0)
9262 || (argvars[0].v_type == VAR_STRING
9263 && argvars[0].vval.v_string != NULL
9264 && *argvars[0].vval.v_string != NUL));
9265}
9266
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267/*********************************************
9268 * Implementation of the built-in functions
9269 */
9270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009271#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009272static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009273
9274/*
9275 * Get the float value of "argvars[0]" into "f".
9276 * Returns FAIL when the argument is not a Number or Float.
9277 */
9278 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009279get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009280{
9281 if (argvars[0].v_type == VAR_FLOAT)
9282 {
9283 *f = argvars[0].vval.v_float;
9284 return OK;
9285 }
9286 if (argvars[0].v_type == VAR_NUMBER)
9287 {
9288 *f = (float_T)argvars[0].vval.v_number;
9289 return OK;
9290 }
9291 EMSG(_("E808: Number or Float required"));
9292 return FAIL;
9293}
9294
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009295/*
9296 * "abs(expr)" function
9297 */
9298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009299f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009300{
9301 if (argvars[0].v_type == VAR_FLOAT)
9302 {
9303 rettv->v_type = VAR_FLOAT;
9304 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9305 }
9306 else
9307 {
9308 varnumber_T n;
9309 int error = FALSE;
9310
9311 n = get_tv_number_chk(&argvars[0], &error);
9312 if (error)
9313 rettv->vval.v_number = -1;
9314 else if (n > 0)
9315 rettv->vval.v_number = n;
9316 else
9317 rettv->vval.v_number = -n;
9318 }
9319}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009320
9321/*
9322 * "acos()" function
9323 */
9324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009325f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009326{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009327 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009328
9329 rettv->v_type = VAR_FLOAT;
9330 if (get_float_arg(argvars, &f) == OK)
9331 rettv->vval.v_float = acos(f);
9332 else
9333 rettv->vval.v_float = 0.0;
9334}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009335#endif
9336
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009338 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 */
9340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009341f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
Bram Moolenaar33570922005-01-25 22:26:29 +00009343 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009345 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009346 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009348 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009349 && !tv_check_lock(l->lv_lock,
9350 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009351 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009353 }
9354 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009355 EMSG(_(e_listreq));
9356}
9357
9358/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009359 * "alloc_fail(id, countdown, repeat)" function
9360 */
9361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009362f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009363{
9364 if (argvars[0].v_type != VAR_NUMBER
9365 || argvars[0].vval.v_number <= 0
9366 || argvars[1].v_type != VAR_NUMBER
9367 || argvars[1].vval.v_number < 0
9368 || argvars[2].v_type != VAR_NUMBER)
9369 EMSG(_(e_invarg));
9370 else
9371 {
9372 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009373 if (alloc_fail_id >= aid_last)
9374 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009375 alloc_fail_countdown = argvars[1].vval.v_number;
9376 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009377 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009378 }
9379}
9380
9381/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009382 * "and(expr, expr)" function
9383 */
9384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009385f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009386{
9387 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9388 & get_tv_number_chk(&argvars[1], NULL);
9389}
9390
9391/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009392 * "append(lnum, string/list)" function
9393 */
9394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009395f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009396{
9397 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009399 list_T *l = NULL;
9400 listitem_T *li = NULL;
9401 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009402 long added = 0;
9403
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009404 /* When coming here from Insert mode, sync undo, so that this can be
9405 * undone separately from what was previously inserted. */
9406 if (u_sync_once == 2)
9407 {
9408 u_sync_once = 1; /* notify that u_sync() was called */
9409 u_sync(TRUE);
9410 }
9411
Bram Moolenaar0d660222005-01-07 21:51:51 +00009412 lnum = get_tv_lnum(argvars);
9413 if (lnum >= 0
9414 && lnum <= curbuf->b_ml.ml_line_count
9415 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009416 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009417 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009418 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009419 l = argvars[1].vval.v_list;
9420 if (l == NULL)
9421 return;
9422 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009423 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009424 for (;;)
9425 {
9426 if (l == NULL)
9427 tv = &argvars[1]; /* append a string */
9428 else if (li == NULL)
9429 break; /* end of list */
9430 else
9431 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009432 line = get_tv_string_chk(tv);
9433 if (line == NULL) /* type error */
9434 {
9435 rettv->vval.v_number = 1; /* Failed */
9436 break;
9437 }
9438 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009439 ++added;
9440 if (l == NULL)
9441 break;
9442 li = li->li_next;
9443 }
9444
9445 appended_lines_mark(lnum, added);
9446 if (curwin->w_cursor.lnum > lnum)
9447 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009449 else
9450 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451}
9452
9453/*
9454 * "argc()" function
9455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009457f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460}
9461
9462/*
9463 * "argidx()" function
9464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009466f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469}
9470
9471/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009472 * "arglistid()" function
9473 */
9474 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009475f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009476{
9477 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009478
9479 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009480 wp = find_tabwin(&argvars[0], &argvars[1]);
9481 if (wp != NULL)
9482 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009483}
9484
9485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 * "argv(nr)" function
9487 */
9488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009489f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490{
9491 int idx;
9492
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009493 if (argvars[0].v_type != VAR_UNKNOWN)
9494 {
9495 idx = get_tv_number_chk(&argvars[0], NULL);
9496 if (idx >= 0 && idx < ARGCOUNT)
9497 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9498 else
9499 rettv->vval.v_string = NULL;
9500 rettv->v_type = VAR_STRING;
9501 }
9502 else if (rettv_list_alloc(rettv) == OK)
9503 for (idx = 0; idx < ARGCOUNT; ++idx)
9504 list_append_string(rettv->vval.v_list,
9505 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506}
9507
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009508typedef enum
9509{
9510 ASSERT_EQUAL,
9511 ASSERT_NOTEQUAL,
9512 ASSERT_MATCH,
9513 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009514 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009515} assert_type_T;
9516
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009517static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009518static 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 +01009519static void assert_error(garray_T *gap);
9520static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009521
9522/*
9523 * Prepare "gap" for an assert error and add the sourcing position.
9524 */
9525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009526prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009527{
9528 char buf[NUMBUFLEN];
9529
9530 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009531 if (sourcing_name != NULL)
9532 {
9533 ga_concat(gap, sourcing_name);
9534 if (sourcing_lnum > 0)
9535 ga_concat(gap, (char_u *)" ");
9536 }
9537 if (sourcing_lnum > 0)
9538 {
9539 sprintf(buf, "line %ld", (long)sourcing_lnum);
9540 ga_concat(gap, (char_u *)buf);
9541 }
9542 if (sourcing_name != NULL || sourcing_lnum > 0)
9543 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009544}
9545
9546/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009547 * Append "str" to "gap", escaping unprintable characters.
9548 * Changes NL to \n, CR to \r, etc.
9549 */
9550 static void
9551ga_concat_esc(garray_T *gap, char_u *str)
9552{
9553 char_u *p;
9554 char_u buf[NUMBUFLEN];
9555
Bram Moolenaarf1551962016-03-15 12:55:58 +01009556 if (str == NULL)
9557 {
9558 ga_concat(gap, (char_u *)"NULL");
9559 return;
9560 }
9561
Bram Moolenaar23689172016-02-15 22:37:37 +01009562 for (p = str; *p != NUL; ++p)
9563 switch (*p)
9564 {
9565 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9566 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9567 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9568 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9569 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9570 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9571 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9572 default:
9573 if (*p < ' ')
9574 {
9575 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9576 ga_concat(gap, buf);
9577 }
9578 else
9579 ga_append(gap, *p);
9580 break;
9581 }
9582}
9583
9584/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009585 * Fill "gap" with information about an assert error.
9586 */
9587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009588fill_assert_error(
9589 garray_T *gap,
9590 typval_T *opt_msg_tv,
9591 char_u *exp_str,
9592 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009593 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009594 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009595{
9596 char_u numbuf[NUMBUFLEN];
9597 char_u *tofree;
9598
9599 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9600 {
9601 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9602 vim_free(tofree);
9603 }
9604 else
9605 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009606 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009607 ga_concat(gap, (char_u *)"Pattern ");
9608 else
9609 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009610 if (exp_str == NULL)
9611 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009612 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009613 vim_free(tofree);
9614 }
9615 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009616 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009617 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009618 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009619 else if (atype == ASSERT_NOTMATCH)
9620 ga_concat(gap, (char_u *)" does match ");
9621 else if (atype == ASSERT_NOTEQUAL)
9622 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009623 else
9624 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009625 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009626 vim_free(tofree);
9627 }
9628}
Bram Moolenaar43345542015-11-29 17:35:35 +01009629
9630/*
9631 * Add an assert error to v:errors.
9632 */
9633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009634assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009635{
9636 struct vimvar *vp = &vimvars[VV_ERRORS];
9637
9638 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9639 /* Make sure v:errors is a list. */
9640 set_vim_var_list(VV_ERRORS, list_alloc());
9641 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9642}
9643
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009644 static void
9645assert_equal_common(typval_T *argvars, assert_type_T atype)
9646{
9647 garray_T ga;
9648
9649 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9650 != (atype == ASSERT_EQUAL))
9651 {
9652 prepare_assert_error(&ga);
9653 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9654 atype);
9655 assert_error(&ga);
9656 ga_clear(&ga);
9657 }
9658}
9659
Bram Moolenaar43345542015-11-29 17:35:35 +01009660/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009661 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009662 */
9663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009664f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009665{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009666 assert_equal_common(argvars, ASSERT_EQUAL);
9667}
Bram Moolenaar43345542015-11-29 17:35:35 +01009668
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009669/*
9670 * "assert_notequal(expected, actual[, msg])" function
9671 */
9672 static void
9673f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9674{
9675 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009676}
9677
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009678/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009679 * "assert_exception(string[, msg])" function
9680 */
9681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009682f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009683{
9684 garray_T ga;
9685 char *error;
9686
9687 error = (char *)get_tv_string_chk(&argvars[0]);
9688 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9689 {
9690 prepare_assert_error(&ga);
9691 ga_concat(&ga, (char_u *)"v:exception is not set");
9692 assert_error(&ga);
9693 ga_clear(&ga);
9694 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009695 else if (error != NULL
9696 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009697 {
9698 prepare_assert_error(&ga);
9699 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009700 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009701 assert_error(&ga);
9702 ga_clear(&ga);
9703 }
9704}
9705
9706/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009707 * "assert_fails(cmd [, error])" function
9708 */
9709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009710f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009711{
9712 char_u *cmd = get_tv_string_chk(&argvars[0]);
9713 garray_T ga;
9714
9715 called_emsg = FALSE;
9716 suppress_errthrow = TRUE;
9717 emsg_silent = TRUE;
9718 do_cmdline_cmd(cmd);
9719 if (!called_emsg)
9720 {
9721 prepare_assert_error(&ga);
9722 ga_concat(&ga, (char_u *)"command did not fail: ");
9723 ga_concat(&ga, cmd);
9724 assert_error(&ga);
9725 ga_clear(&ga);
9726 }
9727 else if (argvars[1].v_type != VAR_UNKNOWN)
9728 {
9729 char_u buf[NUMBUFLEN];
9730 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9731
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009732 if (error == NULL
9733 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009734 {
9735 prepare_assert_error(&ga);
9736 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009737 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009738 assert_error(&ga);
9739 ga_clear(&ga);
9740 }
9741 }
9742
9743 called_emsg = FALSE;
9744 suppress_errthrow = FALSE;
9745 emsg_silent = FALSE;
9746 emsg_on_display = FALSE;
9747 set_vim_var_string(VV_ERRMSG, NULL, 0);
9748}
9749
9750/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009751 * Common for assert_true() and assert_false().
9752 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009754assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009755{
9756 int error = FALSE;
9757 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009758
Bram Moolenaar37127922016-02-06 20:29:28 +01009759 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009760 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009761 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009762 if (argvars[0].v_type != VAR_NUMBER
9763 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9764 || error)
9765 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009766 prepare_assert_error(&ga);
9767 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009768 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009769 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009770 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009771 ga_clear(&ga);
9772 }
9773}
9774
9775/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009776 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009777 */
9778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009779f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009780{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009781 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009782}
9783
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009784 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009785assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009786{
9787 garray_T ga;
9788 char_u buf1[NUMBUFLEN];
9789 char_u buf2[NUMBUFLEN];
9790 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9791 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9792
Bram Moolenaar72188e92016-03-28 22:48:29 +02009793 if (pat == NULL || text == NULL)
9794 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009795 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009796 {
9797 prepare_assert_error(&ga);
9798 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009799 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009800 assert_error(&ga);
9801 ga_clear(&ga);
9802 }
9803}
9804
9805/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009806 * "assert_match(pattern, actual[, msg])" function
9807 */
9808 static void
9809f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9810{
9811 assert_match_common(argvars, ASSERT_MATCH);
9812}
9813
9814/*
9815 * "assert_notmatch(pattern, actual[, msg])" function
9816 */
9817 static void
9818f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9819{
9820 assert_match_common(argvars, ASSERT_NOTMATCH);
9821}
9822
9823/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009824 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009825 */
9826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009827f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009828{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009829 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009830}
9831
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009832#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009833/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009834 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009835 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009837f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009838{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009839 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009840
9841 rettv->v_type = VAR_FLOAT;
9842 if (get_float_arg(argvars, &f) == OK)
9843 rettv->vval.v_float = asin(f);
9844 else
9845 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009846}
9847
9848/*
9849 * "atan()" function
9850 */
9851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009852f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009853{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009854 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009855
9856 rettv->v_type = VAR_FLOAT;
9857 if (get_float_arg(argvars, &f) == OK)
9858 rettv->vval.v_float = atan(f);
9859 else
9860 rettv->vval.v_float = 0.0;
9861}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009862
9863/*
9864 * "atan2()" function
9865 */
9866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009867f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009868{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009869 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009870
9871 rettv->v_type = VAR_FLOAT;
9872 if (get_float_arg(argvars, &fx) == OK
9873 && get_float_arg(&argvars[1], &fy) == OK)
9874 rettv->vval.v_float = atan2(fx, fy);
9875 else
9876 rettv->vval.v_float = 0.0;
9877}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009878#endif
9879
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880/*
9881 * "browse(save, title, initdir, default)" function
9882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009884f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
9886#ifdef FEAT_BROWSE
9887 int save;
9888 char_u *title;
9889 char_u *initdir;
9890 char_u *defname;
9891 char_u buf[NUMBUFLEN];
9892 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009895 save = get_tv_number_chk(&argvars[0], &error);
9896 title = get_tv_string_chk(&argvars[1]);
9897 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9898 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009900 if (error || title == NULL || initdir == NULL || defname == NULL)
9901 rettv->vval.v_string = NULL;
9902 else
9903 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009904 do_browse(save ? BROWSE_SAVE : 0,
9905 title, defname, NULL, initdir, NULL, curbuf);
9906#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009908#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009910}
9911
9912/*
9913 * "browsedir(title, initdir)" function
9914 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009916f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009917{
9918#ifdef FEAT_BROWSE
9919 char_u *title;
9920 char_u *initdir;
9921 char_u buf[NUMBUFLEN];
9922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 title = get_tv_string_chk(&argvars[0]);
9924 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009926 if (title == NULL || initdir == NULL)
9927 rettv->vval.v_string = NULL;
9928 else
9929 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009930 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935}
9936
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009937static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009938
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939/*
9940 * Find a buffer by number or exact name.
9941 */
9942 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009943find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944{
9945 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009947 if (avar->v_type == VAR_NUMBER)
9948 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009949 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009951 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009952 if (buf == NULL)
9953 {
9954 /* No full path name match, try a match with a URL or a "nofile"
9955 * buffer, these don't use the full path. */
9956 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9957 if (buf->b_fname != NULL
9958 && (path_with_url(buf->b_fname)
9959#ifdef FEAT_QUICKFIX
9960 || bt_nofile(buf)
9961#endif
9962 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009963 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009964 break;
9965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 }
9967 return buf;
9968}
9969
9970/*
9971 * "bufexists(expr)" function
9972 */
9973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009974f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977}
9978
9979/*
9980 * "buflisted(expr)" function
9981 */
9982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009983f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984{
9985 buf_T *buf;
9986
9987 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009988 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989}
9990
9991/*
9992 * "bufloaded(expr)" function
9993 */
9994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009995f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996{
9997 buf_T *buf;
9998
9999 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001}
10002
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010003 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010004buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 int save_magic;
10007 char_u *save_cpo;
10008 buf_T *buf;
10009
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10011 save_magic = p_magic;
10012 p_magic = TRUE;
10013 save_cpo = p_cpo;
10014 p_cpo = (char_u *)"";
10015
10016 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010017 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018
10019 p_magic = save_magic;
10020 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010021 return buf;
10022}
10023
10024/*
10025 * Get buffer by number or pattern.
10026 */
10027 static buf_T *
10028get_buf_tv(typval_T *tv, int curtab_only)
10029{
10030 char_u *name = tv->vval.v_string;
10031 buf_T *buf;
10032
10033 if (tv->v_type == VAR_NUMBER)
10034 return buflist_findnr((int)tv->vval.v_number);
10035 if (tv->v_type != VAR_STRING)
10036 return NULL;
10037 if (name == NULL || *name == NUL)
10038 return curbuf;
10039 if (name[0] == '$' && name[1] == NUL)
10040 return lastbuf;
10041
10042 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
10044 /* If not found, try expanding the name, like done for bufexists(). */
10045 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
10048 return buf;
10049}
10050
10051/*
10052 * "bufname(expr)" function
10053 */
10054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010055f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056{
10057 buf_T *buf;
10058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010059 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010061 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 --emsg_off;
10068}
10069
10070/*
10071 * "bufnr(expr)" function
10072 */
10073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010074f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075{
10076 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010077 int error = FALSE;
10078 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010080 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010082 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010083 --emsg_off;
10084
10085 /* If the buffer isn't found and the second argument is not zero create a
10086 * new buffer. */
10087 if (buf == NULL
10088 && argvars[1].v_type != VAR_UNKNOWN
10089 && get_tv_number_chk(&argvars[1], &error) != 0
10090 && !error
10091 && (name = get_tv_string_chk(&argvars[0])) != NULL
10092 && !error)
10093 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10094
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099}
10100
10101/*
10102 * "bufwinnr(nr)" function
10103 */
10104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010105f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106{
10107#ifdef FEAT_WINDOWS
10108 win_T *wp;
10109 int winnr = 0;
10110#endif
10111 buf_T *buf;
10112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010115 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116#ifdef FEAT_WINDOWS
10117 for (wp = firstwin; wp; wp = wp->w_next)
10118 {
10119 ++winnr;
10120 if (wp->w_buffer == buf)
10121 break;
10122 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010125 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126#endif
10127 --emsg_off;
10128}
10129
10130/*
10131 * "byte2line(byte)" function
10132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010134f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135{
10136#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138#else
10139 long boff = 0;
10140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010141 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 (linenr_T)0, &boff);
10147#endif
10148}
10149
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010151byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010152{
10153#ifdef FEAT_MBYTE
10154 char_u *t;
10155#endif
10156 char_u *str;
10157 long idx;
10158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 str = get_tv_string_chk(&argvars[0]);
10160 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010162 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010163 return;
10164
10165#ifdef FEAT_MBYTE
10166 t = str;
10167 for ( ; idx > 0; idx--)
10168 {
10169 if (*t == NUL) /* EOL reached */
10170 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010171 if (enc_utf8 && comp)
10172 t += utf_ptr2len(t);
10173 else
10174 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010175 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010176 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010177#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010178 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010179 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010180#endif
10181}
10182
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010183/*
10184 * "byteidx()" function
10185 */
10186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010187f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010188{
10189 byteidx(argvars, rettv, FALSE);
10190}
10191
10192/*
10193 * "byteidxcomp()" function
10194 */
10195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010196f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010197{
10198 byteidx(argvars, rettv, TRUE);
10199}
10200
Bram Moolenaardb913952012-06-29 12:54:53 +020010201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010202func_call(
10203 char_u *name,
10204 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010205 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010206 dict_T *selfdict,
10207 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010208{
10209 listitem_T *item;
10210 typval_T argv[MAX_FUNC_ARGS + 1];
10211 int argc = 0;
10212 int dummy;
10213 int r = 0;
10214
10215 for (item = args->vval.v_list->lv_first; item != NULL;
10216 item = item->li_next)
10217 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010218 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010219 {
10220 EMSG(_("E699: Too many arguments"));
10221 break;
10222 }
10223 /* Make a copy of each argument. This is needed to be able to set
10224 * v_lock to VAR_FIXED in the copy without changing the original list.
10225 */
10226 copy_tv(&item->li_tv, &argv[argc++]);
10227 }
10228
10229 if (item == NULL)
10230 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10231 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010232 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010233
10234 /* Free the arguments. */
10235 while (argc > 0)
10236 clear_tv(&argv[--argc]);
10237
10238 return r;
10239}
10240
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010241/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010242 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010243 */
10244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010245f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010246{
10247 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010248 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010249 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010250
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010251 if (argvars[1].v_type != VAR_LIST)
10252 {
10253 EMSG(_(e_listreq));
10254 return;
10255 }
10256 if (argvars[1].vval.v_list == NULL)
10257 return;
10258
10259 if (argvars[0].v_type == VAR_FUNC)
10260 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010261 else if (argvars[0].v_type == VAR_PARTIAL)
10262 {
10263 partial = argvars[0].vval.v_partial;
10264 func = partial->pt_name;
10265 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010266 else
10267 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010268 if (*func == NUL)
10269 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010270
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 if (argvars[2].v_type != VAR_UNKNOWN)
10272 {
10273 if (argvars[2].v_type != VAR_DICT)
10274 {
10275 EMSG(_(e_dictreq));
10276 return;
10277 }
10278 selfdict = argvars[2].vval.v_dict;
10279 }
10280
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010281 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010282}
10283
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010284#ifdef FEAT_FLOAT
10285/*
10286 * "ceil({float})" function
10287 */
10288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010289f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010290{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010291 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010292
10293 rettv->v_type = VAR_FLOAT;
10294 if (get_float_arg(argvars, &f) == OK)
10295 rettv->vval.v_float = ceil(f);
10296 else
10297 rettv->vval.v_float = 0.0;
10298}
10299#endif
10300
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010301#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010302/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010303 * "ch_close()" function
10304 */
10305 static void
10306f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10307{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010308 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010309
10310 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010311 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010312 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010313 channel_clear(channel);
10314 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010315}
10316
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010317/*
10318 * "ch_getbufnr()" function
10319 */
10320 static void
10321f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10322{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010323 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010324
10325 rettv->vval.v_number = -1;
10326 if (channel != NULL)
10327 {
10328 char_u *what = get_tv_string(&argvars[1]);
10329 int part;
10330
10331 if (STRCMP(what, "err") == 0)
10332 part = PART_ERR;
10333 else if (STRCMP(what, "out") == 0)
10334 part = PART_OUT;
10335 else if (STRCMP(what, "in") == 0)
10336 part = PART_IN;
10337 else
10338 part = PART_SOCK;
10339 if (channel->ch_part[part].ch_buffer != NULL)
10340 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10341 }
10342}
10343
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010344/*
10345 * "ch_getjob()" function
10346 */
10347 static void
10348f_ch_getjob(typval_T *argvars, typval_T *rettv)
10349{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010350 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010351
10352 if (channel != NULL)
10353 {
10354 rettv->v_type = VAR_JOB;
10355 rettv->vval.v_job = channel->ch_job;
10356 if (channel->ch_job != NULL)
10357 ++channel->ch_job->jv_refcount;
10358 }
10359}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010360
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010361/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010362 * "ch_info()" function
10363 */
10364 static void
10365f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10366{
10367 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10368
10369 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10370 channel_info(channel, rettv->vval.v_dict);
10371}
10372
10373/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010374 * "ch_log()" function
10375 */
10376 static void
10377f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10378{
10379 char_u *msg = get_tv_string(&argvars[0]);
10380 channel_T *channel = NULL;
10381
10382 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010383 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010384
10385 ch_log(channel, (char *)msg);
10386}
10387
10388/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010389 * "ch_logfile()" function
10390 */
10391 static void
10392f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10393{
10394 char_u *fname;
10395 char_u *opt = (char_u *)"";
10396 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010397
10398 fname = get_tv_string(&argvars[0]);
10399 if (argvars[1].v_type == VAR_STRING)
10400 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010401 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010402}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010403
10404/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010405 * "ch_open()" function
10406 */
10407 static void
10408f_ch_open(typval_T *argvars, typval_T *rettv)
10409{
Bram Moolenaar77073442016-02-13 23:23:53 +010010410 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010411 if (check_restricted() || check_secure())
10412 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010413 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010414}
10415
10416/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010417 * "ch_read()" function
10418 */
10419 static void
10420f_ch_read(typval_T *argvars, typval_T *rettv)
10421{
10422 common_channel_read(argvars, rettv, FALSE);
10423}
10424
10425/*
10426 * "ch_readraw()" function
10427 */
10428 static void
10429f_ch_readraw(typval_T *argvars, typval_T *rettv)
10430{
10431 common_channel_read(argvars, rettv, TRUE);
10432}
10433
10434/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010435 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010436 */
10437 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010438f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10439{
10440 ch_expr_common(argvars, rettv, TRUE);
10441}
10442
10443/*
10444 * "ch_sendexpr()" function
10445 */
10446 static void
10447f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10448{
10449 ch_expr_common(argvars, rettv, FALSE);
10450}
10451
10452/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010453 * "ch_evalraw()" function
10454 */
10455 static void
10456f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10457{
10458 ch_raw_common(argvars, rettv, TRUE);
10459}
10460
10461/*
10462 * "ch_sendraw()" function
10463 */
10464 static void
10465f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10466{
10467 ch_raw_common(argvars, rettv, FALSE);
10468}
10469
10470/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010471 * "ch_setoptions()" function
10472 */
10473 static void
10474f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10475{
10476 channel_T *channel;
10477 jobopt_T opt;
10478
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010479 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010480 if (channel == NULL)
10481 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010482 clear_job_options(&opt);
10483 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010484 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10485 channel_set_options(channel, &opt);
10486 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010487}
10488
10489/*
10490 * "ch_status()" function
10491 */
10492 static void
10493f_ch_status(typval_T *argvars, typval_T *rettv)
10494{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010495 channel_T *channel;
10496
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010497 /* return an empty string by default */
10498 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010499 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010500
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010501 channel = get_channel_arg(&argvars[0], FALSE);
10502 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010503}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010504#endif
10505
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010506/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010507 * "changenr()" function
10508 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010510f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010511{
10512 rettv->vval.v_number = curbuf->b_u_seq_cur;
10513}
10514
10515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 * "char2nr(string)" function
10517 */
10518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010519f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520{
10521#ifdef FEAT_MBYTE
10522 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010523 {
10524 int utf8 = 0;
10525
10526 if (argvars[1].v_type != VAR_UNKNOWN)
10527 utf8 = get_tv_number_chk(&argvars[1], NULL);
10528
10529 if (utf8)
10530 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10531 else
10532 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 else
10535#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010536 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537}
10538
10539/*
10540 * "cindent(lnum)" function
10541 */
10542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010543f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544{
10545#ifdef FEAT_CINDENT
10546 pos_T pos;
10547 linenr_T lnum;
10548
10549 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10552 {
10553 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 curwin->w_cursor = pos;
10556 }
10557 else
10558#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010559 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560}
10561
10562/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010563 * "clearmatches()" function
10564 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010566f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010567{
10568#ifdef FEAT_SEARCH_EXTRA
10569 clear_matches(curwin);
10570#endif
10571}
10572
10573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 * "col(string)" function
10575 */
10576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010577f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578{
10579 colnr_T col = 0;
10580 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010581 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010583 fp = var2fpos(&argvars[0], FALSE, &fnum);
10584 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 {
10586 if (fp->col == MAXCOL)
10587 {
10588 /* '> can be MAXCOL, get the length of the line then */
10589 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010590 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 else
10592 col = MAXCOL;
10593 }
10594 else
10595 {
10596 col = fp->col + 1;
10597#ifdef FEAT_VIRTUALEDIT
10598 /* col(".") when the cursor is on the NUL at the end of the line
10599 * because of "coladd" can be seen as an extra column. */
10600 if (virtual_active() && fp == &curwin->w_cursor)
10601 {
10602 char_u *p = ml_get_cursor();
10603
10604 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10605 curwin->w_virtcol - curwin->w_cursor.coladd))
10606 {
10607# ifdef FEAT_MBYTE
10608 int l;
10609
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010610 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 col += l;
10612# else
10613 if (*p != NUL && p[1] == NUL)
10614 ++col;
10615# endif
10616 }
10617 }
10618#endif
10619 }
10620 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010621 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622}
10623
Bram Moolenaar572cb562005-08-05 21:35:02 +000010624#if defined(FEAT_INS_EXPAND)
10625/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010626 * "complete()" function
10627 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010629f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010630{
10631 int startcol;
10632
10633 if ((State & INSERT) == 0)
10634 {
10635 EMSG(_("E785: complete() can only be used in Insert mode"));
10636 return;
10637 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010638
10639 /* Check for undo allowed here, because if something was already inserted
10640 * the line was already saved for undo and this check isn't done. */
10641 if (!undo_allowed())
10642 return;
10643
Bram Moolenaarade00832006-03-10 21:46:58 +000010644 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10645 {
10646 EMSG(_(e_invarg));
10647 return;
10648 }
10649
10650 startcol = get_tv_number_chk(&argvars[0], NULL);
10651 if (startcol <= 0)
10652 return;
10653
10654 set_completion(startcol - 1, argvars[1].vval.v_list);
10655}
10656
10657/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010658 * "complete_add()" function
10659 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010661f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010662{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010663 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010664}
10665
10666/*
10667 * "complete_check()" function
10668 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010670f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010671{
10672 int saved = RedrawingDisabled;
10673
10674 RedrawingDisabled = 0;
10675 ins_compl_check_keys(0);
10676 rettv->vval.v_number = compl_interrupted;
10677 RedrawingDisabled = saved;
10678}
10679#endif
10680
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681/*
10682 * "confirm(message, buttons[, default [, type]])" function
10683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010685f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686{
10687#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10688 char_u *message;
10689 char_u *buttons = NULL;
10690 char_u buf[NUMBUFLEN];
10691 char_u buf2[NUMBUFLEN];
10692 int def = 1;
10693 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010694 char_u *typestr;
10695 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010697 message = get_tv_string_chk(&argvars[0]);
10698 if (message == NULL)
10699 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010700 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010702 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10703 if (buttons == NULL)
10704 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010705 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010708 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10711 if (typestr == NULL)
10712 error = TRUE;
10713 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010715 switch (TOUPPER_ASC(*typestr))
10716 {
10717 case 'E': type = VIM_ERROR; break;
10718 case 'Q': type = VIM_QUESTION; break;
10719 case 'I': type = VIM_INFO; break;
10720 case 'W': type = VIM_WARNING; break;
10721 case 'G': type = VIM_GENERIC; break;
10722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 }
10724 }
10725 }
10726 }
10727
10728 if (buttons == NULL || *buttons == NUL)
10729 buttons = (char_u *)_("&Ok");
10730
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010731 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010732 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010733 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734#endif
10735}
10736
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010737/*
10738 * "copy()" function
10739 */
10740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010741f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010742{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010743 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010744}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010746#ifdef FEAT_FLOAT
10747/*
10748 * "cos()" function
10749 */
10750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010751f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010752{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010753 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010754
10755 rettv->v_type = VAR_FLOAT;
10756 if (get_float_arg(argvars, &f) == OK)
10757 rettv->vval.v_float = cos(f);
10758 else
10759 rettv->vval.v_float = 0.0;
10760}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010761
10762/*
10763 * "cosh()" function
10764 */
10765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010766f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010767{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010768 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010769
10770 rettv->v_type = VAR_FLOAT;
10771 if (get_float_arg(argvars, &f) == OK)
10772 rettv->vval.v_float = cosh(f);
10773 else
10774 rettv->vval.v_float = 0.0;
10775}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010776#endif
10777
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010779 * "count()" function
10780 */
10781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010782f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010783{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010784 long n = 0;
10785 int ic = FALSE;
10786
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010788 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 listitem_T *li;
10790 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010791 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010792
Bram Moolenaare9a41262005-01-15 22:18:47 +000010793 if ((l = argvars[0].vval.v_list) != NULL)
10794 {
10795 li = l->lv_first;
10796 if (argvars[2].v_type != VAR_UNKNOWN)
10797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 int error = FALSE;
10799
10800 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010801 if (argvars[3].v_type != VAR_UNKNOWN)
10802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010803 idx = get_tv_number_chk(&argvars[3], &error);
10804 if (!error)
10805 {
10806 li = list_find(l, idx);
10807 if (li == NULL)
10808 EMSGN(_(e_listidx), idx);
10809 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010810 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811 if (error)
10812 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010813 }
10814
10815 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010816 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010817 ++n;
10818 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820 else if (argvars[0].v_type == VAR_DICT)
10821 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010822 int todo;
10823 dict_T *d;
10824 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010825
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010826 if ((d = argvars[0].vval.v_dict) != NULL)
10827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 int error = FALSE;
10829
Bram Moolenaare9a41262005-01-15 22:18:47 +000010830 if (argvars[2].v_type != VAR_UNKNOWN)
10831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010832 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010833 if (argvars[3].v_type != VAR_UNKNOWN)
10834 EMSG(_(e_invarg));
10835 }
10836
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010837 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010838 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010839 {
10840 if (!HASHITEM_EMPTY(hi))
10841 {
10842 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010843 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010844 ++n;
10845 }
10846 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010847 }
10848 }
10849 else
10850 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010851 rettv->vval.v_number = n;
10852}
10853
10854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10856 *
10857 * Checks the existence of a cscope connection.
10858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010860f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861{
10862#ifdef FEAT_CSCOPE
10863 int num = 0;
10864 char_u *dbpath = NULL;
10865 char_u *prepend = NULL;
10866 char_u buf[NUMBUFLEN];
10867
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010868 if (argvars[0].v_type != VAR_UNKNOWN
10869 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871 num = (int)get_tv_number(&argvars[0]);
10872 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010873 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 }
10876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010877 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878#endif
10879}
10880
10881/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010882 * "cursor(lnum, col)" function, or
10883 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010885 * Moves the cursor to the specified line and column.
10886 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010889f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890{
10891 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010892#ifdef FEAT_VIRTUALEDIT
10893 long coladd = 0;
10894#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010895 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010897 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010898 if (argvars[1].v_type == VAR_UNKNOWN)
10899 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010900 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010901 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010902
Bram Moolenaar493c1782014-05-28 14:34:46 +020010903 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010904 {
10905 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010906 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010907 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010908 line = pos.lnum;
10909 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010910#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010911 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010912#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010913 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010914 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010915 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010916 set_curswant = FALSE;
10917 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010918 }
10919 else
10920 {
10921 line = get_tv_lnum(argvars);
10922 col = get_tv_number_chk(&argvars[1], NULL);
10923#ifdef FEAT_VIRTUALEDIT
10924 if (argvars[2].v_type != VAR_UNKNOWN)
10925 coladd = get_tv_number_chk(&argvars[2], NULL);
10926#endif
10927 }
10928 if (line < 0 || col < 0
10929#ifdef FEAT_VIRTUALEDIT
10930 || coladd < 0
10931#endif
10932 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010933 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 if (line > 0)
10935 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 if (col > 0)
10937 curwin->w_cursor.col = col - 1;
10938#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010939 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940#endif
10941
10942 /* Make sure the cursor is in a valid position. */
10943 check_cursor();
10944#ifdef FEAT_MBYTE
10945 /* Correct cursor for multi-byte character. */
10946 if (has_mbyte)
10947 mb_adjust_cursor();
10948#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010949
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010950 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010951 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952}
10953
10954/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010955 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 */
10957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010958f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010960 int noref = 0;
10961
10962 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010963 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010964 if (noref < 0 || noref > 1)
10965 EMSG(_(e_invarg));
10966 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010967 {
10968 current_copyID += COPYID_INC;
10969 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971}
10972
10973/*
10974 * "delete()" function
10975 */
10976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010977f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010979 char_u nbuf[NUMBUFLEN];
10980 char_u *name;
10981 char_u *flags;
10982
10983 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010985 return;
10986
10987 name = get_tv_string(&argvars[0]);
10988 if (name == NULL || *name == NUL)
10989 {
10990 EMSG(_(e_invarg));
10991 return;
10992 }
10993
10994 if (argvars[1].v_type != VAR_UNKNOWN)
10995 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010997 flags = (char_u *)"";
10998
10999 if (*flags == NUL)
11000 /* delete a file */
11001 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11002 else if (STRCMP(flags, "d") == 0)
11003 /* delete an empty directory */
11004 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11005 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011006 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011007 rettv->vval.v_number = delete_recursive(name);
11008 else
11009 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010}
11011
11012/*
11013 * "did_filetype()" function
11014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011016f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017{
11018#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011019 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020#endif
11021}
11022
11023/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011024 * "diff_filler()" function
11025 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011027f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011028{
11029#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011031#endif
11032}
11033
11034/*
11035 * "diff_hlID()" function
11036 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011038f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011039{
11040#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011042 static linenr_T prev_lnum = 0;
11043 static int changedtick = 0;
11044 static int fnum = 0;
11045 static int change_start = 0;
11046 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011047 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011048 int filler_lines;
11049 int col;
11050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011051 if (lnum < 0) /* ignore type error in {lnum} arg */
11052 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011053 if (lnum != prev_lnum
11054 || changedtick != curbuf->b_changedtick
11055 || fnum != curbuf->b_fnum)
11056 {
11057 /* New line, buffer, change: need to get the values. */
11058 filler_lines = diff_check(curwin, lnum);
11059 if (filler_lines < 0)
11060 {
11061 if (filler_lines == -1)
11062 {
11063 change_start = MAXCOL;
11064 change_end = -1;
11065 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11066 hlID = HLF_ADD; /* added line */
11067 else
11068 hlID = HLF_CHD; /* changed line */
11069 }
11070 else
11071 hlID = HLF_ADD; /* added line */
11072 }
11073 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011074 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011075 prev_lnum = lnum;
11076 changedtick = curbuf->b_changedtick;
11077 fnum = curbuf->b_fnum;
11078 }
11079
11080 if (hlID == HLF_CHD || hlID == HLF_TXD)
11081 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011082 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011083 if (col >= change_start && col <= change_end)
11084 hlID = HLF_TXD; /* changed text */
11085 else
11086 hlID = HLF_CHD; /* changed line */
11087 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011088 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011089#endif
11090}
11091
11092/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011093 * "disable_char_avail_for_testing({expr})" function
11094 */
11095 static void
11096f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11097{
11098 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11099}
11100
11101/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011102 * "empty({expr})" function
11103 */
11104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011105f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011106{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011107 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011108
11109 switch (argvars[0].v_type)
11110 {
11111 case VAR_STRING:
11112 case VAR_FUNC:
11113 n = argvars[0].vval.v_string == NULL
11114 || *argvars[0].vval.v_string == NUL;
11115 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011116 case VAR_PARTIAL:
11117 n = FALSE;
11118 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011119 case VAR_NUMBER:
11120 n = argvars[0].vval.v_number == 0;
11121 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011122 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011123#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011124 n = argvars[0].vval.v_float == 0.0;
11125 break;
11126#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011127 case VAR_LIST:
11128 n = argvars[0].vval.v_list == NULL
11129 || argvars[0].vval.v_list->lv_first == NULL;
11130 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011131 case VAR_DICT:
11132 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011133 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011134 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011135 case VAR_SPECIAL:
11136 n = argvars[0].vval.v_number != VVAL_TRUE;
11137 break;
11138
Bram Moolenaar835dc632016-02-07 14:27:38 +010011139 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011141 n = argvars[0].vval.v_job == NULL
11142 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11143 break;
11144#endif
11145 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011146#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011147 n = argvars[0].vval.v_channel == NULL
11148 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011149 break;
11150#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011151 case VAR_UNKNOWN:
11152 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11153 n = TRUE;
11154 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011155 }
11156
11157 rettv->vval.v_number = n;
11158}
11159
11160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 * "escape({string}, {chars})" function
11162 */
11163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011164f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165{
11166 char_u buf[NUMBUFLEN];
11167
Bram Moolenaar758711c2005-02-02 23:11:38 +000011168 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11169 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171}
11172
11173/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011174 * "eval()" function
11175 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011177f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011178{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011179 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011180
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011181 s = get_tv_string_chk(&argvars[0]);
11182 if (s != NULL)
11183 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011184
Bram Moolenaar615b9972015-01-14 17:15:05 +010011185 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011186 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11187 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011188 if (p != NULL && !aborting())
11189 EMSG2(_(e_invexpr2), p);
11190 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011191 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011192 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011193 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011194 else if (*s != NUL)
11195 EMSG(_(e_trailing));
11196}
11197
11198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 * "eventhandler()" function
11200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011202f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011204 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205}
11206
11207/*
11208 * "executable()" function
11209 */
11210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011211f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011213 char_u *name = get_tv_string(&argvars[0]);
11214
11215 /* Check in $PATH and also check directly if there is a directory name. */
11216 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11217 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011218}
11219
11220/*
11221 * "exepath()" function
11222 */
11223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011224f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011225{
11226 char_u *p = NULL;
11227
Bram Moolenaarb5971142015-03-21 17:32:19 +010011228 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011229 rettv->v_type = VAR_STRING;
11230 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231}
11232
11233/*
11234 * "exists()" function
11235 */
11236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011237f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238{
11239 char_u *p;
11240 char_u *name;
11241 int n = FALSE;
11242 int len = 0;
11243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245 if (*p == '$') /* environment variable */
11246 {
11247 /* first try "normal" environment variables (fast) */
11248 if (mch_getenv(p + 1) != NULL)
11249 n = TRUE;
11250 else
11251 {
11252 /* try expanding things like $VIM and ${HOME} */
11253 p = expand_env_save(p);
11254 if (p != NULL && *p != '$')
11255 n = TRUE;
11256 vim_free(p);
11257 }
11258 }
11259 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011262 if (*skipwhite(p) != NUL)
11263 n = FALSE; /* trailing garbage */
11264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 else if (*p == '*') /* internal or user defined function */
11266 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011267 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 }
11269 else if (*p == ':')
11270 {
11271 n = cmd_exists(p + 1);
11272 }
11273 else if (*p == '#')
11274 {
11275#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011276 if (p[1] == '#')
11277 n = autocmd_supported(p + 2);
11278 else
11279 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280#endif
11281 }
11282 else /* internal variable */
11283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011284 char_u *tofree;
11285 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011287 /* get_name_len() takes care of expanding curly braces */
11288 name = p;
11289 len = get_name_len(&p, &tofree, TRUE, FALSE);
11290 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011292 if (tofree != NULL)
11293 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011294 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011295 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011297 /* handle d.key, l[idx], f(expr) */
11298 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11299 if (n)
11300 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 }
11302 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011303 if (*p != NUL)
11304 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011306 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 }
11308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310}
11311
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011312#ifdef FEAT_FLOAT
11313/*
11314 * "exp()" function
11315 */
11316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011317f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011318{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011319 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011320
11321 rettv->v_type = VAR_FLOAT;
11322 if (get_float_arg(argvars, &f) == OK)
11323 rettv->vval.v_float = exp(f);
11324 else
11325 rettv->vval.v_float = 0.0;
11326}
11327#endif
11328
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329/*
11330 * "expand()" function
11331 */
11332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011333f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
11335 char_u *s;
11336 int len;
11337 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011338 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011340 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011341 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011344 if (argvars[1].v_type != VAR_UNKNOWN
11345 && argvars[2].v_type != VAR_UNKNOWN
11346 && get_tv_number_chk(&argvars[2], &error)
11347 && !error)
11348 {
11349 rettv->v_type = VAR_LIST;
11350 rettv->vval.v_list = NULL;
11351 }
11352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 if (*s == '%' || *s == '#' || *s == '<')
11355 {
11356 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011357 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011359 if (rettv->v_type == VAR_LIST)
11360 {
11361 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11362 list_append_string(rettv->vval.v_list, result, -1);
11363 else
11364 vim_free(result);
11365 }
11366 else
11367 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 }
11369 else
11370 {
11371 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011372 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 if (argvars[1].v_type != VAR_UNKNOWN
11374 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011375 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011376 if (!error)
11377 {
11378 ExpandInit(&xpc);
11379 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011380 if (p_wic)
11381 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011382 if (rettv->v_type == VAR_STRING)
11383 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11384 options, WILD_ALL);
11385 else if (rettv_list_alloc(rettv) != FAIL)
11386 {
11387 int i;
11388
11389 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11390 for (i = 0; i < xpc.xp_numfiles; i++)
11391 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11392 ExpandCleanup(&xpc);
11393 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011394 }
11395 else
11396 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 }
11398}
11399
11400/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011401 * Go over all entries in "d2" and add them to "d1".
11402 * When "action" is "error" then a duplicate key is an error.
11403 * When "action" is "force" then a duplicate key is overwritten.
11404 * Otherwise duplicate keys are ignored ("action" is "keep").
11405 */
11406 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011407dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011408{
11409 dictitem_T *di1;
11410 hashitem_T *hi2;
11411 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011412 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011413
11414 todo = (int)d2->dv_hashtab.ht_used;
11415 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11416 {
11417 if (!HASHITEM_EMPTY(hi2))
11418 {
11419 --todo;
11420 di1 = dict_find(d1, hi2->hi_key, -1);
11421 if (d1->dv_scope != 0)
11422 {
11423 /* Disallow replacing a builtin function in l: and g:.
11424 * Check the key to be valid when adding to any
11425 * scope. */
11426 if (d1->dv_scope == VAR_DEF_SCOPE
11427 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11428 && var_check_func_name(hi2->hi_key,
11429 di1 == NULL))
11430 break;
11431 if (!valid_varname(hi2->hi_key))
11432 break;
11433 }
11434 if (di1 == NULL)
11435 {
11436 di1 = dictitem_copy(HI2DI(hi2));
11437 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11438 dictitem_free(di1);
11439 }
11440 else if (*action == 'e')
11441 {
11442 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11443 break;
11444 }
11445 else if (*action == 'f' && HI2DI(hi2) != di1)
11446 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011447 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11448 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011449 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011450 clear_tv(&di1->di_tv);
11451 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11452 }
11453 }
11454 }
11455}
11456
11457/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011458 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011459 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011460 */
11461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011462f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011463{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011464 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011465
Bram Moolenaare9a41262005-01-15 22:18:47 +000011466 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011467 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011468 list_T *l1, *l2;
11469 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011470 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011471 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011472
Bram Moolenaare9a41262005-01-15 22:18:47 +000011473 l1 = argvars[0].vval.v_list;
11474 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011475 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011476 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011477 {
11478 if (argvars[2].v_type != VAR_UNKNOWN)
11479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011480 before = get_tv_number_chk(&argvars[2], &error);
11481 if (error)
11482 return; /* type error; errmsg already given */
11483
Bram Moolenaar758711c2005-02-02 23:11:38 +000011484 if (before == l1->lv_len)
11485 item = NULL;
11486 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011487 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011488 item = list_find(l1, before);
11489 if (item == NULL)
11490 {
11491 EMSGN(_(e_listidx), before);
11492 return;
11493 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011494 }
11495 }
11496 else
11497 item = NULL;
11498 list_extend(l1, l2, item);
11499
Bram Moolenaare9a41262005-01-15 22:18:47 +000011500 copy_tv(&argvars[0], rettv);
11501 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011502 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011503 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11504 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011505 dict_T *d1, *d2;
11506 char_u *action;
11507 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011508
11509 d1 = argvars[0].vval.v_dict;
11510 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011511 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011512 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011513 {
11514 /* Check the third argument. */
11515 if (argvars[2].v_type != VAR_UNKNOWN)
11516 {
11517 static char *(av[]) = {"keep", "force", "error"};
11518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519 action = get_tv_string_chk(&argvars[2]);
11520 if (action == NULL)
11521 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011522 for (i = 0; i < 3; ++i)
11523 if (STRCMP(action, av[i]) == 0)
11524 break;
11525 if (i == 3)
11526 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011527 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011528 return;
11529 }
11530 }
11531 else
11532 action = (char_u *)"force";
11533
Bram Moolenaara9922d62013-05-30 13:01:18 +020011534 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011535
Bram Moolenaare9a41262005-01-15 22:18:47 +000011536 copy_tv(&argvars[0], rettv);
11537 }
11538 }
11539 else
11540 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011541}
11542
11543/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011544 * "feedkeys()" function
11545 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011547f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011548{
11549 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011550 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011551 char_u *keys, *flags;
11552 char_u nbuf[NUMBUFLEN];
11553 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011554 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011555 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011556 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011557
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011558 /* This is not allowed in the sandbox. If the commands would still be
11559 * executed in the sandbox it would be OK, but it probably happens later,
11560 * when "sandbox" is no longer set. */
11561 if (check_secure())
11562 return;
11563
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011564 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011565
11566 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011567 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011568 flags = get_tv_string_buf(&argvars[1], nbuf);
11569 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011570 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011571 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011572 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011573 case 'n': remap = FALSE; break;
11574 case 'm': remap = TRUE; break;
11575 case 't': typed = TRUE; break;
11576 case 'i': insert = TRUE; break;
11577 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011578 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011579 }
11580 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011581 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011582
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011583 if (*keys != NUL || execute)
11584 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011585 /* Need to escape K_SPECIAL and CSI before putting the string in the
11586 * typeahead buffer. */
11587 keys_esc = vim_strsave_escape_csi(keys);
11588 if (keys_esc != NULL)
11589 {
11590 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011591 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011592 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011593 if (vgetc_busy)
11594 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011595 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011596 {
11597 int save_msg_scroll = msg_scroll;
11598
11599 /* Avoid a 1 second delay when the keys start Insert mode. */
11600 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011601
Bram Moolenaar245c4102016-04-20 17:37:41 +020011602 if (!dangerous)
11603 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011604 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011605 if (!dangerous)
11606 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011607 msg_scroll |= save_msg_scroll;
11608 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011609 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011610 }
11611}
11612
11613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614 * "filereadable()" function
11615 */
11616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011617f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011619 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620 char_u *p;
11621 int n;
11622
Bram Moolenaarc236c162008-07-13 17:41:49 +000011623#ifndef O_NONBLOCK
11624# define O_NONBLOCK 0
11625#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011626 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011627 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11628 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 {
11630 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011631 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 }
11633 else
11634 n = FALSE;
11635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011636 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637}
11638
11639/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011640 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 * rights to write into.
11642 */
11643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011644f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011646 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647}
11648
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011649 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011650findfilendir(
11651 typval_T *argvars UNUSED,
11652 typval_T *rettv,
11653 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011654{
11655#ifdef FEAT_SEARCHPATH
11656 char_u *fname;
11657 char_u *fresult = NULL;
11658 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11659 char_u *p;
11660 char_u pathbuf[NUMBUFLEN];
11661 int count = 1;
11662 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011663 int error = FALSE;
11664#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011665
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011666 rettv->vval.v_string = NULL;
11667 rettv->v_type = VAR_STRING;
11668
11669#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011671
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011672 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011674 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11675 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011676 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011677 else
11678 {
11679 if (*p != NUL)
11680 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011681
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011683 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011684 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011685 }
11686
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011687 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11688 error = TRUE;
11689
11690 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011692 do
11693 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011694 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011695 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011696 fresult = find_file_in_path_option(first ? fname : NULL,
11697 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011698 0, first, path,
11699 find_what,
11700 curbuf->b_ffname,
11701 find_what == FINDFILE_DIR
11702 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011703 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011704
11705 if (fresult != NULL && rettv->v_type == VAR_LIST)
11706 list_append_string(rettv->vval.v_list, fresult, -1);
11707
11708 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011710
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011711 if (rettv->v_type == VAR_STRING)
11712 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011713#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011714}
11715
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011716static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11717static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011718
11719/*
11720 * Implementation of map() and filter().
11721 */
11722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011723filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011724{
11725 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011726 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011727 listitem_T *li, *nli;
11728 list_T *l = NULL;
11729 dictitem_T *di;
11730 hashtab_T *ht;
11731 hashitem_T *hi;
11732 dict_T *d = NULL;
11733 typval_T save_val;
11734 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011735 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011736 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011737 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011738 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011739 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011740 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011741 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011742
Bram Moolenaare9a41262005-01-15 22:18:47 +000011743 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011744 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011745 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011746 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011747 return;
11748 }
11749 else if (argvars[0].v_type == VAR_DICT)
11750 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011751 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011752 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011753 return;
11754 }
11755 else
11756 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011757 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011758 return;
11759 }
11760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011761 expr = get_tv_string_buf_chk(&argvars[1], buf);
11762 /* On type errors, the preceding call has already displayed an error
11763 * message. Avoid a misleading error message for an empty string that
11764 * was not passed as argument. */
11765 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011767 prepare_vimvar(VV_VAL, &save_val);
11768 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011769
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011770 /* We reset "did_emsg" to be able to detect whether an error
11771 * occurred during evaluation of the expression. */
11772 save_did_emsg = did_emsg;
11773 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011774
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011775 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011776 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011778 vimvars[VV_KEY].vv_type = VAR_STRING;
11779
11780 ht = &d->dv_hashtab;
11781 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011782 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011783 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 if (!HASHITEM_EMPTY(hi))
11786 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011787 int r;
11788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011789 --todo;
11790 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011791 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011792 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11793 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011794 break;
11795 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011796 r = filter_map_one(&di->di_tv, expr, map, &rem);
11797 clear_tv(&vimvars[VV_KEY].vv_tv);
11798 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011799 break;
11800 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011801 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011802 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11803 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011804 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011805 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011806 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011807 }
11808 }
11809 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011810 }
11811 else
11812 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011813 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 for (li = l->lv_first; li != NULL; li = nli)
11816 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011817 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011818 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011820 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011821 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011822 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011823 break;
11824 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011825 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011826 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011827 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011828 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011829
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011830 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011831 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011832
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011833 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011834 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011835
11836 copy_tv(&argvars[0], rettv);
11837}
11838
11839 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011840filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011841{
Bram Moolenaar33570922005-01-25 22:26:29 +000011842 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011843 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011844 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011845
Bram Moolenaar33570922005-01-25 22:26:29 +000011846 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011847 s = expr;
11848 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011849 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011850 if (*s != NUL) /* check for trailing chars after expr */
11851 {
11852 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011853 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011854 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011855 }
11856 if (map)
11857 {
11858 /* map(): replace the list item value */
11859 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011860 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011861 *tv = rettv;
11862 }
11863 else
11864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011865 int error = FALSE;
11866
Bram Moolenaare9a41262005-01-15 22:18:47 +000011867 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011868 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011869 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011870 /* On type error, nothing has been removed; return FAIL to stop the
11871 * loop. The error message was given by get_tv_number_chk(). */
11872 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011873 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011874 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011875 retval = OK;
11876theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011877 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011878 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011879}
11880
11881/*
11882 * "filter()" function
11883 */
11884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011885f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011886{
11887 filter_map(argvars, rettv, FALSE);
11888}
11889
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011890/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011891 * "finddir({fname}[, {path}[, {count}]])" function
11892 */
11893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011894f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011895{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011896 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011897}
11898
11899/*
11900 * "findfile({fname}[, {path}[, {count}]])" function
11901 */
11902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011903f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011904{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011905 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011906}
11907
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011908#ifdef FEAT_FLOAT
11909/*
11910 * "float2nr({float})" function
11911 */
11912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011913f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011914{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011915 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011916
11917 if (get_float_arg(argvars, &f) == OK)
11918 {
11919 if (f < -0x7fffffff)
11920 rettv->vval.v_number = -0x7fffffff;
11921 else if (f > 0x7fffffff)
11922 rettv->vval.v_number = 0x7fffffff;
11923 else
11924 rettv->vval.v_number = (varnumber_T)f;
11925 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011926}
11927
11928/*
11929 * "floor({float})" function
11930 */
11931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011932f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011933{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011934 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011935
11936 rettv->v_type = VAR_FLOAT;
11937 if (get_float_arg(argvars, &f) == OK)
11938 rettv->vval.v_float = floor(f);
11939 else
11940 rettv->vval.v_float = 0.0;
11941}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011942
11943/*
11944 * "fmod()" function
11945 */
11946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011947f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011948{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011949 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011950
11951 rettv->v_type = VAR_FLOAT;
11952 if (get_float_arg(argvars, &fx) == OK
11953 && get_float_arg(&argvars[1], &fy) == OK)
11954 rettv->vval.v_float = fmod(fx, fy);
11955 else
11956 rettv->vval.v_float = 0.0;
11957}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011958#endif
11959
Bram Moolenaar0d660222005-01-07 21:51:51 +000011960/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011961 * "fnameescape({string})" function
11962 */
11963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011964f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011965{
11966 rettv->vval.v_string = vim_strsave_fnameescape(
11967 get_tv_string(&argvars[0]), FALSE);
11968 rettv->v_type = VAR_STRING;
11969}
11970
11971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972 * "fnamemodify({fname}, {mods})" function
11973 */
11974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011975f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976{
11977 char_u *fname;
11978 char_u *mods;
11979 int usedlen = 0;
11980 int len;
11981 char_u *fbuf = NULL;
11982 char_u buf[NUMBUFLEN];
11983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011984 fname = get_tv_string_chk(&argvars[0]);
11985 mods = get_tv_string_buf_chk(&argvars[1], buf);
11986 if (fname == NULL || mods == NULL)
11987 fname = NULL;
11988 else
11989 {
11990 len = (int)STRLEN(fname);
11991 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011994 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011996 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011998 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999 vim_free(fbuf);
12000}
12001
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012002static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003
12004/*
12005 * "foldclosed()" function
12006 */
12007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012008foldclosed_both(
12009 typval_T *argvars UNUSED,
12010 typval_T *rettv,
12011 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012{
12013#ifdef FEAT_FOLDING
12014 linenr_T lnum;
12015 linenr_T first, last;
12016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012017 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12019 {
12020 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12021 {
12022 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026 return;
12027 }
12028 }
12029#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012030 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031}
12032
12033/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012034 * "foldclosed()" function
12035 */
12036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012037f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012038{
12039 foldclosed_both(argvars, rettv, FALSE);
12040}
12041
12042/*
12043 * "foldclosedend()" function
12044 */
12045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012046f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012047{
12048 foldclosed_both(argvars, rettv, TRUE);
12049}
12050
12051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052 * "foldlevel()" function
12053 */
12054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012055f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056{
12057#ifdef FEAT_FOLDING
12058 linenr_T lnum;
12059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012060 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064}
12065
12066/*
12067 * "foldtext()" function
12068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012070f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071{
12072#ifdef FEAT_FOLDING
12073 linenr_T lnum;
12074 char_u *s;
12075 char_u *r;
12076 int len;
12077 char *txt;
12078#endif
12079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080 rettv->v_type = VAR_STRING;
12081 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012083 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12084 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12085 <= curbuf->b_ml.ml_line_count
12086 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 {
12088 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012089 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12090 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 {
12092 if (!linewhite(lnum))
12093 break;
12094 ++lnum;
12095 }
12096
12097 /* Find interesting text in this line. */
12098 s = skipwhite(ml_get(lnum));
12099 /* skip C comment-start */
12100 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012103 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012104 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012105 {
12106 s = skipwhite(ml_get(lnum + 1));
12107 if (*s == '*')
12108 s = skipwhite(s + 1);
12109 }
12110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111 txt = _("+-%s%3ld lines: ");
12112 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012113 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114 + 20 /* for %3ld */
12115 + STRLEN(s))); /* concatenated */
12116 if (r != NULL)
12117 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012118 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12119 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12120 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121 len = (int)STRLEN(r);
12122 STRCAT(r, s);
12123 /* remove 'foldmarker' and 'commentstring' */
12124 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 }
12127 }
12128#endif
12129}
12130
12131/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012132 * "foldtextresult(lnum)" function
12133 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012135f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012136{
12137#ifdef FEAT_FOLDING
12138 linenr_T lnum;
12139 char_u *text;
12140 char_u buf[51];
12141 foldinfo_T foldinfo;
12142 int fold_count;
12143#endif
12144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012145 rettv->v_type = VAR_STRING;
12146 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012147#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012149 /* treat illegal types and illegal string values for {lnum} the same */
12150 if (lnum < 0)
12151 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012152 fold_count = foldedCount(curwin, lnum, &foldinfo);
12153 if (fold_count > 0)
12154 {
12155 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12156 &foldinfo, buf);
12157 if (text == buf)
12158 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012159 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012160 }
12161#endif
12162}
12163
12164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 * "foreground()" function
12166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012168f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170#ifdef FEAT_GUI
12171 if (gui.in_use)
12172 gui_mch_set_foreground();
12173#else
12174# ifdef WIN32
12175 win32_set_foreground();
12176# endif
12177#endif
12178}
12179
12180/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012181 * "function()" function
12182 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012184f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012185{
12186 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012187 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012188 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012189 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012190
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012191 if (argvars[0].v_type == VAR_FUNC)
12192 {
12193 /* function(MyFunc, [arg], dict) */
12194 s = argvars[0].vval.v_string;
12195 }
12196 else if (argvars[0].v_type == VAR_PARTIAL
12197 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012198 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012199 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012200 arg_pt = argvars[0].vval.v_partial;
12201 s = arg_pt->pt_name;
12202 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012203 else
12204 {
12205 /* function('MyFunc', [arg], dict) */
12206 s = get_tv_string(&argvars[0]);
12207 use_string = TRUE;
12208 }
12209
12210 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012211 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012212 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012213 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12214 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012215 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012216 else
12217 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012218 int dict_idx = 0;
12219 int arg_idx = 0;
12220 list_T *list = NULL;
12221
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012222 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012223 {
12224 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012225 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012226
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012227 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12228 * also be called from another script. Using trans_function_name()
12229 * would also work, but some plugins depend on the name being
12230 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012231 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012232 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12233 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012234 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012235 STRCPY(name, sid_buf);
12236 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012237 }
12238 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012239 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012240 name = vim_strsave(s);
12241
12242 if (argvars[1].v_type != VAR_UNKNOWN)
12243 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012244 if (argvars[2].v_type != VAR_UNKNOWN)
12245 {
12246 /* function(name, [args], dict) */
12247 arg_idx = 1;
12248 dict_idx = 2;
12249 }
12250 else if (argvars[1].v_type == VAR_DICT)
12251 /* function(name, dict) */
12252 dict_idx = 1;
12253 else
12254 /* function(name, [args]) */
12255 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012256 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012257 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012258 if (argvars[dict_idx].v_type != VAR_DICT)
12259 {
12260 EMSG(_("E922: expected a dict"));
12261 vim_free(name);
12262 return;
12263 }
12264 if (argvars[dict_idx].vval.v_dict == NULL)
12265 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012266 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012267 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012268 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012269 if (argvars[arg_idx].v_type != VAR_LIST)
12270 {
12271 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12272 vim_free(name);
12273 return;
12274 }
12275 list = argvars[arg_idx].vval.v_list;
12276 if (list == NULL || list->lv_len == 0)
12277 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012278 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012279 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012280 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012281 {
12282 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012283
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012284 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012285 if (pt == NULL)
12286 vim_free(name);
12287 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012288 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012289 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012290 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012291 listitem_T *li;
12292 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012293 int arg_len = 0;
12294 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012295
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012296 if (arg_pt != NULL)
12297 arg_len = arg_pt->pt_argc;
12298 if (list != NULL)
12299 lv_len = list->lv_len;
12300 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012301 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012302 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012303 if (pt->pt_argv == NULL)
12304 {
12305 vim_free(pt);
12306 vim_free(name);
12307 return;
12308 }
12309 else
12310 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012311 for (i = 0; i < arg_len; i++)
12312 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12313 if (lv_len > 0)
12314 for (li = list->lv_first; li != NULL;
12315 li = li->li_next)
12316 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012317 }
12318 }
12319
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012320 /* For "function(dict.func, [], dict)" and "func" is a partial
12321 * use "dict". That is backwards compatible. */
12322 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012323 {
12324 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12325 ++pt->pt_dict->dv_refcount;
12326 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012327 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012328 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012329 pt->pt_dict = arg_pt->pt_dict;
12330 if (pt->pt_dict != NULL)
12331 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012332 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012333
12334 pt->pt_refcount = 1;
12335 pt->pt_name = name;
12336 func_ref(pt->pt_name);
12337 }
12338 rettv->v_type = VAR_PARTIAL;
12339 rettv->vval.v_partial = pt;
12340 }
12341 else
12342 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012343 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012344 rettv->v_type = VAR_FUNC;
12345 rettv->vval.v_string = name;
12346 func_ref(name);
12347 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012348 }
12349}
12350
12351/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012352 * "garbagecollect()" function
12353 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012355f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012356{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012357 /* This is postponed until we are back at the toplevel, because we may be
12358 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12359 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012360
12361 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12362 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012363}
12364
12365/*
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +020012366 * "garbagecollect_for_testing()" function
12367 */
12368 static void
12369f_garbagecollect_for_testing(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12370{
12371 /* This is dangerous, any Lists and Dicts used internally may be freed
12372 * while still in use. */
12373 garbage_collect(TRUE);
12374}
12375
12376/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012377 * "get()" function
12378 */
12379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012380f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012381{
Bram Moolenaar33570922005-01-25 22:26:29 +000012382 listitem_T *li;
12383 list_T *l;
12384 dictitem_T *di;
12385 dict_T *d;
12386 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012387
Bram Moolenaare9a41262005-01-15 22:18:47 +000012388 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012389 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012390 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012392 int error = FALSE;
12393
12394 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12395 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012396 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012397 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012399 else if (argvars[0].v_type == VAR_DICT)
12400 {
12401 if ((d = argvars[0].vval.v_dict) != NULL)
12402 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012403 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012404 if (di != NULL)
12405 tv = &di->di_tv;
12406 }
12407 }
12408 else
12409 EMSG2(_(e_listdictarg), "get()");
12410
12411 if (tv == NULL)
12412 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012413 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012414 copy_tv(&argvars[2], rettv);
12415 }
12416 else
12417 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012418}
12419
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012420static 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 +000012421
12422/*
12423 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012424 * Return a range (from start to end) of lines in rettv from the specified
12425 * buffer.
12426 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012427 */
12428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012429get_buffer_lines(
12430 buf_T *buf,
12431 linenr_T start,
12432 linenr_T end,
12433 int retlist,
12434 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012435{
12436 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012437
Bram Moolenaar959a1432013-12-14 12:17:38 +010012438 rettv->v_type = VAR_STRING;
12439 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012440 if (retlist && rettv_list_alloc(rettv) == FAIL)
12441 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012442
12443 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12444 return;
12445
12446 if (!retlist)
12447 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012448 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12449 p = ml_get_buf(buf, start, FALSE);
12450 else
12451 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012452 rettv->vval.v_string = vim_strsave(p);
12453 }
12454 else
12455 {
12456 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012457 return;
12458
12459 if (start < 1)
12460 start = 1;
12461 if (end > buf->b_ml.ml_line_count)
12462 end = buf->b_ml.ml_line_count;
12463 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012464 if (list_append_string(rettv->vval.v_list,
12465 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012466 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012467 }
12468}
12469
12470/*
12471 * "getbufline()" function
12472 */
12473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012474f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012475{
12476 linenr_T lnum;
12477 linenr_T end;
12478 buf_T *buf;
12479
12480 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12481 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012482 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012483 --emsg_off;
12484
Bram Moolenaar661b1822005-07-28 22:36:45 +000012485 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012486 if (argvars[2].v_type == VAR_UNKNOWN)
12487 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012488 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012489 end = get_tv_lnum_buf(&argvars[2], buf);
12490
Bram Moolenaar342337a2005-07-21 21:11:17 +000012491 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012492}
12493
Bram Moolenaar0d660222005-01-07 21:51:51 +000012494/*
12495 * "getbufvar()" function
12496 */
12497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012498f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012499{
12500 buf_T *buf;
12501 buf_T *save_curbuf;
12502 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012503 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012504 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012506 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12507 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012509 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012510
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012511 rettv->v_type = VAR_STRING;
12512 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012513
12514 if (buf != NULL && varname != NULL)
12515 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012516 /* set curbuf to be our buf, temporarily */
12517 save_curbuf = curbuf;
12518 curbuf = buf;
12519
Bram Moolenaar0d660222005-01-07 21:51:51 +000012520 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012521 {
12522 if (get_option_tv(&varname, rettv, TRUE) == OK)
12523 done = TRUE;
12524 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012525 else if (STRCMP(varname, "changedtick") == 0)
12526 {
12527 rettv->v_type = VAR_NUMBER;
12528 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012529 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012530 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012531 else
12532 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012533 /* Look up the variable. */
12534 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12535 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12536 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012537 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012538 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012539 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012540 done = TRUE;
12541 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012543
12544 /* restore previous notion of curbuf */
12545 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012546 }
12547
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012548 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12549 /* use the default value */
12550 copy_tv(&argvars[2], rettv);
12551
Bram Moolenaar0d660222005-01-07 21:51:51 +000012552 --emsg_off;
12553}
12554
12555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 * "getchar()" function
12557 */
12558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012559f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560{
12561 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012562 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012564 /* Position the cursor. Needed after a message that ends in a space. */
12565 windgoto(msg_row, msg_col);
12566
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 ++no_mapping;
12568 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012569 for (;;)
12570 {
12571 if (argvars[0].v_type == VAR_UNKNOWN)
12572 /* getchar(): blocking wait. */
12573 n = safe_vgetc();
12574 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12575 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012576 n = vpeekc_any();
12577 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012578 /* illegal argument or getchar(0) and no char avail: return zero */
12579 n = 0;
12580 else
12581 /* getchar(0) and char avail: return char */
12582 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012583
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012584 if (n == K_IGNORE)
12585 continue;
12586 break;
12587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588 --no_mapping;
12589 --allow_keys;
12590
Bram Moolenaar219b8702006-11-01 14:32:36 +000012591 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12592 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12593 vimvars[VV_MOUSE_COL].vv_nr = 0;
12594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012595 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 if (IS_SPECIAL(n) || mod_mask != 0)
12597 {
12598 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12599 int i = 0;
12600
12601 /* Turn a special key into three bytes, plus modifier. */
12602 if (mod_mask != 0)
12603 {
12604 temp[i++] = K_SPECIAL;
12605 temp[i++] = KS_MODIFIER;
12606 temp[i++] = mod_mask;
12607 }
12608 if (IS_SPECIAL(n))
12609 {
12610 temp[i++] = K_SPECIAL;
12611 temp[i++] = K_SECOND(n);
12612 temp[i++] = K_THIRD(n);
12613 }
12614#ifdef FEAT_MBYTE
12615 else if (has_mbyte)
12616 i += (*mb_char2bytes)(n, temp + i);
12617#endif
12618 else
12619 temp[i++] = n;
12620 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012621 rettv->v_type = VAR_STRING;
12622 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012623
12624#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012625 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012626 {
12627 int row = mouse_row;
12628 int col = mouse_col;
12629 win_T *win;
12630 linenr_T lnum;
12631# ifdef FEAT_WINDOWS
12632 win_T *wp;
12633# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012634 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012635
12636 if (row >= 0 && col >= 0)
12637 {
12638 /* Find the window at the mouse coordinates and compute the
12639 * text position. */
12640 win = mouse_find_win(&row, &col);
12641 (void)mouse_comp_pos(win, &row, &col, &lnum);
12642# ifdef FEAT_WINDOWS
12643 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012644 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012645# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012646 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012647 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12648 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12649 }
12650 }
12651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 }
12653}
12654
12655/*
12656 * "getcharmod()" function
12657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012659f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012661 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662}
12663
12664/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012665 * "getcharsearch()" function
12666 */
12667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012668f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012669{
12670 if (rettv_dict_alloc(rettv) != FAIL)
12671 {
12672 dict_T *dict = rettv->vval.v_dict;
12673
12674 dict_add_nr_str(dict, "char", 0L, last_csearch());
12675 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12676 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12677 }
12678}
12679
12680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 * "getcmdline()" function
12682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012684f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686 rettv->v_type = VAR_STRING;
12687 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688}
12689
12690/*
12691 * "getcmdpos()" function
12692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012694f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012696 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697}
12698
12699/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012700 * "getcmdtype()" function
12701 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012703f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012704{
12705 rettv->v_type = VAR_STRING;
12706 rettv->vval.v_string = alloc(2);
12707 if (rettv->vval.v_string != NULL)
12708 {
12709 rettv->vval.v_string[0] = get_cmdline_type();
12710 rettv->vval.v_string[1] = NUL;
12711 }
12712}
12713
12714/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012715 * "getcmdwintype()" function
12716 */
12717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012718f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012719{
12720 rettv->v_type = VAR_STRING;
12721 rettv->vval.v_string = NULL;
12722#ifdef FEAT_CMDWIN
12723 rettv->vval.v_string = alloc(2);
12724 if (rettv->vval.v_string != NULL)
12725 {
12726 rettv->vval.v_string[0] = cmdwin_type;
12727 rettv->vval.v_string[1] = NUL;
12728 }
12729#endif
12730}
12731
12732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 * "getcwd()" function
12734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012736f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012738 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012739 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012741 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012742 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012743
12744 wp = find_tabwin(&argvars[0], &argvars[1]);
12745 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012747 if (wp->w_localdir != NULL)
12748 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12749 else if(globaldir != NULL)
12750 rettv->vval.v_string = vim_strsave(globaldir);
12751 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012752 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012753 cwd = alloc(MAXPATHL);
12754 if (cwd != NULL)
12755 {
12756 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12757 rettv->vval.v_string = vim_strsave(cwd);
12758 vim_free(cwd);
12759 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012760 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012761#ifdef BACKSLASH_IN_FILENAME
12762 if (rettv->vval.v_string != NULL)
12763 slash_adjust(rettv->vval.v_string);
12764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765 }
12766}
12767
12768/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012769 * "getfontname()" function
12770 */
12771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012772f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012773{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->v_type = VAR_STRING;
12775 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012776#ifdef FEAT_GUI
12777 if (gui.in_use)
12778 {
12779 GuiFont font;
12780 char_u *name = NULL;
12781
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012782 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012783 {
12784 /* Get the "Normal" font. Either the name saved by
12785 * hl_set_font_name() or from the font ID. */
12786 font = gui.norm_font;
12787 name = hl_get_font_name();
12788 }
12789 else
12790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012792 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12793 return;
12794 font = gui_mch_get_font(name, FALSE);
12795 if (font == NOFONT)
12796 return; /* Invalid font name, return empty string. */
12797 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012799 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012800 gui_mch_free_font(font);
12801 }
12802#endif
12803}
12804
12805/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012806 * "getfperm({fname})" function
12807 */
12808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012809f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012810{
12811 char_u *fname;
12812 struct stat st;
12813 char_u *perm = NULL;
12814 char_u flags[] = "rwx";
12815 int i;
12816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012820 if (mch_stat((char *)fname, &st) >= 0)
12821 {
12822 perm = vim_strsave((char_u *)"---------");
12823 if (perm != NULL)
12824 {
12825 for (i = 0; i < 9; i++)
12826 {
12827 if (st.st_mode & (1 << (8 - i)))
12828 perm[i] = flags[i % 3];
12829 }
12830 }
12831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012832 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012833}
12834
12835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836 * "getfsize({fname})" function
12837 */
12838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012839f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840{
12841 char_u *fname;
12842 struct stat st;
12843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847
12848 if (mch_stat((char *)fname, &st) >= 0)
12849 {
12850 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012853 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012855
12856 /* non-perfect check for overflow */
12857 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12858 rettv->vval.v_number = -2;
12859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 }
12861 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863}
12864
12865/*
12866 * "getftime({fname})" function
12867 */
12868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012869f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870{
12871 char_u *fname;
12872 struct stat st;
12873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875
12876 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880}
12881
12882/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012883 * "getftype({fname})" function
12884 */
12885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012886f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012887{
12888 char_u *fname;
12889 struct stat st;
12890 char_u *type = NULL;
12891 char *t;
12892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012895 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012896 if (mch_lstat((char *)fname, &st) >= 0)
12897 {
12898#ifdef S_ISREG
12899 if (S_ISREG(st.st_mode))
12900 t = "file";
12901 else if (S_ISDIR(st.st_mode))
12902 t = "dir";
12903# ifdef S_ISLNK
12904 else if (S_ISLNK(st.st_mode))
12905 t = "link";
12906# endif
12907# ifdef S_ISBLK
12908 else if (S_ISBLK(st.st_mode))
12909 t = "bdev";
12910# endif
12911# ifdef S_ISCHR
12912 else if (S_ISCHR(st.st_mode))
12913 t = "cdev";
12914# endif
12915# ifdef S_ISFIFO
12916 else if (S_ISFIFO(st.st_mode))
12917 t = "fifo";
12918# endif
12919# ifdef S_ISSOCK
12920 else if (S_ISSOCK(st.st_mode))
12921 t = "fifo";
12922# endif
12923 else
12924 t = "other";
12925#else
12926# ifdef S_IFMT
12927 switch (st.st_mode & S_IFMT)
12928 {
12929 case S_IFREG: t = "file"; break;
12930 case S_IFDIR: t = "dir"; break;
12931# ifdef S_IFLNK
12932 case S_IFLNK: t = "link"; break;
12933# endif
12934# ifdef S_IFBLK
12935 case S_IFBLK: t = "bdev"; break;
12936# endif
12937# ifdef S_IFCHR
12938 case S_IFCHR: t = "cdev"; break;
12939# endif
12940# ifdef S_IFIFO
12941 case S_IFIFO: t = "fifo"; break;
12942# endif
12943# ifdef S_IFSOCK
12944 case S_IFSOCK: t = "socket"; break;
12945# endif
12946 default: t = "other";
12947 }
12948# else
12949 if (mch_isdir(fname))
12950 t = "dir";
12951 else
12952 t = "file";
12953# endif
12954#endif
12955 type = vim_strsave((char_u *)t);
12956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012957 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012958}
12959
12960/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012961 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012962 */
12963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012964f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012965{
12966 linenr_T lnum;
12967 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012968 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012969
12970 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012971 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012972 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012973 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012974 retlist = FALSE;
12975 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012976 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012977 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012978 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012979 retlist = TRUE;
12980 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012981
Bram Moolenaar342337a2005-07-21 21:11:17 +000012982 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012983}
12984
12985/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012986 * "getmatches()" function
12987 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012989f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012990{
12991#ifdef FEAT_SEARCH_EXTRA
12992 dict_T *dict;
12993 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012994 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012995
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012996 if (rettv_list_alloc(rettv) == OK)
12997 {
12998 while (cur != NULL)
12999 {
13000 dict = dict_alloc();
13001 if (dict == NULL)
13002 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013003 if (cur->match.regprog == NULL)
13004 {
13005 /* match added with matchaddpos() */
13006 for (i = 0; i < MAXPOSMATCH; ++i)
13007 {
13008 llpos_T *llpos;
13009 char buf[6];
13010 list_T *l;
13011
13012 llpos = &cur->pos.pos[i];
13013 if (llpos->lnum == 0)
13014 break;
13015 l = list_alloc();
13016 if (l == NULL)
13017 break;
13018 list_append_number(l, (varnumber_T)llpos->lnum);
13019 if (llpos->col > 0)
13020 {
13021 list_append_number(l, (varnumber_T)llpos->col);
13022 list_append_number(l, (varnumber_T)llpos->len);
13023 }
13024 sprintf(buf, "pos%d", i + 1);
13025 dict_add_list(dict, buf, l);
13026 }
13027 }
13028 else
13029 {
13030 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13031 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013032 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013033 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13034 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013035# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013036 if (cur->conceal_char)
13037 {
13038 char_u buf[MB_MAXBYTES + 1];
13039
13040 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13041 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13042 }
13043# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013044 list_append_dict(rettv->vval.v_list, dict);
13045 cur = cur->next;
13046 }
13047 }
13048#endif
13049}
13050
13051/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013052 * "getpid()" function
13053 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013055f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013056{
13057 rettv->vval.v_number = mch_get_pid();
13058}
13059
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013061getpos_both(
13062 typval_T *argvars,
13063 typval_T *rettv,
13064 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013065{
Bram Moolenaara5525202006-03-02 22:52:09 +000013066 pos_T *fp;
13067 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013068 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013069
13070 if (rettv_list_alloc(rettv) == OK)
13071 {
13072 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013073 if (getcurpos)
13074 fp = &curwin->w_cursor;
13075 else
13076 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013077 if (fnum != -1)
13078 list_append_number(l, (varnumber_T)fnum);
13079 else
13080 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013081 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13082 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013083 list_append_number(l, (fp != NULL)
13084 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013085 : (varnumber_T)0);
13086 list_append_number(l,
13087#ifdef FEAT_VIRTUALEDIT
13088 (fp != NULL) ? (varnumber_T)fp->coladd :
13089#endif
13090 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013091 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013092 {
13093 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013094 list_append_number(l, curwin->w_curswant == MAXCOL ?
13095 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013096 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013097 }
13098 else
13099 rettv->vval.v_number = FALSE;
13100}
13101
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013102
13103/*
13104 * "getcurpos()" function
13105 */
13106 static void
13107f_getcurpos(typval_T *argvars, typval_T *rettv)
13108{
13109 getpos_both(argvars, rettv, TRUE);
13110}
13111
13112/*
13113 * "getpos(string)" function
13114 */
13115 static void
13116f_getpos(typval_T *argvars, typval_T *rettv)
13117{
13118 getpos_both(argvars, rettv, FALSE);
13119}
13120
Bram Moolenaara5525202006-03-02 22:52:09 +000013121/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013122 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013123 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013125f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013126{
13127#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013128 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013129#endif
13130
Bram Moolenaar2641f772005-03-25 21:58:17 +000013131#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013132 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013133 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013134 wp = NULL;
13135 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13136 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013137 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013138 if (wp == NULL)
13139 return;
13140 }
13141
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013142 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013143 }
13144#endif
13145}
13146
13147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 * "getreg()" function
13149 */
13150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013151f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152{
13153 char_u *strregname;
13154 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013155 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013156 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013157 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013159 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013161 strregname = get_tv_string_chk(&argvars[0]);
13162 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013163 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013165 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013166 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13167 return_list = get_tv_number_chk(&argvars[2], &error);
13168 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013171 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013172
13173 if (error)
13174 return;
13175
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 regname = (strregname == NULL ? '"' : *strregname);
13177 if (regname == 0)
13178 regname = '"';
13179
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013180 if (return_list)
13181 {
13182 rettv->v_type = VAR_LIST;
13183 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13184 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013185 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013186 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013187 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013188 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013189 }
13190 else
13191 {
13192 rettv->v_type = VAR_STRING;
13193 rettv->vval.v_string = get_reg_contents(regname,
13194 arg2 ? GREG_EXPR_SRC : 0);
13195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196}
13197
13198/*
13199 * "getregtype()" function
13200 */
13201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013202f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203{
13204 char_u *strregname;
13205 int regname;
13206 char_u buf[NUMBUFLEN + 2];
13207 long reglen = 0;
13208
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013209 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013210 {
13211 strregname = get_tv_string_chk(&argvars[0]);
13212 if (strregname == NULL) /* type error; errmsg already given */
13213 {
13214 rettv->v_type = VAR_STRING;
13215 rettv->vval.v_string = NULL;
13216 return;
13217 }
13218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 else
13220 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013221 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222
13223 regname = (strregname == NULL ? '"' : *strregname);
13224 if (regname == 0)
13225 regname = '"';
13226
13227 buf[0] = NUL;
13228 buf[1] = NUL;
13229 switch (get_reg_type(regname, &reglen))
13230 {
13231 case MLINE: buf[0] = 'V'; break;
13232 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 case MBLOCK:
13234 buf[0] = Ctrl_V;
13235 sprintf((char *)buf + 1, "%ld", reglen + 1);
13236 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238 rettv->v_type = VAR_STRING;
13239 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240}
13241
13242/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013243 * "gettabvar()" function
13244 */
13245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013246f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013247{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013248 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013249 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013250 dictitem_T *v;
13251 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013252 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013253
13254 rettv->v_type = VAR_STRING;
13255 rettv->vval.v_string = NULL;
13256
13257 varname = get_tv_string_chk(&argvars[1]);
13258 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13259 if (tp != NULL && varname != NULL)
13260 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013261 /* Set tp to be our tabpage, temporarily. Also set the window to the
13262 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013263 if (switch_win(&oldcurwin, &oldtabpage,
13264 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013265 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013266 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013267 /* look up the variable */
13268 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13269 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13270 if (v != NULL)
13271 {
13272 copy_tv(&v->di_tv, rettv);
13273 done = TRUE;
13274 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013275 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013276
13277 /* restore previous notion of curwin */
13278 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013279 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013280
13281 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013282 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013283}
13284
13285/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013286 * "gettabwinvar()" function
13287 */
13288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013289f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013290{
13291 getwinvar(argvars, rettv, 1);
13292}
13293
13294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 * "getwinposx()" function
13296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013298f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301#ifdef FEAT_GUI
13302 if (gui.in_use)
13303 {
13304 int x, y;
13305
13306 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308 }
13309#endif
13310}
13311
13312/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013313 * "win_findbuf()" function
13314 */
13315 static void
13316f_win_findbuf(typval_T *argvars, typval_T *rettv)
13317{
13318 if (rettv_list_alloc(rettv) != FAIL)
13319 win_findbuf(argvars, rettv->vval.v_list);
13320}
13321
13322/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013323 * "win_getid()" function
13324 */
13325 static void
13326f_win_getid(typval_T *argvars, typval_T *rettv)
13327{
13328 rettv->vval.v_number = win_getid(argvars);
13329}
13330
13331/*
13332 * "win_gotoid()" function
13333 */
13334 static void
13335f_win_gotoid(typval_T *argvars, typval_T *rettv)
13336{
13337 rettv->vval.v_number = win_gotoid(argvars);
13338}
13339
13340/*
13341 * "win_id2tabwin()" function
13342 */
13343 static void
13344f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13345{
13346 if (rettv_list_alloc(rettv) != FAIL)
13347 win_id2tabwin(argvars, rettv->vval.v_list);
13348}
13349
13350/*
13351 * "win_id2win()" function
13352 */
13353 static void
13354f_win_id2win(typval_T *argvars, typval_T *rettv)
13355{
13356 rettv->vval.v_number = win_id2win(argvars);
13357}
13358
13359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 * "getwinposy()" function
13361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013363f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013365 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366#ifdef FEAT_GUI
13367 if (gui.in_use)
13368 {
13369 int x, y;
13370
13371 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013372 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373 }
13374#endif
13375}
13376
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013377/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013378 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013379 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013380 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013381find_win_by_nr(
13382 typval_T *vp,
13383 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013384{
13385#ifdef FEAT_WINDOWS
13386 win_T *wp;
13387#endif
13388 int nr;
13389
13390 nr = get_tv_number_chk(vp, NULL);
13391
13392#ifdef FEAT_WINDOWS
13393 if (nr < 0)
13394 return NULL;
13395 if (nr == 0)
13396 return curwin;
13397
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013398 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13399 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013400 if (--nr <= 0)
13401 break;
13402 return wp;
13403#else
13404 if (nr == 0 || nr == 1)
13405 return curwin;
13406 return NULL;
13407#endif
13408}
13409
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013411 * Find window specified by "wvp" in tabpage "tvp".
13412 */
13413 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013414find_tabwin(
13415 typval_T *wvp, /* VAR_UNKNOWN for current window */
13416 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013417{
13418 win_T *wp = NULL;
13419 tabpage_T *tp = NULL;
13420 long n;
13421
13422 if (wvp->v_type != VAR_UNKNOWN)
13423 {
13424 if (tvp->v_type != VAR_UNKNOWN)
13425 {
13426 n = get_tv_number(tvp);
13427 if (n >= 0)
13428 tp = find_tabpage(n);
13429 }
13430 else
13431 tp = curtab;
13432
13433 if (tp != NULL)
13434 wp = find_win_by_nr(wvp, tp);
13435 }
13436 else
13437 wp = curwin;
13438
13439 return wp;
13440}
13441
13442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443 * "getwinvar()" function
13444 */
13445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013446f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013448 getwinvar(argvars, rettv, 0);
13449}
13450
13451/*
13452 * getwinvar() and gettabwinvar()
13453 */
13454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013455getwinvar(
13456 typval_T *argvars,
13457 typval_T *rettv,
13458 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013459{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013460 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013462 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013463 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013464 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013465#ifdef FEAT_WINDOWS
13466 win_T *oldcurwin;
13467 tabpage_T *oldtabpage;
13468 int need_switch_win;
13469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013471#ifdef FEAT_WINDOWS
13472 if (off == 1)
13473 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13474 else
13475 tp = curtab;
13476#endif
13477 win = find_win_by_nr(&argvars[off], tp);
13478 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013479 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013481 rettv->v_type = VAR_STRING;
13482 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483
13484 if (win != NULL && varname != NULL)
13485 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013486#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013487 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013488 * otherwise the window is not valid. Only do this when needed,
13489 * autocommands get blocked. */
13490 need_switch_win = !(tp == curtab && win == curwin);
13491 if (!need_switch_win
13492 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13493#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013494 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013495 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013496 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013497 if (get_option_tv(&varname, rettv, 1) == OK)
13498 done = TRUE;
13499 }
13500 else
13501 {
13502 /* Look up the variable. */
13503 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13504 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13505 varname, FALSE);
13506 if (v != NULL)
13507 {
13508 copy_tv(&v->di_tv, rettv);
13509 done = TRUE;
13510 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013513
Bram Moolenaarba117c22015-09-29 16:53:22 +020013514#ifdef FEAT_WINDOWS
13515 if (need_switch_win)
13516 /* restore previous notion of curwin */
13517 restore_win(oldcurwin, oldtabpage, TRUE);
13518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 }
13520
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013521 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13522 /* use the default return value */
13523 copy_tv(&argvars[off + 2], rettv);
13524
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525 --emsg_off;
13526}
13527
13528/*
13529 * "glob()" function
13530 */
13531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013532f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013534 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013536 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013538 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013539 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013541 if (argvars[1].v_type != VAR_UNKNOWN)
13542 {
13543 if (get_tv_number_chk(&argvars[1], &error))
13544 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013545 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013546 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013547 if (get_tv_number_chk(&argvars[2], &error))
13548 {
13549 rettv->v_type = VAR_LIST;
13550 rettv->vval.v_list = NULL;
13551 }
13552 if (argvars[3].v_type != VAR_UNKNOWN
13553 && get_tv_number_chk(&argvars[3], &error))
13554 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013555 }
13556 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013557 if (!error)
13558 {
13559 ExpandInit(&xpc);
13560 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013561 if (p_wic)
13562 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013563 if (rettv->v_type == VAR_STRING)
13564 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013565 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013566 else if (rettv_list_alloc(rettv) != FAIL)
13567 {
13568 int i;
13569
13570 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13571 NULL, options, WILD_ALL_KEEP);
13572 for (i = 0; i < xpc.xp_numfiles; i++)
13573 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13574
13575 ExpandCleanup(&xpc);
13576 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013577 }
13578 else
13579 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580}
13581
13582/*
13583 * "globpath()" function
13584 */
13585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013586f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013588 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013590 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013591 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013592 garray_T ga;
13593 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013595 /* When the optional second argument is non-zero, don't remove matches
13596 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013598 if (argvars[2].v_type != VAR_UNKNOWN)
13599 {
13600 if (get_tv_number_chk(&argvars[2], &error))
13601 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013602 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013603 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013604 if (get_tv_number_chk(&argvars[3], &error))
13605 {
13606 rettv->v_type = VAR_LIST;
13607 rettv->vval.v_list = NULL;
13608 }
13609 if (argvars[4].v_type != VAR_UNKNOWN
13610 && get_tv_number_chk(&argvars[4], &error))
13611 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013612 }
13613 }
13614 if (file != NULL && !error)
13615 {
13616 ga_init2(&ga, (int)sizeof(char_u *), 10);
13617 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13618 if (rettv->v_type == VAR_STRING)
13619 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13620 else if (rettv_list_alloc(rettv) != FAIL)
13621 for (i = 0; i < ga.ga_len; ++i)
13622 list_append_string(rettv->vval.v_list,
13623 ((char_u **)(ga.ga_data))[i], -1);
13624 ga_clear_strings(&ga);
13625 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013626 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013627 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628}
13629
13630/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013631 * "glob2regpat()" function
13632 */
13633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013634f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013635{
13636 char_u *pat = get_tv_string_chk(&argvars[0]);
13637
13638 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013639 rettv->vval.v_string = (pat == NULL)
13640 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013641}
13642
13643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 * "has()" function
13645 */
13646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013647f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648{
13649 int i;
13650 char_u *name;
13651 int n = FALSE;
13652 static char *(has_list[]) =
13653 {
13654#ifdef AMIGA
13655 "amiga",
13656# ifdef FEAT_ARP
13657 "arp",
13658# endif
13659#endif
13660#ifdef __BEOS__
13661 "beos",
13662#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013663#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 "mac",
13665#endif
13666#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013667 "macunix", /* built with 'darwin' enabled */
13668#endif
13669#if defined(__APPLE__) && __APPLE__ == 1
13670 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672#ifdef __QNX__
13673 "qnx",
13674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675#ifdef UNIX
13676 "unix",
13677#endif
13678#ifdef VMS
13679 "vms",
13680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681#ifdef WIN32
13682 "win32",
13683#endif
13684#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13685 "win32unix",
13686#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013687#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 "win64",
13689#endif
13690#ifdef EBCDIC
13691 "ebcdic",
13692#endif
13693#ifndef CASE_INSENSITIVE_FILENAME
13694 "fname_case",
13695#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013696#ifdef HAVE_ACL
13697 "acl",
13698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699#ifdef FEAT_ARABIC
13700 "arabic",
13701#endif
13702#ifdef FEAT_AUTOCMD
13703 "autocmd",
13704#endif
13705#ifdef FEAT_BEVAL
13706 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013707# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13708 "balloon_multiline",
13709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710#endif
13711#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13712 "builtin_terms",
13713# ifdef ALL_BUILTIN_TCAPS
13714 "all_builtin_terms",
13715# endif
13716#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013717#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13718 || defined(FEAT_GUI_W32) \
13719 || defined(FEAT_GUI_MOTIF))
13720 "browsefilter",
13721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722#ifdef FEAT_BYTEOFF
13723 "byte_offset",
13724#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013725#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013726 "channel",
13727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728#ifdef FEAT_CINDENT
13729 "cindent",
13730#endif
13731#ifdef FEAT_CLIENTSERVER
13732 "clientserver",
13733#endif
13734#ifdef FEAT_CLIPBOARD
13735 "clipboard",
13736#endif
13737#ifdef FEAT_CMDL_COMPL
13738 "cmdline_compl",
13739#endif
13740#ifdef FEAT_CMDHIST
13741 "cmdline_hist",
13742#endif
13743#ifdef FEAT_COMMENTS
13744 "comments",
13745#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013746#ifdef FEAT_CONCEAL
13747 "conceal",
13748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749#ifdef FEAT_CRYPT
13750 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013751 "crypt-blowfish",
13752 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753#endif
13754#ifdef FEAT_CSCOPE
13755 "cscope",
13756#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013757#ifdef FEAT_CURSORBIND
13758 "cursorbind",
13759#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013760#ifdef CURSOR_SHAPE
13761 "cursorshape",
13762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763#ifdef DEBUG
13764 "debug",
13765#endif
13766#ifdef FEAT_CON_DIALOG
13767 "dialog_con",
13768#endif
13769#ifdef FEAT_GUI_DIALOG
13770 "dialog_gui",
13771#endif
13772#ifdef FEAT_DIFF
13773 "diff",
13774#endif
13775#ifdef FEAT_DIGRAPHS
13776 "digraphs",
13777#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013778#ifdef FEAT_DIRECTX
13779 "directx",
13780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781#ifdef FEAT_DND
13782 "dnd",
13783#endif
13784#ifdef FEAT_EMACS_TAGS
13785 "emacs_tags",
13786#endif
13787 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013788 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789#ifdef FEAT_SEARCH_EXTRA
13790 "extra_search",
13791#endif
13792#ifdef FEAT_FKMAP
13793 "farsi",
13794#endif
13795#ifdef FEAT_SEARCHPATH
13796 "file_in_path",
13797#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013798#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013799 "filterpipe",
13800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801#ifdef FEAT_FIND_ID
13802 "find_in_path",
13803#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013804#ifdef FEAT_FLOAT
13805 "float",
13806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807#ifdef FEAT_FOLDING
13808 "folding",
13809#endif
13810#ifdef FEAT_FOOTER
13811 "footer",
13812#endif
13813#if !defined(USE_SYSTEM) && defined(UNIX)
13814 "fork",
13815#endif
13816#ifdef FEAT_GETTEXT
13817 "gettext",
13818#endif
13819#ifdef FEAT_GUI
13820 "gui",
13821#endif
13822#ifdef FEAT_GUI_ATHENA
13823# ifdef FEAT_GUI_NEXTAW
13824 "gui_neXtaw",
13825# else
13826 "gui_athena",
13827# endif
13828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829#ifdef FEAT_GUI_GTK
13830 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013831# ifdef USE_GTK3
13832 "gui_gtk3",
13833# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013837#ifdef FEAT_GUI_GNOME
13838 "gui_gnome",
13839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840#ifdef FEAT_GUI_MAC
13841 "gui_mac",
13842#endif
13843#ifdef FEAT_GUI_MOTIF
13844 "gui_motif",
13845#endif
13846#ifdef FEAT_GUI_PHOTON
13847 "gui_photon",
13848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849#ifdef FEAT_GUI_W32
13850 "gui_win32",
13851#endif
13852#ifdef FEAT_HANGULIN
13853 "hangul_input",
13854#endif
13855#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13856 "iconv",
13857#endif
13858#ifdef FEAT_INS_EXPAND
13859 "insert_expand",
13860#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013861#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013862 "job",
13863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864#ifdef FEAT_JUMPLIST
13865 "jumplist",
13866#endif
13867#ifdef FEAT_KEYMAP
13868 "keymap",
13869#endif
13870#ifdef FEAT_LANGMAP
13871 "langmap",
13872#endif
13873#ifdef FEAT_LIBCALL
13874 "libcall",
13875#endif
13876#ifdef FEAT_LINEBREAK
13877 "linebreak",
13878#endif
13879#ifdef FEAT_LISP
13880 "lispindent",
13881#endif
13882#ifdef FEAT_LISTCMDS
13883 "listcmds",
13884#endif
13885#ifdef FEAT_LOCALMAP
13886 "localmap",
13887#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013888#ifdef FEAT_LUA
13889# ifndef DYNAMIC_LUA
13890 "lua",
13891# endif
13892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893#ifdef FEAT_MENU
13894 "menu",
13895#endif
13896#ifdef FEAT_SESSION
13897 "mksession",
13898#endif
13899#ifdef FEAT_MODIFY_FNAME
13900 "modify_fname",
13901#endif
13902#ifdef FEAT_MOUSE
13903 "mouse",
13904#endif
13905#ifdef FEAT_MOUSESHAPE
13906 "mouseshape",
13907#endif
13908#if defined(UNIX) || defined(VMS)
13909# ifdef FEAT_MOUSE_DEC
13910 "mouse_dec",
13911# endif
13912# ifdef FEAT_MOUSE_GPM
13913 "mouse_gpm",
13914# endif
13915# ifdef FEAT_MOUSE_JSB
13916 "mouse_jsbterm",
13917# endif
13918# ifdef FEAT_MOUSE_NET
13919 "mouse_netterm",
13920# endif
13921# ifdef FEAT_MOUSE_PTERM
13922 "mouse_pterm",
13923# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013924# ifdef FEAT_MOUSE_SGR
13925 "mouse_sgr",
13926# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013927# ifdef FEAT_SYSMOUSE
13928 "mouse_sysmouse",
13929# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013930# ifdef FEAT_MOUSE_URXVT
13931 "mouse_urxvt",
13932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933# ifdef FEAT_MOUSE_XTERM
13934 "mouse_xterm",
13935# endif
13936#endif
13937#ifdef FEAT_MBYTE
13938 "multi_byte",
13939#endif
13940#ifdef FEAT_MBYTE_IME
13941 "multi_byte_ime",
13942#endif
13943#ifdef FEAT_MULTI_LANG
13944 "multi_lang",
13945#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013946#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013947#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013948 "mzscheme",
13949#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951#ifdef FEAT_OLE
13952 "ole",
13953#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013954 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955#ifdef FEAT_PATH_EXTRA
13956 "path_extra",
13957#endif
13958#ifdef FEAT_PERL
13959#ifndef DYNAMIC_PERL
13960 "perl",
13961#endif
13962#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013963#ifdef FEAT_PERSISTENT_UNDO
13964 "persistent_undo",
13965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966#ifdef FEAT_PYTHON
13967#ifndef DYNAMIC_PYTHON
13968 "python",
13969#endif
13970#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013971#ifdef FEAT_PYTHON3
13972#ifndef DYNAMIC_PYTHON3
13973 "python3",
13974#endif
13975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976#ifdef FEAT_POSTSCRIPT
13977 "postscript",
13978#endif
13979#ifdef FEAT_PRINTER
13980 "printer",
13981#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013982#ifdef FEAT_PROFILE
13983 "profile",
13984#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013985#ifdef FEAT_RELTIME
13986 "reltime",
13987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988#ifdef FEAT_QUICKFIX
13989 "quickfix",
13990#endif
13991#ifdef FEAT_RIGHTLEFT
13992 "rightleft",
13993#endif
13994#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13995 "ruby",
13996#endif
13997#ifdef FEAT_SCROLLBIND
13998 "scrollbind",
13999#endif
14000#ifdef FEAT_CMDL_INFO
14001 "showcmd",
14002 "cmdline_info",
14003#endif
14004#ifdef FEAT_SIGNS
14005 "signs",
14006#endif
14007#ifdef FEAT_SMARTINDENT
14008 "smartindent",
14009#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014010#ifdef STARTUPTIME
14011 "startuptime",
14012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013#ifdef FEAT_STL_OPT
14014 "statusline",
14015#endif
14016#ifdef FEAT_SUN_WORKSHOP
14017 "sun_workshop",
14018#endif
14019#ifdef FEAT_NETBEANS_INTG
14020 "netbeans_intg",
14021#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014022#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014023 "spell",
14024#endif
14025#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 "syntax",
14027#endif
14028#if defined(USE_SYSTEM) || !defined(UNIX)
14029 "system",
14030#endif
14031#ifdef FEAT_TAG_BINS
14032 "tag_binary",
14033#endif
14034#ifdef FEAT_TAG_OLDSTATIC
14035 "tag_old_static",
14036#endif
14037#ifdef FEAT_TAG_ANYWHITE
14038 "tag_any_white",
14039#endif
14040#ifdef FEAT_TCL
14041# ifndef DYNAMIC_TCL
14042 "tcl",
14043# endif
14044#endif
14045#ifdef TERMINFO
14046 "terminfo",
14047#endif
14048#ifdef FEAT_TERMRESPONSE
14049 "termresponse",
14050#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +020014051#ifdef FEAT_TERMTRUECOLOR
14052 "termtruecolor",
14053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054#ifdef FEAT_TEXTOBJ
14055 "textobjects",
14056#endif
14057#ifdef HAVE_TGETENT
14058 "tgetent",
14059#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014060#ifdef FEAT_TIMERS
14061 "timers",
14062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063#ifdef FEAT_TITLE
14064 "title",
14065#endif
14066#ifdef FEAT_TOOLBAR
14067 "toolbar",
14068#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014069#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14070 "unnamedplus",
14071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072#ifdef FEAT_USR_CMDS
14073 "user-commands", /* was accidentally included in 5.4 */
14074 "user_commands",
14075#endif
14076#ifdef FEAT_VIMINFO
14077 "viminfo",
14078#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014079#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 "vertsplit",
14081#endif
14082#ifdef FEAT_VIRTUALEDIT
14083 "virtualedit",
14084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086#ifdef FEAT_VISUALEXTRA
14087 "visualextra",
14088#endif
14089#ifdef FEAT_VREPLACE
14090 "vreplace",
14091#endif
14092#ifdef FEAT_WILDIGN
14093 "wildignore",
14094#endif
14095#ifdef FEAT_WILDMENU
14096 "wildmenu",
14097#endif
14098#ifdef FEAT_WINDOWS
14099 "windows",
14100#endif
14101#ifdef FEAT_WAK
14102 "winaltkeys",
14103#endif
14104#ifdef FEAT_WRITEBACKUP
14105 "writebackup",
14106#endif
14107#ifdef FEAT_XIM
14108 "xim",
14109#endif
14110#ifdef FEAT_XFONTSET
14111 "xfontset",
14112#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014113#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014114 "xpm",
14115 "xpm_w32", /* for backward compatibility */
14116#else
14117# if defined(HAVE_XPM)
14118 "xpm",
14119# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121#ifdef USE_XSMP
14122 "xsmp",
14123#endif
14124#ifdef USE_XSMP_INTERACT
14125 "xsmp_interact",
14126#endif
14127#ifdef FEAT_XCLIPBOARD
14128 "xterm_clipboard",
14129#endif
14130#ifdef FEAT_XTERM_SAVE
14131 "xterm_save",
14132#endif
14133#if defined(UNIX) && defined(FEAT_X11)
14134 "X11",
14135#endif
14136 NULL
14137 };
14138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014139 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140 for (i = 0; has_list[i] != NULL; ++i)
14141 if (STRICMP(name, has_list[i]) == 0)
14142 {
14143 n = TRUE;
14144 break;
14145 }
14146
14147 if (n == FALSE)
14148 {
14149 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014150 {
14151 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014152 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014153 && vim_isdigit(name[6])
14154 && vim_isdigit(name[8])
14155 && vim_isdigit(name[10]))
14156 {
14157 int major = atoi((char *)name + 6);
14158 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014159
14160 /* Expect "patch-9.9.01234". */
14161 n = (major < VIM_VERSION_MAJOR
14162 || (major == VIM_VERSION_MAJOR
14163 && (minor < VIM_VERSION_MINOR
14164 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014165 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014166 }
14167 else
14168 n = has_patch(atoi((char *)name + 5));
14169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170 else if (STRICMP(name, "vim_starting") == 0)
14171 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014172#ifdef FEAT_MBYTE
14173 else if (STRICMP(name, "multi_byte_encoding") == 0)
14174 n = has_mbyte;
14175#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014176#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14177 else if (STRICMP(name, "balloon_multiline") == 0)
14178 n = multiline_balloon_available();
14179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180#ifdef DYNAMIC_TCL
14181 else if (STRICMP(name, "tcl") == 0)
14182 n = tcl_enabled(FALSE);
14183#endif
14184#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14185 else if (STRICMP(name, "iconv") == 0)
14186 n = iconv_enabled(FALSE);
14187#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014188#ifdef DYNAMIC_LUA
14189 else if (STRICMP(name, "lua") == 0)
14190 n = lua_enabled(FALSE);
14191#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014192#ifdef DYNAMIC_MZSCHEME
14193 else if (STRICMP(name, "mzscheme") == 0)
14194 n = mzscheme_enabled(FALSE);
14195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196#ifdef DYNAMIC_RUBY
14197 else if (STRICMP(name, "ruby") == 0)
14198 n = ruby_enabled(FALSE);
14199#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014200#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201#ifdef DYNAMIC_PYTHON
14202 else if (STRICMP(name, "python") == 0)
14203 n = python_enabled(FALSE);
14204#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014205#endif
14206#ifdef FEAT_PYTHON3
14207#ifdef DYNAMIC_PYTHON3
14208 else if (STRICMP(name, "python3") == 0)
14209 n = python3_enabled(FALSE);
14210#endif
14211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212#ifdef DYNAMIC_PERL
14213 else if (STRICMP(name, "perl") == 0)
14214 n = perl_enabled(FALSE);
14215#endif
14216#ifdef FEAT_GUI
14217 else if (STRICMP(name, "gui_running") == 0)
14218 n = (gui.in_use || gui.starting);
14219# ifdef FEAT_GUI_W32
14220 else if (STRICMP(name, "gui_win32s") == 0)
14221 n = gui_is_win32s();
14222# endif
14223# ifdef FEAT_BROWSE
14224 else if (STRICMP(name, "browse") == 0)
14225 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14226# endif
14227#endif
14228#ifdef FEAT_SYN_HL
14229 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014230 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231#endif
14232#if defined(WIN3264)
14233 else if (STRICMP(name, "win95") == 0)
14234 n = mch_windows95();
14235#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014236#ifdef FEAT_NETBEANS_INTG
14237 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014238 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 }
14241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243}
14244
14245/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014246 * "has_key()" function
14247 */
14248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014249f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014250{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014251 if (argvars[0].v_type != VAR_DICT)
14252 {
14253 EMSG(_(e_dictreq));
14254 return;
14255 }
14256 if (argvars[0].vval.v_dict == NULL)
14257 return;
14258
14259 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014260 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014261}
14262
14263/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014264 * "haslocaldir()" function
14265 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014267f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014268{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014269 win_T *wp = NULL;
14270
14271 wp = find_tabwin(&argvars[0], &argvars[1]);
14272 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014273}
14274
14275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 * "hasmapto()" function
14277 */
14278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014279f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280{
14281 char_u *name;
14282 char_u *mode;
14283 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014284 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014287 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 mode = (char_u *)"nvo";
14289 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014291 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014292 if (argvars[2].v_type != VAR_UNKNOWN)
14293 abbr = get_tv_number(&argvars[2]);
14294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295
Bram Moolenaar2c932302006-03-18 21:42:09 +000014296 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014299 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300}
14301
14302/*
14303 * "histadd()" function
14304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014306f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307{
14308#ifdef FEAT_CMDHIST
14309 int histype;
14310 char_u *str;
14311 char_u buf[NUMBUFLEN];
14312#endif
14313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 if (check_restricted() || check_secure())
14316 return;
14317#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014318 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14319 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320 if (histype >= 0)
14321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014322 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 if (*str != NUL)
14324 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014325 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 return;
14329 }
14330 }
14331#endif
14332}
14333
14334/*
14335 * "histdel()" function
14336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014338f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339{
14340#ifdef FEAT_CMDHIST
14341 int n;
14342 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014345 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14346 if (str == NULL)
14347 n = 0;
14348 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014350 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014351 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014353 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014354 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 else
14356 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358 get_tv_string_buf(&argvars[1], buf));
14359 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360#endif
14361}
14362
14363/*
14364 * "histget()" function
14365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014367f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368{
14369#ifdef FEAT_CMDHIST
14370 int type;
14371 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014372 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014374 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14375 if (str == NULL)
14376 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014378 {
14379 type = get_histtype(str);
14380 if (argvars[1].v_type == VAR_UNKNOWN)
14381 idx = get_history_idx(type);
14382 else
14383 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14384 /* -1 on type error */
14385 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391}
14392
14393/*
14394 * "histnr()" function
14395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014397f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398{
14399 int i;
14400
14401#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014402 char_u *history = get_tv_string_chk(&argvars[0]);
14403
14404 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 if (i >= HIST_CMD && i < HIST_COUNT)
14406 i = get_history_idx(i);
14407 else
14408#endif
14409 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014410 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411}
14412
14413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 * "highlightID(name)" function
14415 */
14416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014417f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014419 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420}
14421
14422/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014423 * "highlight_exists()" function
14424 */
14425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014426f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014427{
14428 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14429}
14430
14431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 * "hostname()" function
14433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014435f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436{
14437 char_u hostname[256];
14438
14439 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014440 rettv->v_type = VAR_STRING;
14441 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442}
14443
14444/*
14445 * iconv() function
14446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014448f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449{
14450#ifdef FEAT_MBYTE
14451 char_u buf1[NUMBUFLEN];
14452 char_u buf2[NUMBUFLEN];
14453 char_u *from, *to, *str;
14454 vimconv_T vimconv;
14455#endif
14456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014457 rettv->v_type = VAR_STRING;
14458 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459
14460#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 str = get_tv_string(&argvars[0]);
14462 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14463 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464 vimconv.vc_type = CONV_NONE;
14465 convert_setup(&vimconv, from, to);
14466
14467 /* If the encodings are equal, no conversion needed. */
14468 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014471 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472
14473 convert_setup(&vimconv, NULL, NULL);
14474 vim_free(from);
14475 vim_free(to);
14476#endif
14477}
14478
14479/*
14480 * "indent()" function
14481 */
14482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014483f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484{
14485 linenr_T lnum;
14486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014487 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014489 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492}
14493
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014494/*
14495 * "index()" function
14496 */
14497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014498f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014499{
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 list_T *l;
14501 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014502 long idx = 0;
14503 int ic = FALSE;
14504
14505 rettv->vval.v_number = -1;
14506 if (argvars[0].v_type != VAR_LIST)
14507 {
14508 EMSG(_(e_listreq));
14509 return;
14510 }
14511 l = argvars[0].vval.v_list;
14512 if (l != NULL)
14513 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014514 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014515 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014517 int error = FALSE;
14518
Bram Moolenaar758711c2005-02-02 23:11:38 +000014519 /* Start at specified item. Use the cached index that list_find()
14520 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014522 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014523 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014524 ic = get_tv_number_chk(&argvars[3], &error);
14525 if (error)
14526 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014527 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014528
Bram Moolenaar758711c2005-02-02 23:11:38 +000014529 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014530 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014531 {
14532 rettv->vval.v_number = idx;
14533 break;
14534 }
14535 }
14536}
14537
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538static int inputsecret_flag = 0;
14539
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014540static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014541
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014543 * This function is used by f_input() and f_inputdialog() functions. The third
14544 * argument to f_input() specifies the type of completion to use at the
14545 * prompt. The third argument to f_inputdialog() specifies the value to return
14546 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 */
14548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014549get_user_input(
14550 typval_T *argvars,
14551 typval_T *rettv,
14552 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014554 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 char_u *p = NULL;
14556 int c;
14557 char_u buf[NUMBUFLEN];
14558 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014559 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014560 int xp_type = EXPAND_NOTHING;
14561 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014564 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565
14566#ifdef NO_CONSOLE_INPUT
14567 /* While starting up, there is no place to enter text. */
14568 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570#endif
14571
14572 cmd_silent = FALSE; /* Want to see the prompt. */
14573 if (prompt != NULL)
14574 {
14575 /* Only the part of the message after the last NL is considered as
14576 * prompt for the command line */
14577 p = vim_strrchr(prompt, '\n');
14578 if (p == NULL)
14579 p = prompt;
14580 else
14581 {
14582 ++p;
14583 c = *p;
14584 *p = NUL;
14585 msg_start();
14586 msg_clr_eos();
14587 msg_puts_attr(prompt, echo_attr);
14588 msg_didout = FALSE;
14589 msg_starthere();
14590 *p = c;
14591 }
14592 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014594 if (argvars[1].v_type != VAR_UNKNOWN)
14595 {
14596 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14597 if (defstr != NULL)
14598 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014600 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014601 {
14602 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014603 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014604 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014605
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014606 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014607 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014608
Bram Moolenaar4463f292005-09-25 22:20:24 +000014609 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14610 if (xp_name == NULL)
14611 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014612
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014613 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014614
Bram Moolenaar4463f292005-09-25 22:20:24 +000014615 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14616 &xp_arg) == FAIL)
14617 return;
14618 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014619 }
14620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014622 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014623 int save_ex_normal_busy = ex_normal_busy;
14624 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014625 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014626 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14627 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014628 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014629 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014630 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014631 && argvars[1].v_type != VAR_UNKNOWN
14632 && argvars[2].v_type != VAR_UNKNOWN)
14633 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14634 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014635
14636 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014638 /* since the user typed this, no need to wait for return */
14639 need_wait_return = FALSE;
14640 msg_didout = FALSE;
14641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 cmd_silent = cmd_silent_save;
14643}
14644
14645/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014646 * "input()" function
14647 * Also handles inputsecret() when inputsecret is set.
14648 */
14649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014650f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014651{
14652 get_user_input(argvars, rettv, FALSE);
14653}
14654
14655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656 * "inputdialog()" function
14657 */
14658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014659f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660{
14661#if defined(FEAT_GUI_TEXTDIALOG)
14662 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14663 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14664 {
14665 char_u *message;
14666 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014669 message = get_tv_string_chk(&argvars[0]);
14670 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014671 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014672 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 else
14674 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 if (message != NULL && defstr != NULL
14676 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014677 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014678 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679 else
14680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014681 if (message != NULL && defstr != NULL
14682 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014683 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014684 rettv->vval.v_string = vim_strsave(
14685 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014689 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 }
14691 else
14692#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014693 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694}
14695
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014696/*
14697 * "inputlist()" function
14698 */
14699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014700f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014701{
14702 listitem_T *li;
14703 int selected;
14704 int mouse_used;
14705
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014706#ifdef NO_CONSOLE_INPUT
14707 /* While starting up, there is no place to enter text. */
14708 if (no_console_input())
14709 return;
14710#endif
14711 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14712 {
14713 EMSG2(_(e_listarg), "inputlist()");
14714 return;
14715 }
14716
14717 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014718 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014719 lines_left = Rows; /* avoid more prompt */
14720 msg_scroll = TRUE;
14721 msg_clr_eos();
14722
14723 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14724 {
14725 msg_puts(get_tv_string(&li->li_tv));
14726 msg_putchar('\n');
14727 }
14728
14729 /* Ask for choice. */
14730 selected = prompt_for_number(&mouse_used);
14731 if (mouse_used)
14732 selected -= lines_left;
14733
14734 rettv->vval.v_number = selected;
14735}
14736
14737
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14739
14740/*
14741 * "inputrestore()" function
14742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014744f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745{
14746 if (ga_userinput.ga_len > 0)
14747 {
14748 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14750 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014751 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 }
14753 else if (p_verbose > 1)
14754 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014755 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014756 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 }
14758}
14759
14760/*
14761 * "inputsave()" function
14762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014764f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014766 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 if (ga_grow(&ga_userinput, 1) == OK)
14768 {
14769 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14770 + ga_userinput.ga_len);
14771 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014772 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 }
14774 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014775 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776}
14777
14778/*
14779 * "inputsecret()" function
14780 */
14781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014782f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783{
14784 ++cmdline_star;
14785 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014786 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787 --cmdline_star;
14788 --inputsecret_flag;
14789}
14790
14791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014792 * "insert()" function
14793 */
14794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014795f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014796{
14797 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014798 listitem_T *item;
14799 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014800 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014801
14802 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014803 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014804 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014805 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014806 {
14807 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014808 before = get_tv_number_chk(&argvars[2], &error);
14809 if (error)
14810 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014811
Bram Moolenaar758711c2005-02-02 23:11:38 +000014812 if (before == l->lv_len)
14813 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014814 else
14815 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014816 item = list_find(l, before);
14817 if (item == NULL)
14818 {
14819 EMSGN(_(e_listidx), before);
14820 l = NULL;
14821 }
14822 }
14823 if (l != NULL)
14824 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014825 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014826 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014827 }
14828 }
14829}
14830
14831/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014832 * "invert(expr)" function
14833 */
14834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014835f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014836{
14837 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14838}
14839
14840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 * "isdirectory()" function
14842 */
14843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014844f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847}
14848
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014849/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014850 * "islocked()" function
14851 */
14852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014853f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014854{
14855 lval_T lv;
14856 char_u *end;
14857 dictitem_T *di;
14858
14859 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014860 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14861 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014862 if (end != NULL && lv.ll_name != NULL)
14863 {
14864 if (*end != NUL)
14865 EMSG(_(e_trailing));
14866 else
14867 {
14868 if (lv.ll_tv == NULL)
14869 {
14870 if (check_changedtick(lv.ll_name))
14871 rettv->vval.v_number = 1; /* always locked */
14872 else
14873 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014874 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014875 if (di != NULL)
14876 {
14877 /* Consider a variable locked when:
14878 * 1. the variable itself is locked
14879 * 2. the value of the variable is locked.
14880 * 3. the List or Dict value is locked.
14881 */
14882 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14883 || tv_islocked(&di->di_tv));
14884 }
14885 }
14886 }
14887 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014888 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014889 else if (lv.ll_newkey != NULL)
14890 EMSG2(_(e_dictkey), lv.ll_newkey);
14891 else if (lv.ll_list != NULL)
14892 /* List item. */
14893 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14894 else
14895 /* Dictionary item. */
14896 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14897 }
14898 }
14899
14900 clear_lval(&lv);
14901}
14902
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014903#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14904/*
14905 * "isnan()" function
14906 */
14907 static void
14908f_isnan(typval_T *argvars, typval_T *rettv)
14909{
14910 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14911 && isnan(argvars[0].vval.v_float);
14912}
14913#endif
14914
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014915static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014916
14917/*
14918 * Turn a dict into a list:
14919 * "what" == 0: list of keys
14920 * "what" == 1: list of values
14921 * "what" == 2: list of items
14922 */
14923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014924dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014925{
Bram Moolenaar33570922005-01-25 22:26:29 +000014926 list_T *l2;
14927 dictitem_T *di;
14928 hashitem_T *hi;
14929 listitem_T *li;
14930 listitem_T *li2;
14931 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014932 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014933
Bram Moolenaar8c711452005-01-14 21:53:12 +000014934 if (argvars[0].v_type != VAR_DICT)
14935 {
14936 EMSG(_(e_dictreq));
14937 return;
14938 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014939 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014940 return;
14941
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014942 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014943 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014944
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014945 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014946 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014947 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014948 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014949 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014950 --todo;
14951 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014952
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014953 li = listitem_alloc();
14954 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014955 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014956 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014957
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014958 if (what == 0)
14959 {
14960 /* keys() */
14961 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014962 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014963 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14964 }
14965 else if (what == 1)
14966 {
14967 /* values() */
14968 copy_tv(&di->di_tv, &li->li_tv);
14969 }
14970 else
14971 {
14972 /* items() */
14973 l2 = list_alloc();
14974 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014975 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014976 li->li_tv.vval.v_list = l2;
14977 if (l2 == NULL)
14978 break;
14979 ++l2->lv_refcount;
14980
14981 li2 = listitem_alloc();
14982 if (li2 == NULL)
14983 break;
14984 list_append(l2, li2);
14985 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014986 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014987 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14988
14989 li2 = listitem_alloc();
14990 if (li2 == NULL)
14991 break;
14992 list_append(l2, li2);
14993 copy_tv(&di->di_tv, &li2->li_tv);
14994 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014995 }
14996 }
14997}
14998
14999/*
15000 * "items(dict)" function
15001 */
15002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015003f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015004{
15005 dict_list(argvars, rettv, 2);
15006}
15007
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015008#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015009/*
15010 * Get the job from the argument.
15011 * Returns NULL if the job is invalid.
15012 */
15013 static job_T *
15014get_job_arg(typval_T *tv)
15015{
15016 job_T *job;
15017
15018 if (tv->v_type != VAR_JOB)
15019 {
15020 EMSG2(_(e_invarg2), get_tv_string(tv));
15021 return NULL;
15022 }
15023 job = tv->vval.v_job;
15024
15025 if (job == NULL)
15026 EMSG(_("E916: not a valid job"));
15027 return job;
15028}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015029
Bram Moolenaar835dc632016-02-07 14:27:38 +010015030/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015031 * "job_getchannel()" function
15032 */
15033 static void
15034f_job_getchannel(typval_T *argvars, typval_T *rettv)
15035{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015036 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015037
Bram Moolenaar65edff82016-02-21 16:40:11 +010015038 if (job != NULL)
15039 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015040 rettv->v_type = VAR_CHANNEL;
15041 rettv->vval.v_channel = job->jv_channel;
15042 if (job->jv_channel != NULL)
15043 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015044 }
15045}
15046
15047/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015048 * "job_info()" function
15049 */
15050 static void
15051f_job_info(typval_T *argvars, typval_T *rettv)
15052{
15053 job_T *job = get_job_arg(&argvars[0]);
15054
15055 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15056 job_info(job, rettv->vval.v_dict);
15057}
15058
15059/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015060 * "job_setoptions()" function
15061 */
15062 static void
15063f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15064{
15065 job_T *job = get_job_arg(&argvars[0]);
15066 jobopt_T opt;
15067
15068 if (job == NULL)
15069 return;
15070 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015071 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15072 job_set_options(job, &opt);
15073 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015074}
15075
15076/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015077 * "job_start()" function
15078 */
15079 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015080f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015081{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015082 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015083 if (check_restricted() || check_secure())
15084 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015085 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015086}
15087
15088/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015089 * "job_status()" function
15090 */
15091 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015092f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015093{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015094 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015095
Bram Moolenaar65edff82016-02-21 16:40:11 +010015096 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015097 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015098 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015099 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015100 }
15101}
15102
15103/*
15104 * "job_stop()" function
15105 */
15106 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015107f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015108{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015109 job_T *job = get_job_arg(&argvars[0]);
15110
15111 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015112 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015113}
15114#endif
15115
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015117 * "join()" function
15118 */
15119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015120f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015121{
15122 garray_T ga;
15123 char_u *sep;
15124
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015125 if (argvars[0].v_type != VAR_LIST)
15126 {
15127 EMSG(_(e_listreq));
15128 return;
15129 }
15130 if (argvars[0].vval.v_list == NULL)
15131 return;
15132 if (argvars[1].v_type == VAR_UNKNOWN)
15133 sep = (char_u *)" ";
15134 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015135 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015136
15137 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015138
15139 if (sep != NULL)
15140 {
15141 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015142 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015143 ga_append(&ga, NUL);
15144 rettv->vval.v_string = (char_u *)ga.ga_data;
15145 }
15146 else
15147 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015148}
15149
15150/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015151 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015152 */
15153 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015154f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015155{
15156 js_read_T reader;
15157
15158 reader.js_buf = get_tv_string(&argvars[0]);
15159 reader.js_fill = NULL;
15160 reader.js_used = 0;
15161 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15162 EMSG(_(e_invarg));
15163}
15164
15165/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015166 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015167 */
15168 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015169f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015170{
15171 rettv->v_type = VAR_STRING;
15172 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15173}
15174
15175/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015176 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015177 */
15178 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015179f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015180{
15181 js_read_T reader;
15182
15183 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015184 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015185 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015186 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015187 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015188}
15189
15190/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015191 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015192 */
15193 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015194f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015195{
15196 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015197 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015198}
15199
15200/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015201 * "keys()" function
15202 */
15203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015204f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015205{
15206 dict_list(argvars, rettv, 0);
15207}
15208
15209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210 * "last_buffer_nr()" function.
15211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015213f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214{
15215 int n = 0;
15216 buf_T *buf;
15217
15218 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15219 if (n < buf->b_fnum)
15220 n = buf->b_fnum;
15221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015222 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015223}
15224
15225/*
15226 * "len()" function
15227 */
15228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015229f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015230{
15231 switch (argvars[0].v_type)
15232 {
15233 case VAR_STRING:
15234 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015235 rettv->vval.v_number = (varnumber_T)STRLEN(
15236 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015237 break;
15238 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015239 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015240 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015241 case VAR_DICT:
15242 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15243 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015244 case VAR_UNKNOWN:
15245 case VAR_SPECIAL:
15246 case VAR_FLOAT:
15247 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015248 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015249 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015250 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015251 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015252 break;
15253 }
15254}
15255
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015256static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015257
15258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015259libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015260{
15261#ifdef FEAT_LIBCALL
15262 char_u *string_in;
15263 char_u **string_result;
15264 int nr_result;
15265#endif
15266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015268 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015270
15271 if (check_restricted() || check_secure())
15272 return;
15273
15274#ifdef FEAT_LIBCALL
15275 /* The first two args must be strings, otherwise its meaningless */
15276 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15277 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015278 string_in = NULL;
15279 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015280 string_in = argvars[2].vval.v_string;
15281 if (type == VAR_NUMBER)
15282 string_result = NULL;
15283 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015284 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015285 if (mch_libcall(argvars[0].vval.v_string,
15286 argvars[1].vval.v_string,
15287 string_in,
15288 argvars[2].vval.v_number,
15289 string_result,
15290 &nr_result) == OK
15291 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015292 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015293 }
15294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295}
15296
15297/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015298 * "libcall()" function
15299 */
15300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015301f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015302{
15303 libcall_common(argvars, rettv, VAR_STRING);
15304}
15305
15306/*
15307 * "libcallnr()" function
15308 */
15309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015310f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015311{
15312 libcall_common(argvars, rettv, VAR_NUMBER);
15313}
15314
15315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 * "line(string)" function
15317 */
15318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015319f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320{
15321 linenr_T lnum = 0;
15322 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015323 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015325 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326 if (fp != NULL)
15327 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015328 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329}
15330
15331/*
15332 * "line2byte(lnum)" function
15333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015335f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336{
15337#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015338 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339#else
15340 linenr_T lnum;
15341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015342 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015344 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015346 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15347 if (rettv->vval.v_number >= 0)
15348 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349#endif
15350}
15351
15352/*
15353 * "lispindent(lnum)" function
15354 */
15355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015356f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357{
15358#ifdef FEAT_LISP
15359 pos_T pos;
15360 linenr_T lnum;
15361
15362 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15365 {
15366 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015367 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 curwin->w_cursor = pos;
15369 }
15370 else
15371#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373}
15374
15375/*
15376 * "localtime()" function
15377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015379f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382}
15383
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015384static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385
15386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015387get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388{
15389 char_u *keys;
15390 char_u *which;
15391 char_u buf[NUMBUFLEN];
15392 char_u *keys_buf = NULL;
15393 char_u *rhs;
15394 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015395 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015396 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015397 mapblock_T *mp;
15398 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399
15400 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015401 rettv->v_type = VAR_STRING;
15402 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 if (*keys == NUL)
15406 return;
15407
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015408 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015410 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015411 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015412 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015413 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015414 if (argvars[3].v_type != VAR_UNKNOWN)
15415 get_dict = get_tv_number(&argvars[3]);
15416 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 else
15419 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015420 if (which == NULL)
15421 return;
15422
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 mode = get_map_mode(&which, 0);
15424
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015425 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015426 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015428
15429 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015431 /* Return a string. */
15432 if (rhs != NULL)
15433 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434
Bram Moolenaarbd743252010-10-20 21:23:33 +020015435 }
15436 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15437 {
15438 /* Return a dictionary. */
15439 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15440 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15441 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442
Bram Moolenaarbd743252010-10-20 21:23:33 +020015443 dict_add_nr_str(dict, "lhs", 0L, lhs);
15444 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15445 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15446 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15447 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15448 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15449 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015450 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015451 dict_add_nr_str(dict, "mode", 0L, mapmode);
15452
15453 vim_free(lhs);
15454 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 }
15456}
15457
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015458#ifdef FEAT_FLOAT
15459/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015460 * "log()" function
15461 */
15462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015463f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015464{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015465 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015466
15467 rettv->v_type = VAR_FLOAT;
15468 if (get_float_arg(argvars, &f) == OK)
15469 rettv->vval.v_float = log(f);
15470 else
15471 rettv->vval.v_float = 0.0;
15472}
15473
15474/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015475 * "log10()" function
15476 */
15477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015478f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015479{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015480 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015481
15482 rettv->v_type = VAR_FLOAT;
15483 if (get_float_arg(argvars, &f) == OK)
15484 rettv->vval.v_float = log10(f);
15485 else
15486 rettv->vval.v_float = 0.0;
15487}
15488#endif
15489
Bram Moolenaar1dced572012-04-05 16:54:08 +020015490#ifdef FEAT_LUA
15491/*
15492 * "luaeval()" function
15493 */
15494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015495f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015496{
15497 char_u *str;
15498 char_u buf[NUMBUFLEN];
15499
15500 str = get_tv_string_buf(&argvars[0], buf);
15501 do_luaeval(str, argvars + 1, rettv);
15502}
15503#endif
15504
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015506 * "map()" function
15507 */
15508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015509f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015510{
15511 filter_map(argvars, rettv, TRUE);
15512}
15513
15514/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015515 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 */
15517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015518f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015520 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521}
15522
15523/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015524 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 */
15526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015527f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015529 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530}
15531
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015532static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533
15534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015535find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015537 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015538 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015539 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 char_u *pat;
15541 regmatch_T regmatch;
15542 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015543 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 char_u *save_cpo;
15545 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015546 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015547 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015548 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015549 list_T *l = NULL;
15550 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015551 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015552 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553
15554 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15555 save_cpo = p_cpo;
15556 p_cpo = (char_u *)"";
15557
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015558 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015559 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015560 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015561 /* type 3: return empty list when there are no matches.
15562 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015563 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015564 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015565 if (type == 4
15566 && (list_append_string(rettv->vval.v_list,
15567 (char_u *)"", 0) == FAIL
15568 || list_append_number(rettv->vval.v_list,
15569 (varnumber_T)-1) == FAIL
15570 || list_append_number(rettv->vval.v_list,
15571 (varnumber_T)-1) == FAIL
15572 || list_append_number(rettv->vval.v_list,
15573 (varnumber_T)-1) == FAIL))
15574 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015575 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015576 rettv->vval.v_list = NULL;
15577 goto theend;
15578 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015579 }
15580 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015582 rettv->v_type = VAR_STRING;
15583 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015586 if (argvars[0].v_type == VAR_LIST)
15587 {
15588 if ((l = argvars[0].vval.v_list) == NULL)
15589 goto theend;
15590 li = l->lv_first;
15591 }
15592 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015593 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015594 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015595 len = (long)STRLEN(str);
15596 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015598 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15599 if (pat == NULL)
15600 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015601
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015602 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015604 int error = FALSE;
15605
15606 start = get_tv_number_chk(&argvars[2], &error);
15607 if (error)
15608 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015609 if (l != NULL)
15610 {
15611 li = list_find(l, start);
15612 if (li == NULL)
15613 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015614 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015615 }
15616 else
15617 {
15618 if (start < 0)
15619 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015620 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015621 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015622 /* When "count" argument is there ignore matches before "start",
15623 * otherwise skip part of the string. Differs when pattern is "^"
15624 * or "\<". */
15625 if (argvars[3].v_type != VAR_UNKNOWN)
15626 startcol = start;
15627 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015628 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015629 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015630 len -= start;
15631 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015632 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015633
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015634 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015635 nth = get_tv_number_chk(&argvars[3], &error);
15636 if (error)
15637 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 }
15639
15640 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15641 if (regmatch.regprog != NULL)
15642 {
15643 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015644
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015645 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015646 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015647 if (l != NULL)
15648 {
15649 if (li == NULL)
15650 {
15651 match = FALSE;
15652 break;
15653 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015654 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015655 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015656 if (str == NULL)
15657 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015658 }
15659
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015660 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015661
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015662 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015663 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015664 if (l == NULL && !match)
15665 break;
15666
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015667 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015668 if (l != NULL)
15669 {
15670 li = li->li_next;
15671 ++idx;
15672 }
15673 else
15674 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015675#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015676 startcol = (colnr_T)(regmatch.startp[0]
15677 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015678#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015679 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015680#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015681 if (startcol > (colnr_T)len
15682 || str + startcol <= regmatch.startp[0])
15683 {
15684 match = FALSE;
15685 break;
15686 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015687 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015688 }
15689
15690 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015692 if (type == 4)
15693 {
15694 listitem_T *li1 = rettv->vval.v_list->lv_first;
15695 listitem_T *li2 = li1->li_next;
15696 listitem_T *li3 = li2->li_next;
15697 listitem_T *li4 = li3->li_next;
15698
15699 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15700 (int)(regmatch.endp[0] - regmatch.startp[0]));
15701 li3->li_tv.vval.v_number =
15702 (varnumber_T)(regmatch.startp[0] - expr);
15703 li4->li_tv.vval.v_number =
15704 (varnumber_T)(regmatch.endp[0] - expr);
15705 if (l != NULL)
15706 li2->li_tv.vval.v_number = (varnumber_T)idx;
15707 }
15708 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015709 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015710 int i;
15711
15712 /* return list with matched string and submatches */
15713 for (i = 0; i < NSUBEXP; ++i)
15714 {
15715 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015716 {
15717 if (list_append_string(rettv->vval.v_list,
15718 (char_u *)"", 0) == FAIL)
15719 break;
15720 }
15721 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015722 regmatch.startp[i],
15723 (int)(regmatch.endp[i] - regmatch.startp[i]))
15724 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015725 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015726 }
15727 }
15728 else if (type == 2)
15729 {
15730 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015731 if (l != NULL)
15732 copy_tv(&li->li_tv, rettv);
15733 else
15734 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015736 }
15737 else if (l != NULL)
15738 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 else
15740 {
15741 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015742 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743 (varnumber_T)(regmatch.startp[0] - str);
15744 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015745 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015747 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 }
15749 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015750 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751 }
15752
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015753 if (type == 4 && l == NULL)
15754 /* matchstrpos() without a list: drop the second item. */
15755 listitem_remove(rettv->vval.v_list,
15756 rettv->vval.v_list->lv_first->li_next);
15757
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015759 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 p_cpo = save_cpo;
15761}
15762
15763/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015764 * "match()" function
15765 */
15766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015767f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015768{
15769 find_some_match(argvars, rettv, 1);
15770}
15771
15772/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015773 * "matchadd()" function
15774 */
15775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015776f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015777{
15778#ifdef FEAT_SEARCH_EXTRA
15779 char_u buf[NUMBUFLEN];
15780 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15781 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15782 int prio = 10; /* default priority */
15783 int id = -1;
15784 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015785 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015786
15787 rettv->vval.v_number = -1;
15788
15789 if (grp == NULL || pat == NULL)
15790 return;
15791 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015792 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015793 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015794 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015795 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015796 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015797 if (argvars[4].v_type != VAR_UNKNOWN)
15798 {
15799 if (argvars[4].v_type != VAR_DICT)
15800 {
15801 EMSG(_(e_dictreq));
15802 return;
15803 }
15804 if (dict_find(argvars[4].vval.v_dict,
15805 (char_u *)"conceal", -1) != NULL)
15806 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15807 (char_u *)"conceal", FALSE);
15808 }
15809 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015810 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015811 if (error == TRUE)
15812 return;
15813 if (id >= 1 && id <= 3)
15814 {
15815 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15816 return;
15817 }
15818
Bram Moolenaar6561d522015-07-21 15:48:27 +020015819 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15820 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015821#endif
15822}
15823
15824/*
15825 * "matchaddpos()" function
15826 */
15827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015828f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015829{
15830#ifdef FEAT_SEARCH_EXTRA
15831 char_u buf[NUMBUFLEN];
15832 char_u *group;
15833 int prio = 10;
15834 int id = -1;
15835 int error = FALSE;
15836 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015837 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015838
15839 rettv->vval.v_number = -1;
15840
15841 group = get_tv_string_buf_chk(&argvars[0], buf);
15842 if (group == NULL)
15843 return;
15844
15845 if (argvars[1].v_type != VAR_LIST)
15846 {
15847 EMSG2(_(e_listarg), "matchaddpos()");
15848 return;
15849 }
15850 l = argvars[1].vval.v_list;
15851 if (l == NULL)
15852 return;
15853
15854 if (argvars[2].v_type != VAR_UNKNOWN)
15855 {
15856 prio = get_tv_number_chk(&argvars[2], &error);
15857 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015858 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015859 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015860 if (argvars[4].v_type != VAR_UNKNOWN)
15861 {
15862 if (argvars[4].v_type != VAR_DICT)
15863 {
15864 EMSG(_(e_dictreq));
15865 return;
15866 }
15867 if (dict_find(argvars[4].vval.v_dict,
15868 (char_u *)"conceal", -1) != NULL)
15869 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15870 (char_u *)"conceal", FALSE);
15871 }
15872 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015873 }
15874 if (error == TRUE)
15875 return;
15876
15877 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15878 if (id == 1 || id == 2)
15879 {
15880 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15881 return;
15882 }
15883
Bram Moolenaar6561d522015-07-21 15:48:27 +020015884 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15885 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015886#endif
15887}
15888
15889/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015890 * "matcharg()" function
15891 */
15892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015893f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015894{
15895 if (rettv_list_alloc(rettv) == OK)
15896 {
15897#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015898 int id = get_tv_number(&argvars[0]);
15899 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015900
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015901 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015902 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015903 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15904 {
15905 list_append_string(rettv->vval.v_list,
15906 syn_id2name(m->hlg_id), -1);
15907 list_append_string(rettv->vval.v_list, m->pattern, -1);
15908 }
15909 else
15910 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015911 list_append_string(rettv->vval.v_list, NULL, -1);
15912 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015913 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015914 }
15915#endif
15916 }
15917}
15918
15919/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015920 * "matchdelete()" function
15921 */
15922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015923f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015924{
15925#ifdef FEAT_SEARCH_EXTRA
15926 rettv->vval.v_number = match_delete(curwin,
15927 (int)get_tv_number(&argvars[0]), TRUE);
15928#endif
15929}
15930
15931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015932 * "matchend()" function
15933 */
15934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015935f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015936{
15937 find_some_match(argvars, rettv, 0);
15938}
15939
15940/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015941 * "matchlist()" function
15942 */
15943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015944f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015945{
15946 find_some_match(argvars, rettv, 3);
15947}
15948
15949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015950 * "matchstr()" function
15951 */
15952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015953f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015954{
15955 find_some_match(argvars, rettv, 2);
15956}
15957
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015958/*
15959 * "matchstrpos()" function
15960 */
15961 static void
15962f_matchstrpos(typval_T *argvars, typval_T *rettv)
15963{
15964 find_some_match(argvars, rettv, 4);
15965}
15966
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015967static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015968
15969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015970max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015971{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015972 long n = 0;
15973 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015974 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015975
15976 if (argvars[0].v_type == VAR_LIST)
15977 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015978 list_T *l;
15979 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015980
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015981 l = argvars[0].vval.v_list;
15982 if (l != NULL)
15983 {
15984 li = l->lv_first;
15985 if (li != NULL)
15986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015987 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015988 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015989 {
15990 li = li->li_next;
15991 if (li == NULL)
15992 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015993 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015994 if (domax ? i > n : i < n)
15995 n = i;
15996 }
15997 }
15998 }
15999 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016000 else if (argvars[0].v_type == VAR_DICT)
16001 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016003 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016004 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016005 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016006
16007 d = argvars[0].vval.v_dict;
16008 if (d != NULL)
16009 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016010 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016011 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016012 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016013 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016014 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016015 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016016 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016017 if (first)
16018 {
16019 n = i;
16020 first = FALSE;
16021 }
16022 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016023 n = i;
16024 }
16025 }
16026 }
16027 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016028 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016029 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016030 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016031}
16032
16033/*
16034 * "max()" function
16035 */
16036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016037f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016038{
16039 max_min(argvars, rettv, TRUE);
16040}
16041
16042/*
16043 * "min()" function
16044 */
16045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016046f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016047{
16048 max_min(argvars, rettv, FALSE);
16049}
16050
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016051static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016052
16053/*
16054 * Create the directory in which "dir" is located, and higher levels when
16055 * needed.
16056 */
16057 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016058mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016059{
16060 char_u *p;
16061 char_u *updir;
16062 int r = FAIL;
16063
16064 /* Get end of directory name in "dir".
16065 * We're done when it's "/" or "c:/". */
16066 p = gettail_sep(dir);
16067 if (p <= get_past_head(dir))
16068 return OK;
16069
16070 /* If the directory exists we're done. Otherwise: create it.*/
16071 updir = vim_strnsave(dir, (int)(p - dir));
16072 if (updir == NULL)
16073 return FAIL;
16074 if (mch_isdir(updir))
16075 r = OK;
16076 else if (mkdir_recurse(updir, prot) == OK)
16077 r = vim_mkdir_emsg(updir, prot);
16078 vim_free(updir);
16079 return r;
16080}
16081
16082#ifdef vim_mkdir
16083/*
16084 * "mkdir()" function
16085 */
16086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016087f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016088{
16089 char_u *dir;
16090 char_u buf[NUMBUFLEN];
16091 int prot = 0755;
16092
16093 rettv->vval.v_number = FAIL;
16094 if (check_restricted() || check_secure())
16095 return;
16096
16097 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016098 if (*dir == NUL)
16099 rettv->vval.v_number = FAIL;
16100 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016101 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016102 if (*gettail(dir) == NUL)
16103 /* remove trailing slashes */
16104 *gettail_sep(dir) = NUL;
16105
16106 if (argvars[1].v_type != VAR_UNKNOWN)
16107 {
16108 if (argvars[2].v_type != VAR_UNKNOWN)
16109 prot = get_tv_number_chk(&argvars[2], NULL);
16110 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16111 mkdir_recurse(dir, prot);
16112 }
16113 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016114 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016115}
16116#endif
16117
Bram Moolenaar0d660222005-01-07 21:51:51 +000016118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 * "mode()" function
16120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016122f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016124 char_u buf[3];
16125
16126 buf[1] = NUL;
16127 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129 if (VIsual_active)
16130 {
16131 if (VIsual_select)
16132 buf[0] = VIsual_mode + 's' - 'v';
16133 else
16134 buf[0] = VIsual_mode;
16135 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016136 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016137 || State == CONFIRM)
16138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016140 if (State == ASKMORE)
16141 buf[1] = 'm';
16142 else if (State == CONFIRM)
16143 buf[1] = '?';
16144 }
16145 else if (State == EXTERNCMD)
16146 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 else if (State & INSERT)
16148 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016149#ifdef FEAT_VREPLACE
16150 if (State & VREPLACE_FLAG)
16151 {
16152 buf[0] = 'R';
16153 buf[1] = 'v';
16154 }
16155 else
16156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 if (State & REPLACE_FLAG)
16158 buf[0] = 'R';
16159 else
16160 buf[0] = 'i';
16161 }
16162 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016165 if (exmode_active)
16166 buf[1] = 'v';
16167 }
16168 else if (exmode_active)
16169 {
16170 buf[0] = 'c';
16171 buf[1] = 'e';
16172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016174 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016176 if (finish_op)
16177 buf[1] = 'o';
16178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016180 /* Clear out the minor mode when the argument is not a non-zero number or
16181 * non-empty string. */
16182 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016183 buf[1] = NUL;
16184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016185 rettv->vval.v_string = vim_strsave(buf);
16186 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187}
16188
Bram Moolenaar429fa852013-04-15 12:27:36 +020016189#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016190/*
16191 * "mzeval()" function
16192 */
16193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016194f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016195{
16196 char_u *str;
16197 char_u buf[NUMBUFLEN];
16198
16199 str = get_tv_string_buf(&argvars[0], buf);
16200 do_mzeval(str, rettv);
16201}
Bram Moolenaar75676462013-01-30 14:55:42 +010016202
16203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016204mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016205{
16206 typval_T argvars[3];
16207
16208 argvars[0].v_type = VAR_STRING;
16209 argvars[0].vval.v_string = name;
16210 copy_tv(args, &argvars[1]);
16211 argvars[2].v_type = VAR_UNKNOWN;
16212 f_call(argvars, rettv);
16213 clear_tv(&argvars[1]);
16214}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016215#endif
16216
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016218 * "nextnonblank()" function
16219 */
16220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016221f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016222{
16223 linenr_T lnum;
16224
16225 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016227 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016228 {
16229 lnum = 0;
16230 break;
16231 }
16232 if (*skipwhite(ml_get(lnum)) != NUL)
16233 break;
16234 }
16235 rettv->vval.v_number = lnum;
16236}
16237
16238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 * "nr2char()" function
16240 */
16241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016242f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243{
16244 char_u buf[NUMBUFLEN];
16245
16246#ifdef FEAT_MBYTE
16247 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016248 {
16249 int utf8 = 0;
16250
16251 if (argvars[1].v_type != VAR_UNKNOWN)
16252 utf8 = get_tv_number_chk(&argvars[1], NULL);
16253 if (utf8)
16254 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16255 else
16256 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 else
16259#endif
16260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016261 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 buf[1] = NUL;
16263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016264 rettv->v_type = VAR_STRING;
16265 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016266}
16267
16268/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016269 * "or(expr, expr)" function
16270 */
16271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016272f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016273{
16274 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16275 | get_tv_number_chk(&argvars[1], NULL);
16276}
16277
16278/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016279 * "pathshorten()" function
16280 */
16281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016282f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016283{
16284 char_u *p;
16285
16286 rettv->v_type = VAR_STRING;
16287 p = get_tv_string_chk(&argvars[0]);
16288 if (p == NULL)
16289 rettv->vval.v_string = NULL;
16290 else
16291 {
16292 p = vim_strsave(p);
16293 rettv->vval.v_string = p;
16294 if (p != NULL)
16295 shorten_dir(p);
16296 }
16297}
16298
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016299#ifdef FEAT_PERL
16300/*
16301 * "perleval()" function
16302 */
16303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016304f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016305{
16306 char_u *str;
16307 char_u buf[NUMBUFLEN];
16308
16309 str = get_tv_string_buf(&argvars[0], buf);
16310 do_perleval(str, rettv);
16311}
16312#endif
16313
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016314#ifdef FEAT_FLOAT
16315/*
16316 * "pow()" function
16317 */
16318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016319f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016320{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016321 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016322
16323 rettv->v_type = VAR_FLOAT;
16324 if (get_float_arg(argvars, &fx) == OK
16325 && get_float_arg(&argvars[1], &fy) == OK)
16326 rettv->vval.v_float = pow(fx, fy);
16327 else
16328 rettv->vval.v_float = 0.0;
16329}
16330#endif
16331
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016332/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016333 * "prevnonblank()" function
16334 */
16335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016336f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337{
16338 linenr_T lnum;
16339
16340 lnum = get_tv_lnum(argvars);
16341 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16342 lnum = 0;
16343 else
16344 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16345 --lnum;
16346 rettv->vval.v_number = lnum;
16347}
16348
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016349/* This dummy va_list is here because:
16350 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16351 * - locally in the function results in a "used before set" warning
16352 * - using va_start() to initialize it gives "function with fixed args" error */
16353static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016354
Bram Moolenaar8c711452005-01-14 21:53:12 +000016355/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016356 * "printf()" function
16357 */
16358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016359f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016360{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016361 char_u buf[NUMBUFLEN];
16362 int len;
16363 char_u *s;
16364 int saved_did_emsg = did_emsg;
16365 char *fmt;
16366
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016367 rettv->v_type = VAR_STRING;
16368 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016369
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016370 /* Get the required length, allocate the buffer and do it for real. */
16371 did_emsg = FALSE;
16372 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16373 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16374 if (!did_emsg)
16375 {
16376 s = alloc(len + 1);
16377 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016378 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016379 rettv->vval.v_string = s;
16380 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016381 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016382 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016383 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016384}
16385
16386/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016387 * "pumvisible()" function
16388 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016390f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016391{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016392#ifdef FEAT_INS_EXPAND
16393 if (pum_visible())
16394 rettv->vval.v_number = 1;
16395#endif
16396}
16397
Bram Moolenaardb913952012-06-29 12:54:53 +020016398#ifdef FEAT_PYTHON3
16399/*
16400 * "py3eval()" function
16401 */
16402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016403f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016404{
16405 char_u *str;
16406 char_u buf[NUMBUFLEN];
16407
16408 str = get_tv_string_buf(&argvars[0], buf);
16409 do_py3eval(str, rettv);
16410}
16411#endif
16412
16413#ifdef FEAT_PYTHON
16414/*
16415 * "pyeval()" function
16416 */
16417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016418f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016419{
16420 char_u *str;
16421 char_u buf[NUMBUFLEN];
16422
16423 str = get_tv_string_buf(&argvars[0], buf);
16424 do_pyeval(str, rettv);
16425}
16426#endif
16427
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016428/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016429 * "range()" function
16430 */
16431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016432f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016433{
16434 long start;
16435 long end;
16436 long stride = 1;
16437 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016438 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016440 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016441 if (argvars[1].v_type == VAR_UNKNOWN)
16442 {
16443 end = start - 1;
16444 start = 0;
16445 }
16446 else
16447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016448 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016449 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016450 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016451 }
16452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016453 if (error)
16454 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016455 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016456 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016457 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016458 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016459 else
16460 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016461 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016462 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016463 if (list_append_number(rettv->vval.v_list,
16464 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016465 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016466 }
16467}
16468
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016469/*
16470 * "readfile()" function
16471 */
16472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016473f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016474{
16475 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016476 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016477 char_u *fname;
16478 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016479 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16480 int io_size = sizeof(buf);
16481 int readlen; /* size of last fread() */
16482 char_u *prev = NULL; /* previously read bytes, if any */
16483 long prevlen = 0; /* length of data in prev */
16484 long prevsize = 0; /* size of prev buffer */
16485 long maxline = MAXLNUM;
16486 long cnt = 0;
16487 char_u *p; /* position in buf */
16488 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016489
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016490 if (argvars[1].v_type != VAR_UNKNOWN)
16491 {
16492 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16493 binary = TRUE;
16494 if (argvars[2].v_type != VAR_UNKNOWN)
16495 maxline = get_tv_number(&argvars[2]);
16496 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016497
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016498 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016499 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016500
16501 /* Always open the file in binary mode, library functions have a mind of
16502 * their own about CR-LF conversion. */
16503 fname = get_tv_string(&argvars[0]);
16504 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16505 {
16506 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16507 return;
16508 }
16509
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016510 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016511 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016512 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016513
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016514 /* This for loop processes what was read, but is also entered at end
16515 * of file so that either:
16516 * - an incomplete line gets written
16517 * - a "binary" file gets an empty line at the end if it ends in a
16518 * newline. */
16519 for (p = buf, start = buf;
16520 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16521 ++p)
16522 {
16523 if (*p == '\n' || readlen <= 0)
16524 {
16525 listitem_T *li;
16526 char_u *s = NULL;
16527 long_u len = p - start;
16528
16529 /* Finished a line. Remove CRs before NL. */
16530 if (readlen > 0 && !binary)
16531 {
16532 while (len > 0 && start[len - 1] == '\r')
16533 --len;
16534 /* removal may cross back to the "prev" string */
16535 if (len == 0)
16536 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16537 --prevlen;
16538 }
16539 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016540 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016541 else
16542 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016543 /* Change "prev" buffer to be the right size. This way
16544 * the bytes are only copied once, and very long lines are
16545 * allocated only once. */
16546 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016547 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016548 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016549 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016550 prev = NULL; /* the list will own the string */
16551 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016552 }
16553 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016554 if (s == NULL)
16555 {
16556 do_outofmem_msg((long_u) prevlen + len + 1);
16557 failed = TRUE;
16558 break;
16559 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016560
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016561 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016562 {
16563 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016564 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016565 break;
16566 }
16567 li->li_tv.v_type = VAR_STRING;
16568 li->li_tv.v_lock = 0;
16569 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016570 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016571
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016572 start = p + 1; /* step over newline */
16573 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016574 break;
16575 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016576 else if (*p == NUL)
16577 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016578#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016579 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16580 * when finding the BF and check the previous two bytes. */
16581 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016582 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016583 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16584 * + 1, these may be in the "prev" string. */
16585 char_u back1 = p >= buf + 1 ? p[-1]
16586 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16587 char_u back2 = p >= buf + 2 ? p[-2]
16588 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16589 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016590
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016591 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016592 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016593 char_u *dest = p - 2;
16594
16595 /* Usually a BOM is at the beginning of a file, and so at
16596 * the beginning of a line; then we can just step over it.
16597 */
16598 if (start == dest)
16599 start = p + 1;
16600 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016601 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016602 /* have to shuffle buf to close gap */
16603 int adjust_prevlen = 0;
16604
16605 if (dest < buf)
16606 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016607 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016608 dest = buf;
16609 }
16610 if (readlen > p - buf + 1)
16611 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16612 readlen -= 3 - adjust_prevlen;
16613 prevlen -= adjust_prevlen;
16614 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016615 }
16616 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016617 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016618#endif
16619 } /* for */
16620
16621 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16622 break;
16623 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016624 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016625 /* There's part of a line in buf, store it in "prev". */
16626 if (p - start + prevlen >= prevsize)
16627 {
16628 /* need bigger "prev" buffer */
16629 char_u *newprev;
16630
16631 /* A common use case is ordinary text files and "prev" gets a
16632 * fragment of a line, so the first allocation is made
16633 * small, to avoid repeatedly 'allocing' large and
16634 * 'reallocing' small. */
16635 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016636 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016637 else
16638 {
16639 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016640 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016641 prevsize = grow50pc > growmin ? grow50pc : growmin;
16642 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016643 newprev = prev == NULL ? alloc(prevsize)
16644 : vim_realloc(prev, prevsize);
16645 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016646 {
16647 do_outofmem_msg((long_u)prevsize);
16648 failed = TRUE;
16649 break;
16650 }
16651 prev = newprev;
16652 }
16653 /* Add the line part to end of "prev". */
16654 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016655 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016656 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016657 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016658
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016659 /*
16660 * For a negative line count use only the lines at the end of the file,
16661 * free the rest.
16662 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016663 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016664 while (cnt > -maxline)
16665 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016666 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016667 --cnt;
16668 }
16669
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016670 if (failed)
16671 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016672 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016673 /* readfile doc says an empty list is returned on error */
16674 rettv->vval.v_list = list_alloc();
16675 }
16676
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016677 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016678 fclose(fd);
16679}
16680
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016681#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016682static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016683
16684/*
16685 * Convert a List to proftime_T.
16686 * Return FAIL when there is something wrong.
16687 */
16688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016689list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016690{
16691 long n1, n2;
16692 int error = FALSE;
16693
16694 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16695 || arg->vval.v_list->lv_len != 2)
16696 return FAIL;
16697 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16698 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16699# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016700 tm->HighPart = n1;
16701 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016702# else
16703 tm->tv_sec = n1;
16704 tm->tv_usec = n2;
16705# endif
16706 return error ? FAIL : OK;
16707}
16708#endif /* FEAT_RELTIME */
16709
16710/*
16711 * "reltime()" function
16712 */
16713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016714f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016715{
16716#ifdef FEAT_RELTIME
16717 proftime_T res;
16718 proftime_T start;
16719
16720 if (argvars[0].v_type == VAR_UNKNOWN)
16721 {
16722 /* No arguments: get current time. */
16723 profile_start(&res);
16724 }
16725 else if (argvars[1].v_type == VAR_UNKNOWN)
16726 {
16727 if (list2proftime(&argvars[0], &res) == FAIL)
16728 return;
16729 profile_end(&res);
16730 }
16731 else
16732 {
16733 /* Two arguments: compute the difference. */
16734 if (list2proftime(&argvars[0], &start) == FAIL
16735 || list2proftime(&argvars[1], &res) == FAIL)
16736 return;
16737 profile_sub(&res, &start);
16738 }
16739
16740 if (rettv_list_alloc(rettv) == OK)
16741 {
16742 long n1, n2;
16743
16744# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016745 n1 = res.HighPart;
16746 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016747# else
16748 n1 = res.tv_sec;
16749 n2 = res.tv_usec;
16750# endif
16751 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16752 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16753 }
16754#endif
16755}
16756
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016757#ifdef FEAT_FLOAT
16758/*
16759 * "reltimefloat()" function
16760 */
16761 static void
16762f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16763{
16764# ifdef FEAT_RELTIME
16765 proftime_T tm;
16766# endif
16767
16768 rettv->v_type = VAR_FLOAT;
16769 rettv->vval.v_float = 0;
16770# ifdef FEAT_RELTIME
16771 if (list2proftime(&argvars[0], &tm) == OK)
16772 rettv->vval.v_float = profile_float(&tm);
16773# endif
16774}
16775#endif
16776
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016777/*
16778 * "reltimestr()" function
16779 */
16780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016781f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016782{
16783#ifdef FEAT_RELTIME
16784 proftime_T tm;
16785#endif
16786
16787 rettv->v_type = VAR_STRING;
16788 rettv->vval.v_string = NULL;
16789#ifdef FEAT_RELTIME
16790 if (list2proftime(&argvars[0], &tm) == OK)
16791 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16792#endif
16793}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016794
Bram Moolenaar0d660222005-01-07 21:51:51 +000016795#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016796static void make_connection(void);
16797static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016798
16799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016800make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016801{
16802 if (X_DISPLAY == NULL
16803# ifdef FEAT_GUI
16804 && !gui.in_use
16805# endif
16806 )
16807 {
16808 x_force_connect = TRUE;
16809 setup_term_clip();
16810 x_force_connect = FALSE;
16811 }
16812}
16813
16814 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016815check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016816{
16817 make_connection();
16818 if (X_DISPLAY == NULL)
16819 {
16820 EMSG(_("E240: No connection to Vim server"));
16821 return FAIL;
16822 }
16823 return OK;
16824}
16825#endif
16826
16827#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016829remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016830{
16831 char_u *server_name;
16832 char_u *keys;
16833 char_u *r = NULL;
16834 char_u buf[NUMBUFLEN];
16835# ifdef WIN32
16836 HWND w;
16837# else
16838 Window w;
16839# endif
16840
16841 if (check_restricted() || check_secure())
16842 return;
16843
16844# ifdef FEAT_X11
16845 if (check_connection() == FAIL)
16846 return;
16847# endif
16848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016849 server_name = get_tv_string_chk(&argvars[0]);
16850 if (server_name == NULL)
16851 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016852 keys = get_tv_string_buf(&argvars[1], buf);
16853# ifdef WIN32
16854 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16855# else
16856 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16857 < 0)
16858# endif
16859 {
16860 if (r != NULL)
16861 EMSG(r); /* sending worked but evaluation failed */
16862 else
16863 EMSG2(_("E241: Unable to send to %s"), server_name);
16864 return;
16865 }
16866
16867 rettv->vval.v_string = r;
16868
16869 if (argvars[2].v_type != VAR_UNKNOWN)
16870 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016871 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016872 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016873 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016875 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016876 v.di_tv.v_type = VAR_STRING;
16877 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016878 idvar = get_tv_string_chk(&argvars[2]);
16879 if (idvar != NULL)
16880 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016881 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016882 }
16883}
16884#endif
16885
16886/*
16887 * "remote_expr()" function
16888 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016890f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891{
16892 rettv->v_type = VAR_STRING;
16893 rettv->vval.v_string = NULL;
16894#ifdef FEAT_CLIENTSERVER
16895 remote_common(argvars, rettv, TRUE);
16896#endif
16897}
16898
16899/*
16900 * "remote_foreground()" function
16901 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016903f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016904{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905#ifdef FEAT_CLIENTSERVER
16906# ifdef WIN32
16907 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016908 {
16909 char_u *server_name = get_tv_string_chk(&argvars[0]);
16910
16911 if (server_name != NULL)
16912 serverForeground(server_name);
16913 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914# else
16915 /* Send a foreground() expression to the server. */
16916 argvars[1].v_type = VAR_STRING;
16917 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16918 argvars[2].v_type = VAR_UNKNOWN;
16919 remote_common(argvars, rettv, TRUE);
16920 vim_free(argvars[1].vval.v_string);
16921# endif
16922#endif
16923}
16924
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016926f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016927{
16928#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016929 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 char_u *s = NULL;
16931# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016932 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016934 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016935
16936 if (check_restricted() || check_secure())
16937 {
16938 rettv->vval.v_number = -1;
16939 return;
16940 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016941 serverid = get_tv_string_chk(&argvars[0]);
16942 if (serverid == NULL)
16943 {
16944 rettv->vval.v_number = -1;
16945 return; /* type error; errmsg already given */
16946 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016947# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016948 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 if (n == 0)
16950 rettv->vval.v_number = -1;
16951 else
16952 {
16953 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16954 rettv->vval.v_number = (s != NULL);
16955 }
16956# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957 if (check_connection() == FAIL)
16958 return;
16959
16960 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016961 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962# endif
16963
16964 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016966 char_u *retvar;
16967
Bram Moolenaar33570922005-01-25 22:26:29 +000016968 v.di_tv.v_type = VAR_STRING;
16969 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016970 retvar = get_tv_string_chk(&argvars[1]);
16971 if (retvar != NULL)
16972 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016973 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974 }
16975#else
16976 rettv->vval.v_number = -1;
16977#endif
16978}
16979
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016981f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982{
16983 char_u *r = NULL;
16984
16985#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016986 char_u *serverid = get_tv_string_chk(&argvars[0]);
16987
16988 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989 {
16990# ifdef WIN32
16991 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016992 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016994 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016995 if (n != 0)
16996 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16997 if (r == NULL)
16998# else
16999 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017000 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017001# endif
17002 EMSG(_("E277: Unable to read a server reply"));
17003 }
17004#endif
17005 rettv->v_type = VAR_STRING;
17006 rettv->vval.v_string = r;
17007}
17008
17009/*
17010 * "remote_send()" function
17011 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017013f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014{
17015 rettv->v_type = VAR_STRING;
17016 rettv->vval.v_string = NULL;
17017#ifdef FEAT_CLIENTSERVER
17018 remote_common(argvars, rettv, FALSE);
17019#endif
17020}
17021
17022/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017023 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017024 */
17025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017026f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017027{
Bram Moolenaar33570922005-01-25 22:26:29 +000017028 list_T *l;
17029 listitem_T *item, *item2;
17030 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017031 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017032 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017033 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017034 dict_T *d;
17035 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017036 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017037
Bram Moolenaar8c711452005-01-14 21:53:12 +000017038 if (argvars[0].v_type == VAR_DICT)
17039 {
17040 if (argvars[2].v_type != VAR_UNKNOWN)
17041 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017042 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017043 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017045 key = get_tv_string_chk(&argvars[1]);
17046 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017048 di = dict_find(d, key, -1);
17049 if (di == NULL)
17050 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017051 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17052 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017053 {
17054 *rettv = di->di_tv;
17055 init_tv(&di->di_tv);
17056 dictitem_remove(d, di);
17057 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017059 }
17060 }
17061 else if (argvars[0].v_type != VAR_LIST)
17062 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017063 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017064 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017066 int error = FALSE;
17067
17068 idx = get_tv_number_chk(&argvars[1], &error);
17069 if (error)
17070 ; /* type error: do nothing, errmsg already given */
17071 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017072 EMSGN(_(e_listidx), idx);
17073 else
17074 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017075 if (argvars[2].v_type == VAR_UNKNOWN)
17076 {
17077 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017078 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017079 *rettv = item->li_tv;
17080 vim_free(item);
17081 }
17082 else
17083 {
17084 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017085 end = get_tv_number_chk(&argvars[2], &error);
17086 if (error)
17087 ; /* type error: do nothing */
17088 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017089 EMSGN(_(e_listidx), end);
17090 else
17091 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017092 int cnt = 0;
17093
17094 for (li = item; li != NULL; li = li->li_next)
17095 {
17096 ++cnt;
17097 if (li == item2)
17098 break;
17099 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017100 if (li == NULL) /* didn't find "item2" after "item" */
17101 EMSG(_(e_invrange));
17102 else
17103 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017104 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017105 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017106 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017107 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017108 l->lv_first = item;
17109 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017110 item->li_prev = NULL;
17111 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017112 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017113 }
17114 }
17115 }
17116 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017117 }
17118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119}
17120
17121/*
17122 * "rename({from}, {to})" function
17123 */
17124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017125f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126{
17127 char_u buf[NUMBUFLEN];
17128
17129 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017130 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017132 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17133 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134}
17135
17136/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017137 * "repeat()" function
17138 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017140f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017141{
17142 char_u *p;
17143 int n;
17144 int slen;
17145 int len;
17146 char_u *r;
17147 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017148
17149 n = get_tv_number(&argvars[1]);
17150 if (argvars[0].v_type == VAR_LIST)
17151 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017152 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017153 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017154 if (list_extend(rettv->vval.v_list,
17155 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017156 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017157 }
17158 else
17159 {
17160 p = get_tv_string(&argvars[0]);
17161 rettv->v_type = VAR_STRING;
17162 rettv->vval.v_string = NULL;
17163
17164 slen = (int)STRLEN(p);
17165 len = slen * n;
17166 if (len <= 0)
17167 return;
17168
17169 r = alloc(len + 1);
17170 if (r != NULL)
17171 {
17172 for (i = 0; i < n; i++)
17173 mch_memmove(r + i * slen, p, (size_t)slen);
17174 r[len] = NUL;
17175 }
17176
17177 rettv->vval.v_string = r;
17178 }
17179}
17180
17181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 * "resolve()" function
17183 */
17184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017185f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186{
17187 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017188#ifdef HAVE_READLINK
17189 char_u *buf = NULL;
17190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017192 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193#ifdef FEAT_SHORTCUT
17194 {
17195 char_u *v = NULL;
17196
17197 v = mch_resolve_shortcut(p);
17198 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017201 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202 }
17203#else
17204# ifdef HAVE_READLINK
17205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206 char_u *cpy;
17207 int len;
17208 char_u *remain = NULL;
17209 char_u *q;
17210 int is_relative_to_current = FALSE;
17211 int has_trailing_pathsep = FALSE;
17212 int limit = 100;
17213
17214 p = vim_strsave(p);
17215
17216 if (p[0] == '.' && (vim_ispathsep(p[1])
17217 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17218 is_relative_to_current = TRUE;
17219
17220 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017221 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017224 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226
17227 q = getnextcomp(p);
17228 if (*q != NUL)
17229 {
17230 /* Separate the first path component in "p", and keep the
17231 * remainder (beginning with the path separator). */
17232 remain = vim_strsave(q - 1);
17233 q[-1] = NUL;
17234 }
17235
Bram Moolenaard9462e32011-04-11 21:35:11 +020017236 buf = alloc(MAXPATHL + 1);
17237 if (buf == NULL)
17238 goto fail;
17239
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 for (;;)
17241 {
17242 for (;;)
17243 {
17244 len = readlink((char *)p, (char *)buf, MAXPATHL);
17245 if (len <= 0)
17246 break;
17247 buf[len] = NUL;
17248
17249 if (limit-- == 0)
17250 {
17251 vim_free(p);
17252 vim_free(remain);
17253 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017254 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255 goto fail;
17256 }
17257
17258 /* Ensure that the result will have a trailing path separator
17259 * if the argument has one. */
17260 if (remain == NULL && has_trailing_pathsep)
17261 add_pathsep(buf);
17262
17263 /* Separate the first path component in the link value and
17264 * concatenate the remainders. */
17265 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17266 if (*q != NUL)
17267 {
17268 if (remain == NULL)
17269 remain = vim_strsave(q - 1);
17270 else
17271 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017272 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 if (cpy != NULL)
17274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 vim_free(remain);
17276 remain = cpy;
17277 }
17278 }
17279 q[-1] = NUL;
17280 }
17281
17282 q = gettail(p);
17283 if (q > p && *q == NUL)
17284 {
17285 /* Ignore trailing path separator. */
17286 q[-1] = NUL;
17287 q = gettail(p);
17288 }
17289 if (q > p && !mch_isFullName(buf))
17290 {
17291 /* symlink is relative to directory of argument */
17292 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17293 if (cpy != NULL)
17294 {
17295 STRCPY(cpy, p);
17296 STRCPY(gettail(cpy), buf);
17297 vim_free(p);
17298 p = cpy;
17299 }
17300 }
17301 else
17302 {
17303 vim_free(p);
17304 p = vim_strsave(buf);
17305 }
17306 }
17307
17308 if (remain == NULL)
17309 break;
17310
17311 /* Append the first path component of "remain" to "p". */
17312 q = getnextcomp(remain + 1);
17313 len = q - remain - (*q != NUL);
17314 cpy = vim_strnsave(p, STRLEN(p) + len);
17315 if (cpy != NULL)
17316 {
17317 STRNCAT(cpy, remain, len);
17318 vim_free(p);
17319 p = cpy;
17320 }
17321 /* Shorten "remain". */
17322 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017323 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 else
17325 {
17326 vim_free(remain);
17327 remain = NULL;
17328 }
17329 }
17330
17331 /* If the result is a relative path name, make it explicitly relative to
17332 * the current directory if and only if the argument had this form. */
17333 if (!vim_ispathsep(*p))
17334 {
17335 if (is_relative_to_current
17336 && *p != NUL
17337 && !(p[0] == '.'
17338 && (p[1] == NUL
17339 || vim_ispathsep(p[1])
17340 || (p[1] == '.'
17341 && (p[2] == NUL
17342 || vim_ispathsep(p[2]))))))
17343 {
17344 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017345 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 if (cpy != NULL)
17347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 vim_free(p);
17349 p = cpy;
17350 }
17351 }
17352 else if (!is_relative_to_current)
17353 {
17354 /* Strip leading "./". */
17355 q = p;
17356 while (q[0] == '.' && vim_ispathsep(q[1]))
17357 q += 2;
17358 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017359 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 }
17361 }
17362
17363 /* Ensure that the result will have no trailing path separator
17364 * if the argument had none. But keep "/" or "//". */
17365 if (!has_trailing_pathsep)
17366 {
17367 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017368 if (after_pathsep(p, q))
17369 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 }
17371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017372 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 }
17374# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376# endif
17377#endif
17378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017379 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380
17381#ifdef HAVE_READLINK
17382fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017383 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017385 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386}
17387
17388/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017389 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 */
17391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017392f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393{
Bram Moolenaar33570922005-01-25 22:26:29 +000017394 list_T *l;
17395 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396
Bram Moolenaar0d660222005-01-07 21:51:51 +000017397 if (argvars[0].v_type != VAR_LIST)
17398 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017399 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017400 && !tv_check_lock(l->lv_lock,
17401 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017402 {
17403 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017404 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017405 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017406 while (li != NULL)
17407 {
17408 ni = li->li_prev;
17409 list_append(l, li);
17410 li = ni;
17411 }
17412 rettv->vval.v_list = l;
17413 rettv->v_type = VAR_LIST;
17414 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017415 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417}
17418
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017419#define SP_NOMOVE 0x01 /* don't move cursor */
17420#define SP_REPEAT 0x02 /* repeat to find outer pair */
17421#define SP_RETCOUNT 0x04 /* return matchcount */
17422#define SP_SETPCMARK 0x08 /* set previous context mark */
17423#define SP_START 0x10 /* accept match at start position */
17424#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17425#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017426#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017427
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017428static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017429
17430/*
17431 * Get flags for a search function.
17432 * Possibly sets "p_ws".
17433 * Returns BACKWARD, FORWARD or zero (for an error).
17434 */
17435 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017436get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017437{
17438 int dir = FORWARD;
17439 char_u *flags;
17440 char_u nbuf[NUMBUFLEN];
17441 int mask;
17442
17443 if (varp->v_type != VAR_UNKNOWN)
17444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017445 flags = get_tv_string_buf_chk(varp, nbuf);
17446 if (flags == NULL)
17447 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017448 while (*flags != NUL)
17449 {
17450 switch (*flags)
17451 {
17452 case 'b': dir = BACKWARD; break;
17453 case 'w': p_ws = TRUE; break;
17454 case 'W': p_ws = FALSE; break;
17455 default: mask = 0;
17456 if (flagsp != NULL)
17457 switch (*flags)
17458 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017459 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017460 case 'e': mask = SP_END; break;
17461 case 'm': mask = SP_RETCOUNT; break;
17462 case 'n': mask = SP_NOMOVE; break;
17463 case 'p': mask = SP_SUBPAT; break;
17464 case 'r': mask = SP_REPEAT; break;
17465 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017466 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017467 }
17468 if (mask == 0)
17469 {
17470 EMSG2(_(e_invarg2), flags);
17471 dir = 0;
17472 }
17473 else
17474 *flagsp |= mask;
17475 }
17476 if (dir == 0)
17477 break;
17478 ++flags;
17479 }
17480 }
17481 return dir;
17482}
17483
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017485 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017487 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017488search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017490 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491 char_u *pat;
17492 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017493 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 int save_p_ws = p_ws;
17495 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017496 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017497 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017498 proftime_T tm;
17499#ifdef FEAT_RELTIME
17500 long time_limit = 0;
17501#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017502 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017503 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017505 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017506 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017507 if (dir == 0)
17508 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017509 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017510 if (flags & SP_START)
17511 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017512 if (flags & SP_END)
17513 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017514 if (flags & SP_COLUMN)
17515 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017516
Bram Moolenaar76929292008-01-06 19:07:36 +000017517 /* Optional arguments: line number to stop searching and timeout. */
17518 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017519 {
17520 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17521 if (lnum_stop < 0)
17522 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017523#ifdef FEAT_RELTIME
17524 if (argvars[3].v_type != VAR_UNKNOWN)
17525 {
17526 time_limit = get_tv_number_chk(&argvars[3], NULL);
17527 if (time_limit < 0)
17528 goto theend;
17529 }
17530#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017531 }
17532
Bram Moolenaar76929292008-01-06 19:07:36 +000017533#ifdef FEAT_RELTIME
17534 /* Set the time limit, if there is one. */
17535 profile_setlimit(time_limit, &tm);
17536#endif
17537
Bram Moolenaar231334e2005-07-25 20:46:57 +000017538 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017539 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017540 * Check to make sure only those flags are set.
17541 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17542 * flags cannot be set. Check for that condition also.
17543 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017544 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017545 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017547 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017548 goto theend;
17549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017551 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017552 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017553 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017554 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017556 if (flags & SP_SUBPAT)
17557 retval = subpatnum;
17558 else
17559 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017560 if (flags & SP_SETPCMARK)
17561 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017563 if (match_pos != NULL)
17564 {
17565 /* Store the match cursor position */
17566 match_pos->lnum = pos.lnum;
17567 match_pos->col = pos.col + 1;
17568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 /* "/$" will put the cursor after the end of the line, may need to
17570 * correct that here */
17571 check_cursor();
17572 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017573
17574 /* If 'n' flag is used: restore cursor position. */
17575 if (flags & SP_NOMOVE)
17576 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017577 else
17578 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017579theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017581
17582 return retval;
17583}
17584
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017585#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017586
17587/*
17588 * round() is not in C90, use ceil() or floor() instead.
17589 */
17590 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017591vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017592{
17593 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17594}
17595
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017596/*
17597 * "round({float})" function
17598 */
17599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017600f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017601{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017602 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017603
17604 rettv->v_type = VAR_FLOAT;
17605 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017606 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017607 else
17608 rettv->vval.v_float = 0.0;
17609}
17610#endif
17611
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017612/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017613 * "screenattr()" function
17614 */
17615 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017616f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017617{
17618 int row;
17619 int col;
17620 int c;
17621
17622 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17623 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17624 if (row < 0 || row >= screen_Rows
17625 || col < 0 || col >= screen_Columns)
17626 c = -1;
17627 else
17628 c = ScreenAttrs[LineOffset[row] + col];
17629 rettv->vval.v_number = c;
17630}
17631
17632/*
17633 * "screenchar()" function
17634 */
17635 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017636f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017637{
17638 int row;
17639 int col;
17640 int off;
17641 int c;
17642
17643 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17644 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17645 if (row < 0 || row >= screen_Rows
17646 || col < 0 || col >= screen_Columns)
17647 c = -1;
17648 else
17649 {
17650 off = LineOffset[row] + col;
17651#ifdef FEAT_MBYTE
17652 if (enc_utf8 && ScreenLinesUC[off] != 0)
17653 c = ScreenLinesUC[off];
17654 else
17655#endif
17656 c = ScreenLines[off];
17657 }
17658 rettv->vval.v_number = c;
17659}
17660
17661/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017662 * "screencol()" function
17663 *
17664 * First column is 1 to be consistent with virtcol().
17665 */
17666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017667f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017668{
17669 rettv->vval.v_number = screen_screencol() + 1;
17670}
17671
17672/*
17673 * "screenrow()" function
17674 */
17675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017676f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017677{
17678 rettv->vval.v_number = screen_screenrow() + 1;
17679}
17680
17681/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017682 * "search()" function
17683 */
17684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017685f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017686{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017687 int flags = 0;
17688
17689 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690}
17691
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017693 * "searchdecl()" function
17694 */
17695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017696f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017697{
17698 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017699 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017700 int error = FALSE;
17701 char_u *name;
17702
17703 rettv->vval.v_number = 1; /* default: FAIL */
17704
17705 name = get_tv_string_chk(&argvars[0]);
17706 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017707 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017708 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017709 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17710 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17711 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017712 if (!error && name != NULL)
17713 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017714 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017715}
17716
17717/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017718 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017720 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017721searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722{
17723 char_u *spat, *mpat, *epat;
17724 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 int dir;
17727 int flags = 0;
17728 char_u nbuf1[NUMBUFLEN];
17729 char_u nbuf2[NUMBUFLEN];
17730 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017731 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017732 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017733 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017736 spat = get_tv_string_chk(&argvars[0]);
17737 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17738 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17739 if (spat == NULL || mpat == NULL || epat == NULL)
17740 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742 /* Handle the optional fourth argument: flags */
17743 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017744 if (dir == 0)
17745 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017746
17747 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017748 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17749 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017750 if ((flags & (SP_END | SP_SUBPAT)) != 0
17751 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017752 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017753 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017754 goto theend;
17755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017757 /* Using 'r' implies 'W', otherwise it doesn't work. */
17758 if (flags & SP_REPEAT)
17759 p_ws = FALSE;
17760
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017761 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017762 if (argvars[3].v_type == VAR_UNKNOWN
17763 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 skip = (char_u *)"";
17765 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017767 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017768 if (argvars[5].v_type != VAR_UNKNOWN)
17769 {
17770 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17771 if (lnum_stop < 0)
17772 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017773#ifdef FEAT_RELTIME
17774 if (argvars[6].v_type != VAR_UNKNOWN)
17775 {
17776 time_limit = get_tv_number_chk(&argvars[6], NULL);
17777 if (time_limit < 0)
17778 goto theend;
17779 }
17780#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017781 }
17782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017783 if (skip == NULL)
17784 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017786 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017787 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017788
17789theend:
17790 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017791
17792 return retval;
17793}
17794
17795/*
17796 * "searchpair()" function
17797 */
17798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017799f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017800{
17801 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17802}
17803
17804/*
17805 * "searchpairpos()" function
17806 */
17807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017808f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017809{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017810 pos_T match_pos;
17811 int lnum = 0;
17812 int col = 0;
17813
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017814 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017815 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017816
17817 if (searchpair_cmn(argvars, &match_pos) > 0)
17818 {
17819 lnum = match_pos.lnum;
17820 col = match_pos.col;
17821 }
17822
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017823 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17824 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017825}
17826
17827/*
17828 * Search for a start/middle/end thing.
17829 * Used by searchpair(), see its documentation for the details.
17830 * Returns 0 or -1 for no match,
17831 */
17832 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017833do_searchpair(
17834 char_u *spat, /* start pattern */
17835 char_u *mpat, /* middle pattern */
17836 char_u *epat, /* end pattern */
17837 int dir, /* BACKWARD or FORWARD */
17838 char_u *skip, /* skip expression */
17839 int flags, /* SP_SETPCMARK and other SP_ values */
17840 pos_T *match_pos,
17841 linenr_T lnum_stop, /* stop at this line if not zero */
17842 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017843{
17844 char_u *save_cpo;
17845 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17846 long retval = 0;
17847 pos_T pos;
17848 pos_T firstpos;
17849 pos_T foundpos;
17850 pos_T save_cursor;
17851 pos_T save_pos;
17852 int n;
17853 int r;
17854 int nest = 1;
17855 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017856 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017857 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017858
17859 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17860 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017861 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017862
Bram Moolenaar76929292008-01-06 19:07:36 +000017863#ifdef FEAT_RELTIME
17864 /* Set the time limit, if there is one. */
17865 profile_setlimit(time_limit, &tm);
17866#endif
17867
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017868 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17869 * start/middle/end (pat3, for the top pair). */
17870 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17871 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17872 if (pat2 == NULL || pat3 == NULL)
17873 goto theend;
17874 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17875 if (*mpat == NUL)
17876 STRCPY(pat3, pat2);
17877 else
17878 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17879 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017880 if (flags & SP_START)
17881 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017882
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883 save_cursor = curwin->w_cursor;
17884 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017885 clearpos(&firstpos);
17886 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887 pat = pat3;
17888 for (;;)
17889 {
17890 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017891 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17893 /* didn't find it or found the first match again: FAIL */
17894 break;
17895
17896 if (firstpos.lnum == 0)
17897 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017898 if (equalpos(pos, foundpos))
17899 {
17900 /* Found the same position again. Can happen with a pattern that
17901 * has "\zs" at the end and searching backwards. Advance one
17902 * character and try again. */
17903 if (dir == BACKWARD)
17904 decl(&pos);
17905 else
17906 incl(&pos);
17907 }
17908 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017910 /* clear the start flag to avoid getting stuck here */
17911 options &= ~SEARCH_START;
17912
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913 /* If the skip pattern matches, ignore this match. */
17914 if (*skip != NUL)
17915 {
17916 save_pos = curwin->w_cursor;
17917 curwin->w_cursor = pos;
17918 r = eval_to_bool(skip, &err, NULL, FALSE);
17919 curwin->w_cursor = save_pos;
17920 if (err)
17921 {
17922 /* Evaluating {skip} caused an error, break here. */
17923 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017924 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 break;
17926 }
17927 if (r)
17928 continue;
17929 }
17930
17931 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17932 {
17933 /* Found end when searching backwards or start when searching
17934 * forward: nested pair. */
17935 ++nest;
17936 pat = pat2; /* nested, don't search for middle */
17937 }
17938 else
17939 {
17940 /* Found end when searching forward or start when searching
17941 * backward: end of (nested) pair; or found middle in outer pair. */
17942 if (--nest == 1)
17943 pat = pat3; /* outer level, search for middle */
17944 }
17945
17946 if (nest == 0)
17947 {
17948 /* Found the match: return matchcount or line number. */
17949 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017950 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017952 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017953 if (flags & SP_SETPCMARK)
17954 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955 curwin->w_cursor = pos;
17956 if (!(flags & SP_REPEAT))
17957 break;
17958 nest = 1; /* search for next unmatched */
17959 }
17960 }
17961
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017962 if (match_pos != NULL)
17963 {
17964 /* Store the match cursor position */
17965 match_pos->lnum = curwin->w_cursor.lnum;
17966 match_pos->col = curwin->w_cursor.col + 1;
17967 }
17968
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017970 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971 curwin->w_cursor = save_cursor;
17972
17973theend:
17974 vim_free(pat2);
17975 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017976 if (p_cpo == empty_option)
17977 p_cpo = save_cpo;
17978 else
17979 /* Darn, evaluating the {skip} expression changed the value. */
17980 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017981
17982 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983}
17984
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017985/*
17986 * "searchpos()" function
17987 */
17988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017989f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017990{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017991 pos_T match_pos;
17992 int lnum = 0;
17993 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017994 int n;
17995 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017996
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017997 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017998 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017999
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018000 n = search_cmn(argvars, &match_pos, &flags);
18001 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018002 {
18003 lnum = match_pos.lnum;
18004 col = match_pos.col;
18005 }
18006
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018007 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18008 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018009 if (flags & SP_SUBPAT)
18010 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018011}
18012
Bram Moolenaar0d660222005-01-07 21:51:51 +000018013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018014f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018016#ifdef FEAT_CLIENTSERVER
18017 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018018 char_u *server = get_tv_string_chk(&argvars[0]);
18019 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020
Bram Moolenaar0d660222005-01-07 21:51:51 +000018021 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018022 if (server == NULL || reply == NULL)
18023 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018024 if (check_restricted() || check_secure())
18025 return;
18026# ifdef FEAT_X11
18027 if (check_connection() == FAIL)
18028 return;
18029# endif
18030
18031 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018033 EMSG(_("E258: Unable to send to client"));
18034 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018036 rettv->vval.v_number = 0;
18037#else
18038 rettv->vval.v_number = -1;
18039#endif
18040}
18041
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018043f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018044{
18045 char_u *r = NULL;
18046
18047#ifdef FEAT_CLIENTSERVER
18048# ifdef WIN32
18049 r = serverGetVimNames();
18050# else
18051 make_connection();
18052 if (X_DISPLAY != NULL)
18053 r = serverGetVimNames(X_DISPLAY);
18054# endif
18055#endif
18056 rettv->v_type = VAR_STRING;
18057 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058}
18059
18060/*
18061 * "setbufvar()" function
18062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018064f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065{
18066 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018069 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 char_u nbuf[NUMBUFLEN];
18071
18072 if (check_restricted() || check_secure())
18073 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018074 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18075 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018076 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 varp = &argvars[2];
18078
18079 if (buf != NULL && varname != NULL && varp != NULL)
18080 {
18081 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083
18084 if (*varname == '&')
18085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018086 long numval;
18087 char_u *strval;
18088 int error = FALSE;
18089
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018091 numval = get_tv_number_chk(varp, &error);
18092 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018093 if (!error && strval != NULL)
18094 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 }
18096 else
18097 {
18098 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18099 if (bufvarname != NULL)
18100 {
18101 STRCPY(bufvarname, "b:");
18102 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018103 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 vim_free(bufvarname);
18105 }
18106 }
18107
18108 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111}
18112
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018114f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018115{
18116 dict_T *d;
18117 dictitem_T *di;
18118 char_u *csearch;
18119
18120 if (argvars[0].v_type != VAR_DICT)
18121 {
18122 EMSG(_(e_dictreq));
18123 return;
18124 }
18125
18126 if ((d = argvars[0].vval.v_dict) != NULL)
18127 {
18128 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18129 if (csearch != NULL)
18130 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018131#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018132 if (enc_utf8)
18133 {
18134 int pcc[MAX_MCO];
18135 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018136
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018137 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18138 }
18139 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018140#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018141 set_last_csearch(PTR2CHAR(csearch),
18142 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018143 }
18144
18145 di = dict_find(d, (char_u *)"forward", -1);
18146 if (di != NULL)
18147 set_csearch_direction(get_tv_number(&di->di_tv)
18148 ? FORWARD : BACKWARD);
18149
18150 di = dict_find(d, (char_u *)"until", -1);
18151 if (di != NULL)
18152 set_csearch_until(!!get_tv_number(&di->di_tv));
18153 }
18154}
18155
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156/*
18157 * "setcmdpos()" function
18158 */
18159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018160f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018162 int pos = (int)get_tv_number(&argvars[0]) - 1;
18163
18164 if (pos >= 0)
18165 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166}
18167
18168/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018169 * "setfperm({fname}, {mode})" function
18170 */
18171 static void
18172f_setfperm(typval_T *argvars, typval_T *rettv)
18173{
18174 char_u *fname;
18175 char_u modebuf[NUMBUFLEN];
18176 char_u *mode_str;
18177 int i;
18178 int mask;
18179 int mode = 0;
18180
18181 rettv->vval.v_number = 0;
18182 fname = get_tv_string_chk(&argvars[0]);
18183 if (fname == NULL)
18184 return;
18185 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18186 if (mode_str == NULL)
18187 return;
18188 if (STRLEN(mode_str) != 9)
18189 {
18190 EMSG2(_(e_invarg2), mode_str);
18191 return;
18192 }
18193
18194 mask = 1;
18195 for (i = 8; i >= 0; --i)
18196 {
18197 if (mode_str[i] != '-')
18198 mode |= mask;
18199 mask = mask << 1;
18200 }
18201 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18202}
18203
18204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 * "setline()" function
18206 */
18207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018208f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209{
18210 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018211 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018212 list_T *l = NULL;
18213 listitem_T *li = NULL;
18214 long added = 0;
18215 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018217 lnum = get_tv_lnum(&argvars[0]);
18218 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018220 l = argvars[1].vval.v_list;
18221 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018223 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018224 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018225
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018226 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018227 for (;;)
18228 {
18229 if (l != NULL)
18230 {
18231 /* list argument, get next string */
18232 if (li == NULL)
18233 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018234 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018235 li = li->li_next;
18236 }
18237
18238 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018239 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018240 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018241
18242 /* When coming here from Insert mode, sync undo, so that this can be
18243 * undone separately from what was previously inserted. */
18244 if (u_sync_once == 2)
18245 {
18246 u_sync_once = 1; /* notify that u_sync() was called */
18247 u_sync(TRUE);
18248 }
18249
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018250 if (lnum <= curbuf->b_ml.ml_line_count)
18251 {
18252 /* existing line, replace it */
18253 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18254 {
18255 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018256 if (lnum == curwin->w_cursor.lnum)
18257 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018258 rettv->vval.v_number = 0; /* OK */
18259 }
18260 }
18261 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18262 {
18263 /* lnum is one past the last line, append the line */
18264 ++added;
18265 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18266 rettv->vval.v_number = 0; /* OK */
18267 }
18268
18269 if (l == NULL) /* only one string argument */
18270 break;
18271 ++lnum;
18272 }
18273
18274 if (added > 0)
18275 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276}
18277
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018278static 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 +000018279
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018281 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018282 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018284set_qf_ll_list(
18285 win_T *wp UNUSED,
18286 typval_T *list_arg UNUSED,
18287 typval_T *action_arg UNUSED,
18288 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018289{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018290#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018291 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018292 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018293 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018294#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018295
Bram Moolenaar2641f772005-03-25 21:58:17 +000018296 rettv->vval.v_number = -1;
18297
18298#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018299 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018300 EMSG(_(e_listreq));
18301 else
18302 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018303 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018304
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018305 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018306 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018307 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018308 if (act == NULL)
18309 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018310 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018311 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018312 else
18313 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018314 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018315 else if (action_arg->v_type == VAR_UNKNOWN)
18316 action = ' ';
18317 else
18318 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018319
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018320 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018321 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018322 rettv->vval.v_number = 0;
18323 }
18324#endif
18325}
18326
18327/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018328 * "setloclist()" function
18329 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018331f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018332{
18333 win_T *win;
18334
18335 rettv->vval.v_number = -1;
18336
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018337 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018338 if (win != NULL)
18339 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18340}
18341
18342/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018343 * "setmatches()" function
18344 */
18345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018346f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018347{
18348#ifdef FEAT_SEARCH_EXTRA
18349 list_T *l;
18350 listitem_T *li;
18351 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018352 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018353
18354 rettv->vval.v_number = -1;
18355 if (argvars[0].v_type != VAR_LIST)
18356 {
18357 EMSG(_(e_listreq));
18358 return;
18359 }
18360 if ((l = argvars[0].vval.v_list) != NULL)
18361 {
18362
18363 /* To some extent make sure that we are dealing with a list from
18364 * "getmatches()". */
18365 li = l->lv_first;
18366 while (li != NULL)
18367 {
18368 if (li->li_tv.v_type != VAR_DICT
18369 || (d = li->li_tv.vval.v_dict) == NULL)
18370 {
18371 EMSG(_(e_invarg));
18372 return;
18373 }
18374 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018375 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18376 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018377 && dict_find(d, (char_u *)"priority", -1) != NULL
18378 && dict_find(d, (char_u *)"id", -1) != NULL))
18379 {
18380 EMSG(_(e_invarg));
18381 return;
18382 }
18383 li = li->li_next;
18384 }
18385
18386 clear_matches(curwin);
18387 li = l->lv_first;
18388 while (li != NULL)
18389 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018390 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018391 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018392 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018393 char_u *group;
18394 int priority;
18395 int id;
18396 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018397
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018398 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018399 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18400 {
18401 if (s == NULL)
18402 {
18403 s = list_alloc();
18404 if (s == NULL)
18405 return;
18406 }
18407
18408 /* match from matchaddpos() */
18409 for (i = 1; i < 9; i++)
18410 {
18411 sprintf((char *)buf, (char *)"pos%d", i);
18412 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18413 {
18414 if (di->di_tv.v_type != VAR_LIST)
18415 return;
18416
18417 list_append_tv(s, &di->di_tv);
18418 s->lv_refcount++;
18419 }
18420 else
18421 break;
18422 }
18423 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018424
18425 group = get_dict_string(d, (char_u *)"group", FALSE);
18426 priority = (int)get_dict_number(d, (char_u *)"priority");
18427 id = (int)get_dict_number(d, (char_u *)"id");
18428 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18429 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18430 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018431 if (i == 0)
18432 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018433 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018434 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018435 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018436 }
18437 else
18438 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018439 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018440 list_unref(s);
18441 s = NULL;
18442 }
18443
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018444 li = li->li_next;
18445 }
18446 rettv->vval.v_number = 0;
18447 }
18448#endif
18449}
18450
18451/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018452 * "setpos()" function
18453 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018455f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018456{
18457 pos_T pos;
18458 int fnum;
18459 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018460 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018461
Bram Moolenaar08250432008-02-13 11:42:46 +000018462 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018463 name = get_tv_string_chk(argvars);
18464 if (name != NULL)
18465 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018466 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018467 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018468 if (--pos.col < 0)
18469 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018470 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018471 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018472 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018473 if (fnum == curbuf->b_fnum)
18474 {
18475 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018476 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018477 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018478 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018479 curwin->w_set_curswant = FALSE;
18480 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018481 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018482 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018483 }
18484 else
18485 EMSG(_(e_invarg));
18486 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018487 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18488 {
18489 /* set mark */
18490 if (setmark_pos(name[1], &pos, fnum) == OK)
18491 rettv->vval.v_number = 0;
18492 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018493 else
18494 EMSG(_(e_invarg));
18495 }
18496 }
18497}
18498
18499/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018500 * "setqflist()" function
18501 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018503f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018504{
18505 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18506}
18507
18508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509 * "setreg()" function
18510 */
18511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018512f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513{
18514 int regname;
18515 char_u *strregname;
18516 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018517 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 int append;
18519 char_u yank_type;
18520 long block_len;
18521
18522 block_len = -1;
18523 yank_type = MAUTO;
18524 append = FALSE;
18525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018526 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018527 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018529 if (strregname == NULL)
18530 return; /* type error; errmsg already given */
18531 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532 if (regname == 0 || regname == '@')
18533 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018535 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018537 stropt = get_tv_string_chk(&argvars[2]);
18538 if (stropt == NULL)
18539 return; /* type error */
18540 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 switch (*stropt)
18542 {
18543 case 'a': case 'A': /* append */
18544 append = TRUE;
18545 break;
18546 case 'v': case 'c': /* character-wise selection */
18547 yank_type = MCHAR;
18548 break;
18549 case 'V': case 'l': /* line-wise selection */
18550 yank_type = MLINE;
18551 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 case 'b': case Ctrl_V: /* block-wise selection */
18553 yank_type = MBLOCK;
18554 if (VIM_ISDIGIT(stropt[1]))
18555 {
18556 ++stropt;
18557 block_len = getdigits(&stropt) - 1;
18558 --stropt;
18559 }
18560 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 }
18562 }
18563
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018564 if (argvars[1].v_type == VAR_LIST)
18565 {
18566 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018567 char_u **allocval;
18568 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018569 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018570 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018571 int len = argvars[1].vval.v_list->lv_len;
18572 listitem_T *li;
18573
Bram Moolenaar7d647822014-04-05 21:28:56 +020018574 /* First half: use for pointers to result lines; second half: use for
18575 * pointers to allocated copies. */
18576 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018577 if (lstval == NULL)
18578 return;
18579 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018580 allocval = lstval + len + 2;
18581 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018582
18583 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18584 li = li->li_next)
18585 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018586 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018587 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018588 goto free_lstval;
18589 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018590 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018591 /* Need to make a copy, next get_tv_string_buf_chk() will
18592 * overwrite the string. */
18593 strval = vim_strsave(buf);
18594 if (strval == NULL)
18595 goto free_lstval;
18596 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018597 }
18598 *curval++ = strval;
18599 }
18600 *curval++ = NULL;
18601
18602 write_reg_contents_lst(regname, lstval, -1,
18603 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018604free_lstval:
18605 while (curallocval > allocval)
18606 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018607 vim_free(lstval);
18608 }
18609 else
18610 {
18611 strval = get_tv_string_chk(&argvars[1]);
18612 if (strval == NULL)
18613 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018614 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018616 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618}
18619
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018620/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018621 * "settabvar()" function
18622 */
18623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018624f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018625{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018626#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018627 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018628 tabpage_T *tp;
18629#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018630 char_u *varname, *tabvarname;
18631 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018632
18633 rettv->vval.v_number = 0;
18634
18635 if (check_restricted() || check_secure())
18636 return;
18637
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018638#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018639 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018640#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018641 varname = get_tv_string_chk(&argvars[1]);
18642 varp = &argvars[2];
18643
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018644 if (varname != NULL && varp != NULL
18645#ifdef FEAT_WINDOWS
18646 && tp != NULL
18647#endif
18648 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018649 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018650#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018651 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018652 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018653#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018654
18655 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18656 if (tabvarname != NULL)
18657 {
18658 STRCPY(tabvarname, "t:");
18659 STRCPY(tabvarname + 2, varname);
18660 set_var(tabvarname, varp, TRUE);
18661 vim_free(tabvarname);
18662 }
18663
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018664#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018665 /* Restore current tabpage */
18666 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018667 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018668#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018669 }
18670}
18671
18672/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018673 * "settabwinvar()" function
18674 */
18675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018676f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018677{
18678 setwinvar(argvars, rettv, 1);
18679}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680
18681/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018682 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018685f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018687 setwinvar(argvars, rettv, 0);
18688}
18689
18690/*
18691 * "setwinvar()" and "settabwinvar()" functions
18692 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018693
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018695setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018696{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697 win_T *win;
18698#ifdef FEAT_WINDOWS
18699 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018700 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018701 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702#endif
18703 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018704 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018706 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707
18708 if (check_restricted() || check_secure())
18709 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018710
18711#ifdef FEAT_WINDOWS
18712 if (off == 1)
18713 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18714 else
18715 tp = curtab;
18716#endif
18717 win = find_win_by_nr(&argvars[off], tp);
18718 varname = get_tv_string_chk(&argvars[off + 1]);
18719 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720
18721 if (win != NULL && varname != NULL && varp != NULL)
18722 {
18723#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018724 need_switch_win = !(tp == curtab && win == curwin);
18725 if (!need_switch_win
18726 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018729 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018731 long numval;
18732 char_u *strval;
18733 int error = FALSE;
18734
18735 ++varname;
18736 numval = get_tv_number_chk(varp, &error);
18737 strval = get_tv_string_buf_chk(varp, nbuf);
18738 if (!error && strval != NULL)
18739 set_option_value(varname, numval, strval, OPT_LOCAL);
18740 }
18741 else
18742 {
18743 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18744 if (winvarname != NULL)
18745 {
18746 STRCPY(winvarname, "w:");
18747 STRCPY(winvarname + 2, varname);
18748 set_var(winvarname, varp, TRUE);
18749 vim_free(winvarname);
18750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 }
18752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018754 if (need_switch_win)
18755 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756#endif
18757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758}
18759
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018760#ifdef FEAT_CRYPT
18761/*
18762 * "sha256({string})" function
18763 */
18764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018765f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018766{
18767 char_u *p;
18768
18769 p = get_tv_string(&argvars[0]);
18770 rettv->vval.v_string = vim_strsave(
18771 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18772 rettv->v_type = VAR_STRING;
18773}
18774#endif /* FEAT_CRYPT */
18775
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018777 * "shellescape({string})" function
18778 */
18779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018780f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018781{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018782 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018783 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018784 rettv->v_type = VAR_STRING;
18785}
18786
18787/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018788 * shiftwidth() function
18789 */
18790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018791f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018792{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018793 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018794}
18795
18796/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018797 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 */
18799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018800f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018802 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803
Bram Moolenaar0d660222005-01-07 21:51:51 +000018804 p = get_tv_string(&argvars[0]);
18805 rettv->vval.v_string = vim_strsave(p);
18806 simplify_filename(rettv->vval.v_string); /* simplify in place */
18807 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808}
18809
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018810#ifdef FEAT_FLOAT
18811/*
18812 * "sin()" function
18813 */
18814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018815f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018816{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018817 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018818
18819 rettv->v_type = VAR_FLOAT;
18820 if (get_float_arg(argvars, &f) == OK)
18821 rettv->vval.v_float = sin(f);
18822 else
18823 rettv->vval.v_float = 0.0;
18824}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018825
18826/*
18827 * "sinh()" function
18828 */
18829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018830f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018831{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018832 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018833
18834 rettv->v_type = VAR_FLOAT;
18835 if (get_float_arg(argvars, &f) == OK)
18836 rettv->vval.v_float = sinh(f);
18837 else
18838 rettv->vval.v_float = 0.0;
18839}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018840#endif
18841
Bram Moolenaar0d660222005-01-07 21:51:51 +000018842static int
18843#ifdef __BORLANDC__
18844 _RTLENTRYF
18845#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018846 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018847static int
18848#ifdef __BORLANDC__
18849 _RTLENTRYF
18850#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018851 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018852
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018853/* struct used in the array that's given to qsort() */
18854typedef struct
18855{
18856 listitem_T *item;
18857 int idx;
18858} sortItem_T;
18859
Bram Moolenaar0b962472016-02-22 22:51:33 +010018860/* struct storing information about current sort */
18861typedef struct
18862{
18863 int item_compare_ic;
18864 int item_compare_numeric;
18865 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018866#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018867 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018868#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018869 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018870 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018871 dict_T *item_compare_selfdict;
18872 int item_compare_func_err;
18873 int item_compare_keep_zero;
18874} sortinfo_T;
18875static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018876static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018877#define ITEM_COMPARE_FAIL 999
18878
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018880 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018882 static int
18883#ifdef __BORLANDC__
18884_RTLENTRYF
18885#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018886item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018888 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018889 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018890 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018891 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018892 int res;
18893 char_u numbuf1[NUMBUFLEN];
18894 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018896 si1 = (sortItem_T *)s1;
18897 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018898 tv1 = &si1->item->li_tv;
18899 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018900
Bram Moolenaar0b962472016-02-22 22:51:33 +010018901 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018902 {
18903 long v1 = get_tv_number(tv1);
18904 long v2 = get_tv_number(tv2);
18905
18906 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18907 }
18908
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018909#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018910 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018911 {
18912 float_T v1 = get_tv_float(tv1);
18913 float_T v2 = get_tv_float(tv2);
18914
18915 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18916 }
18917#endif
18918
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018919 /* tv2string() puts quotes around a string and allocates memory. Don't do
18920 * that for string variables. Use a single quote when comparing with a
18921 * non-string to do what the docs promise. */
18922 if (tv1->v_type == VAR_STRING)
18923 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018924 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018925 p1 = (char_u *)"'";
18926 else
18927 p1 = tv1->vval.v_string;
18928 }
18929 else
18930 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18931 if (tv2->v_type == VAR_STRING)
18932 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018933 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018934 p2 = (char_u *)"'";
18935 else
18936 p2 = tv2->vval.v_string;
18937 }
18938 else
18939 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018940 if (p1 == NULL)
18941 p1 = (char_u *)"";
18942 if (p2 == NULL)
18943 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018944 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018945 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018946 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018947 res = STRICMP(p1, p2);
18948 else
18949 res = STRCMP(p1, p2);
18950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018952 {
18953 double n1, n2;
18954 n1 = strtod((char *)p1, (char **)&p1);
18955 n2 = strtod((char *)p2, (char **)&p2);
18956 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18957 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018958
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018959 /* When the result would be zero, compare the item indexes. Makes the
18960 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018961 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018962 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018963
Bram Moolenaar0d660222005-01-07 21:51:51 +000018964 vim_free(tofree1);
18965 vim_free(tofree2);
18966 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967}
18968
18969 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018970#ifdef __BORLANDC__
18971_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018973item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018974{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018975 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018976 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018977 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018978 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018979 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018980 char_u *func_name;
18981 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018983 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018984 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018985 return 0;
18986
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018987 si1 = (sortItem_T *)s1;
18988 si2 = (sortItem_T *)s2;
18989
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018990 if (partial == NULL)
18991 func_name = sortinfo->item_compare_func;
18992 else
18993 func_name = partial->pt_name;
18994
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018995 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018996 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018997 copy_tv(&si1->item->li_tv, &argv[0]);
18998 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999
19000 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019001 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019002 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019003 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019004 clear_tv(&argv[0]);
19005 clear_tv(&argv[1]);
19006
19007 if (res == FAIL)
19008 res = ITEM_COMPARE_FAIL;
19009 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019010 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19011 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019012 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019014
19015 /* When the result would be zero, compare the pointers themselves. Makes
19016 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019017 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019018 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019019
Bram Moolenaar0d660222005-01-07 21:51:51 +000019020 return res;
19021}
19022
19023/*
19024 * "sort({list})" function
19025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019027do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028{
Bram Moolenaar33570922005-01-25 22:26:29 +000019029 list_T *l;
19030 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019031 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019032 sortinfo_T *old_sortinfo;
19033 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019034 long len;
19035 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036
Bram Moolenaar0b962472016-02-22 22:51:33 +010019037 /* Pointer to current info struct used in compare function. Save and
19038 * restore the current one for nested calls. */
19039 old_sortinfo = sortinfo;
19040 sortinfo = &info;
19041
Bram Moolenaar0d660222005-01-07 21:51:51 +000019042 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019043 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 else
19045 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019046 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019047 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019048 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19049 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019050 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019051 rettv->vval.v_list = l;
19052 rettv->v_type = VAR_LIST;
19053 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054
Bram Moolenaar0d660222005-01-07 21:51:51 +000019055 len = list_len(l);
19056 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019057 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058
Bram Moolenaar0b962472016-02-22 22:51:33 +010019059 info.item_compare_ic = FALSE;
19060 info.item_compare_numeric = FALSE;
19061 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019062#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019063 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019064#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019065 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019066 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019067 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019068 if (argvars[1].v_type != VAR_UNKNOWN)
19069 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019070 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019071 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019072 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019073 else if (argvars[1].v_type == VAR_PARTIAL)
19074 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019075 else
19076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019077 int error = FALSE;
19078
19079 i = get_tv_number_chk(&argvars[1], &error);
19080 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019081 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019082 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019083 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019084 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019085 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019086 else if (i != 0)
19087 {
19088 EMSG(_(e_invarg));
19089 goto theend;
19090 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019091 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019092 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019093 if (*info.item_compare_func == NUL)
19094 {
19095 /* empty string means default sort */
19096 info.item_compare_func = NULL;
19097 }
19098 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019099 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019100 info.item_compare_func = NULL;
19101 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019102 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019103 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019104 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019105 info.item_compare_func = NULL;
19106 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019107 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019108#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019109 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019110 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019111 info.item_compare_func = NULL;
19112 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019113 }
19114#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019115 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019116 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019117 info.item_compare_func = NULL;
19118 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019119 }
19120 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019121 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019122
19123 if (argvars[2].v_type != VAR_UNKNOWN)
19124 {
19125 /* optional third argument: {dict} */
19126 if (argvars[2].v_type != VAR_DICT)
19127 {
19128 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019129 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019130 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019131 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019132 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134
Bram Moolenaar0d660222005-01-07 21:51:51 +000019135 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019136 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019137 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019138 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139
Bram Moolenaar327aa022014-03-25 18:24:23 +010019140 i = 0;
19141 if (sort)
19142 {
19143 /* sort(): ptrs will be the list to sort */
19144 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019145 {
19146 ptrs[i].item = li;
19147 ptrs[i].idx = i;
19148 ++i;
19149 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019150
Bram Moolenaar0b962472016-02-22 22:51:33 +010019151 info.item_compare_func_err = FALSE;
19152 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019153 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019154 if ((info.item_compare_func != NULL
19155 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019156 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019157 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019158 EMSG(_("E702: Sort compare function failed"));
19159 else
19160 {
19161 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019162 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019163 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019164 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019165 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019166
Bram Moolenaar0b962472016-02-22 22:51:33 +010019167 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019168 {
19169 /* Clear the List and append the items in sorted order. */
19170 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19171 l->lv_len = 0;
19172 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019173 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019174 }
19175 }
19176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019178 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019179 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019180
19181 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019182 info.item_compare_func_err = FALSE;
19183 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019184 item_compare_func_ptr = info.item_compare_func != NULL
19185 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019186 ? item_compare2 : item_compare;
19187
19188 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19189 li = li->li_next)
19190 {
19191 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19192 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019193 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019194 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019195 {
19196 EMSG(_("E882: Uniq compare function failed"));
19197 break;
19198 }
19199 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019200
Bram Moolenaar0b962472016-02-22 22:51:33 +010019201 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019202 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019203 while (--i >= 0)
19204 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019205 li = ptrs[i].item->li_next;
19206 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019207 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019208 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019209 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019210 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019211 list_fix_watch(l, li);
19212 listitem_free(li);
19213 l->lv_len--;
19214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019215 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019216 }
19217
19218 vim_free(ptrs);
19219 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019220theend:
19221 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019222}
19223
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019224/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019225 * "sort({list})" function
19226 */
19227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019228f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019229{
19230 do_sort_uniq(argvars, rettv, TRUE);
19231}
19232
19233/*
19234 * "uniq({list})" function
19235 */
19236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019237f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019238{
19239 do_sort_uniq(argvars, rettv, FALSE);
19240}
19241
19242/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019243 * "soundfold({word})" function
19244 */
19245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019246f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019247{
19248 char_u *s;
19249
19250 rettv->v_type = VAR_STRING;
19251 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019252#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019253 rettv->vval.v_string = eval_soundfold(s);
19254#else
19255 rettv->vval.v_string = vim_strsave(s);
19256#endif
19257}
19258
19259/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019260 * "spellbadword()" function
19261 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019263f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019264{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019265 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019266 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019267 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019268
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019269 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019270 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019271
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019272#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019273 if (argvars[0].v_type == VAR_UNKNOWN)
19274 {
19275 /* Find the start and length of the badly spelled word. */
19276 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19277 if (len != 0)
19278 word = ml_get_cursor();
19279 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019280 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019281 {
19282 char_u *str = get_tv_string_chk(&argvars[0]);
19283 int capcol = -1;
19284
19285 if (str != NULL)
19286 {
19287 /* Check the argument for spelling. */
19288 while (*str != NUL)
19289 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019290 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019291 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019292 {
19293 word = str;
19294 break;
19295 }
19296 str += len;
19297 }
19298 }
19299 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019300#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019301
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019302 list_append_string(rettv->vval.v_list, word, len);
19303 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019304 attr == HLF_SPB ? "bad" :
19305 attr == HLF_SPR ? "rare" :
19306 attr == HLF_SPL ? "local" :
19307 attr == HLF_SPC ? "caps" :
19308 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019309}
19310
19311/*
19312 * "spellsuggest()" function
19313 */
19314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019315f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019316{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019317#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019318 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019319 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019320 int maxcount;
19321 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019322 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019323 listitem_T *li;
19324 int need_capital = FALSE;
19325#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019326
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019327 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019328 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019329
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019330#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019331 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019332 {
19333 str = get_tv_string(&argvars[0]);
19334 if (argvars[1].v_type != VAR_UNKNOWN)
19335 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019336 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019337 if (maxcount <= 0)
19338 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019339 if (argvars[2].v_type != VAR_UNKNOWN)
19340 {
19341 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19342 if (typeerr)
19343 return;
19344 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019345 }
19346 else
19347 maxcount = 25;
19348
Bram Moolenaar4770d092006-01-12 23:22:24 +000019349 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019350
19351 for (i = 0; i < ga.ga_len; ++i)
19352 {
19353 str = ((char_u **)ga.ga_data)[i];
19354
19355 li = listitem_alloc();
19356 if (li == NULL)
19357 vim_free(str);
19358 else
19359 {
19360 li->li_tv.v_type = VAR_STRING;
19361 li->li_tv.v_lock = 0;
19362 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019363 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019364 }
19365 }
19366 ga_clear(&ga);
19367 }
19368#endif
19369}
19370
Bram Moolenaar0d660222005-01-07 21:51:51 +000019371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019372f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019373{
19374 char_u *str;
19375 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019376 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019377 regmatch_T regmatch;
19378 char_u patbuf[NUMBUFLEN];
19379 char_u *save_cpo;
19380 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019381 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019382 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019383 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019384
19385 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19386 save_cpo = p_cpo;
19387 p_cpo = (char_u *)"";
19388
19389 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019390 if (argvars[1].v_type != VAR_UNKNOWN)
19391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019392 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19393 if (pat == NULL)
19394 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019395 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019396 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019397 }
19398 if (pat == NULL || *pat == NUL)
19399 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019400
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019401 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019403 if (typeerr)
19404 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405
Bram Moolenaar0d660222005-01-07 21:51:51 +000019406 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19407 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019409 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019410 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019411 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019412 if (*str == NUL)
19413 match = FALSE; /* empty item at the end */
19414 else
19415 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019416 if (match)
19417 end = regmatch.startp[0];
19418 else
19419 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019420 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19421 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019422 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019423 if (list_append_string(rettv->vval.v_list, str,
19424 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019425 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019426 }
19427 if (!match)
19428 break;
19429 /* Advance to just after the match. */
19430 if (regmatch.endp[0] > str)
19431 col = 0;
19432 else
19433 {
19434 /* Don't get stuck at the same match. */
19435#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019436 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019437#else
19438 col = 1;
19439#endif
19440 }
19441 str = regmatch.endp[0];
19442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443
Bram Moolenaar473de612013-06-08 18:19:48 +020019444 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446
Bram Moolenaar0d660222005-01-07 21:51:51 +000019447 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448}
19449
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019450#ifdef FEAT_FLOAT
19451/*
19452 * "sqrt()" function
19453 */
19454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019455f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019456{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019457 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019458
19459 rettv->v_type = VAR_FLOAT;
19460 if (get_float_arg(argvars, &f) == OK)
19461 rettv->vval.v_float = sqrt(f);
19462 else
19463 rettv->vval.v_float = 0.0;
19464}
19465
19466/*
19467 * "str2float()" function
19468 */
19469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019470f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019471{
19472 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19473
19474 if (*p == '+')
19475 p = skipwhite(p + 1);
19476 (void)string2float(p, &rettv->vval.v_float);
19477 rettv->v_type = VAR_FLOAT;
19478}
19479#endif
19480
Bram Moolenaar2c932302006-03-18 21:42:09 +000019481/*
19482 * "str2nr()" function
19483 */
19484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019485f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019486{
19487 int base = 10;
19488 char_u *p;
19489 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019490 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019491
19492 if (argvars[1].v_type != VAR_UNKNOWN)
19493 {
19494 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019495 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019496 {
19497 EMSG(_(e_invarg));
19498 return;
19499 }
19500 }
19501
19502 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019503 if (*p == '+')
19504 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019505 switch (base)
19506 {
19507 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19508 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19509 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19510 default: what = 0;
19511 }
19512 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019513 rettv->vval.v_number = n;
19514}
19515
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516#ifdef HAVE_STRFTIME
19517/*
19518 * "strftime({format}[, {time}])" function
19519 */
19520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019521f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522{
19523 char_u result_buf[256];
19524 struct tm *curtime;
19525 time_t seconds;
19526 char_u *p;
19527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019528 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019530 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019531 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 seconds = time(NULL);
19533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019534 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 curtime = localtime(&seconds);
19536 /* MSVC returns NULL for an invalid value of seconds. */
19537 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019538 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 else
19540 {
19541# ifdef FEAT_MBYTE
19542 vimconv_T conv;
19543 char_u *enc;
19544
19545 conv.vc_type = CONV_NONE;
19546 enc = enc_locale();
19547 convert_setup(&conv, p_enc, enc);
19548 if (conv.vc_type != CONV_NONE)
19549 p = string_convert(&conv, p, NULL);
19550# endif
19551 if (p != NULL)
19552 (void)strftime((char *)result_buf, sizeof(result_buf),
19553 (char *)p, curtime);
19554 else
19555 result_buf[0] = NUL;
19556
19557# ifdef FEAT_MBYTE
19558 if (conv.vc_type != CONV_NONE)
19559 vim_free(p);
19560 convert_setup(&conv, enc, p_enc);
19561 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019562 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 else
19564# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019565 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566
19567# ifdef FEAT_MBYTE
19568 /* Release conversion descriptors */
19569 convert_setup(&conv, NULL, NULL);
19570 vim_free(enc);
19571# endif
19572 }
19573}
19574#endif
19575
19576/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019577 * "strgetchar()" function
19578 */
19579 static void
19580f_strgetchar(typval_T *argvars, typval_T *rettv)
19581{
19582 char_u *str;
19583 int len;
19584 int error = FALSE;
19585 int charidx;
19586
19587 rettv->vval.v_number = -1;
19588 str = get_tv_string_chk(&argvars[0]);
19589 if (str == NULL)
19590 return;
19591 len = (int)STRLEN(str);
19592 charidx = get_tv_number_chk(&argvars[1], &error);
19593 if (error)
19594 return;
19595#ifdef FEAT_MBYTE
19596 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019597 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019598
19599 while (charidx >= 0 && byteidx < len)
19600 {
19601 if (charidx == 0)
19602 {
19603 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19604 break;
19605 }
19606 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019607 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019608 }
19609 }
19610#else
19611 if (charidx < len)
19612 rettv->vval.v_number = str[charidx];
19613#endif
19614}
19615
19616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 * "stridx()" function
19618 */
19619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019620f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621{
19622 char_u buf[NUMBUFLEN];
19623 char_u *needle;
19624 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019627 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019629 needle = get_tv_string_chk(&argvars[1]);
19630 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019631 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019632 if (needle == NULL || haystack == NULL)
19633 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634
Bram Moolenaar33570922005-01-25 22:26:29 +000019635 if (argvars[2].v_type != VAR_UNKNOWN)
19636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019637 int error = FALSE;
19638
19639 start_idx = get_tv_number_chk(&argvars[2], &error);
19640 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019641 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019642 if (start_idx >= 0)
19643 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 }
19645
19646 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19647 if (pos != NULL)
19648 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649}
19650
19651/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019652 * "string()" function
19653 */
19654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019655f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019656{
19657 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019658 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019660 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019661 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19662 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019663 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019664 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019665 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666}
19667
19668/*
19669 * "strlen()" function
19670 */
19671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019672f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019674 rettv->vval.v_number = (varnumber_T)(STRLEN(
19675 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676}
19677
19678/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019679 * "strchars()" function
19680 */
19681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019682f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019683{
19684 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019685 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019686#ifdef FEAT_MBYTE
19687 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019688 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019689#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019690
19691 if (argvars[1].v_type != VAR_UNKNOWN)
19692 skipcc = get_tv_number_chk(&argvars[1], NULL);
19693 if (skipcc < 0 || skipcc > 1)
19694 EMSG(_(e_invarg));
19695 else
19696 {
19697#ifdef FEAT_MBYTE
19698 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19699 while (*s != NUL)
19700 {
19701 func_mb_ptr2char_adv(&s);
19702 ++len;
19703 }
19704 rettv->vval.v_number = len;
19705#else
19706 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19707#endif
19708 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019709}
19710
19711/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019712 * "strdisplaywidth()" function
19713 */
19714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019715f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019716{
19717 char_u *s = get_tv_string(&argvars[0]);
19718 int col = 0;
19719
19720 if (argvars[1].v_type != VAR_UNKNOWN)
19721 col = get_tv_number(&argvars[1]);
19722
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019723 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019724}
19725
19726/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019727 * "strwidth()" function
19728 */
19729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019730f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019731{
19732 char_u *s = get_tv_string(&argvars[0]);
19733
19734 rettv->vval.v_number = (varnumber_T)(
19735#ifdef FEAT_MBYTE
19736 mb_string2cells(s, -1)
19737#else
19738 STRLEN(s)
19739#endif
19740 );
19741}
19742
19743/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019744 * "strcharpart()" function
19745 */
19746 static void
19747f_strcharpart(typval_T *argvars, typval_T *rettv)
19748{
19749#ifdef FEAT_MBYTE
19750 char_u *p;
19751 int nchar;
19752 int nbyte = 0;
19753 int charlen;
19754 int len = 0;
19755 int slen;
19756 int error = FALSE;
19757
19758 p = get_tv_string(&argvars[0]);
19759 slen = (int)STRLEN(p);
19760
19761 nchar = get_tv_number_chk(&argvars[1], &error);
19762 if (!error)
19763 {
19764 if (nchar > 0)
19765 while (nchar > 0 && nbyte < slen)
19766 {
19767 nbyte += mb_char2len(p[nbyte]);
19768 --nchar;
19769 }
19770 else
19771 nbyte = nchar;
19772 if (argvars[2].v_type != VAR_UNKNOWN)
19773 {
19774 charlen = get_tv_number(&argvars[2]);
19775 while (charlen > 0 && nbyte + len < slen)
19776 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019777 int off = nbyte + len;
19778
19779 if (off < 0)
19780 len += 1;
19781 else
19782 len += mb_char2len(p[off]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019783 --charlen;
19784 }
19785 }
19786 else
19787 len = slen - nbyte; /* default: all bytes that are available. */
19788 }
19789
19790 /*
19791 * Only return the overlap between the specified part and the actual
19792 * string.
19793 */
19794 if (nbyte < 0)
19795 {
19796 len += nbyte;
19797 nbyte = 0;
19798 }
19799 else if (nbyte > slen)
19800 nbyte = slen;
19801 if (len < 0)
19802 len = 0;
19803 else if (nbyte + len > slen)
19804 len = slen - nbyte;
19805
19806 rettv->v_type = VAR_STRING;
19807 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19808#else
19809 f_strpart(argvars, rettv);
19810#endif
19811}
19812
19813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 * "strpart()" function
19815 */
19816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019817f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818{
19819 char_u *p;
19820 int n;
19821 int len;
19822 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019823 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 slen = (int)STRLEN(p);
19827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019828 n = get_tv_number_chk(&argvars[1], &error);
19829 if (error)
19830 len = 0;
19831 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019832 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833 else
19834 len = slen - n; /* default len: all bytes that are available. */
19835
19836 /*
19837 * Only return the overlap between the specified part and the actual
19838 * string.
19839 */
19840 if (n < 0)
19841 {
19842 len += n;
19843 n = 0;
19844 }
19845 else if (n > slen)
19846 n = slen;
19847 if (len < 0)
19848 len = 0;
19849 else if (n + len > slen)
19850 len = slen - n;
19851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019852 rettv->v_type = VAR_STRING;
19853 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854}
19855
19856/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019857 * "strridx()" function
19858 */
19859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019860f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019861{
19862 char_u buf[NUMBUFLEN];
19863 char_u *needle;
19864 char_u *haystack;
19865 char_u *rest;
19866 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019867 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019869 needle = get_tv_string_chk(&argvars[1]);
19870 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019871
19872 rettv->vval.v_number = -1;
19873 if (needle == NULL || haystack == NULL)
19874 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019875
19876 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019877 if (argvars[2].v_type != VAR_UNKNOWN)
19878 {
19879 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019880 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019881 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019882 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019883 }
19884 else
19885 end_idx = haystack_len;
19886
Bram Moolenaar0d660222005-01-07 21:51:51 +000019887 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019888 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019889 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019890 lastmatch = haystack + end_idx;
19891 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019892 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019893 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019894 for (rest = haystack; *rest != '\0'; ++rest)
19895 {
19896 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019897 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019898 break;
19899 lastmatch = rest;
19900 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019901 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019902
19903 if (lastmatch == NULL)
19904 rettv->vval.v_number = -1;
19905 else
19906 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19907}
19908
19909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 * "strtrans()" function
19911 */
19912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019913f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019915 rettv->v_type = VAR_STRING;
19916 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917}
19918
19919/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019920 * "submatch()" function
19921 */
19922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019923f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019924{
Bram Moolenaar41571762014-04-02 19:00:58 +020019925 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019926 int no;
19927 int retList = 0;
19928
19929 no = (int)get_tv_number_chk(&argvars[0], &error);
19930 if (error)
19931 return;
19932 error = FALSE;
19933 if (argvars[1].v_type != VAR_UNKNOWN)
19934 retList = get_tv_number_chk(&argvars[1], &error);
19935 if (error)
19936 return;
19937
19938 if (retList == 0)
19939 {
19940 rettv->v_type = VAR_STRING;
19941 rettv->vval.v_string = reg_submatch(no);
19942 }
19943 else
19944 {
19945 rettv->v_type = VAR_LIST;
19946 rettv->vval.v_list = reg_submatch_list(no);
19947 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019948}
19949
19950/*
19951 * "substitute()" function
19952 */
19953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019954f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019955{
19956 char_u patbuf[NUMBUFLEN];
19957 char_u subbuf[NUMBUFLEN];
19958 char_u flagsbuf[NUMBUFLEN];
19959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019960 char_u *str = get_tv_string_chk(&argvars[0]);
19961 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19962 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19963 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19964
Bram Moolenaar0d660222005-01-07 21:51:51 +000019965 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019966 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19967 rettv->vval.v_string = NULL;
19968 else
19969 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019970}
19971
19972/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019973 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019976f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977{
19978 int id = 0;
19979#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019980 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981 long col;
19982 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019983 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019985 lnum = get_tv_lnum(argvars); /* -1 on type error */
19986 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19987 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019989 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019990 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019991 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992#endif
19993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019994 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995}
19996
19997/*
19998 * "synIDattr(id, what [, mode])" function
19999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020001f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002{
20003 char_u *p = NULL;
20004#ifdef FEAT_SYN_HL
20005 int id;
20006 char_u *what;
20007 char_u *mode;
20008 char_u modebuf[NUMBUFLEN];
20009 int modec;
20010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020011 id = get_tv_number(&argvars[0]);
20012 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020013 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020015 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020017 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 modec = 0; /* replace invalid with current */
20019 }
20020 else
20021 {
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020022#if defined(FEAT_GUI) || defined(FEAT_TERMTRUECOLOR)
20023 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 modec = 'g';
20025 else
20026#endif
20027 if (t_colors > 1)
20028 modec = 'c';
20029 else
20030 modec = 't';
20031 }
20032
20033
20034 switch (TOLOWER_ASC(what[0]))
20035 {
20036 case 'b':
20037 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20038 p = highlight_color(id, what, modec);
20039 else /* bold */
20040 p = highlight_has_attr(id, HL_BOLD, modec);
20041 break;
20042
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020043 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 p = highlight_color(id, what, modec);
20045 break;
20046
20047 case 'i':
20048 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20049 p = highlight_has_attr(id, HL_INVERSE, modec);
20050 else /* italic */
20051 p = highlight_has_attr(id, HL_ITALIC, modec);
20052 break;
20053
20054 case 'n': /* name */
20055 p = get_highlight_name(NULL, id - 1);
20056 break;
20057
20058 case 'r': /* reverse */
20059 p = highlight_has_attr(id, HL_INVERSE, modec);
20060 break;
20061
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020062 case 's':
20063 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20064 p = highlight_color(id, what, modec);
20065 else /* standout */
20066 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 break;
20068
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020069 case 'u':
20070 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20071 /* underline */
20072 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20073 else
20074 /* undercurl */
20075 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 break;
20077 }
20078
20079 if (p != NULL)
20080 p = vim_strsave(p);
20081#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020082 rettv->v_type = VAR_STRING;
20083 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084}
20085
20086/*
20087 * "synIDtrans(id)" function
20088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020090f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091{
20092 int id;
20093
20094#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020095 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096
20097 if (id > 0)
20098 id = syn_get_final_id(id);
20099 else
20100#endif
20101 id = 0;
20102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020103 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104}
20105
20106/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020107 * "synconcealed(lnum, col)" function
20108 */
20109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020110f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020111{
20112#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20113 long lnum;
20114 long col;
20115 int syntax_flags = 0;
20116 int cchar;
20117 int matchid = 0;
20118 char_u str[NUMBUFLEN];
20119#endif
20120
20121 rettv->v_type = VAR_LIST;
20122 rettv->vval.v_list = NULL;
20123
20124#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20125 lnum = get_tv_lnum(argvars); /* -1 on type error */
20126 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20127
20128 vim_memset(str, NUL, sizeof(str));
20129
20130 if (rettv_list_alloc(rettv) != FAIL)
20131 {
20132 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20133 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20134 && curwin->w_p_cole > 0)
20135 {
20136 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20137 syntax_flags = get_syntax_info(&matchid);
20138
20139 /* get the conceal character */
20140 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20141 {
20142 cchar = syn_get_sub_char();
20143 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20144 cchar = lcs_conceal;
20145 if (cchar != NUL)
20146 {
20147# ifdef FEAT_MBYTE
20148 if (has_mbyte)
20149 (*mb_char2bytes)(cchar, str);
20150 else
20151# endif
20152 str[0] = cchar;
20153 }
20154 }
20155 }
20156
20157 list_append_number(rettv->vval.v_list,
20158 (syntax_flags & HL_CONCEAL) != 0);
20159 /* -1 to auto-determine strlen */
20160 list_append_string(rettv->vval.v_list, str, -1);
20161 list_append_number(rettv->vval.v_list, matchid);
20162 }
20163#endif
20164}
20165
20166/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020167 * "synstack(lnum, col)" function
20168 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020170f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020171{
20172#ifdef FEAT_SYN_HL
20173 long lnum;
20174 long col;
20175 int i;
20176 int id;
20177#endif
20178
20179 rettv->v_type = VAR_LIST;
20180 rettv->vval.v_list = NULL;
20181
20182#ifdef FEAT_SYN_HL
20183 lnum = get_tv_lnum(argvars); /* -1 on type error */
20184 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20185
20186 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020187 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020188 && rettv_list_alloc(rettv) != FAIL)
20189 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020190 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020191 for (i = 0; ; ++i)
20192 {
20193 id = syn_get_stack_item(i);
20194 if (id < 0)
20195 break;
20196 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20197 break;
20198 }
20199 }
20200#endif
20201}
20202
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020204get_cmd_output_as_rettv(
20205 typval_T *argvars,
20206 typval_T *rettv,
20207 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020209 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020211 char_u *infile = NULL;
20212 char_u buf[NUMBUFLEN];
20213 int err = FALSE;
20214 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020215 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020216 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020218 rettv->v_type = VAR_STRING;
20219 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020220 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020221 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020222
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020223 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020224 {
20225 /*
20226 * Write the string to a temp file, to be used for input of the shell
20227 * command.
20228 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020229 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020230 {
20231 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020232 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020233 }
20234
20235 fd = mch_fopen((char *)infile, WRITEBIN);
20236 if (fd == NULL)
20237 {
20238 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020239 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020240 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020241 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020242 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020243 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20244 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020245 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020246 else
20247 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020248 size_t len;
20249
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020250 p = get_tv_string_buf_chk(&argvars[1], buf);
20251 if (p == NULL)
20252 {
20253 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020254 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020255 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020256 len = STRLEN(p);
20257 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020258 err = TRUE;
20259 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020260 if (fclose(fd) != 0)
20261 err = TRUE;
20262 if (err)
20263 {
20264 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020265 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020266 }
20267 }
20268
Bram Moolenaar52a72462014-08-29 15:53:52 +020020269 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20270 * echoes typeahead, that messes up the display. */
20271 if (!msg_silent)
20272 flags += SHELL_COOKED;
20273
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020274 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020276 int len;
20277 listitem_T *li;
20278 char_u *s = NULL;
20279 char_u *start;
20280 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020281 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282
Bram Moolenaar52a72462014-08-29 15:53:52 +020020283 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020284 if (res == NULL)
20285 goto errret;
20286
20287 list = list_alloc();
20288 if (list == NULL)
20289 goto errret;
20290
20291 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020293 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020294 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020295 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020296 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020297
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020298 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020299 if (s == NULL)
20300 goto errret;
20301
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020302 for (p = s; start < end; ++p, ++start)
20303 *p = *start == NUL ? NL : *start;
20304 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020305
20306 li = listitem_alloc();
20307 if (li == NULL)
20308 {
20309 vim_free(s);
20310 goto errret;
20311 }
20312 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020313 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020314 li->li_tv.vval.v_string = s;
20315 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020317
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020318 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020319 rettv->v_type = VAR_LIST;
20320 rettv->vval.v_list = list;
20321 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020323 else
20324 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020325 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020326#ifdef USE_CR
20327 /* translate <CR> into <NL> */
20328 if (res != NULL)
20329 {
20330 char_u *s;
20331
20332 for (s = res; *s; ++s)
20333 {
20334 if (*s == CAR)
20335 *s = NL;
20336 }
20337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338#else
20339# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020340 /* translate <CR><NL> into <NL> */
20341 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020343 char_u *s, *d;
20344
20345 d = res;
20346 for (s = res; *s; ++s)
20347 {
20348 if (s[0] == CAR && s[1] == NL)
20349 ++s;
20350 *d++ = *s;
20351 }
20352 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354# endif
20355#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020356 rettv->vval.v_string = res;
20357 res = NULL;
20358 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020359
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020360errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020361 if (infile != NULL)
20362 {
20363 mch_remove(infile);
20364 vim_free(infile);
20365 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020366 if (res != NULL)
20367 vim_free(res);
20368 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020369 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020370}
20371
20372/*
20373 * "system()" function
20374 */
20375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020376f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020377{
20378 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20379}
20380
20381/*
20382 * "systemlist()" function
20383 */
20384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020385f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020386{
20387 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388}
20389
20390/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020391 * "tabpagebuflist()" function
20392 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020394f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020395{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020396#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020397 tabpage_T *tp;
20398 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020399
20400 if (argvars[0].v_type == VAR_UNKNOWN)
20401 wp = firstwin;
20402 else
20403 {
20404 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20405 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020406 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020407 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020408 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020409 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020410 for (; wp != NULL; wp = wp->w_next)
20411 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020412 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020413 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020414 }
20415#endif
20416}
20417
20418
20419/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020420 * "tabpagenr()" function
20421 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020423f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020424{
20425 int nr = 1;
20426#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020427 char_u *arg;
20428
20429 if (argvars[0].v_type != VAR_UNKNOWN)
20430 {
20431 arg = get_tv_string_chk(&argvars[0]);
20432 nr = 0;
20433 if (arg != NULL)
20434 {
20435 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020436 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020437 else
20438 EMSG2(_(e_invexpr2), arg);
20439 }
20440 }
20441 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020442 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020443#endif
20444 rettv->vval.v_number = nr;
20445}
20446
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020447
20448#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020449static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020450
20451/*
20452 * Common code for tabpagewinnr() and winnr().
20453 */
20454 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020455get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020456{
20457 win_T *twin;
20458 int nr = 1;
20459 win_T *wp;
20460 char_u *arg;
20461
20462 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20463 if (argvar->v_type != VAR_UNKNOWN)
20464 {
20465 arg = get_tv_string_chk(argvar);
20466 if (arg == NULL)
20467 nr = 0; /* type error; errmsg already given */
20468 else if (STRCMP(arg, "$") == 0)
20469 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20470 else if (STRCMP(arg, "#") == 0)
20471 {
20472 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20473 if (twin == NULL)
20474 nr = 0;
20475 }
20476 else
20477 {
20478 EMSG2(_(e_invexpr2), arg);
20479 nr = 0;
20480 }
20481 }
20482
20483 if (nr > 0)
20484 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20485 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020486 {
20487 if (wp == NULL)
20488 {
20489 /* didn't find it in this tabpage */
20490 nr = 0;
20491 break;
20492 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020493 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020494 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020495 return nr;
20496}
20497#endif
20498
20499/*
20500 * "tabpagewinnr()" function
20501 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020503f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020504{
20505 int nr = 1;
20506#ifdef FEAT_WINDOWS
20507 tabpage_T *tp;
20508
20509 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20510 if (tp == NULL)
20511 nr = 0;
20512 else
20513 nr = get_winnr(tp, &argvars[1]);
20514#endif
20515 rettv->vval.v_number = nr;
20516}
20517
20518
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020519/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020520 * "tagfiles()" function
20521 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020523f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020524{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020525 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020526 tagname_T tn;
20527 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020528
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020529 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020530 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020531 fname = alloc(MAXPATHL);
20532 if (fname == NULL)
20533 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020534
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020535 for (first = TRUE; ; first = FALSE)
20536 if (get_tagfname(&tn, first, fname) == FAIL
20537 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020538 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020539 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020540 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020541}
20542
20543/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020544 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020545 */
20546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020547f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020548{
20549 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020550
20551 tag_pattern = get_tv_string(&argvars[0]);
20552
20553 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020554 if (*tag_pattern == NUL)
20555 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020556
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020557 if (rettv_list_alloc(rettv) == OK)
20558 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020559}
20560
20561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 * "tempname()" function
20563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020565f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566{
20567 static int x = 'A';
20568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020569 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020570 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571
20572 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20573 * names. Skip 'I' and 'O', they are used for shell redirection. */
20574 do
20575 {
20576 if (x == 'Z')
20577 x = '0';
20578 else if (x == '9')
20579 x = 'A';
20580 else
20581 {
20582#ifdef EBCDIC
20583 if (x == 'I')
20584 x = 'J';
20585 else if (x == 'R')
20586 x = 'S';
20587 else
20588#endif
20589 ++x;
20590 }
20591 } while (x == 'I' || x == 'O');
20592}
20593
20594/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020595 * "test(list)" function: Just checking the walls...
20596 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020598f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020599{
20600 /* Used for unit testing. Change the code below to your liking. */
20601#if 0
20602 listitem_T *li;
20603 list_T *l;
20604 char_u *bad, *good;
20605
20606 if (argvars[0].v_type != VAR_LIST)
20607 return;
20608 l = argvars[0].vval.v_list;
20609 if (l == NULL)
20610 return;
20611 li = l->lv_first;
20612 if (li == NULL)
20613 return;
20614 bad = get_tv_string(&li->li_tv);
20615 li = li->li_next;
20616 if (li == NULL)
20617 return;
20618 good = get_tv_string(&li->li_tv);
20619 rettv->vval.v_number = test_edit_score(bad, good);
20620#endif
20621}
20622
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020623#ifdef FEAT_FLOAT
20624/*
20625 * "tan()" function
20626 */
20627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020628f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020629{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020630 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020631
20632 rettv->v_type = VAR_FLOAT;
20633 if (get_float_arg(argvars, &f) == OK)
20634 rettv->vval.v_float = tan(f);
20635 else
20636 rettv->vval.v_float = 0.0;
20637}
20638
20639/*
20640 * "tanh()" function
20641 */
20642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020643f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020644{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020645 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020646
20647 rettv->v_type = VAR_FLOAT;
20648 if (get_float_arg(argvars, &f) == OK)
20649 rettv->vval.v_float = tanh(f);
20650 else
20651 rettv->vval.v_float = 0.0;
20652}
20653#endif
20654
Bram Moolenaar975b5272016-03-15 23:10:59 +010020655#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20656/*
20657 * Get a callback from "arg". It can be a Funcref or a function name.
20658 * When "arg" is zero return an empty string.
20659 * Return NULL for an invalid argument.
20660 */
20661 char_u *
20662get_callback(typval_T *arg, partial_T **pp)
20663{
20664 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20665 {
20666 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020667 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020668 return (*pp)->pt_name;
20669 }
20670 *pp = NULL;
20671 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20672 return arg->vval.v_string;
20673 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20674 return (char_u *)"";
20675 EMSG(_("E921: Invalid callback argument"));
20676 return NULL;
20677}
20678#endif
20679
20680#ifdef FEAT_TIMERS
20681/*
20682 * "timer_start(time, callback [, options])" function
20683 */
20684 static void
20685f_timer_start(typval_T *argvars, typval_T *rettv)
20686{
20687 long msec = get_tv_number(&argvars[0]);
20688 timer_T *timer;
20689 int repeat = 0;
20690 char_u *callback;
20691 dict_T *dict;
20692
Bram Moolenaar38499922016-04-22 20:46:52 +020020693 if (check_secure())
20694 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020695 if (argvars[2].v_type != VAR_UNKNOWN)
20696 {
20697 if (argvars[2].v_type != VAR_DICT
20698 || (dict = argvars[2].vval.v_dict) == NULL)
20699 {
20700 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20701 return;
20702 }
20703 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20704 repeat = get_dict_number(dict, (char_u *)"repeat");
20705 }
20706
20707 timer = create_timer(msec, repeat);
20708 callback = get_callback(&argvars[1], &timer->tr_partial);
20709 if (callback == NULL)
20710 {
20711 stop_timer(timer);
20712 rettv->vval.v_number = -1;
20713 }
20714 else
20715 {
20716 timer->tr_callback = vim_strsave(callback);
20717 rettv->vval.v_number = timer->tr_id;
20718 }
20719}
20720
20721/*
20722 * "timer_stop(timer)" function
20723 */
20724 static void
20725f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20726{
20727 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20728
20729 if (timer != NULL)
20730 stop_timer(timer);
20731}
20732#endif
20733
Bram Moolenaard52d9742005-08-21 22:20:28 +000020734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 * "tolower(string)" function
20736 */
20737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020738f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739{
20740 char_u *p;
20741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020742 p = vim_strsave(get_tv_string(&argvars[0]));
20743 rettv->v_type = VAR_STRING;
20744 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745
20746 if (p != NULL)
20747 while (*p != NUL)
20748 {
20749#ifdef FEAT_MBYTE
20750 int l;
20751
20752 if (enc_utf8)
20753 {
20754 int c, lc;
20755
20756 c = utf_ptr2char(p);
20757 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020758 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759 /* TODO: reallocate string when byte count changes. */
20760 if (utf_char2len(lc) == l)
20761 utf_char2bytes(lc, p);
20762 p += l;
20763 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020764 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765 p += l; /* skip multi-byte character */
20766 else
20767#endif
20768 {
20769 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20770 ++p;
20771 }
20772 }
20773}
20774
20775/*
20776 * "toupper(string)" function
20777 */
20778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020779f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020781 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020782 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783}
20784
20785/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020786 * "tr(string, fromstr, tostr)" function
20787 */
20788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020789f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020790{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020791 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020792 char_u *fromstr;
20793 char_u *tostr;
20794 char_u *p;
20795#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020796 int inlen;
20797 int fromlen;
20798 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020799 int idx;
20800 char_u *cpstr;
20801 int cplen;
20802 int first = TRUE;
20803#endif
20804 char_u buf[NUMBUFLEN];
20805 char_u buf2[NUMBUFLEN];
20806 garray_T ga;
20807
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020808 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020809 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20810 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020811
20812 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020813 rettv->v_type = VAR_STRING;
20814 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020815 if (fromstr == NULL || tostr == NULL)
20816 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020817 ga_init2(&ga, (int)sizeof(char), 80);
20818
20819#ifdef FEAT_MBYTE
20820 if (!has_mbyte)
20821#endif
20822 /* not multi-byte: fromstr and tostr must be the same length */
20823 if (STRLEN(fromstr) != STRLEN(tostr))
20824 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020825#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020826error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020827#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020828 EMSG2(_(e_invarg2), fromstr);
20829 ga_clear(&ga);
20830 return;
20831 }
20832
20833 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020834 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020835 {
20836#ifdef FEAT_MBYTE
20837 if (has_mbyte)
20838 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020839 inlen = (*mb_ptr2len)(in_str);
20840 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020841 cplen = inlen;
20842 idx = 0;
20843 for (p = fromstr; *p != NUL; p += fromlen)
20844 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020845 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020846 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020847 {
20848 for (p = tostr; *p != NUL; p += tolen)
20849 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020850 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020851 if (idx-- == 0)
20852 {
20853 cplen = tolen;
20854 cpstr = p;
20855 break;
20856 }
20857 }
20858 if (*p == NUL) /* tostr is shorter than fromstr */
20859 goto error;
20860 break;
20861 }
20862 ++idx;
20863 }
20864
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020865 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020866 {
20867 /* Check that fromstr and tostr have the same number of
20868 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020869 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020870 first = FALSE;
20871 for (p = tostr; *p != NUL; p += tolen)
20872 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020873 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020874 --idx;
20875 }
20876 if (idx != 0)
20877 goto error;
20878 }
20879
Bram Moolenaarcde88542015-08-11 19:14:00 +020020880 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020881 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020882 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020883
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020884 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020885 }
20886 else
20887#endif
20888 {
20889 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020890 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020891 if (p != NULL)
20892 ga_append(&ga, tostr[p - fromstr]);
20893 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020894 ga_append(&ga, *in_str);
20895 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020896 }
20897 }
20898
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020899 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020900 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020901 ga_append(&ga, NUL);
20902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020903 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020904}
20905
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020906#ifdef FEAT_FLOAT
20907/*
20908 * "trunc({float})" function
20909 */
20910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020911f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020912{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020913 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020914
20915 rettv->v_type = VAR_FLOAT;
20916 if (get_float_arg(argvars, &f) == OK)
20917 /* trunc() is not in C90, use floor() or ceil() instead. */
20918 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20919 else
20920 rettv->vval.v_float = 0.0;
20921}
20922#endif
20923
Bram Moolenaar8299df92004-07-10 09:47:34 +000020924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925 * "type(expr)" function
20926 */
20927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020928f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020930 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020931
20932 switch (argvars[0].v_type)
20933 {
20934 case VAR_NUMBER: n = 0; break;
20935 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020936 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020937 case VAR_FUNC: n = 2; break;
20938 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020939 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020940 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020941 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020942 if (argvars[0].vval.v_number == VVAL_FALSE
20943 || argvars[0].vval.v_number == VVAL_TRUE)
20944 n = 6;
20945 else
20946 n = 7;
20947 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020948 case VAR_JOB: n = 8; break;
20949 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020950 case VAR_UNKNOWN:
20951 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20952 n = -1;
20953 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020954 }
20955 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956}
20957
20958/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020959 * "undofile(name)" function
20960 */
20961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020962f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020963{
20964 rettv->v_type = VAR_STRING;
20965#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020966 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020967 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020968
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020969 if (*fname == NUL)
20970 {
20971 /* If there is no file name there will be no undo file. */
20972 rettv->vval.v_string = NULL;
20973 }
20974 else
20975 {
20976 char_u *ffname = FullName_save(fname, FALSE);
20977
20978 if (ffname != NULL)
20979 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20980 vim_free(ffname);
20981 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020982 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020983#else
20984 rettv->vval.v_string = NULL;
20985#endif
20986}
20987
20988/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020989 * "undotree()" function
20990 */
20991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020992f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020993{
20994 if (rettv_dict_alloc(rettv) == OK)
20995 {
20996 dict_T *dict = rettv->vval.v_dict;
20997 list_T *list;
20998
Bram Moolenaar730cde92010-06-27 05:18:54 +020020999 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021000 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021001 dict_add_nr_str(dict, "save_last",
21002 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021003 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21004 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021005 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021006
21007 list = list_alloc();
21008 if (list != NULL)
21009 {
21010 u_eval_tree(curbuf->b_u_oldhead, list);
21011 dict_add_list(dict, "entries", list);
21012 }
21013 }
21014}
21015
21016/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021017 * "values(dict)" function
21018 */
21019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021020f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021021{
21022 dict_list(argvars, rettv, 1);
21023}
21024
21025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 * "virtcol(string)" function
21027 */
21028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030{
21031 colnr_T vcol = 0;
21032 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021033 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021035 fp = var2fpos(&argvars[0], FALSE, &fnum);
21036 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21037 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 {
21039 getvvcol(curwin, fp, NULL, NULL, &vcol);
21040 ++vcol;
21041 }
21042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021043 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044}
21045
21046/*
21047 * "visualmode()" function
21048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021049 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021050f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 char_u str[2];
21053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021054 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 str[0] = curbuf->b_visual_mode_eval;
21056 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021057 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058
21059 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021060 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062}
21063
21064/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021065 * "wildmenumode()" function
21066 */
21067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021068f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021069{
21070#ifdef FEAT_WILDMENU
21071 if (wild_menu_showing)
21072 rettv->vval.v_number = 1;
21073#endif
21074}
21075
21076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 * "winbufnr(nr)" function
21078 */
21079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021080f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081{
21082 win_T *wp;
21083
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021084 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021086 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021088 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089}
21090
21091/*
21092 * "wincol()" function
21093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021095f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096{
21097 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021098 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099}
21100
21101/*
21102 * "winheight(nr)" function
21103 */
21104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021105f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106{
21107 win_T *wp;
21108
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021109 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021111 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021113 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114}
21115
21116/*
21117 * "winline()" function
21118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021120f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121{
21122 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021123 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124}
21125
21126/*
21127 * "winnr()" function
21128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021130f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131{
21132 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021133
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021135 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021137 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138}
21139
21140/*
21141 * "winrestcmd()" function
21142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021144f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145{
21146#ifdef FEAT_WINDOWS
21147 win_T *wp;
21148 int winnr = 1;
21149 garray_T ga;
21150 char_u buf[50];
21151
21152 ga_init2(&ga, (int)sizeof(char), 70);
21153 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21154 {
21155 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21156 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21158 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 ++winnr;
21160 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021161 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021163 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021165 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021167 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168}
21169
21170/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021171 * "winrestview()" function
21172 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021174f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021175{
21176 dict_T *dict;
21177
21178 if (argvars[0].v_type != VAR_DICT
21179 || (dict = argvars[0].vval.v_dict) == NULL)
21180 EMSG(_(e_invarg));
21181 else
21182 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021183 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21184 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21185 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21186 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021187#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021188 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21189 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021190#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021191 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21192 {
21193 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21194 curwin->w_set_curswant = FALSE;
21195 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021196
Bram Moolenaar82c25852014-05-28 16:47:16 +020021197 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21198 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021199#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021200 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21201 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021202#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021203 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21204 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21205 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21206 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021207
21208 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021209 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021210# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021211 win_new_width(curwin, W_WIDTH(curwin));
21212# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021213 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021214
Bram Moolenaarb851a962014-10-31 15:45:52 +010021215 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021216 curwin->w_topline = 1;
21217 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21218 curwin->w_topline = curbuf->b_ml.ml_line_count;
21219#ifdef FEAT_DIFF
21220 check_topfill(curwin, TRUE);
21221#endif
21222 }
21223}
21224
21225/*
21226 * "winsaveview()" function
21227 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021229f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021230{
21231 dict_T *dict;
21232
Bram Moolenaara800b422010-06-27 01:15:55 +020021233 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021234 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021235 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021236
21237 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21238 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21239#ifdef FEAT_VIRTUALEDIT
21240 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21241#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021242 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021243 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21244
21245 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21246#ifdef FEAT_DIFF
21247 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21248#endif
21249 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21250 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21251}
21252
21253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254 * "winwidth(nr)" function
21255 */
21256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021257f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258{
21259 win_T *wp;
21260
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021261 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021263 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021265#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021266 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021268 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269#endif
21270}
21271
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021273 * "wordcount()" function
21274 */
21275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021276f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021277{
21278 if (rettv_dict_alloc(rettv) == FAIL)
21279 return;
21280 cursor_pos_info(rettv->vval.v_dict);
21281}
21282
21283/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021284 * Write list of strings to file
21285 */
21286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021287write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021288{
21289 listitem_T *li;
21290 int c;
21291 int ret = OK;
21292 char_u *s;
21293
21294 for (li = list->lv_first; li != NULL; li = li->li_next)
21295 {
21296 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21297 {
21298 if (*s == '\n')
21299 c = putc(NUL, fd);
21300 else
21301 c = putc(*s, fd);
21302 if (c == EOF)
21303 {
21304 ret = FAIL;
21305 break;
21306 }
21307 }
21308 if (!binary || li->li_next != NULL)
21309 if (putc('\n', fd) == EOF)
21310 {
21311 ret = FAIL;
21312 break;
21313 }
21314 if (ret == FAIL)
21315 {
21316 EMSG(_(e_write));
21317 break;
21318 }
21319 }
21320 return ret;
21321}
21322
21323/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021324 * "writefile()" function
21325 */
21326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021327f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021328{
21329 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021330 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021331 char_u *fname;
21332 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021333 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021334
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021335 if (check_restricted() || check_secure())
21336 return;
21337
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021338 if (argvars[0].v_type != VAR_LIST)
21339 {
21340 EMSG2(_(e_listarg), "writefile()");
21341 return;
21342 }
21343 if (argvars[0].vval.v_list == NULL)
21344 return;
21345
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021346 if (argvars[2].v_type != VAR_UNKNOWN)
21347 {
21348 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21349 binary = TRUE;
21350 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21351 append = TRUE;
21352 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021353
21354 /* Always open the file in binary mode, library functions have a mind of
21355 * their own about CR-LF conversion. */
21356 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021357 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21358 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021359 {
21360 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21361 ret = -1;
21362 }
21363 else
21364 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021365 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21366 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021367 fclose(fd);
21368 }
21369
21370 rettv->vval.v_number = ret;
21371}
21372
21373/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021374 * "xor(expr, expr)" function
21375 */
21376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021377f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021378{
21379 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21380 ^ get_tv_number_chk(&argvars[1], NULL);
21381}
21382
21383
21384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021386 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 */
21388 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021389var2fpos(
21390 typval_T *varp,
21391 int dollar_lnum, /* TRUE when $ is last line */
21392 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021394 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021396 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397
Bram Moolenaara5525202006-03-02 22:52:09 +000021398 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021399 if (varp->v_type == VAR_LIST)
21400 {
21401 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021402 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021403 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021404 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021405
21406 l = varp->vval.v_list;
21407 if (l == NULL)
21408 return NULL;
21409
21410 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021411 pos.lnum = list_find_nr(l, 0L, &error);
21412 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021413 return NULL; /* invalid line number */
21414
21415 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021416 pos.col = list_find_nr(l, 1L, &error);
21417 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021418 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021419 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021420
21421 /* We accept "$" for the column number: last column. */
21422 li = list_find(l, 1L);
21423 if (li != NULL && li->li_tv.v_type == VAR_STRING
21424 && li->li_tv.vval.v_string != NULL
21425 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21426 pos.col = len + 1;
21427
Bram Moolenaara5525202006-03-02 22:52:09 +000021428 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021429 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021430 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021431 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021432
Bram Moolenaara5525202006-03-02 22:52:09 +000021433#ifdef FEAT_VIRTUALEDIT
21434 /* Get the virtual offset. Defaults to zero. */
21435 pos.coladd = list_find_nr(l, 2L, &error);
21436 if (error)
21437 pos.coladd = 0;
21438#endif
21439
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021440 return &pos;
21441 }
21442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021443 name = get_tv_string_chk(varp);
21444 if (name == NULL)
21445 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021446 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021448 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21449 {
21450 if (VIsual_active)
21451 return &VIsual;
21452 return &curwin->w_cursor;
21453 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021454 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021456 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21458 return NULL;
21459 return pp;
21460 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021461
21462#ifdef FEAT_VIRTUALEDIT
21463 pos.coladd = 0;
21464#endif
21465
Bram Moolenaar477933c2007-07-17 14:32:23 +000021466 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021467 {
21468 pos.col = 0;
21469 if (name[1] == '0') /* "w0": first visible line */
21470 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021471 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021472 pos.lnum = curwin->w_topline;
21473 return &pos;
21474 }
21475 else if (name[1] == '$') /* "w$": last visible line */
21476 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021477 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021478 pos.lnum = curwin->w_botline - 1;
21479 return &pos;
21480 }
21481 }
21482 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021484 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 {
21486 pos.lnum = curbuf->b_ml.ml_line_count;
21487 pos.col = 0;
21488 }
21489 else
21490 {
21491 pos.lnum = curwin->w_cursor.lnum;
21492 pos.col = (colnr_T)STRLEN(ml_get_curline());
21493 }
21494 return &pos;
21495 }
21496 return NULL;
21497}
21498
21499/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021500 * Convert list in "arg" into a position and optional file number.
21501 * When "fnump" is NULL there is no file number, only 3 items.
21502 * Note that the column is passed on as-is, the caller may want to decrement
21503 * it to use 1 for the first column.
21504 * Return FAIL when conversion is not possible, doesn't check the position for
21505 * validity.
21506 */
21507 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021508list2fpos(
21509 typval_T *arg,
21510 pos_T *posp,
21511 int *fnump,
21512 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021513{
21514 list_T *l = arg->vval.v_list;
21515 long i = 0;
21516 long n;
21517
Bram Moolenaar493c1782014-05-28 14:34:46 +020021518 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21519 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021520 if (arg->v_type != VAR_LIST
21521 || l == NULL
21522 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021523 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021524 return FAIL;
21525
21526 if (fnump != NULL)
21527 {
21528 n = list_find_nr(l, i++, NULL); /* fnum */
21529 if (n < 0)
21530 return FAIL;
21531 if (n == 0)
21532 n = curbuf->b_fnum; /* current buffer */
21533 *fnump = n;
21534 }
21535
21536 n = list_find_nr(l, i++, NULL); /* lnum */
21537 if (n < 0)
21538 return FAIL;
21539 posp->lnum = n;
21540
21541 n = list_find_nr(l, i++, NULL); /* col */
21542 if (n < 0)
21543 return FAIL;
21544 posp->col = n;
21545
21546#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021547 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021548 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021549 posp->coladd = 0;
21550 else
21551 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021552#endif
21553
Bram Moolenaar493c1782014-05-28 14:34:46 +020021554 if (curswantp != NULL)
21555 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21556
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021557 return OK;
21558}
21559
21560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561 * Get the length of an environment variable name.
21562 * Advance "arg" to the first character after the name.
21563 * Return 0 for error.
21564 */
21565 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021566get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567{
21568 char_u *p;
21569 int len;
21570
21571 for (p = *arg; vim_isIDc(*p); ++p)
21572 ;
21573 if (p == *arg) /* no name found */
21574 return 0;
21575
21576 len = (int)(p - *arg);
21577 *arg = p;
21578 return len;
21579}
21580
21581/*
21582 * Get the length of the name of a function or internal variable.
21583 * "arg" is advanced to the first non-white character after the name.
21584 * Return 0 if something is wrong.
21585 */
21586 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021587get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588{
21589 char_u *p;
21590 int len;
21591
21592 /* Find the end of the name. */
21593 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021594 {
21595 if (*p == ':')
21596 {
21597 /* "s:" is start of "s:var", but "n:" is not and can be used in
21598 * slice "[n:]". Also "xx:" is not a namespace. */
21599 len = (int)(p - *arg);
21600 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21601 || len > 1)
21602 break;
21603 }
21604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 if (p == *arg) /* no name found */
21606 return 0;
21607
21608 len = (int)(p - *arg);
21609 *arg = skipwhite(p);
21610
21611 return len;
21612}
21613
21614/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021615 * Get the length of the name of a variable or function.
21616 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021618 * Return -1 if curly braces expansion failed.
21619 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 * If the name contains 'magic' {}'s, expand them and return the
21621 * expanded name in an allocated string via 'alias' - caller must free.
21622 */
21623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021624get_name_len(
21625 char_u **arg,
21626 char_u **alias,
21627 int evaluate,
21628 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629{
21630 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 char_u *p;
21632 char_u *expr_start;
21633 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634
21635 *alias = NULL; /* default to no alias */
21636
21637 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21638 && (*arg)[2] == (int)KE_SNR)
21639 {
21640 /* hard coded <SNR>, already translated */
21641 *arg += 3;
21642 return get_id_len(arg) + 3;
21643 }
21644 len = eval_fname_script(*arg);
21645 if (len > 0)
21646 {
21647 /* literal "<SID>", "s:" or "<SNR>" */
21648 *arg += len;
21649 }
21650
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021652 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021654 p = find_name_end(*arg, &expr_start, &expr_end,
21655 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 if (expr_start != NULL)
21657 {
21658 char_u *temp_string;
21659
21660 if (!evaluate)
21661 {
21662 len += (int)(p - *arg);
21663 *arg = skipwhite(p);
21664 return len;
21665 }
21666
21667 /*
21668 * Include any <SID> etc in the expanded string:
21669 * Thus the -len here.
21670 */
21671 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21672 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021673 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 *alias = temp_string;
21675 *arg = skipwhite(p);
21676 return (int)STRLEN(temp_string);
21677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678
21679 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021680 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 EMSG2(_(e_invexpr2), *arg);
21682
21683 return len;
21684}
21685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021686/*
21687 * Find the end of a variable or function name, taking care of magic braces.
21688 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21689 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021690 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021691 * Return a pointer to just after the name. Equal to "arg" if there is no
21692 * valid name.
21693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021695find_name_end(
21696 char_u *arg,
21697 char_u **expr_start,
21698 char_u **expr_end,
21699 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021701 int mb_nest = 0;
21702 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021704 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021706 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021708 *expr_start = NULL;
21709 *expr_end = NULL;
21710 }
21711
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021712 /* Quick check for valid starting character. */
21713 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21714 return arg;
21715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021716 for (p = arg; *p != NUL
21717 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021718 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021719 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021720 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021721 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021722 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021723 if (*p == '\'')
21724 {
21725 /* skip over 'string' to avoid counting [ and ] inside it. */
21726 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21727 ;
21728 if (*p == NUL)
21729 break;
21730 }
21731 else if (*p == '"')
21732 {
21733 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21734 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21735 if (*p == '\\' && p[1] != NUL)
21736 ++p;
21737 if (*p == NUL)
21738 break;
21739 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021740 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21741 {
21742 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021743 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021744 len = (int)(p - arg);
21745 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021746 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021747 break;
21748 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021750 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021752 if (*p == '[')
21753 ++br_nest;
21754 else if (*p == ']')
21755 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021758 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021760 if (*p == '{')
21761 {
21762 mb_nest++;
21763 if (expr_start != NULL && *expr_start == NULL)
21764 *expr_start = p;
21765 }
21766 else if (*p == '}')
21767 {
21768 mb_nest--;
21769 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21770 *expr_end = p;
21771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773 }
21774
21775 return p;
21776}
21777
21778/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021779 * Expands out the 'magic' {}'s in a variable/function name.
21780 * Note that this can call itself recursively, to deal with
21781 * constructs like foo{bar}{baz}{bam}
21782 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21783 * "in_start" ^
21784 * "expr_start" ^
21785 * "expr_end" ^
21786 * "in_end" ^
21787 *
21788 * Returns a new allocated string, which the caller must free.
21789 * Returns NULL for failure.
21790 */
21791 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021792make_expanded_name(
21793 char_u *in_start,
21794 char_u *expr_start,
21795 char_u *expr_end,
21796 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021797{
21798 char_u c1;
21799 char_u *retval = NULL;
21800 char_u *temp_result;
21801 char_u *nextcmd = NULL;
21802
21803 if (expr_end == NULL || in_end == NULL)
21804 return NULL;
21805 *expr_start = NUL;
21806 *expr_end = NUL;
21807 c1 = *in_end;
21808 *in_end = NUL;
21809
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021810 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021811 if (temp_result != NULL && nextcmd == NULL)
21812 {
21813 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21814 + (in_end - expr_end) + 1));
21815 if (retval != NULL)
21816 {
21817 STRCPY(retval, in_start);
21818 STRCAT(retval, temp_result);
21819 STRCAT(retval, expr_end + 1);
21820 }
21821 }
21822 vim_free(temp_result);
21823
21824 *in_end = c1; /* put char back for error messages */
21825 *expr_start = '{';
21826 *expr_end = '}';
21827
21828 if (retval != NULL)
21829 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021830 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021831 if (expr_start != NULL)
21832 {
21833 /* Further expansion! */
21834 temp_result = make_expanded_name(retval, expr_start,
21835 expr_end, temp_result);
21836 vim_free(retval);
21837 retval = temp_result;
21838 }
21839 }
21840
21841 return retval;
21842}
21843
21844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021846 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 */
21848 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021849eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021851 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21852}
21853
21854/*
21855 * Return TRUE if character "c" can be used as the first character in a
21856 * variable or function name (excluding '{' and '}').
21857 */
21858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021859eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021860{
21861 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862}
21863
21864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 * Set number v: variable to "val".
21866 */
21867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021868set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021870 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871}
21872
21873/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021874 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021875 */
21876 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021877get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021879 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880}
21881
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021882/*
21883 * Get string v: variable value. Uses a static buffer, can only be used once.
21884 */
21885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021886get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021887{
21888 return get_tv_string(&vimvars[idx].vv_tv);
21889}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021890
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021892 * Get List v: variable value. Caller must take care of reference count when
21893 * needed.
21894 */
21895 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021896get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021897{
21898 return vimvars[idx].vv_list;
21899}
21900
21901/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021902 * Set v:char to character "c".
21903 */
21904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021905set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021906{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021907 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021908
21909#ifdef FEAT_MBYTE
21910 if (has_mbyte)
21911 buf[(*mb_char2bytes)(c, buf)] = NUL;
21912 else
21913#endif
21914 {
21915 buf[0] = c;
21916 buf[1] = NUL;
21917 }
21918 set_vim_var_string(VV_CHAR, buf, -1);
21919}
21920
21921/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021922 * Set v:count to "count" and v:count1 to "count1".
21923 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 */
21925 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021926set_vcount(
21927 long count,
21928 long count1,
21929 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021931 if (set_prevcount)
21932 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021933 vimvars[VV_COUNT].vv_nr = count;
21934 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935}
21936
21937/*
21938 * Set string v: variable to a copy of "val".
21939 */
21940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021941set_vim_var_string(
21942 int idx,
21943 char_u *val,
21944 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945{
Bram Moolenaara542c682016-01-31 16:28:04 +010021946 clear_tv(&vimvars[idx].vv_di.di_tv);
21947 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021949 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021951 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021953 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954}
21955
21956/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021957 * Set List v: variable to "val".
21958 */
21959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021960set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021961{
Bram Moolenaara542c682016-01-31 16:28:04 +010021962 clear_tv(&vimvars[idx].vv_di.di_tv);
21963 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021964 vimvars[idx].vv_list = val;
21965 if (val != NULL)
21966 ++val->lv_refcount;
21967}
21968
21969/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021970 * Set Dictionary v: variable to "val".
21971 */
21972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021973set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021974{
21975 int todo;
21976 hashitem_T *hi;
21977
Bram Moolenaara542c682016-01-31 16:28:04 +010021978 clear_tv(&vimvars[idx].vv_di.di_tv);
21979 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021980 vimvars[idx].vv_dict = val;
21981 if (val != NULL)
21982 {
21983 ++val->dv_refcount;
21984
21985 /* Set readonly */
21986 todo = (int)val->dv_hashtab.ht_used;
21987 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21988 {
21989 if (HASHITEM_EMPTY(hi))
21990 continue;
21991 --todo;
21992 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21993 }
21994 }
21995}
21996
21997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 * Set v:register if needed.
21999 */
22000 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022001set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002{
22003 char_u regname;
22004
22005 if (c == 0 || c == ' ')
22006 regname = '"';
22007 else
22008 regname = c;
22009 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022010 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 set_vim_var_string(VV_REG, &regname, 1);
22012}
22013
22014/*
22015 * Get or set v:exception. If "oldval" == NULL, return the current value.
22016 * Otherwise, restore the value to "oldval" and return NULL.
22017 * Must always be called in pairs to save and restore v:exception! Does not
22018 * take care of memory allocations.
22019 */
22020 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022021v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022{
22023 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022024 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025
Bram Moolenaare9a41262005-01-15 22:18:47 +000022026 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 return NULL;
22028}
22029
22030/*
22031 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22032 * Otherwise, restore the value to "oldval" and return NULL.
22033 * Must always be called in pairs to save and restore v:throwpoint! Does not
22034 * take care of memory allocations.
22035 */
22036 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022037v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038{
22039 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022040 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041
Bram Moolenaare9a41262005-01-15 22:18:47 +000022042 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 return NULL;
22044}
22045
22046#if defined(FEAT_AUTOCMD) || defined(PROTO)
22047/*
22048 * Set v:cmdarg.
22049 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22050 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22051 * Must always be called in pairs!
22052 */
22053 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022054set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055{
22056 char_u *oldval;
22057 char_u *newval;
22058 unsigned len;
22059
Bram Moolenaare9a41262005-01-15 22:18:47 +000022060 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022061 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022063 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022064 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022065 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 }
22067
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022068 if (eap->force_bin == FORCE_BIN)
22069 len = 6;
22070 else if (eap->force_bin == FORCE_NOBIN)
22071 len = 8;
22072 else
22073 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022074
22075 if (eap->read_edit)
22076 len += 7;
22077
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022078 if (eap->force_ff != 0)
22079 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22080# ifdef FEAT_MBYTE
22081 if (eap->force_enc != 0)
22082 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022083 if (eap->bad_char != 0)
22084 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022085# endif
22086
22087 newval = alloc(len + 1);
22088 if (newval == NULL)
22089 return NULL;
22090
22091 if (eap->force_bin == FORCE_BIN)
22092 sprintf((char *)newval, " ++bin");
22093 else if (eap->force_bin == FORCE_NOBIN)
22094 sprintf((char *)newval, " ++nobin");
22095 else
22096 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022097
22098 if (eap->read_edit)
22099 STRCAT(newval, " ++edit");
22100
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022101 if (eap->force_ff != 0)
22102 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22103 eap->cmd + eap->force_ff);
22104# ifdef FEAT_MBYTE
22105 if (eap->force_enc != 0)
22106 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22107 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022108 if (eap->bad_char == BAD_KEEP)
22109 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22110 else if (eap->bad_char == BAD_DROP)
22111 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22112 else if (eap->bad_char != 0)
22113 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022114# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022115 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022116 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117}
22118#endif
22119
22120/*
22121 * Get the value of internal variable "name".
22122 * Return OK or FAIL.
22123 */
22124 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022125get_var_tv(
22126 char_u *name,
22127 int len, /* length of "name" */
22128 typval_T *rettv, /* NULL when only checking existence */
22129 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22130 int verbose, /* may give error message */
22131 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132{
22133 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022134 typval_T *tv = NULL;
22135 typval_T atv;
22136 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138
22139 /* truncate the name, so that we can use strcmp() */
22140 cc = name[len];
22141 name[len] = NUL;
22142
22143 /*
22144 * Check for "b:changedtick".
22145 */
22146 if (STRCMP(name, "b:changedtick") == 0)
22147 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022148 atv.v_type = VAR_NUMBER;
22149 atv.vval.v_number = curbuf->b_changedtick;
22150 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 }
22152
22153 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 * Check for user-defined variables.
22155 */
22156 else
22157 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022158 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022160 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022161 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022162 if (dip != NULL)
22163 *dip = v;
22164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165 }
22166
Bram Moolenaare9a41262005-01-15 22:18:47 +000022167 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022169 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022170 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 ret = FAIL;
22172 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022173 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022174 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175
22176 name[len] = cc;
22177
22178 return ret;
22179}
22180
22181/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022182 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22183 * Also handle function call with Funcref variable: func(expr)
22184 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22185 */
22186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022187handle_subscript(
22188 char_u **arg,
22189 typval_T *rettv,
22190 int evaluate, /* do more than finding the end */
22191 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022192{
22193 int ret = OK;
22194 dict_T *selfdict = NULL;
22195 char_u *s;
22196 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022197 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022198
22199 while (ret == OK
22200 && (**arg == '['
22201 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022202 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22203 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022204 && !vim_iswhite(*(*arg - 1)))
22205 {
22206 if (**arg == '(')
22207 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022208 partial_T *pt = NULL;
22209
Bram Moolenaard9fba312005-06-26 22:34:35 +000022210 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022211 if (evaluate)
22212 {
22213 functv = *rettv;
22214 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022215
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022216 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022217 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022218 {
22219 pt = functv.vval.v_partial;
22220 s = pt->pt_name;
22221 }
22222 else
22223 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022224 }
22225 else
22226 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022227 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022228 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022229 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022230
22231 /* Clear the funcref afterwards, so that deleting it while
22232 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022233 if (evaluate)
22234 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022235
22236 /* Stop the expression evaluation when immediately aborting on
22237 * error, or when an interrupt occurred or an exception was thrown
22238 * but not caught. */
22239 if (aborting())
22240 {
22241 if (ret == OK)
22242 clear_tv(rettv);
22243 ret = FAIL;
22244 }
22245 dict_unref(selfdict);
22246 selfdict = NULL;
22247 }
22248 else /* **arg == '[' || **arg == '.' */
22249 {
22250 dict_unref(selfdict);
22251 if (rettv->v_type == VAR_DICT)
22252 {
22253 selfdict = rettv->vval.v_dict;
22254 if (selfdict != NULL)
22255 ++selfdict->dv_refcount;
22256 }
22257 else
22258 selfdict = NULL;
22259 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22260 {
22261 clear_tv(rettv);
22262 ret = FAIL;
22263 }
22264 }
22265 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022266
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022267 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
22268 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022269 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022270 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22271 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022272 char_u *tofree = NULL;
22273 ufunc_T *fp;
22274 char_u fname_buf[FLEN_FIXED + 1];
22275 int error;
22276
22277 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022278 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022279 fp = find_func(fname);
22280 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022281
22282 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010022283 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022284 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022285 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22286
22287 if (pt != NULL)
22288 {
22289 pt->pt_refcount = 1;
22290 pt->pt_dict = selfdict;
22291 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022292 if (rettv->v_type == VAR_FUNC)
22293 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022294 /* Just a function: Take over the function name and use
22295 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022296 pt->pt_name = rettv->vval.v_string;
22297 }
22298 else
22299 {
22300 partial_T *ret_pt = rettv->vval.v_partial;
22301 int i;
22302
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022303 /* Partial: copy the function name, use selfdict and copy
22304 * args. Can't take over name or args, the partial might
22305 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022306 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022307 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022308 if (ret_pt->pt_argc > 0)
22309 {
22310 pt->pt_argv = (typval_T *)alloc(
22311 sizeof(typval_T) * ret_pt->pt_argc);
22312 if (pt->pt_argv == NULL)
22313 /* out of memory: drop the arguments */
22314 pt->pt_argc = 0;
22315 else
22316 {
22317 pt->pt_argc = ret_pt->pt_argc;
22318 for (i = 0; i < pt->pt_argc; i++)
22319 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22320 }
22321 }
22322 partial_unref(ret_pt);
22323 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022324 rettv->v_type = VAR_PARTIAL;
22325 rettv->vval.v_partial = pt;
22326 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022327 }
22328 }
22329
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022330 dict_unref(selfdict);
22331 return ret;
22332}
22333
22334/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022335 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022336 * value).
22337 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022338 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022339alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022340{
Bram Moolenaar33570922005-01-25 22:26:29 +000022341 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022342}
22343
22344/*
22345 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346 * The string "s" must have been allocated, it is consumed.
22347 * Return NULL for out of memory, the variable otherwise.
22348 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022349 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022350alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351{
Bram Moolenaar33570922005-01-25 22:26:29 +000022352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022354 rettv = alloc_tv();
22355 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022357 rettv->v_type = VAR_STRING;
22358 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022359 }
22360 else
22361 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022362 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363}
22364
22365/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022366 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022368 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022369free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370{
22371 if (varp != NULL)
22372 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022373 switch (varp->v_type)
22374 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022375 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022376 func_unref(varp->vval.v_string);
22377 /*FALLTHROUGH*/
22378 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022379 vim_free(varp->vval.v_string);
22380 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022381 case VAR_PARTIAL:
22382 partial_unref(varp->vval.v_partial);
22383 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022384 case VAR_LIST:
22385 list_unref(varp->vval.v_list);
22386 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022387 case VAR_DICT:
22388 dict_unref(varp->vval.v_dict);
22389 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022390 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022391#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022392 job_unref(varp->vval.v_job);
22393 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022394#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022395 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022396#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022397 channel_unref(varp->vval.v_channel);
22398 break;
22399#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022400 case VAR_NUMBER:
22401 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022402 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022403 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022404 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 vim_free(varp);
22407 }
22408}
22409
22410/*
22411 * Free the memory for a variable value and set the value to NULL or 0.
22412 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022414clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415{
22416 if (varp != NULL)
22417 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022418 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022420 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022421 func_unref(varp->vval.v_string);
22422 /*FALLTHROUGH*/
22423 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022424 vim_free(varp->vval.v_string);
22425 varp->vval.v_string = NULL;
22426 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022427 case VAR_PARTIAL:
22428 partial_unref(varp->vval.v_partial);
22429 varp->vval.v_partial = NULL;
22430 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022431 case VAR_LIST:
22432 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022433 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022434 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022435 case VAR_DICT:
22436 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022437 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022438 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022439 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022440 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022441 varp->vval.v_number = 0;
22442 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022443 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022444#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022445 varp->vval.v_float = 0.0;
22446 break;
22447#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022448 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022449#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022450 job_unref(varp->vval.v_job);
22451 varp->vval.v_job = NULL;
22452#endif
22453 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022454 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022455#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022456 channel_unref(varp->vval.v_channel);
22457 varp->vval.v_channel = NULL;
22458#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022459 case VAR_UNKNOWN:
22460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022462 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 }
22464}
22465
22466/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022467 * Set the value of a variable to NULL without freeing items.
22468 */
22469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022470init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022471{
22472 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022473 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022474}
22475
22476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477 * Get the number value of a variable.
22478 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022479 * For incompatible types, return 0.
22480 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22481 * caller of incompatible types: it sets *denote to TRUE if "denote"
22482 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022484 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022485get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022487 int error = FALSE;
22488
22489 return get_tv_number_chk(varp, &error); /* return 0L on error */
22490}
22491
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022492 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022493get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022494{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022495 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022497 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022499 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022500 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022501 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022502#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022503 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022504 break;
22505#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022506 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022507 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022508 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022509 break;
22510 case VAR_STRING:
22511 if (varp->vval.v_string != NULL)
22512 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022513 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022514 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022515 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022516 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022517 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022518 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022519 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022520 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022521 case VAR_SPECIAL:
22522 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22523 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022524 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022525#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022526 EMSG(_("E910: Using a Job as a Number"));
22527 break;
22528#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022529 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022530#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022531 EMSG(_("E913: Using a Channel as a Number"));
22532 break;
22533#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022534 case VAR_UNKNOWN:
22535 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022538 if (denote == NULL) /* useful for values that must be unsigned */
22539 n = -1;
22540 else
22541 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022542 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543}
22544
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022545#ifdef FEAT_FLOAT
22546 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022547get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022548{
22549 switch (varp->v_type)
22550 {
22551 case VAR_NUMBER:
22552 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022553 case VAR_FLOAT:
22554 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022555 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022556 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022557 EMSG(_("E891: Using a Funcref as a Float"));
22558 break;
22559 case VAR_STRING:
22560 EMSG(_("E892: Using a String as a Float"));
22561 break;
22562 case VAR_LIST:
22563 EMSG(_("E893: Using a List as a Float"));
22564 break;
22565 case VAR_DICT:
22566 EMSG(_("E894: Using a Dictionary as a Float"));
22567 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022568 case VAR_SPECIAL:
22569 EMSG(_("E907: Using a special value as a Float"));
22570 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022571 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022572# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022573 EMSG(_("E911: Using a Job as a Float"));
22574 break;
22575# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022576 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022577# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022578 EMSG(_("E914: Using a Channel as a Float"));
22579 break;
22580# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022581 case VAR_UNKNOWN:
22582 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022583 break;
22584 }
22585 return 0;
22586}
22587#endif
22588
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022590 * Get the lnum from the first argument.
22591 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022592 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 */
22594 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022595get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596{
Bram Moolenaar33570922005-01-25 22:26:29 +000022597 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 linenr_T lnum;
22599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022600 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 if (lnum == 0) /* no valid number, try using line() */
22602 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022603 rettv.v_type = VAR_NUMBER;
22604 f_line(argvars, &rettv);
22605 lnum = rettv.vval.v_number;
22606 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 }
22608 return lnum;
22609}
22610
22611/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022612 * Get the lnum from the first argument.
22613 * Also accepts "$", then "buf" is used.
22614 * Returns 0 on error.
22615 */
22616 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022617get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022618{
22619 if (argvars[0].v_type == VAR_STRING
22620 && argvars[0].vval.v_string != NULL
22621 && argvars[0].vval.v_string[0] == '$'
22622 && buf != NULL)
22623 return buf->b_ml.ml_line_count;
22624 return get_tv_number_chk(&argvars[0], NULL);
22625}
22626
22627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 * Get the string value of a variable.
22629 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022630 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22631 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632 * If the String variable has never been set, return an empty string.
22633 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022634 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22635 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022637 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022638get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639{
22640 static char_u mybuf[NUMBUFLEN];
22641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022642 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643}
22644
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022645 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022646get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022648 char_u *res = get_tv_string_buf_chk(varp, buf);
22649
22650 return res != NULL ? res : (char_u *)"";
22651}
22652
Bram Moolenaar7d647822014-04-05 21:28:56 +020022653/*
22654 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22655 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022656 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022657get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022658{
22659 static char_u mybuf[NUMBUFLEN];
22660
22661 return get_tv_string_buf_chk(varp, mybuf);
22662}
22663
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022664 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022665get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022666{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022667 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022669 case VAR_NUMBER:
22670 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22671 return buf;
22672 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022673 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022674 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022675 break;
22676 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022677 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022678 break;
22679 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022680 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022681 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022682 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022683#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022684 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022685 break;
22686#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022687 case VAR_STRING:
22688 if (varp->vval.v_string != NULL)
22689 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022690 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022691 case VAR_SPECIAL:
22692 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22693 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022694 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022695#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022696 {
22697 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022698 char *status;
22699
22700 if (job == NULL)
22701 return (char_u *)"no process";
22702 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022703 : job->jv_status == JOB_ENDED ? "dead"
22704 : "run";
22705# ifdef UNIX
22706 vim_snprintf((char *)buf, NUMBUFLEN,
22707 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022708# elif defined(WIN32)
22709 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022710 "process %ld %s",
22711 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022712 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022713# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022714 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022715 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22716# endif
22717 return buf;
22718 }
22719#endif
22720 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022721 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022722#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022723 {
22724 channel_T *channel = varp->vval.v_channel;
22725 char *status = channel_status(channel);
22726
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022727 if (channel == NULL)
22728 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22729 else
22730 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022731 "channel %d %s", channel->ch_id, status);
22732 return buf;
22733 }
22734#endif
22735 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022736 case VAR_UNKNOWN:
22737 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022738 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022740 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741}
22742
22743/*
22744 * Find variable "name" in the list of variables.
22745 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022746 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022747 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022748 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022750 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022751find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022754 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755
Bram Moolenaara7043832005-01-21 11:56:39 +000022756 ht = find_var_ht(name, &varname);
22757 if (htp != NULL)
22758 *htp = ht;
22759 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022761 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762}
22763
22764/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022765 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022766 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022767 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022768 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022769find_var_in_ht(
22770 hashtab_T *ht,
22771 int htname,
22772 char_u *varname,
22773 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022774{
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 hashitem_T *hi;
22776
22777 if (*varname == NUL)
22778 {
22779 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022780 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022781 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022782 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022783 case 'g': return &globvars_var;
22784 case 'v': return &vimvars_var;
22785 case 'b': return &curbuf->b_bufvar;
22786 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022787#ifdef FEAT_WINDOWS
22788 case 't': return &curtab->tp_winvar;
22789#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022790 case 'l': return current_funccal == NULL
22791 ? NULL : &current_funccal->l_vars_var;
22792 case 'a': return current_funccal == NULL
22793 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022794 }
22795 return NULL;
22796 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022797
22798 hi = hash_find(ht, varname);
22799 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022800 {
22801 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022802 * worked find the variable again. Don't auto-load a script if it was
22803 * loaded already, otherwise it would be loaded every time when
22804 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022805 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022806 {
22807 /* Note: script_autoload() may make "hi" invalid. It must either
22808 * be obtained again or not used. */
22809 if (!script_autoload(varname, FALSE) || aborting())
22810 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022811 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022812 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022813 if (HASHITEM_EMPTY(hi))
22814 return NULL;
22815 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022816 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022817}
22818
22819/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022820 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022821 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022822 * Set "varname" to the start of name without ':'.
22823 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022824 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022825find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022827 hashitem_T *hi;
22828
Bram Moolenaar73627d02015-08-11 15:46:09 +020022829 if (name[0] == NUL)
22830 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 if (name[1] != ':')
22832 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022833 /* The name must not start with a colon or #. */
22834 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835 return NULL;
22836 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022837
22838 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022839 hi = hash_find(&compat_hashtab, name);
22840 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022841 return &compat_hashtab;
22842
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022844 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022845 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 }
22847 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022848 if (*name == 'g') /* global variable */
22849 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022850 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22851 */
22852 if (vim_strchr(name + 2, ':') != NULL
22853 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022854 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022856 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022858 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022859#ifdef FEAT_WINDOWS
22860 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022861 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022862#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022863 if (*name == 'v') /* v: variable */
22864 return &vimvarht;
22865 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022866 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022867 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022868 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 if (*name == 's' /* script variable */
22870 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22871 return &SCRIPT_VARS(current_SID);
22872 return NULL;
22873}
22874
22875/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022876 * Get function call environment based on bactrace debug level
22877 */
22878 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022879get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022880{
22881 int i;
22882 funccall_T *funccal;
22883 funccall_T *temp_funccal;
22884
22885 funccal = current_funccal;
22886 if (debug_backtrace_level > 0)
22887 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022888 for (i = 0; i < debug_backtrace_level; i++)
22889 {
22890 temp_funccal = funccal->caller;
22891 if (temp_funccal)
22892 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022893 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022894 /* backtrace level overflow. reset to max */
22895 debug_backtrace_level = i;
22896 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022897 }
22898 return funccal;
22899}
22900
22901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022903 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 * Returns NULL when it doesn't exist.
22905 */
22906 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022907get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908{
Bram Moolenaar33570922005-01-25 22:26:29 +000022909 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022911 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 if (v == NULL)
22913 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022914 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915}
22916
22917/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022918 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 * sourcing this script and when executing functions defined in the script.
22920 */
22921 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022922new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923{
Bram Moolenaara7043832005-01-21 11:56:39 +000022924 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022925 hashtab_T *ht;
22926 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022927
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22929 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022930 /* Re-allocating ga_data means that an ht_array pointing to
22931 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022932 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022933 for (i = 1; i <= ga_scripts.ga_len; ++i)
22934 {
22935 ht = &SCRIPT_VARS(i);
22936 if (ht->ht_mask == HT_INIT_SIZE - 1)
22937 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022938 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022939 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022940 }
22941
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 while (ga_scripts.ga_len < id)
22943 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022944 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022945 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022946 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 }
22949 }
22950}
22951
22952/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022953 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22954 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 */
22956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022957init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958{
Bram Moolenaar33570922005-01-25 22:26:29 +000022959 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022960 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022961 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022962 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022963 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022964 dict_var->di_tv.vval.v_dict = dict;
22965 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022966 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022967 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22968 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969}
22970
22971/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022972 * Unreference a dictionary initialized by init_var_dict().
22973 */
22974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022975unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022976{
22977 /* Now the dict needs to be freed if no one else is using it, go back to
22978 * normal reference counting. */
22979 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22980 dict_unref(dict);
22981}
22982
22983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022985 * Frees all allocated variables and the value they contain.
22986 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 */
22988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022989vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022990{
22991 vars_clear_ext(ht, TRUE);
22992}
22993
22994/*
22995 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22996 */
22997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022998vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999{
Bram Moolenaara7043832005-01-21 11:56:39 +000023000 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023001 hashitem_T *hi;
23002 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003
Bram Moolenaar33570922005-01-25 22:26:29 +000023004 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023005 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023006 for (hi = ht->ht_array; todo > 0; ++hi)
23007 {
23008 if (!HASHITEM_EMPTY(hi))
23009 {
23010 --todo;
23011
Bram Moolenaar33570922005-01-25 22:26:29 +000023012 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023013 * ht_array might change then. hash_clear() takes care of it
23014 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023015 v = HI2DI(hi);
23016 if (free_val)
23017 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023018 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023019 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023020 }
23021 }
23022 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023023 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024}
23025
Bram Moolenaara7043832005-01-21 11:56:39 +000023026/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023027 * Delete a variable from hashtab "ht" at item "hi".
23028 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023031delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032{
Bram Moolenaar33570922005-01-25 22:26:29 +000023033 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023034
23035 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023036 clear_tv(&di->di_tv);
23037 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038}
23039
23040/*
23041 * List the value of one internal variable.
23042 */
23043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023044list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023046 char_u *tofree;
23047 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023048 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023049
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023050 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023051 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023052 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023053 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054}
23055
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023057list_one_var_a(
23058 char_u *prefix,
23059 char_u *name,
23060 int type,
23061 char_u *string,
23062 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063{
Bram Moolenaar31859182007-08-14 20:41:13 +000023064 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23065 msg_start();
23066 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 if (name != NULL) /* "a:" vars don't have a name stored */
23068 msg_puts(name);
23069 msg_putchar(' ');
23070 msg_advance(22);
23071 if (type == VAR_NUMBER)
23072 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023073 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023074 msg_putchar('*');
23075 else if (type == VAR_LIST)
23076 {
23077 msg_putchar('[');
23078 if (*string == '[')
23079 ++string;
23080 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023081 else if (type == VAR_DICT)
23082 {
23083 msg_putchar('{');
23084 if (*string == '{')
23085 ++string;
23086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087 else
23088 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023089
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023091
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023092 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023093 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023094 if (*first)
23095 {
23096 msg_clr_eos();
23097 *first = FALSE;
23098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099}
23100
23101/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023102 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 * If the variable already exists, the value is updated.
23104 * Otherwise the variable is created.
23105 */
23106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023107set_var(
23108 char_u *name,
23109 typval_T *tv,
23110 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111{
Bram Moolenaar33570922005-01-25 22:26:29 +000023112 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023114 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023116 ht = find_var_ht(name, &varname);
23117 if (ht == NULL || *varname == NUL)
23118 {
23119 EMSG2(_(e_illvar), name);
23120 return;
23121 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023122 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023123
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023124 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23125 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023126 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023127
Bram Moolenaar33570922005-01-25 22:26:29 +000023128 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023130 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023131 if (var_check_ro(v->di_flags, name, FALSE)
23132 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023133 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023134
23135 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023136 * Handle setting internal v: variables separately where needed to
23137 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023138 */
23139 if (ht == &vimvarht)
23140 {
23141 if (v->di_tv.v_type == VAR_STRING)
23142 {
23143 vim_free(v->di_tv.vval.v_string);
23144 if (copy || tv->v_type != VAR_STRING)
23145 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23146 else
23147 {
23148 /* Take over the string to avoid an extra alloc/free. */
23149 v->di_tv.vval.v_string = tv->vval.v_string;
23150 tv->vval.v_string = NULL;
23151 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023152 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023153 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023154 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023155 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023156 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023157 if (STRCMP(varname, "searchforward") == 0)
23158 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023159#ifdef FEAT_SEARCH_EXTRA
23160 else if (STRCMP(varname, "hlsearch") == 0)
23161 {
23162 no_hlsearch = !v->di_tv.vval.v_number;
23163 redraw_all_later(SOME_VALID);
23164 }
23165#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023166 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023167 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023168 else if (v->di_tv.v_type != tv->v_type)
23169 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 }
23171
23172 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 }
23174 else /* add a new variable */
23175 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023176 /* Can't add "v:" variable. */
23177 if (ht == &vimvarht)
23178 {
23179 EMSG2(_(e_illvar), name);
23180 return;
23181 }
23182
Bram Moolenaar92124a32005-06-17 22:03:40 +000023183 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023184 if (!valid_varname(varname))
23185 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023186
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023187 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23188 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023189 if (v == NULL)
23190 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023191 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023192 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023194 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 return;
23196 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023197 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023199
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023200 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023201 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023202 else
23203 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023204 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023205 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023206 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208}
23209
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023210/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023211 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023212 * Also give an error message.
23213 */
23214 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023215var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023216{
23217 if (flags & DI_FLAGS_RO)
23218 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023219 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023220 return TRUE;
23221 }
23222 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23223 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023224 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023225 return TRUE;
23226 }
23227 return FALSE;
23228}
23229
23230/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023231 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23232 * Also give an error message.
23233 */
23234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023235var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023236{
23237 if (flags & DI_FLAGS_FIX)
23238 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023239 EMSG2(_("E795: Cannot delete variable %s"),
23240 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023241 return TRUE;
23242 }
23243 return FALSE;
23244}
23245
23246/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023247 * Check if a funcref is assigned to a valid variable name.
23248 * Return TRUE and give an error if not.
23249 */
23250 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023251var_check_func_name(
23252 char_u *name, /* points to start of variable name */
23253 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023254{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023255 /* Allow for w: b: s: and t:. */
23256 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023257 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23258 ? name[2] : name[0]))
23259 {
23260 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23261 name);
23262 return TRUE;
23263 }
23264 /* Don't allow hiding a function. When "v" is not NULL we might be
23265 * assigning another function to the same var, the type is checked
23266 * below. */
23267 if (new_var && function_exists(name))
23268 {
23269 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23270 name);
23271 return TRUE;
23272 }
23273 return FALSE;
23274}
23275
23276/*
23277 * Check if a variable name is valid.
23278 * Return FALSE and give an error if not.
23279 */
23280 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023281valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023282{
23283 char_u *p;
23284
23285 for (p = varname; *p != NUL; ++p)
23286 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23287 && *p != AUTOLOAD_CHAR)
23288 {
23289 EMSG2(_(e_illvar), varname);
23290 return FALSE;
23291 }
23292 return TRUE;
23293}
23294
23295/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023296 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023297 * Also give an error message, using "name" or _("name") when use_gettext is
23298 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023299 */
23300 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023301tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023302{
23303 if (lock & VAR_LOCKED)
23304 {
23305 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023306 name == NULL ? (char_u *)_("Unknown")
23307 : use_gettext ? (char_u *)_(name)
23308 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023309 return TRUE;
23310 }
23311 if (lock & VAR_FIXED)
23312 {
23313 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023314 name == NULL ? (char_u *)_("Unknown")
23315 : use_gettext ? (char_u *)_(name)
23316 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023317 return TRUE;
23318 }
23319 return FALSE;
23320}
23321
23322/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023323 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023324 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023325 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023326 * It is OK for "from" and "to" to point to the same item. This is used to
23327 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023328 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023330copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023331{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023332 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023333 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023334 switch (from->v_type)
23335 {
23336 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023337 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023338 to->vval.v_number = from->vval.v_number;
23339 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023340 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023341#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023342 to->vval.v_float = from->vval.v_float;
23343 break;
23344#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023345 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023346#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023347 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023348 if (to->vval.v_job != NULL)
23349 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023350 break;
23351#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023352 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023353#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023354 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023355 if (to->vval.v_channel != NULL)
23356 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023357 break;
23358#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023359 case VAR_STRING:
23360 case VAR_FUNC:
23361 if (from->vval.v_string == NULL)
23362 to->vval.v_string = NULL;
23363 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023364 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023365 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023366 if (from->v_type == VAR_FUNC)
23367 func_ref(to->vval.v_string);
23368 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023369 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023370 case VAR_PARTIAL:
23371 if (from->vval.v_partial == NULL)
23372 to->vval.v_partial = NULL;
23373 else
23374 {
23375 to->vval.v_partial = from->vval.v_partial;
23376 ++to->vval.v_partial->pt_refcount;
23377 }
23378 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023379 case VAR_LIST:
23380 if (from->vval.v_list == NULL)
23381 to->vval.v_list = NULL;
23382 else
23383 {
23384 to->vval.v_list = from->vval.v_list;
23385 ++to->vval.v_list->lv_refcount;
23386 }
23387 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023388 case VAR_DICT:
23389 if (from->vval.v_dict == NULL)
23390 to->vval.v_dict = NULL;
23391 else
23392 {
23393 to->vval.v_dict = from->vval.v_dict;
23394 ++to->vval.v_dict->dv_refcount;
23395 }
23396 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023397 case VAR_UNKNOWN:
23398 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023399 break;
23400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401}
23402
23403/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023404 * Make a copy of an item.
23405 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023406 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23407 * reference to an already copied list/dict can be used.
23408 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023409 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023410 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023411item_copy(
23412 typval_T *from,
23413 typval_T *to,
23414 int deep,
23415 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023416{
23417 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023418 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023419
Bram Moolenaar33570922005-01-25 22:26:29 +000023420 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023421 {
23422 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023423 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023424 }
23425 ++recurse;
23426
23427 switch (from->v_type)
23428 {
23429 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023430 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023431 case VAR_STRING:
23432 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023433 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023434 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023435 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023436 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023437 copy_tv(from, to);
23438 break;
23439 case VAR_LIST:
23440 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023441 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023442 if (from->vval.v_list == NULL)
23443 to->vval.v_list = NULL;
23444 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23445 {
23446 /* use the copy made earlier */
23447 to->vval.v_list = from->vval.v_list->lv_copylist;
23448 ++to->vval.v_list->lv_refcount;
23449 }
23450 else
23451 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23452 if (to->vval.v_list == NULL)
23453 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023454 break;
23455 case VAR_DICT:
23456 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023457 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023458 if (from->vval.v_dict == NULL)
23459 to->vval.v_dict = NULL;
23460 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23461 {
23462 /* use the copy made earlier */
23463 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23464 ++to->vval.v_dict->dv_refcount;
23465 }
23466 else
23467 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23468 if (to->vval.v_dict == NULL)
23469 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023470 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023471 case VAR_UNKNOWN:
23472 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023473 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023474 }
23475 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023476 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023477}
23478
23479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 * ":echo expr1 ..." print each argument separated with a space, add a
23481 * newline at the end.
23482 * ":echon expr1 ..." print each argument plain.
23483 */
23484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023485ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486{
23487 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023488 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023489 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490 char_u *p;
23491 int needclr = TRUE;
23492 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023493 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494
23495 if (eap->skip)
23496 ++emsg_skip;
23497 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23498 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023499 /* If eval1() causes an error message the text from the command may
23500 * still need to be cleared. E.g., "echo 22,44". */
23501 need_clr_eos = needclr;
23502
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023504 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023505 {
23506 /*
23507 * Report the invalid expression unless the expression evaluation
23508 * has been cancelled due to an aborting error, an interrupt, or an
23509 * exception.
23510 */
23511 if (!aborting())
23512 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023513 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 break;
23515 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023516 need_clr_eos = FALSE;
23517
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518 if (!eap->skip)
23519 {
23520 if (atstart)
23521 {
23522 atstart = FALSE;
23523 /* Call msg_start() after eval1(), evaluating the expression
23524 * may cause a message to appear. */
23525 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023526 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023527 /* Mark the saved text as finishing the line, so that what
23528 * follows is displayed on a new line when scrolling back
23529 * at the more prompt. */
23530 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533 }
23534 else if (eap->cmdidx == CMD_echo)
23535 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023536 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023537 if (p != NULL)
23538 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023540 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023542 if (*p != TAB && needclr)
23543 {
23544 /* remove any text still there from the command */
23545 msg_clr_eos();
23546 needclr = FALSE;
23547 }
23548 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549 }
23550 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023551 {
23552#ifdef FEAT_MBYTE
23553 if (has_mbyte)
23554 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023555 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023556
23557 (void)msg_outtrans_len_attr(p, i, echo_attr);
23558 p += i - 1;
23559 }
23560 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023562 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023565 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023567 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 arg = skipwhite(arg);
23569 }
23570 eap->nextcmd = check_nextcmd(arg);
23571
23572 if (eap->skip)
23573 --emsg_skip;
23574 else
23575 {
23576 /* remove text that may still be there from the command */
23577 if (needclr)
23578 msg_clr_eos();
23579 if (eap->cmdidx == CMD_echo)
23580 msg_end();
23581 }
23582}
23583
23584/*
23585 * ":echohl {name}".
23586 */
23587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023588ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589{
23590 int id;
23591
23592 id = syn_name2id(eap->arg);
23593 if (id == 0)
23594 echo_attr = 0;
23595 else
23596 echo_attr = syn_id2attr(id);
23597}
23598
23599/*
23600 * ":execute expr1 ..." execute the result of an expression.
23601 * ":echomsg expr1 ..." Print a message
23602 * ":echoerr expr1 ..." Print an error
23603 * Each gets spaces around each argument and a newline at the end for
23604 * echo commands
23605 */
23606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023607ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608{
23609 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023610 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611 int ret = OK;
23612 char_u *p;
23613 garray_T ga;
23614 int len;
23615 int save_did_emsg;
23616
23617 ga_init2(&ga, 1, 80);
23618
23619 if (eap->skip)
23620 ++emsg_skip;
23621 while (*arg != NUL && *arg != '|' && *arg != '\n')
23622 {
23623 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023624 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 {
23626 /*
23627 * Report the invalid expression unless the expression evaluation
23628 * has been cancelled due to an aborting error, an interrupt, or an
23629 * exception.
23630 */
23631 if (!aborting())
23632 EMSG2(_(e_invexpr2), p);
23633 ret = FAIL;
23634 break;
23635 }
23636
23637 if (!eap->skip)
23638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023639 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 len = (int)STRLEN(p);
23641 if (ga_grow(&ga, len + 2) == FAIL)
23642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023643 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644 ret = FAIL;
23645 break;
23646 }
23647 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 ga.ga_len += len;
23651 }
23652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 arg = skipwhite(arg);
23655 }
23656
23657 if (ret != FAIL && ga.ga_data != NULL)
23658 {
23659 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023660 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023662 out_flush();
23663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 else if (eap->cmdidx == CMD_echoerr)
23665 {
23666 /* We don't want to abort following commands, restore did_emsg. */
23667 save_did_emsg = did_emsg;
23668 EMSG((char_u *)ga.ga_data);
23669 if (!force_abort)
23670 did_emsg = save_did_emsg;
23671 }
23672 else if (eap->cmdidx == CMD_execute)
23673 do_cmdline((char_u *)ga.ga_data,
23674 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23675 }
23676
23677 ga_clear(&ga);
23678
23679 if (eap->skip)
23680 --emsg_skip;
23681
23682 eap->nextcmd = check_nextcmd(arg);
23683}
23684
23685/*
23686 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23687 * "arg" points to the "&" or '+' when called, to "option" when returning.
23688 * Returns NULL when no option name found. Otherwise pointer to the char
23689 * after the option name.
23690 */
23691 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023692find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693{
23694 char_u *p = *arg;
23695
23696 ++p;
23697 if (*p == 'g' && p[1] == ':')
23698 {
23699 *opt_flags = OPT_GLOBAL;
23700 p += 2;
23701 }
23702 else if (*p == 'l' && p[1] == ':')
23703 {
23704 *opt_flags = OPT_LOCAL;
23705 p += 2;
23706 }
23707 else
23708 *opt_flags = 0;
23709
23710 if (!ASCII_ISALPHA(*p))
23711 return NULL;
23712 *arg = p;
23713
23714 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23715 p += 4; /* termcap option */
23716 else
23717 while (ASCII_ISALPHA(*p))
23718 ++p;
23719 return p;
23720}
23721
23722/*
23723 * ":function"
23724 */
23725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023726ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023727{
23728 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023729 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730 int j;
23731 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023733 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 char_u *name = NULL;
23735 char_u *p;
23736 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023737 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 garray_T newargs;
23739 garray_T newlines;
23740 int varargs = FALSE;
23741 int mustend = FALSE;
23742 int flags = 0;
23743 ufunc_T *fp;
23744 int indent;
23745 int nesting;
23746 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023747 dictitem_T *v;
23748 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023749 static int func_nr = 0; /* number for nameless function */
23750 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023751 hashtab_T *ht;
23752 int todo;
23753 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023754 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755
23756 /*
23757 * ":function" without argument: list functions.
23758 */
23759 if (ends_excmd(*eap->arg))
23760 {
23761 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023762 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023763 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023764 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023765 {
23766 if (!HASHITEM_EMPTY(hi))
23767 {
23768 --todo;
23769 fp = HI2UF(hi);
23770 if (!isdigit(*fp->uf_name))
23771 list_func_head(fp, FALSE);
23772 }
23773 }
23774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023775 eap->nextcmd = check_nextcmd(eap->arg);
23776 return;
23777 }
23778
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023779 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023780 * ":function /pat": list functions matching pattern.
23781 */
23782 if (*eap->arg == '/')
23783 {
23784 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23785 if (!eap->skip)
23786 {
23787 regmatch_T regmatch;
23788
23789 c = *p;
23790 *p = NUL;
23791 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23792 *p = c;
23793 if (regmatch.regprog != NULL)
23794 {
23795 regmatch.rm_ic = p_ic;
23796
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023797 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023798 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23799 {
23800 if (!HASHITEM_EMPTY(hi))
23801 {
23802 --todo;
23803 fp = HI2UF(hi);
23804 if (!isdigit(*fp->uf_name)
23805 && vim_regexec(&regmatch, fp->uf_name, 0))
23806 list_func_head(fp, FALSE);
23807 }
23808 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023809 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023810 }
23811 }
23812 if (*p == '/')
23813 ++p;
23814 eap->nextcmd = check_nextcmd(p);
23815 return;
23816 }
23817
23818 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023819 * Get the function name. There are these situations:
23820 * func normal function name
23821 * "name" == func, "fudi.fd_dict" == NULL
23822 * dict.func new dictionary entry
23823 * "name" == NULL, "fudi.fd_dict" set,
23824 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23825 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023826 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023827 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23828 * dict.func existing dict entry that's not a Funcref
23829 * "name" == NULL, "fudi.fd_dict" set,
23830 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023831 * s:func script-local function name
23832 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023835 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023836 paren = (vim_strchr(p, '(') != NULL);
23837 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838 {
23839 /*
23840 * Return on an invalid expression in braces, unless the expression
23841 * evaluation has been cancelled due to an aborting error, an
23842 * interrupt, or an exception.
23843 */
23844 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023845 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023846 if (!eap->skip && fudi.fd_newkey != NULL)
23847 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023848 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 else
23852 eap->skip = TRUE;
23853 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023854
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 /* An error in a function call during evaluation of an expression in magic
23856 * braces should not cause the function not to be defined. */
23857 saved_did_emsg = did_emsg;
23858 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023859
23860 /*
23861 * ":function func" with only function name: list function.
23862 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023863 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 {
23865 if (!ends_excmd(*skipwhite(p)))
23866 {
23867 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023868 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 }
23870 eap->nextcmd = check_nextcmd(p);
23871 if (eap->nextcmd != NULL)
23872 *p = NUL;
23873 if (!eap->skip && !got_int)
23874 {
23875 fp = find_func(name);
23876 if (fp != NULL)
23877 {
23878 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023879 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023881 if (FUNCLINE(fp, j) == NULL)
23882 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 msg_putchar('\n');
23884 msg_outnum((long)(j + 1));
23885 if (j < 9)
23886 msg_putchar(' ');
23887 if (j < 99)
23888 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023889 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 out_flush(); /* show a line at a time */
23891 ui_breakcheck();
23892 }
23893 if (!got_int)
23894 {
23895 msg_putchar('\n');
23896 msg_puts((char_u *)" endfunction");
23897 }
23898 }
23899 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023900 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023902 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 }
23904
23905 /*
23906 * ":function name(arg1, arg2)" Define function.
23907 */
23908 p = skipwhite(p);
23909 if (*p != '(')
23910 {
23911 if (!eap->skip)
23912 {
23913 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023914 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915 }
23916 /* attempt to continue by skipping some text */
23917 if (vim_strchr(p, '(') != NULL)
23918 p = vim_strchr(p, '(');
23919 }
23920 p = skipwhite(p + 1);
23921
23922 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23923 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23924
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023925 if (!eap->skip)
23926 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023927 /* Check the name of the function. Unless it's a dictionary function
23928 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023929 if (name != NULL)
23930 arg = name;
23931 else
23932 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023933 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023934 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23935 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023936 {
23937 if (*arg == K_SPECIAL)
23938 j = 3;
23939 else
23940 j = 0;
23941 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23942 : eval_isnamec(arg[j])))
23943 ++j;
23944 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023945 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023946 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023947 /* Disallow using the g: dict. */
23948 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23949 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023950 }
23951
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 /*
23953 * Isolate the arguments: "arg1, arg2, ...)"
23954 */
23955 while (*p != ')')
23956 {
23957 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23958 {
23959 varargs = TRUE;
23960 p += 3;
23961 mustend = TRUE;
23962 }
23963 else
23964 {
23965 arg = p;
23966 while (ASCII_ISALNUM(*p) || *p == '_')
23967 ++p;
23968 if (arg == p || isdigit(*arg)
23969 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23970 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23971 {
23972 if (!eap->skip)
23973 EMSG2(_("E125: Illegal argument: %s"), arg);
23974 break;
23975 }
23976 if (ga_grow(&newargs, 1) == FAIL)
23977 goto erret;
23978 c = *p;
23979 *p = NUL;
23980 arg = vim_strsave(arg);
23981 if (arg == NULL)
23982 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023983
23984 /* Check for duplicate argument name. */
23985 for (i = 0; i < newargs.ga_len; ++i)
23986 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23987 {
23988 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023989 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023990 goto erret;
23991 }
23992
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23994 *p = c;
23995 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996 if (*p == ',')
23997 ++p;
23998 else
23999 mustend = TRUE;
24000 }
24001 p = skipwhite(p);
24002 if (mustend && *p != ')')
24003 {
24004 if (!eap->skip)
24005 EMSG2(_(e_invarg2), eap->arg);
24006 break;
24007 }
24008 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024009 if (*p != ')')
24010 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024011 ++p; /* skip the ')' */
24012
Bram Moolenaare9a41262005-01-15 22:18:47 +000024013 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 for (;;)
24015 {
24016 p = skipwhite(p);
24017 if (STRNCMP(p, "range", 5) == 0)
24018 {
24019 flags |= FC_RANGE;
24020 p += 5;
24021 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024022 else if (STRNCMP(p, "dict", 4) == 0)
24023 {
24024 flags |= FC_DICT;
24025 p += 4;
24026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 else if (STRNCMP(p, "abort", 5) == 0)
24028 {
24029 flags |= FC_ABORT;
24030 p += 5;
24031 }
24032 else
24033 break;
24034 }
24035
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024036 /* When there is a line break use what follows for the function body.
24037 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24038 if (*p == '\n')
24039 line_arg = p + 1;
24040 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041 EMSG(_(e_trailing));
24042
24043 /*
24044 * Read the body of the function, until ":endfunction" is found.
24045 */
24046 if (KeyTyped)
24047 {
24048 /* Check if the function already exists, don't let the user type the
24049 * whole function before telling him it doesn't work! For a script we
24050 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024051 if (!eap->skip && !eap->forceit)
24052 {
24053 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24054 EMSG(_(e_funcdict));
24055 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024056 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024059 if (!eap->skip && did_emsg)
24060 goto erret;
24061
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062 msg_putchar('\n'); /* don't overwrite the function name */
24063 cmdline_row = msg_row;
24064 }
24065
24066 indent = 2;
24067 nesting = 0;
24068 for (;;)
24069 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024070 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024071 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024072 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024073 saved_wait_return = FALSE;
24074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024076 sourcing_lnum_off = sourcing_lnum;
24077
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024078 if (line_arg != NULL)
24079 {
24080 /* Use eap->arg, split up in parts by line breaks. */
24081 theline = line_arg;
24082 p = vim_strchr(theline, '\n');
24083 if (p == NULL)
24084 line_arg += STRLEN(line_arg);
24085 else
24086 {
24087 *p = NUL;
24088 line_arg = p + 1;
24089 }
24090 }
24091 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024092 theline = getcmdline(':', 0L, indent);
24093 else
24094 theline = eap->getline(':', eap->cookie, indent);
24095 if (KeyTyped)
24096 lines_left = Rows - 1;
24097 if (theline == NULL)
24098 {
24099 EMSG(_("E126: Missing :endfunction"));
24100 goto erret;
24101 }
24102
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024103 /* Detect line continuation: sourcing_lnum increased more than one. */
24104 if (sourcing_lnum > sourcing_lnum_off + 1)
24105 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24106 else
24107 sourcing_lnum_off = 0;
24108
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109 if (skip_until != NULL)
24110 {
24111 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24112 * don't check for ":endfunc". */
24113 if (STRCMP(theline, skip_until) == 0)
24114 {
24115 vim_free(skip_until);
24116 skip_until = NULL;
24117 }
24118 }
24119 else
24120 {
24121 /* skip ':' and blanks*/
24122 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24123 ;
24124
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024125 /* Check for "endfunction". */
24126 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024127 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024128 if (line_arg == NULL)
24129 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 break;
24131 }
24132
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024133 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 * at "end". */
24135 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24136 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024137 else if (STRNCMP(p, "if", 2) == 0
24138 || STRNCMP(p, "wh", 2) == 0
24139 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 || STRNCMP(p, "try", 3) == 0)
24141 indent += 2;
24142
24143 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024144 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024146 if (*p == '!')
24147 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024149 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024150 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024152 ++nesting;
24153 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 }
24155 }
24156
24157 /* Check for ":append" or ":insert". */
24158 p = skip_range(p, NULL);
24159 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24160 || (p[0] == 'i'
24161 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24162 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24163 skip_until = vim_strsave((char_u *)".");
24164
24165 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24166 arg = skipwhite(skiptowhite(p));
24167 if (arg[0] == '<' && arg[1] =='<'
24168 && ((p[0] == 'p' && p[1] == 'y'
24169 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24170 || (p[0] == 'p' && p[1] == 'e'
24171 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24172 || (p[0] == 't' && p[1] == 'c'
24173 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024174 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24175 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24177 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024178 || (p[0] == 'm' && p[1] == 'z'
24179 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024180 ))
24181 {
24182 /* ":python <<" continues until a dot, like ":append" */
24183 p = skipwhite(arg + 2);
24184 if (*p == NUL)
24185 skip_until = vim_strsave((char_u *)".");
24186 else
24187 skip_until = vim_strsave(p);
24188 }
24189 }
24190
24191 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024192 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024193 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024194 if (line_arg == NULL)
24195 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024197 }
24198
24199 /* Copy the line to newly allocated memory. get_one_sourceline()
24200 * allocates 250 bytes per line, this saves 80% on average. The cost
24201 * is an extra alloc/free. */
24202 p = vim_strsave(theline);
24203 if (p != NULL)
24204 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024205 if (line_arg == NULL)
24206 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024207 theline = p;
24208 }
24209
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024210 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24211
24212 /* Add NULL lines for continuation lines, so that the line count is
24213 * equal to the index in the growarray. */
24214 while (sourcing_lnum_off-- > 0)
24215 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024216
24217 /* Check for end of eap->arg. */
24218 if (line_arg != NULL && *line_arg == NUL)
24219 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220 }
24221
24222 /* Don't define the function when skipping commands or when an error was
24223 * detected. */
24224 if (eap->skip || did_emsg)
24225 goto erret;
24226
24227 /*
24228 * If there are no errors, add the function
24229 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024230 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024231 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024232 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024233 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024234 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024235 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024236 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024237 goto erret;
24238 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024239
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024240 fp = find_func(name);
24241 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024243 if (!eap->forceit)
24244 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024245 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024246 goto erret;
24247 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024248 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024249 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024250 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024251 name);
24252 goto erret;
24253 }
24254 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024255 ga_clear_strings(&(fp->uf_args));
24256 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024257 vim_free(name);
24258 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260 }
24261 else
24262 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024263 char numbuf[20];
24264
24265 fp = NULL;
24266 if (fudi.fd_newkey == NULL && !eap->forceit)
24267 {
24268 EMSG(_(e_funcdict));
24269 goto erret;
24270 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024271 if (fudi.fd_di == NULL)
24272 {
24273 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024274 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024275 goto erret;
24276 }
24277 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024278 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024279 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024280
24281 /* Give the function a sequential number. Can only be used with a
24282 * Funcref! */
24283 vim_free(name);
24284 sprintf(numbuf, "%d", ++func_nr);
24285 name = vim_strsave((char_u *)numbuf);
24286 if (name == NULL)
24287 goto erret;
24288 }
24289
24290 if (fp == NULL)
24291 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024292 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024293 {
24294 int slen, plen;
24295 char_u *scriptname;
24296
24297 /* Check that the autoload name matches the script name. */
24298 j = FAIL;
24299 if (sourcing_name != NULL)
24300 {
24301 scriptname = autoload_name(name);
24302 if (scriptname != NULL)
24303 {
24304 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024305 plen = (int)STRLEN(p);
24306 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024307 if (slen > plen && fnamecmp(p,
24308 sourcing_name + slen - plen) == 0)
24309 j = OK;
24310 vim_free(scriptname);
24311 }
24312 }
24313 if (j == FAIL)
24314 {
24315 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24316 goto erret;
24317 }
24318 }
24319
24320 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 if (fp == NULL)
24322 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024323
24324 if (fudi.fd_dict != NULL)
24325 {
24326 if (fudi.fd_di == NULL)
24327 {
24328 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024329 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024330 if (fudi.fd_di == NULL)
24331 {
24332 vim_free(fp);
24333 goto erret;
24334 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024335 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24336 {
24337 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024338 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024339 goto erret;
24340 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024341 }
24342 else
24343 /* overwrite existing dict entry */
24344 clear_tv(&fudi.fd_di->di_tv);
24345 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024346 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024347 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024348 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024349
24350 /* behave like "dict" was used */
24351 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024352 }
24353
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024355 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024356 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24357 {
24358 vim_free(fp);
24359 goto erret;
24360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024362 fp->uf_args = newargs;
24363 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024364#ifdef FEAT_PROFILE
24365 fp->uf_tml_count = NULL;
24366 fp->uf_tml_total = NULL;
24367 fp->uf_tml_self = NULL;
24368 fp->uf_profiling = FALSE;
24369 if (prof_def_func())
24370 func_do_profile(fp);
24371#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024372 fp->uf_varargs = varargs;
24373 fp->uf_flags = flags;
24374 fp->uf_calls = 0;
24375 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024376 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377
24378erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379 ga_clear_strings(&newargs);
24380 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024381ret_free:
24382 vim_free(skip_until);
24383 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024384 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024385 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024386 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387}
24388
24389/*
24390 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024391 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024393 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024394 * TFN_INT: internal function name OK
24395 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024396 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397 * Advances "pp" to just after the function name (if no error).
24398 */
24399 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024400trans_function_name(
24401 char_u **pp,
24402 int skip, /* only find the end, don't evaluate */
24403 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024404 funcdict_T *fdp, /* return: info about dictionary used */
24405 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024407 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024408 char_u *start;
24409 char_u *end;
24410 int lead;
24411 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024413 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024414
24415 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024416 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024418
24419 /* Check for hard coded <SNR>: already translated function ID (from a user
24420 * command). */
24421 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24422 && (*pp)[2] == (int)KE_SNR)
24423 {
24424 *pp += 3;
24425 len = get_id_len(pp) + 3;
24426 return vim_strnsave(start, len);
24427 }
24428
24429 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24430 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024431 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024432 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024433 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024434
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024435 /* Note that TFN_ flags use the same values as GLV_ flags. */
24436 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024437 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024438 if (end == start)
24439 {
24440 if (!skip)
24441 EMSG(_("E129: Function name required"));
24442 goto theend;
24443 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024444 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024445 {
24446 /*
24447 * Report an invalid expression in braces, unless the expression
24448 * evaluation has been cancelled due to an aborting error, an
24449 * interrupt, or an exception.
24450 */
24451 if (!aborting())
24452 {
24453 if (end != NULL)
24454 EMSG2(_(e_invarg2), start);
24455 }
24456 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024457 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024458 goto theend;
24459 }
24460
24461 if (lv.ll_tv != NULL)
24462 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024463 if (fdp != NULL)
24464 {
24465 fdp->fd_dict = lv.ll_dict;
24466 fdp->fd_newkey = lv.ll_newkey;
24467 lv.ll_newkey = NULL;
24468 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024469 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024470 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24471 {
24472 name = vim_strsave(lv.ll_tv->vval.v_string);
24473 *pp = end;
24474 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024475 else if (lv.ll_tv->v_type == VAR_PARTIAL
24476 && lv.ll_tv->vval.v_partial != NULL)
24477 {
24478 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24479 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024480 if (partial != NULL)
24481 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024482 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024483 else
24484 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024485 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24486 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024487 EMSG(_(e_funcref));
24488 else
24489 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024490 name = NULL;
24491 }
24492 goto theend;
24493 }
24494
24495 if (lv.ll_name == NULL)
24496 {
24497 /* Error found, but continue after the function name. */
24498 *pp = end;
24499 goto theend;
24500 }
24501
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024502 /* Check if the name is a Funcref. If so, use the value. */
24503 if (lv.ll_exp_name != NULL)
24504 {
24505 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024506 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024507 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024508 if (name == lv.ll_exp_name)
24509 name = NULL;
24510 }
24511 else
24512 {
24513 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024514 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024515 if (name == *pp)
24516 name = NULL;
24517 }
24518 if (name != NULL)
24519 {
24520 name = vim_strsave(name);
24521 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024522 if (STRNCMP(name, "<SNR>", 5) == 0)
24523 {
24524 /* Change "<SNR>" to the byte sequence. */
24525 name[0] = K_SPECIAL;
24526 name[1] = KS_EXTRA;
24527 name[2] = (int)KE_SNR;
24528 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24529 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024530 goto theend;
24531 }
24532
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024533 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024534 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024535 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024536 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24537 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24538 {
24539 /* When there was "s:" already or the name expanded to get a
24540 * leading "s:" then remove it. */
24541 lv.ll_name += 2;
24542 len -= 2;
24543 lead = 2;
24544 }
24545 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024546 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024547 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024548 /* skip over "s:" and "g:" */
24549 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024550 lv.ll_name += 2;
24551 len = (int)(end - lv.ll_name);
24552 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024553
24554 /*
24555 * Copy the function name to allocated memory.
24556 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24557 * Accept <SNR>123_name() outside a script.
24558 */
24559 if (skip)
24560 lead = 0; /* do nothing */
24561 else if (lead > 0)
24562 {
24563 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024564 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24565 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024566 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024567 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024568 if (current_SID <= 0)
24569 {
24570 EMSG(_(e_usingsid));
24571 goto theend;
24572 }
24573 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24574 lead += (int)STRLEN(sid_buf);
24575 }
24576 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024577 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024578 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024579 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024580 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024581 goto theend;
24582 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024583 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024584 {
24585 char_u *cp = vim_strchr(lv.ll_name, ':');
24586
24587 if (cp != NULL && cp < end)
24588 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024589 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024590 goto theend;
24591 }
24592 }
24593
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024594 name = alloc((unsigned)(len + lead + 1));
24595 if (name != NULL)
24596 {
24597 if (lead > 0)
24598 {
24599 name[0] = K_SPECIAL;
24600 name[1] = KS_EXTRA;
24601 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024602 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024603 STRCPY(name + 3, sid_buf);
24604 }
24605 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024606 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024607 }
24608 *pp = end;
24609
24610theend:
24611 clear_lval(&lv);
24612 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613}
24614
24615/*
24616 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24617 * Return 2 if "p" starts with "s:".
24618 * Return 0 otherwise.
24619 */
24620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024621eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024623 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24624 * the standard library function. */
24625 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24626 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024627 return 5;
24628 if (p[0] == 's' && p[1] == ':')
24629 return 2;
24630 return 0;
24631}
24632
24633/*
24634 * Return TRUE if "p" starts with "<SID>" or "s:".
24635 * Only works if eval_fname_script() returned non-zero for "p"!
24636 */
24637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024638eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639{
24640 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24641}
24642
24643/*
24644 * List the head of the function: "name(arg1, arg2)".
24645 */
24646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024647list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024648{
24649 int j;
24650
24651 msg_start();
24652 if (indent)
24653 MSG_PUTS(" ");
24654 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024655 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024656 {
24657 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024658 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 }
24660 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024661 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024662 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024663 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664 {
24665 if (j)
24666 MSG_PUTS(", ");
24667 msg_puts(FUNCARG(fp, j));
24668 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024669 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 {
24671 if (j)
24672 MSG_PUTS(", ");
24673 MSG_PUTS("...");
24674 }
24675 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024676 if (fp->uf_flags & FC_ABORT)
24677 MSG_PUTS(" abort");
24678 if (fp->uf_flags & FC_RANGE)
24679 MSG_PUTS(" range");
24680 if (fp->uf_flags & FC_DICT)
24681 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024682 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024683 if (p_verbose > 0)
24684 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685}
24686
24687/*
24688 * Find a function by name, return pointer to it in ufuncs.
24689 * Return NULL for unknown function.
24690 */
24691 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024692find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024694 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024695
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024696 hi = hash_find(&func_hashtab, name);
24697 if (!HASHITEM_EMPTY(hi))
24698 return HI2UF(hi);
24699 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700}
24701
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024702#if defined(EXITFREE) || defined(PROTO)
24703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024704free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024705{
24706 hashitem_T *hi;
24707
24708 /* Need to start all over every time, because func_free() may change the
24709 * hash table. */
24710 while (func_hashtab.ht_used > 0)
24711 for (hi = func_hashtab.ht_array; ; ++hi)
24712 if (!HASHITEM_EMPTY(hi))
24713 {
24714 func_free(HI2UF(hi));
24715 break;
24716 }
24717}
24718#endif
24719
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024720 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024721translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024722{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024723 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024724 return find_internal_func(name) >= 0;
24725 return find_func(name) != NULL;
24726}
24727
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024728/*
24729 * Return TRUE if a function "name" exists.
24730 */
24731 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024732function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024733{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024734 char_u *nm = name;
24735 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024736 int n = FALSE;
24737
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024738 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024739 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024740 nm = skipwhite(nm);
24741
24742 /* Only accept "funcname", "funcname ", "funcname (..." and
24743 * "funcname(...", not "funcname!...". */
24744 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024745 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024746 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024747 return n;
24748}
24749
Bram Moolenaara1544c02013-05-30 12:35:52 +020024750 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024751get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024752{
24753 char_u *nm = name;
24754 char_u *p;
24755
Bram Moolenaar65639032016-03-16 21:40:30 +010024756 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024757
24758 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024759 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024760 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024761
Bram Moolenaara1544c02013-05-30 12:35:52 +020024762 vim_free(p);
24763 return NULL;
24764}
24765
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024766/*
24767 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024768 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24769 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024770 */
24771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024772builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024773{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024774 char_u *p;
24775
24776 if (!ASCII_ISLOWER(name[0]))
24777 return FALSE;
24778 p = vim_strchr(name, AUTOLOAD_CHAR);
24779 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024780}
24781
Bram Moolenaar05159a02005-02-26 23:04:13 +000024782#if defined(FEAT_PROFILE) || defined(PROTO)
24783/*
24784 * Start profiling function "fp".
24785 */
24786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024787func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024788{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024789 int len = fp->uf_lines.ga_len;
24790
24791 if (len == 0)
24792 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024793 fp->uf_tm_count = 0;
24794 profile_zero(&fp->uf_tm_self);
24795 profile_zero(&fp->uf_tm_total);
24796 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024797 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024798 if (fp->uf_tml_total == NULL)
24799 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024800 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024801 if (fp->uf_tml_self == NULL)
24802 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024803 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024804 fp->uf_tml_idx = -1;
24805 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24806 || fp->uf_tml_self == NULL)
24807 return; /* out of memory */
24808
24809 fp->uf_profiling = TRUE;
24810}
24811
24812/*
24813 * Dump the profiling results for all functions in file "fd".
24814 */
24815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024816func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024817{
24818 hashitem_T *hi;
24819 int todo;
24820 ufunc_T *fp;
24821 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024822 ufunc_T **sorttab;
24823 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024824
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024825 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024826 if (todo == 0)
24827 return; /* nothing to dump */
24828
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024829 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024830
Bram Moolenaar05159a02005-02-26 23:04:13 +000024831 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24832 {
24833 if (!HASHITEM_EMPTY(hi))
24834 {
24835 --todo;
24836 fp = HI2UF(hi);
24837 if (fp->uf_profiling)
24838 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024839 if (sorttab != NULL)
24840 sorttab[st_len++] = fp;
24841
Bram Moolenaar05159a02005-02-26 23:04:13 +000024842 if (fp->uf_name[0] == K_SPECIAL)
24843 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24844 else
24845 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24846 if (fp->uf_tm_count == 1)
24847 fprintf(fd, "Called 1 time\n");
24848 else
24849 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24850 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24851 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24852 fprintf(fd, "\n");
24853 fprintf(fd, "count total (s) self (s)\n");
24854
24855 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24856 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024857 if (FUNCLINE(fp, i) == NULL)
24858 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024859 prof_func_line(fd, fp->uf_tml_count[i],
24860 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024861 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24862 }
24863 fprintf(fd, "\n");
24864 }
24865 }
24866 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024867
24868 if (sorttab != NULL && st_len > 0)
24869 {
24870 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24871 prof_total_cmp);
24872 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24873 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24874 prof_self_cmp);
24875 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24876 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024877
24878 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024879}
Bram Moolenaar73830342005-02-28 22:48:19 +000024880
24881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024882prof_sort_list(
24883 FILE *fd,
24884 ufunc_T **sorttab,
24885 int st_len,
24886 char *title,
24887 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024888{
24889 int i;
24890 ufunc_T *fp;
24891
24892 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24893 fprintf(fd, "count total (s) self (s) function\n");
24894 for (i = 0; i < 20 && i < st_len; ++i)
24895 {
24896 fp = sorttab[i];
24897 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24898 prefer_self);
24899 if (fp->uf_name[0] == K_SPECIAL)
24900 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24901 else
24902 fprintf(fd, " %s()\n", fp->uf_name);
24903 }
24904 fprintf(fd, "\n");
24905}
24906
24907/*
24908 * Print the count and times for one function or function line.
24909 */
24910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024911prof_func_line(
24912 FILE *fd,
24913 int count,
24914 proftime_T *total,
24915 proftime_T *self,
24916 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024917{
24918 if (count > 0)
24919 {
24920 fprintf(fd, "%5d ", count);
24921 if (prefer_self && profile_equal(total, self))
24922 fprintf(fd, " ");
24923 else
24924 fprintf(fd, "%s ", profile_msg(total));
24925 if (!prefer_self && profile_equal(total, self))
24926 fprintf(fd, " ");
24927 else
24928 fprintf(fd, "%s ", profile_msg(self));
24929 }
24930 else
24931 fprintf(fd, " ");
24932}
24933
24934/*
24935 * Compare function for total time sorting.
24936 */
24937 static int
24938#ifdef __BORLANDC__
24939_RTLENTRYF
24940#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024941prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024942{
24943 ufunc_T *p1, *p2;
24944
24945 p1 = *(ufunc_T **)s1;
24946 p2 = *(ufunc_T **)s2;
24947 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24948}
24949
24950/*
24951 * Compare function for self time sorting.
24952 */
24953 static int
24954#ifdef __BORLANDC__
24955_RTLENTRYF
24956#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024957prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024958{
24959 ufunc_T *p1, *p2;
24960
24961 p1 = *(ufunc_T **)s1;
24962 p2 = *(ufunc_T **)s2;
24963 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24964}
24965
Bram Moolenaar05159a02005-02-26 23:04:13 +000024966#endif
24967
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024968/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024969 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024970 * Return TRUE if a package was loaded.
24971 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024973script_autoload(
24974 char_u *name,
24975 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024976{
24977 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024978 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024979 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024980 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024981
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024982 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024983 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024984 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024985 return FALSE;
24986
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024987 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024988
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024989 /* Find the name in the list of previously loaded package names. Skip
24990 * "autoload/", it's always the same. */
24991 for (i = 0; i < ga_loaded.ga_len; ++i)
24992 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24993 break;
24994 if (!reload && i < ga_loaded.ga_len)
24995 ret = FALSE; /* was loaded already */
24996 else
24997 {
24998 /* Remember the name if it wasn't loaded already. */
24999 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25000 {
25001 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25002 tofree = NULL;
25003 }
25004
25005 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025006 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025007 ret = TRUE;
25008 }
25009
25010 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025011 return ret;
25012}
25013
25014/*
25015 * Return the autoload script name for a function or variable name.
25016 * Returns NULL when out of memory.
25017 */
25018 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025019autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025020{
25021 char_u *p;
25022 char_u *scriptname;
25023
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025024 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025025 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25026 if (scriptname == NULL)
25027 return FALSE;
25028 STRCPY(scriptname, "autoload/");
25029 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025030 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025031 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025032 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025033 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025034 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025035}
25036
Bram Moolenaar071d4272004-06-13 20:20:40 +000025037#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25038
25039/*
25040 * Function given to ExpandGeneric() to obtain the list of user defined
25041 * function names.
25042 */
25043 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025044get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025045{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025046 static long_u done;
25047 static hashitem_T *hi;
25048 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049
25050 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025051 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025052 done = 0;
25053 hi = func_hashtab.ht_array;
25054 }
25055 if (done < func_hashtab.ht_used)
25056 {
25057 if (done++ > 0)
25058 ++hi;
25059 while (HASHITEM_EMPTY(hi))
25060 ++hi;
25061 fp = HI2UF(hi);
25062
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025063 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025064 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025065
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025066 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25067 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068
25069 cat_func_name(IObuff, fp);
25070 if (xp->xp_context != EXPAND_USER_FUNC)
25071 {
25072 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025073 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074 STRCAT(IObuff, ")");
25075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076 return IObuff;
25077 }
25078 return NULL;
25079}
25080
25081#endif /* FEAT_CMDL_COMPL */
25082
25083/*
25084 * Copy the function name of "fp" to buffer "buf".
25085 * "buf" must be able to hold the function name plus three bytes.
25086 * Takes care of script-local function names.
25087 */
25088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025089cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025090{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025091 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092 {
25093 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025094 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025095 }
25096 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025097 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025098}
25099
25100/*
25101 * ":delfunction {name}"
25102 */
25103 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025104ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025105{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025106 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025107 char_u *p;
25108 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025109 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110
25111 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025112 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025113 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025114 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025115 {
25116 if (fudi.fd_dict != NULL && !eap->skip)
25117 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025118 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120 if (!ends_excmd(*skipwhite(p)))
25121 {
25122 vim_free(name);
25123 EMSG(_(e_trailing));
25124 return;
25125 }
25126 eap->nextcmd = check_nextcmd(p);
25127 if (eap->nextcmd != NULL)
25128 *p = NUL;
25129
25130 if (!eap->skip)
25131 fp = find_func(name);
25132 vim_free(name);
25133
25134 if (!eap->skip)
25135 {
25136 if (fp == NULL)
25137 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025138 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139 return;
25140 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025141 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142 {
25143 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25144 return;
25145 }
25146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025147 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025148 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025149 /* Delete the dict item that refers to the function, it will
25150 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025151 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025152 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025153 else
25154 func_free(fp);
25155 }
25156}
25157
25158/*
25159 * Free a function and remove it from the list of functions.
25160 */
25161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025162func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025163{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025164 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025165
25166 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025167 ga_clear_strings(&(fp->uf_args));
25168 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025169#ifdef FEAT_PROFILE
25170 vim_free(fp->uf_tml_count);
25171 vim_free(fp->uf_tml_total);
25172 vim_free(fp->uf_tml_self);
25173#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025174
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025175 /* remove the function from the function hashtable */
25176 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25177 if (HASHITEM_EMPTY(hi))
25178 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025179 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025180 hash_remove(&func_hashtab, hi);
25181
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025182 vim_free(fp);
25183}
25184
25185/*
25186 * Unreference a Function: decrement the reference count and free it when it
25187 * becomes zero. Only for numbered functions.
25188 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025190func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025191{
25192 ufunc_T *fp;
25193
25194 if (name != NULL && isdigit(*name))
25195 {
25196 fp = find_func(name);
25197 if (fp == NULL)
25198 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025199 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025200 {
25201 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025202 * when "uf_calls" becomes zero. */
25203 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025204 func_free(fp);
25205 }
25206 }
25207}
25208
25209/*
25210 * Count a reference to a Function.
25211 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025213func_ref(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_ref()");
25222 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025223 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025224 }
25225}
25226
25227/*
25228 * Call a user function.
25229 */
25230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025231call_user_func(
25232 ufunc_T *fp, /* pointer to function */
25233 int argcount, /* nr of args */
25234 typval_T *argvars, /* arguments */
25235 typval_T *rettv, /* return value */
25236 linenr_T firstline, /* first line of range */
25237 linenr_T lastline, /* last line of range */
25238 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239{
Bram Moolenaar33570922005-01-25 22:26:29 +000025240 char_u *save_sourcing_name;
25241 linenr_T save_sourcing_lnum;
25242 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025243 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025244 int save_did_emsg;
25245 static int depth = 0;
25246 dictitem_T *v;
25247 int fixvar_idx = 0; /* index in fixvar[] */
25248 int i;
25249 int ai;
25250 char_u numbuf[NUMBUFLEN];
25251 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025252 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025253#ifdef FEAT_PROFILE
25254 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025255 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025257
25258 /* If depth of calling is getting too high, don't execute the function */
25259 if (depth >= p_mfd)
25260 {
25261 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025262 rettv->v_type = VAR_NUMBER;
25263 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 return;
25265 }
25266 ++depth;
25267
25268 line_breakcheck(); /* check for CTRL-C hit */
25269
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025270 fc = (funccall_T *)alloc(sizeof(funccall_T));
25271 fc->caller = current_funccal;
25272 current_funccal = fc;
25273 fc->func = fp;
25274 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025275 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025276 fc->linenr = 0;
25277 fc->returned = FALSE;
25278 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025280 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25281 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282
Bram Moolenaar33570922005-01-25 22:26:29 +000025283 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025284 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025285 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25286 * each argument variable and saves a lot of time.
25287 */
25288 /*
25289 * Init l: variables.
25290 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025291 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025292 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025293 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025294 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25295 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025296 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025297 name = v->di_key;
25298 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025299 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025300 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025301 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025302 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025303 v->di_tv.vval.v_dict = selfdict;
25304 ++selfdict->dv_refcount;
25305 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025306
Bram Moolenaar33570922005-01-25 22:26:29 +000025307 /*
25308 * Init a: variables.
25309 * Set a:0 to "argcount".
25310 * Set a:000 to a list with room for the "..." arguments.
25311 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025312 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025313 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025314 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025315 /* Use "name" to avoid a warning from some compiler that checks the
25316 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025317 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025318 name = v->di_key;
25319 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025320 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025321 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025322 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025323 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025324 v->di_tv.vval.v_list = &fc->l_varlist;
25325 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25326 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25327 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025328
25329 /*
25330 * Set a:firstline to "firstline" and a:lastline to "lastline".
25331 * Set a:name to named arguments.
25332 * Set a:N to the "..." arguments.
25333 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025334 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025335 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025336 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025337 (varnumber_T)lastline);
25338 for (i = 0; i < argcount; ++i)
25339 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025340 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025341 if (ai < 0)
25342 /* named argument a:name */
25343 name = FUNCARG(fp, i);
25344 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025345 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025346 /* "..." argument a:1, a:2, etc. */
25347 sprintf((char *)numbuf, "%d", ai + 1);
25348 name = numbuf;
25349 }
25350 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25351 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025352 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025353 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25354 }
25355 else
25356 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025357 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25358 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025359 if (v == NULL)
25360 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025361 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025362 }
25363 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025364 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025365
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025366 /* Note: the values are copied directly to avoid alloc/free.
25367 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025368 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025369 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025370
25371 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25372 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025373 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25374 fc->l_listitems[ai].li_tv = argvars[i];
25375 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025376 }
25377 }
25378
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 /* Don't redraw while executing the function. */
25380 ++RedrawingDisabled;
25381 save_sourcing_name = sourcing_name;
25382 save_sourcing_lnum = sourcing_lnum;
25383 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025384 /* need space for function name + ("function " + 3) or "[number]" */
25385 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25386 + STRLEN(fp->uf_name) + 20;
25387 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388 if (sourcing_name != NULL)
25389 {
25390 if (save_sourcing_name != NULL
25391 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025392 sprintf((char *)sourcing_name, "%s[%d]..",
25393 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394 else
25395 STRCPY(sourcing_name, "function ");
25396 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25397
25398 if (p_verbose >= 12)
25399 {
25400 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025401 verbose_enter_scroll();
25402
Bram Moolenaar555b2802005-05-19 21:08:39 +000025403 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404 if (p_verbose >= 14)
25405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025406 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025407 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025408 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025409 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410
25411 msg_puts((char_u *)"(");
25412 for (i = 0; i < argcount; ++i)
25413 {
25414 if (i > 0)
25415 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025416 if (argvars[i].v_type == VAR_NUMBER)
25417 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025418 else
25419 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025420 /* Do not want errors such as E724 here. */
25421 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025422 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025423 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025424 if (s != NULL)
25425 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025426 if (vim_strsize(s) > MSG_BUF_CLEN)
25427 {
25428 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25429 s = buf;
25430 }
25431 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025432 vim_free(tofree);
25433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025434 }
25435 }
25436 msg_puts((char_u *)")");
25437 }
25438 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025439
25440 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441 --no_wait_return;
25442 }
25443 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025444#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025445 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025446 {
25447 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25448 func_do_profile(fp);
25449 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025450 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025451 {
25452 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025453 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025454 profile_zero(&fp->uf_tm_children);
25455 }
25456 script_prof_save(&wait_start);
25457 }
25458#endif
25459
Bram Moolenaar071d4272004-06-13 20:20:40 +000025460 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025461 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025462 save_did_emsg = did_emsg;
25463 did_emsg = FALSE;
25464
25465 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025466 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25468
25469 --RedrawingDisabled;
25470
25471 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025472 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025474 clear_tv(rettv);
25475 rettv->v_type = VAR_NUMBER;
25476 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477 }
25478
Bram Moolenaar05159a02005-02-26 23:04:13 +000025479#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025480 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025481 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025482 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025483 profile_end(&call_start);
25484 profile_sub_wait(&wait_start, &call_start);
25485 profile_add(&fp->uf_tm_total, &call_start);
25486 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025487 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025488 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025489 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25490 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025491 }
25492 }
25493#endif
25494
Bram Moolenaar071d4272004-06-13 20:20:40 +000025495 /* when being verbose, mention the return value */
25496 if (p_verbose >= 12)
25497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025499 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025502 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025503 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025504 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025505 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025506 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025507 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025508 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025509 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025510 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025511 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025512
Bram Moolenaar555b2802005-05-19 21:08:39 +000025513 /* The value may be very long. Skip the middle part, so that we
25514 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025515 * truncate it at the end. Don't want errors such as E724 here. */
25516 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025517 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025518 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025519 if (s != NULL)
25520 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025521 if (vim_strsize(s) > MSG_BUF_CLEN)
25522 {
25523 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25524 s = buf;
25525 }
25526 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025527 vim_free(tofree);
25528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529 }
25530 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025531
25532 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025533 --no_wait_return;
25534 }
25535
25536 vim_free(sourcing_name);
25537 sourcing_name = save_sourcing_name;
25538 sourcing_lnum = save_sourcing_lnum;
25539 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025540#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025541 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025542 script_prof_restore(&wait_start);
25543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544
25545 if (p_verbose >= 12 && sourcing_name != NULL)
25546 {
25547 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025548 verbose_enter_scroll();
25549
Bram Moolenaar555b2802005-05-19 21:08:39 +000025550 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025552
25553 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025554 --no_wait_return;
25555 }
25556
25557 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025558 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025560
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025561 /* If the a:000 list and the l: and a: dicts are not referenced we can
25562 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025563 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25564 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25565 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25566 {
25567 free_funccal(fc, FALSE);
25568 }
25569 else
25570 {
25571 hashitem_T *hi;
25572 listitem_T *li;
25573 int todo;
25574
25575 /* "fc" is still in use. This can happen when returning "a:000" or
25576 * assigning "l:" to a global variable.
25577 * Link "fc" in the list for garbage collection later. */
25578 fc->caller = previous_funccal;
25579 previous_funccal = fc;
25580
25581 /* Make a copy of the a: variables, since we didn't do that above. */
25582 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25583 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25584 {
25585 if (!HASHITEM_EMPTY(hi))
25586 {
25587 --todo;
25588 v = HI2DI(hi);
25589 copy_tv(&v->di_tv, &v->di_tv);
25590 }
25591 }
25592
25593 /* Make a copy of the a:000 items, since we didn't do that above. */
25594 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25595 copy_tv(&li->li_tv, &li->li_tv);
25596 }
25597}
25598
25599/*
25600 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025601 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025602 */
25603 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025604can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025605{
25606 return (fc->l_varlist.lv_copyID != copyID
25607 && fc->l_vars.dv_copyID != copyID
25608 && fc->l_avars.dv_copyID != copyID);
25609}
25610
25611/*
25612 * Free "fc" and what it contains.
25613 */
25614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025615free_funccal(
25616 funccall_T *fc,
25617 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025618{
25619 listitem_T *li;
25620
25621 /* The a: variables typevals may not have been allocated, only free the
25622 * allocated variables. */
25623 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25624
25625 /* free all l: variables */
25626 vars_clear(&fc->l_vars.dv_hashtab);
25627
25628 /* Free the a:000 variables if they were allocated. */
25629 if (free_val)
25630 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25631 clear_tv(&li->li_tv);
25632
25633 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025634}
25635
25636/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025637 * Add a number variable "name" to dict "dp" with value "nr".
25638 */
25639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025640add_nr_var(
25641 dict_T *dp,
25642 dictitem_T *v,
25643 char *name,
25644 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025645{
25646 STRCPY(v->di_key, name);
25647 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25648 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25649 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025650 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025651 v->di_tv.vval.v_number = nr;
25652}
25653
25654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 * ":return [expr]"
25656 */
25657 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025658ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025659{
25660 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025661 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025662 int returning = FALSE;
25663
25664 if (current_funccal == NULL)
25665 {
25666 EMSG(_("E133: :return not inside a function"));
25667 return;
25668 }
25669
25670 if (eap->skip)
25671 ++emsg_skip;
25672
25673 eap->nextcmd = NULL;
25674 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025675 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676 {
25677 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025678 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025679 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025680 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025681 }
25682 /* It's safer to return also on error. */
25683 else if (!eap->skip)
25684 {
25685 /*
25686 * Return unless the expression evaluation has been cancelled due to an
25687 * aborting error, an interrupt, or an exception.
25688 */
25689 if (!aborting())
25690 returning = do_return(eap, FALSE, TRUE, NULL);
25691 }
25692
25693 /* When skipping or the return gets pending, advance to the next command
25694 * in this line (!returning). Otherwise, ignore the rest of the line.
25695 * Following lines will be ignored by get_func_line(). */
25696 if (returning)
25697 eap->nextcmd = NULL;
25698 else if (eap->nextcmd == NULL) /* no argument */
25699 eap->nextcmd = check_nextcmd(arg);
25700
25701 if (eap->skip)
25702 --emsg_skip;
25703}
25704
25705/*
25706 * Return from a function. Possibly makes the return pending. Also called
25707 * for a pending return at the ":endtry" or after returning from an extra
25708 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025709 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025710 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025711 * FALSE when the return gets pending.
25712 */
25713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025714do_return(
25715 exarg_T *eap,
25716 int reanimate,
25717 int is_cmd,
25718 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025719{
25720 int idx;
25721 struct condstack *cstack = eap->cstack;
25722
25723 if (reanimate)
25724 /* Undo the return. */
25725 current_funccal->returned = FALSE;
25726
25727 /*
25728 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25729 * not in its finally clause (which then is to be executed next) is found.
25730 * In this case, make the ":return" pending for execution at the ":endtry".
25731 * Otherwise, return normally.
25732 */
25733 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25734 if (idx >= 0)
25735 {
25736 cstack->cs_pending[idx] = CSTP_RETURN;
25737
25738 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025739 /* A pending return again gets pending. "rettv" points to an
25740 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025742 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025743 else
25744 {
25745 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025746 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025747 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025748 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025750 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751 {
25752 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025753 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025754 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755 else
25756 EMSG(_(e_outofmem));
25757 }
25758 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025759 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025760
25761 if (reanimate)
25762 {
25763 /* The pending return value could be overwritten by a ":return"
25764 * without argument in a finally clause; reset the default
25765 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025766 current_funccal->rettv->v_type = VAR_NUMBER;
25767 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025768 }
25769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025770 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025771 }
25772 else
25773 {
25774 current_funccal->returned = TRUE;
25775
25776 /* If the return is carried out now, store the return value. For
25777 * a return immediately after reanimation, the value is already
25778 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025779 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025781 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025782 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025784 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025785 }
25786 }
25787
25788 return idx < 0;
25789}
25790
25791/*
25792 * Free the variable with a pending return value.
25793 */
25794 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025795discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796{
Bram Moolenaar33570922005-01-25 22:26:29 +000025797 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025798}
25799
25800/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025801 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802 * is an allocated string. Used by report_pending() for verbose messages.
25803 */
25804 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025805get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025807 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025808 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025809 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025810
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025811 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025812 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025813 if (s == NULL)
25814 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025815
25816 STRCPY(IObuff, ":return ");
25817 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25818 if (STRLEN(s) + 8 >= IOSIZE)
25819 STRCPY(IObuff + IOSIZE - 4, "...");
25820 vim_free(tofree);
25821 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822}
25823
25824/*
25825 * Get next function line.
25826 * Called by do_cmdline() to get the next line.
25827 * Returns allocated string, or NULL for end of function.
25828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025830get_func_line(
25831 int c UNUSED,
25832 void *cookie,
25833 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834{
Bram Moolenaar33570922005-01-25 22:26:29 +000025835 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025836 ufunc_T *fp = fcp->func;
25837 char_u *retval;
25838 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025839
25840 /* If breakpoints have been added/deleted need to check for it. */
25841 if (fcp->dbg_tick != debug_tick)
25842 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025843 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025844 sourcing_lnum);
25845 fcp->dbg_tick = debug_tick;
25846 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025847#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025848 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025849 func_line_end(cookie);
25850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025851
Bram Moolenaar05159a02005-02-26 23:04:13 +000025852 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025853 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25854 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025855 retval = NULL;
25856 else
25857 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025858 /* Skip NULL lines (continuation lines). */
25859 while (fcp->linenr < gap->ga_len
25860 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25861 ++fcp->linenr;
25862 if (fcp->linenr >= gap->ga_len)
25863 retval = NULL;
25864 else
25865 {
25866 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25867 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025868#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025869 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025870 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025871#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025873 }
25874
25875 /* Did we encounter a breakpoint? */
25876 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25877 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025878 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025879 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025880 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025881 sourcing_lnum);
25882 fcp->dbg_tick = debug_tick;
25883 }
25884
25885 return retval;
25886}
25887
Bram Moolenaar05159a02005-02-26 23:04:13 +000025888#if defined(FEAT_PROFILE) || defined(PROTO)
25889/*
25890 * Called when starting to read a function line.
25891 * "sourcing_lnum" must be correct!
25892 * When skipping lines it may not actually be executed, but we won't find out
25893 * until later and we need to store the time now.
25894 */
25895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025896func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025897{
25898 funccall_T *fcp = (funccall_T *)cookie;
25899 ufunc_T *fp = fcp->func;
25900
25901 if (fp->uf_profiling && sourcing_lnum >= 1
25902 && sourcing_lnum <= fp->uf_lines.ga_len)
25903 {
25904 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025905 /* Skip continuation lines. */
25906 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25907 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025908 fp->uf_tml_execed = FALSE;
25909 profile_start(&fp->uf_tml_start);
25910 profile_zero(&fp->uf_tml_children);
25911 profile_get_wait(&fp->uf_tml_wait);
25912 }
25913}
25914
25915/*
25916 * Called when actually executing a function line.
25917 */
25918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025919func_line_exec(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 && fp->uf_tml_idx >= 0)
25925 fp->uf_tml_execed = TRUE;
25926}
25927
25928/*
25929 * Called when done with a function line.
25930 */
25931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025932func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025933{
25934 funccall_T *fcp = (funccall_T *)cookie;
25935 ufunc_T *fp = fcp->func;
25936
25937 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25938 {
25939 if (fp->uf_tml_execed)
25940 {
25941 ++fp->uf_tml_count[fp->uf_tml_idx];
25942 profile_end(&fp->uf_tml_start);
25943 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025944 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025945 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25946 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025947 }
25948 fp->uf_tml_idx = -1;
25949 }
25950}
25951#endif
25952
Bram Moolenaar071d4272004-06-13 20:20:40 +000025953/*
25954 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025955 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025956 */
25957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025958func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959{
Bram Moolenaar33570922005-01-25 22:26:29 +000025960 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025961
25962 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25963 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025964 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025965 || fcp->returned);
25966}
25967
25968/*
25969 * return TRUE if cookie indicates a function which "abort"s on errors.
25970 */
25971 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025972func_has_abort(
25973 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025974{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025975 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025976}
25977
25978#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25979typedef enum
25980{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025981 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25982 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25983 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025984} var_flavour_T;
25985
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025986static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987
25988 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025989var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025990{
25991 char_u *p = varname;
25992
25993 if (ASCII_ISUPPER(*p))
25994 {
25995 while (*(++p))
25996 if (ASCII_ISLOWER(*p))
25997 return VAR_FLAVOUR_SESSION;
25998 return VAR_FLAVOUR_VIMINFO;
25999 }
26000 else
26001 return VAR_FLAVOUR_DEFAULT;
26002}
26003#endif
26004
26005#if defined(FEAT_VIMINFO) || defined(PROTO)
26006/*
26007 * Restore global vars that start with a capital from the viminfo file
26008 */
26009 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026010read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026011{
26012 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026013 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026014 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026015 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026016
26017 if (!writing && (find_viminfo_parameter('!') != NULL))
26018 {
26019 tab = vim_strchr(virp->vir_line + 1, '\t');
26020 if (tab != NULL)
26021 {
26022 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026023 switch (*tab)
26024 {
26025 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026026#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026027 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026028#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026029 case 'D': type = VAR_DICT; break;
26030 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026031 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026033
26034 tab = vim_strchr(tab, '\t');
26035 if (tab != NULL)
26036 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026037 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026038 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026039 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026040 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026041#ifdef FEAT_FLOAT
26042 else if (type == VAR_FLOAT)
26043 (void)string2float(tab + 1, &tv.vval.v_float);
26044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026045 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026046 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026047 if (type == VAR_DICT || type == VAR_LIST)
26048 {
26049 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26050
26051 if (etv == NULL)
26052 /* Failed to parse back the dict or list, use it as a
26053 * string. */
26054 tv.v_type = VAR_STRING;
26055 else
26056 {
26057 vim_free(tv.vval.v_string);
26058 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026059 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026060 }
26061 }
26062
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026063 /* when in a function use global variables */
26064 save_funccal = current_funccal;
26065 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026066 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026067 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026068
26069 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026070 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026071 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26072 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026073 }
26074 }
26075 }
26076
26077 return viminfo_readline(virp);
26078}
26079
26080/*
26081 * Write global vars that start with a capital to the viminfo file
26082 */
26083 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026084write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026085{
Bram Moolenaar33570922005-01-25 22:26:29 +000026086 hashitem_T *hi;
26087 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026088 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026089 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026090 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026091 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026092 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026093
26094 if (find_viminfo_parameter('!') == NULL)
26095 return;
26096
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026097 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026098
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026099 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026100 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026101 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026102 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026104 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026105 this_var = HI2DI(hi);
26106 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026107 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026108 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026109 {
26110 case VAR_STRING: s = "STR"; break;
26111 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026112 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026113 case VAR_DICT: s = "DIC"; break;
26114 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026115 case VAR_SPECIAL: s = "XPL"; break;
26116
26117 case VAR_UNKNOWN:
26118 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026119 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026120 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026121 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026122 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026123 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026124 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026125 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026126 if (p != NULL)
26127 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026128 vim_free(tofree);
26129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026130 }
26131 }
26132}
26133#endif
26134
26135#if defined(FEAT_SESSION) || defined(PROTO)
26136 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026137store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026138{
Bram Moolenaar33570922005-01-25 22:26:29 +000026139 hashitem_T *hi;
26140 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026141 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026142 char_u *p, *t;
26143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026144 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026145 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026146 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026147 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026148 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026149 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026150 this_var = HI2DI(hi);
26151 if ((this_var->di_tv.v_type == VAR_NUMBER
26152 || this_var->di_tv.v_type == VAR_STRING)
26153 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026154 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026155 /* Escape special characters with a backslash. Turn a LF and
26156 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026157 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026158 (char_u *)"\\\"\n\r");
26159 if (p == NULL) /* out of memory */
26160 break;
26161 for (t = p; *t != NUL; ++t)
26162 if (*t == '\n')
26163 *t = 'n';
26164 else if (*t == '\r')
26165 *t = 'r';
26166 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026167 this_var->di_key,
26168 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26169 : ' ',
26170 p,
26171 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26172 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026173 || put_eol(fd) == FAIL)
26174 {
26175 vim_free(p);
26176 return FAIL;
26177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026178 vim_free(p);
26179 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026180#ifdef FEAT_FLOAT
26181 else if (this_var->di_tv.v_type == VAR_FLOAT
26182 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26183 {
26184 float_T f = this_var->di_tv.vval.v_float;
26185 int sign = ' ';
26186
26187 if (f < 0)
26188 {
26189 f = -f;
26190 sign = '-';
26191 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026192 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026193 this_var->di_key, sign, f) < 0)
26194 || put_eol(fd) == FAIL)
26195 return FAIL;
26196 }
26197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026198 }
26199 }
26200 return OK;
26201}
26202#endif
26203
Bram Moolenaar661b1822005-07-28 22:36:45 +000026204/*
26205 * Display script name where an item was last set.
26206 * Should only be invoked when 'verbose' is non-zero.
26207 */
26208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026209last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026210{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026211 char_u *p;
26212
Bram Moolenaar661b1822005-07-28 22:36:45 +000026213 if (scriptID != 0)
26214 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026215 p = home_replace_save(NULL, get_scriptname(scriptID));
26216 if (p != NULL)
26217 {
26218 verbose_enter();
26219 MSG_PUTS(_("\n\tLast set from "));
26220 MSG_PUTS(p);
26221 vim_free(p);
26222 verbose_leave();
26223 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026224 }
26225}
26226
Bram Moolenaard812df62008-11-09 12:46:09 +000026227/*
26228 * List v:oldfiles in a nice way.
26229 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026231ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026232{
26233 list_T *l = vimvars[VV_OLDFILES].vv_list;
26234 listitem_T *li;
26235 int nr = 0;
26236
26237 if (l == NULL)
26238 msg((char_u *)_("No old files"));
26239 else
26240 {
26241 msg_start();
26242 msg_scroll = TRUE;
26243 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26244 {
26245 msg_outnum((long)++nr);
26246 MSG_PUTS(": ");
26247 msg_outtrans(get_tv_string(&li->li_tv));
26248 msg_putchar('\n');
26249 out_flush(); /* output one line at a time */
26250 ui_breakcheck();
26251 }
26252 /* Assume "got_int" was set to truncate the listing. */
26253 got_int = FALSE;
26254
26255#ifdef FEAT_BROWSE_CMD
26256 if (cmdmod.browse)
26257 {
26258 quit_more = FALSE;
26259 nr = prompt_for_number(FALSE);
26260 msg_starthere();
26261 if (nr > 0)
26262 {
26263 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26264 (long)nr);
26265
26266 if (p != NULL)
26267 {
26268 p = expand_env_save(p);
26269 eap->arg = p;
26270 eap->cmdidx = CMD_edit;
26271 cmdmod.browse = FALSE;
26272 do_exedit(eap, NULL);
26273 vim_free(p);
26274 }
26275 }
26276 }
26277#endif
26278 }
26279}
26280
Bram Moolenaar53744302015-07-17 17:38:22 +020026281/* reset v:option_new, v:option_old and v:option_type */
26282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026283reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026284{
26285 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26286 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26287 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26288}
26289
26290
Bram Moolenaar071d4272004-06-13 20:20:40 +000026291#endif /* FEAT_EVAL */
26292
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026294#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026295
26296#ifdef WIN3264
26297/*
26298 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26299 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026300static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26301static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26302static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026303
26304/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026305 * Get the short path (8.3) for the filename in "fnamep".
26306 * Only works for a valid file name.
26307 * When the path gets longer "fnamep" is changed and the allocated buffer
26308 * is put in "bufp".
26309 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26310 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026311 */
26312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026313get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026314{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026315 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026316 char_u *newbuf;
26317
26318 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026319 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026320 if (l > len - 1)
26321 {
26322 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026323 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026324 newbuf = vim_strnsave(*fnamep, l);
26325 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026327
26328 vim_free(*bufp);
26329 *fnamep = *bufp = newbuf;
26330
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026331 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026332 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026333 }
26334
26335 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026336 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026337}
26338
26339/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026340 * Get the short path (8.3) for the filename in "fname". The converted
26341 * path is returned in "bufp".
26342 *
26343 * Some of the directories specified in "fname" may not exist. This function
26344 * will shorten the existing directories at the beginning of the path and then
26345 * append the remaining non-existing path.
26346 *
26347 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026348 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026349 * bufp - Pointer to an allocated buffer for the filename.
26350 * fnamelen - Length of the filename pointed to by fname
26351 *
26352 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353 */
26354 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026355shortpath_for_invalid_fname(
26356 char_u **fname,
26357 char_u **bufp,
26358 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026359{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026360 char_u *short_fname, *save_fname, *pbuf_unused;
26361 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026362 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026363 int old_len, len;
26364 int new_len, sfx_len;
26365 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026366
26367 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026368 old_len = *fnamelen;
26369 save_fname = vim_strnsave(*fname, old_len);
26370 pbuf_unused = NULL;
26371 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026372
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026373 endp = save_fname + old_len - 1; /* Find the end of the copy */
26374 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026375
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026376 /*
26377 * Try shortening the supplied path till it succeeds by removing one
26378 * directory at a time from the tail of the path.
26379 */
26380 len = 0;
26381 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026382 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026383 /* go back one path-separator */
26384 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26385 --endp;
26386 if (endp <= save_fname)
26387 break; /* processed the complete path */
26388
26389 /*
26390 * Replace the path separator with a NUL and try to shorten the
26391 * resulting path.
26392 */
26393 ch = *endp;
26394 *endp = 0;
26395 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026396 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026397 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26398 {
26399 retval = FAIL;
26400 goto theend;
26401 }
26402 *endp = ch; /* preserve the string */
26403
26404 if (len > 0)
26405 break; /* successfully shortened the path */
26406
26407 /* failed to shorten the path. Skip the path separator */
26408 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026409 }
26410
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026411 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026412 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026413 /*
26414 * Succeeded in shortening the path. Now concatenate the shortened
26415 * path with the remaining path at the tail.
26416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026417
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026418 /* Compute the length of the new path. */
26419 sfx_len = (int)(save_endp - endp) + 1;
26420 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026422 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026423 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026424 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026425 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026426 /* There is not enough space in the currently allocated string,
26427 * copy it to a buffer big enough. */
26428 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026429 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026430 {
26431 retval = FAIL;
26432 goto theend;
26433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026434 }
26435 else
26436 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026437 /* Transfer short_fname to the main buffer (it's big enough),
26438 * unless get_short_pathname() did its work in-place. */
26439 *fname = *bufp = save_fname;
26440 if (short_fname != save_fname)
26441 vim_strncpy(save_fname, short_fname, len);
26442 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026443 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026444
26445 /* concat the not-shortened part of the path */
26446 vim_strncpy(*fname + len, endp, sfx_len);
26447 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026448 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026449
26450theend:
26451 vim_free(pbuf_unused);
26452 vim_free(save_fname);
26453
26454 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026455}
26456
26457/*
26458 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026459 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026460 */
26461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026462shortpath_for_partial(
26463 char_u **fnamep,
26464 char_u **bufp,
26465 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026466{
26467 int sepcount, len, tflen;
26468 char_u *p;
26469 char_u *pbuf, *tfname;
26470 int hasTilde;
26471
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026472 /* Count up the path separators from the RHS.. so we know which part
26473 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026474 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026475 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026476 if (vim_ispathsep(*p))
26477 ++sepcount;
26478
26479 /* Need full path first (use expand_env() to remove a "~/") */
26480 hasTilde = (**fnamep == '~');
26481 if (hasTilde)
26482 pbuf = tfname = expand_env_save(*fnamep);
26483 else
26484 pbuf = tfname = FullName_save(*fnamep, FALSE);
26485
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026486 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026487
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026488 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26489 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026490
26491 if (len == 0)
26492 {
26493 /* Don't have a valid filename, so shorten the rest of the
26494 * path if we can. This CAN give us invalid 8.3 filenames, but
26495 * there's not a lot of point in guessing what it might be.
26496 */
26497 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026498 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26499 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026500 }
26501
26502 /* Count the paths backward to find the beginning of the desired string. */
26503 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026504 {
26505#ifdef FEAT_MBYTE
26506 if (has_mbyte)
26507 p -= mb_head_off(tfname, p);
26508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026509 if (vim_ispathsep(*p))
26510 {
26511 if (sepcount == 0 || (hasTilde && sepcount == 1))
26512 break;
26513 else
26514 sepcount --;
26515 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026517 if (hasTilde)
26518 {
26519 --p;
26520 if (p >= tfname)
26521 *p = '~';
26522 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026523 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026524 }
26525 else
26526 ++p;
26527
26528 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26529 vim_free(*bufp);
26530 *fnamelen = (int)STRLEN(p);
26531 *bufp = pbuf;
26532 *fnamep = p;
26533
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026534 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026535}
26536#endif /* WIN3264 */
26537
26538/*
26539 * Adjust a filename, according to a string of modifiers.
26540 * *fnamep must be NUL terminated when called. When returning, the length is
26541 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026542 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026543 * When there is an error, *fnamep is set to NULL.
26544 */
26545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026546modify_fname(
26547 char_u *src, /* string with modifiers */
26548 int *usedlen, /* characters after src that are used */
26549 char_u **fnamep, /* file name so far */
26550 char_u **bufp, /* buffer for allocated file name or NULL */
26551 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026552{
26553 int valid = 0;
26554 char_u *tail;
26555 char_u *s, *p, *pbuf;
26556 char_u dirname[MAXPATHL];
26557 int c;
26558 int has_fullname = 0;
26559#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026560 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026561 int has_shortname = 0;
26562#endif
26563
26564repeat:
26565 /* ":p" - full path/file_name */
26566 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26567 {
26568 has_fullname = 1;
26569
26570 valid |= VALID_PATH;
26571 *usedlen += 2;
26572
26573 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26574 if ((*fnamep)[0] == '~'
26575#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26576 && ((*fnamep)[1] == '/'
26577# ifdef BACKSLASH_IN_FILENAME
26578 || (*fnamep)[1] == '\\'
26579# endif
26580 || (*fnamep)[1] == NUL)
26581
26582#endif
26583 )
26584 {
26585 *fnamep = expand_env_save(*fnamep);
26586 vim_free(*bufp); /* free any allocated file name */
26587 *bufp = *fnamep;
26588 if (*fnamep == NULL)
26589 return -1;
26590 }
26591
26592 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026593 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026594 {
26595 if (vim_ispathsep(*p)
26596 && p[1] == '.'
26597 && (p[2] == NUL
26598 || vim_ispathsep(p[2])
26599 || (p[2] == '.'
26600 && (p[3] == NUL || vim_ispathsep(p[3])))))
26601 break;
26602 }
26603
26604 /* FullName_save() is slow, don't use it when not needed. */
26605 if (*p != NUL || !vim_isAbsName(*fnamep))
26606 {
26607 *fnamep = FullName_save(*fnamep, *p != NUL);
26608 vim_free(*bufp); /* free any allocated file name */
26609 *bufp = *fnamep;
26610 if (*fnamep == NULL)
26611 return -1;
26612 }
26613
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026614#ifdef WIN3264
26615# if _WIN32_WINNT >= 0x0500
26616 if (vim_strchr(*fnamep, '~') != NULL)
26617 {
26618 /* Expand 8.3 filename to full path. Needed to make sure the same
26619 * file does not have two different names.
26620 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26621 p = alloc(_MAX_PATH + 1);
26622 if (p != NULL)
26623 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026624 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026625 {
26626 vim_free(*bufp);
26627 *bufp = *fnamep = p;
26628 }
26629 else
26630 vim_free(p);
26631 }
26632 }
26633# endif
26634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026635 /* Append a path separator to a directory. */
26636 if (mch_isdir(*fnamep))
26637 {
26638 /* Make room for one or two extra characters. */
26639 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26640 vim_free(*bufp); /* free any allocated file name */
26641 *bufp = *fnamep;
26642 if (*fnamep == NULL)
26643 return -1;
26644 add_pathsep(*fnamep);
26645 }
26646 }
26647
26648 /* ":." - path relative to the current directory */
26649 /* ":~" - path relative to the home directory */
26650 /* ":8" - shortname path - postponed till after */
26651 while (src[*usedlen] == ':'
26652 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26653 {
26654 *usedlen += 2;
26655 if (c == '8')
26656 {
26657#ifdef WIN3264
26658 has_shortname = 1; /* Postpone this. */
26659#endif
26660 continue;
26661 }
26662 pbuf = NULL;
26663 /* Need full path first (use expand_env() to remove a "~/") */
26664 if (!has_fullname)
26665 {
26666 if (c == '.' && **fnamep == '~')
26667 p = pbuf = expand_env_save(*fnamep);
26668 else
26669 p = pbuf = FullName_save(*fnamep, FALSE);
26670 }
26671 else
26672 p = *fnamep;
26673
26674 has_fullname = 0;
26675
26676 if (p != NULL)
26677 {
26678 if (c == '.')
26679 {
26680 mch_dirname(dirname, MAXPATHL);
26681 s = shorten_fname(p, dirname);
26682 if (s != NULL)
26683 {
26684 *fnamep = s;
26685 if (pbuf != NULL)
26686 {
26687 vim_free(*bufp); /* free any allocated file name */
26688 *bufp = pbuf;
26689 pbuf = NULL;
26690 }
26691 }
26692 }
26693 else
26694 {
26695 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26696 /* Only replace it when it starts with '~' */
26697 if (*dirname == '~')
26698 {
26699 s = vim_strsave(dirname);
26700 if (s != NULL)
26701 {
26702 *fnamep = s;
26703 vim_free(*bufp);
26704 *bufp = s;
26705 }
26706 }
26707 }
26708 vim_free(pbuf);
26709 }
26710 }
26711
26712 tail = gettail(*fnamep);
26713 *fnamelen = (int)STRLEN(*fnamep);
26714
26715 /* ":h" - head, remove "/file_name", can be repeated */
26716 /* Don't remove the first "/" or "c:\" */
26717 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26718 {
26719 valid |= VALID_HEAD;
26720 *usedlen += 2;
26721 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026722 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026723 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026724 *fnamelen = (int)(tail - *fnamep);
26725#ifdef VMS
26726 if (*fnamelen > 0)
26727 *fnamelen += 1; /* the path separator is part of the path */
26728#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026729 if (*fnamelen == 0)
26730 {
26731 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26732 p = vim_strsave((char_u *)".");
26733 if (p == NULL)
26734 return -1;
26735 vim_free(*bufp);
26736 *bufp = *fnamep = tail = p;
26737 *fnamelen = 1;
26738 }
26739 else
26740 {
26741 while (tail > s && !after_pathsep(s, tail))
26742 mb_ptr_back(*fnamep, tail);
26743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026744 }
26745
26746 /* ":8" - shortname */
26747 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26748 {
26749 *usedlen += 2;
26750#ifdef WIN3264
26751 has_shortname = 1;
26752#endif
26753 }
26754
26755#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026756 /*
26757 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026758 */
26759 if (has_shortname)
26760 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026761 /* Copy the string if it is shortened by :h and when it wasn't copied
26762 * yet, because we are going to change it in place. Avoids changing
26763 * the buffer name for "%:8". */
26764 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026765 {
26766 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026767 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026768 return -1;
26769 vim_free(*bufp);
26770 *bufp = *fnamep = p;
26771 }
26772
26773 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026774 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026775 if (!has_fullname && !vim_isAbsName(*fnamep))
26776 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026777 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026778 return -1;
26779 }
26780 else
26781 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026782 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026783
Bram Moolenaardc935552011-08-17 15:23:23 +020026784 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026785 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026786 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026787 return -1;
26788
26789 if (l == 0)
26790 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026791 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026792 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026793 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026794 return -1;
26795 }
26796 *fnamelen = l;
26797 }
26798 }
26799#endif /* WIN3264 */
26800
26801 /* ":t" - tail, just the basename */
26802 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26803 {
26804 *usedlen += 2;
26805 *fnamelen -= (int)(tail - *fnamep);
26806 *fnamep = tail;
26807 }
26808
26809 /* ":e" - extension, can be repeated */
26810 /* ":r" - root, without extension, can be repeated */
26811 while (src[*usedlen] == ':'
26812 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26813 {
26814 /* find a '.' in the tail:
26815 * - for second :e: before the current fname
26816 * - otherwise: The last '.'
26817 */
26818 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26819 s = *fnamep - 2;
26820 else
26821 s = *fnamep + *fnamelen - 1;
26822 for ( ; s > tail; --s)
26823 if (s[0] == '.')
26824 break;
26825 if (src[*usedlen + 1] == 'e') /* :e */
26826 {
26827 if (s > tail)
26828 {
26829 *fnamelen += (int)(*fnamep - (s + 1));
26830 *fnamep = s + 1;
26831#ifdef VMS
26832 /* cut version from the extension */
26833 s = *fnamep + *fnamelen - 1;
26834 for ( ; s > *fnamep; --s)
26835 if (s[0] == ';')
26836 break;
26837 if (s > *fnamep)
26838 *fnamelen = s - *fnamep;
26839#endif
26840 }
26841 else if (*fnamep <= tail)
26842 *fnamelen = 0;
26843 }
26844 else /* :r */
26845 {
26846 if (s > tail) /* remove one extension */
26847 *fnamelen = (int)(s - *fnamep);
26848 }
26849 *usedlen += 2;
26850 }
26851
26852 /* ":s?pat?foo?" - substitute */
26853 /* ":gs?pat?foo?" - global substitute */
26854 if (src[*usedlen] == ':'
26855 && (src[*usedlen + 1] == 's'
26856 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26857 {
26858 char_u *str;
26859 char_u *pat;
26860 char_u *sub;
26861 int sep;
26862 char_u *flags;
26863 int didit = FALSE;
26864
26865 flags = (char_u *)"";
26866 s = src + *usedlen + 2;
26867 if (src[*usedlen + 1] == 'g')
26868 {
26869 flags = (char_u *)"g";
26870 ++s;
26871 }
26872
26873 sep = *s++;
26874 if (sep)
26875 {
26876 /* find end of pattern */
26877 p = vim_strchr(s, sep);
26878 if (p != NULL)
26879 {
26880 pat = vim_strnsave(s, (int)(p - s));
26881 if (pat != NULL)
26882 {
26883 s = p + 1;
26884 /* find end of substitution */
26885 p = vim_strchr(s, sep);
26886 if (p != NULL)
26887 {
26888 sub = vim_strnsave(s, (int)(p - s));
26889 str = vim_strnsave(*fnamep, *fnamelen);
26890 if (sub != NULL && str != NULL)
26891 {
26892 *usedlen = (int)(p + 1 - src);
26893 s = do_string_sub(str, pat, sub, flags);
26894 if (s != NULL)
26895 {
26896 *fnamep = s;
26897 *fnamelen = (int)STRLEN(s);
26898 vim_free(*bufp);
26899 *bufp = s;
26900 didit = TRUE;
26901 }
26902 }
26903 vim_free(sub);
26904 vim_free(str);
26905 }
26906 vim_free(pat);
26907 }
26908 }
26909 /* after using ":s", repeat all the modifiers */
26910 if (didit)
26911 goto repeat;
26912 }
26913 }
26914
Bram Moolenaar26df0922014-02-23 23:39:13 +010026915 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26916 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026917 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026918 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026919 if (c != NUL)
26920 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026921 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026922 if (c != NUL)
26923 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026924 if (p == NULL)
26925 return -1;
26926 vim_free(*bufp);
26927 *bufp = *fnamep = p;
26928 *fnamelen = (int)STRLEN(p);
26929 *usedlen += 2;
26930 }
26931
Bram Moolenaar071d4272004-06-13 20:20:40 +000026932 return valid;
26933}
26934
26935/*
26936 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26937 * "flags" can be "g" to do a global substitute.
26938 * Returns an allocated string, NULL for error.
26939 */
26940 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026941do_string_sub(
26942 char_u *str,
26943 char_u *pat,
26944 char_u *sub,
26945 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026946{
26947 int sublen;
26948 regmatch_T regmatch;
26949 int i;
26950 int do_all;
26951 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026952 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026953 garray_T ga;
26954 char_u *ret;
26955 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026956 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026957
26958 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26959 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026960 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026961
26962 ga_init2(&ga, 1, 200);
26963
26964 do_all = (flags[0] == 'g');
26965
26966 regmatch.rm_ic = p_ic;
26967 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26968 if (regmatch.regprog != NULL)
26969 {
26970 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026971 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026972 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26973 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026974 /* Skip empty match except for first match. */
26975 if (regmatch.startp[0] == regmatch.endp[0])
26976 {
26977 if (zero_width == regmatch.startp[0])
26978 {
26979 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026980 i = MB_PTR2LEN(tail);
26981 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26982 (size_t)i);
26983 ga.ga_len += i;
26984 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026985 continue;
26986 }
26987 zero_width = regmatch.startp[0];
26988 }
26989
Bram Moolenaar071d4272004-06-13 20:20:40 +000026990 /*
26991 * Get some space for a temporary buffer to do the substitution
26992 * into. It will contain:
26993 * - The text up to where the match is.
26994 * - The substituted text.
26995 * - The text after the match.
26996 */
26997 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026998 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026999 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27000 {
27001 ga_clear(&ga);
27002 break;
27003 }
27004
27005 /* copy the text up to where the match is */
27006 i = (int)(regmatch.startp[0] - tail);
27007 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27008 /* add the substituted text */
27009 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27010 + ga.ga_len + i, TRUE, TRUE, FALSE);
27011 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027012 tail = regmatch.endp[0];
27013 if (*tail == NUL)
27014 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027015 if (!do_all)
27016 break;
27017 }
27018
27019 if (ga.ga_data != NULL)
27020 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27021
Bram Moolenaar473de612013-06-08 18:19:48 +020027022 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027023 }
27024
27025 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27026 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027027 if (p_cpo == empty_option)
27028 p_cpo = save_cpo;
27029 else
27030 /* Darn, evaluating {sub} expression changed the value. */
27031 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027032
27033 return ret;
27034}
27035
27036#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */