blob: 2b4f23012c1fcedad75d89d652b3f723fca3624d [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
Bram Moolenaar3266c852016-04-30 18:07:05 +02007028#ifdef FEAT_NETBEANS_INTG
7029 abort = abort || set_ref_in_nb_channel(copyID);
7030#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007031
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007033 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 /*
7035 * 2. Free lists and dictionaries that are not referenced.
7036 */
7037 did_free = free_unref_items(copyID);
7038
7039 /*
7040 * 3. Check if any funccal can be freed now.
7041 */
7042 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007043 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 if (can_free_funccal(*pfc, copyID))
7045 {
7046 fc = *pfc;
7047 *pfc = fc->caller;
7048 free_funccal(fc, TRUE);
7049 did_free = TRUE;
7050 did_free_funccal = TRUE;
7051 }
7052 else
7053 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007054 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007055 if (did_free_funccal)
7056 /* When a funccal was freed some more items might be garbage
7057 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007058 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007059 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007060 else if (p_verbose > 0)
7061 {
7062 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7063 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007064
7065 return did_free;
7066}
7067
7068/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007069 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007070 */
7071 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007072free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007073{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007074 dict_T *dd, *dd_next;
7075 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007076 int did_free = FALSE;
7077
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007078 /* Let all "free" functions know that we are here. This means no
7079 * dictionaries, lists, channels or jobs are to be freed, because we will
7080 * do that here. */
7081 in_free_unref_items = TRUE;
7082
7083 /*
7084 * PASS 1: free the contents of the items. We don't free the items
7085 * themselves yet, so that it is possible to decrement refcount counters
7086 */
7087
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007088 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007089 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007091 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007092 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007094 /* Free the Dictionary and ordinary items it contains, but don't
7095 * recurse into Lists and Dictionaries, they will be in the list
7096 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007097 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007098 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007099 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100
7101 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007102 * Go through the list of lists and free items without the copyID.
7103 * But don't free a list that has a watcher (used in a for loop), these
7104 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007105 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007106 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7107 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7108 && ll->lv_watch == NULL)
7109 {
7110 /* Free the List and ordinary items it contains, but don't recurse
7111 * into Lists and Dictionaries, they will be in the list of dicts
7112 * or list of lists. */
7113 list_free_contents(ll);
7114 did_free = TRUE;
7115 }
7116
7117#ifdef FEAT_JOB_CHANNEL
7118 /* Go through the list of jobs and free items without the copyID. This
7119 * must happen before doing channels, because jobs refer to channels, but
7120 * the reference from the channel to the job isn't tracked. */
7121 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7122
7123 /* Go through the list of channels and free items without the copyID. */
7124 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7125#endif
7126
7127 /*
7128 * PASS 2: free the items themselves.
7129 */
7130 for (dd = first_dict; dd != NULL; dd = dd_next)
7131 {
7132 dd_next = dd->dv_used_next;
7133 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7134 dict_free_dict(dd);
7135 }
7136
7137 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007138 {
7139 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007140 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7141 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007142 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007143 /* Free the List and ordinary items it contains, but don't recurse
7144 * into Lists and Dictionaries, they will be in the list of dicts
7145 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007146 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007147 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007148 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007149
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007150#ifdef FEAT_JOB_CHANNEL
7151 /* Go through the list of jobs and free items without the copyID. This
7152 * must happen before doing channels, because jobs refer to channels, but
7153 * the reference from the channel to the job isn't tracked. */
7154 free_unused_jobs(copyID, COPYID_MASK);
7155
7156 /* Go through the list of channels and free items without the copyID. */
7157 free_unused_channels(copyID, COPYID_MASK);
7158#endif
7159
7160 in_free_unref_items = FALSE;
7161
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007162 return did_free;
7163}
7164
7165/*
7166 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007167 * "list_stack" is used to add lists to be marked. Can be NULL.
7168 *
7169 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007170 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007172set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007173{
7174 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007175 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007176 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007177 hashtab_T *cur_ht;
7178 ht_stack_T *ht_stack = NULL;
7179 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007180
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007181 cur_ht = ht;
7182 for (;;)
7183 {
7184 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007185 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007186 /* Mark each item in the hashtab. If the item contains a hashtab
7187 * it is added to ht_stack, if it contains a list it is added to
7188 * list_stack. */
7189 todo = (int)cur_ht->ht_used;
7190 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7191 if (!HASHITEM_EMPTY(hi))
7192 {
7193 --todo;
7194 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7195 &ht_stack, list_stack);
7196 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007197 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007198
7199 if (ht_stack == NULL)
7200 break;
7201
7202 /* take an item from the stack */
7203 cur_ht = ht_stack->ht;
7204 tempitem = ht_stack;
7205 ht_stack = ht_stack->prev;
7206 free(tempitem);
7207 }
7208
7209 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007210}
7211
7212/*
7213 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007214 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7215 *
7216 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007218 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007219set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007220{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007221 listitem_T *li;
7222 int abort = FALSE;
7223 list_T *cur_l;
7224 list_stack_T *list_stack = NULL;
7225 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007226
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007227 cur_l = l;
7228 for (;;)
7229 {
7230 if (!abort)
7231 /* Mark each item in the list. If the item contains a hashtab
7232 * it is added to ht_stack, if it contains a list it is added to
7233 * list_stack. */
7234 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7235 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7236 ht_stack, &list_stack);
7237 if (list_stack == NULL)
7238 break;
7239
7240 /* take an item from the stack */
7241 cur_l = list_stack->list;
7242 tempitem = list_stack;
7243 list_stack = list_stack->prev;
7244 free(tempitem);
7245 }
7246
7247 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007248}
7249
7250/*
7251 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007252 * "list_stack" is used to add lists to be marked. Can be NULL.
7253 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7254 *
7255 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007256 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007257 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007258set_ref_in_item(
7259 typval_T *tv,
7260 int copyID,
7261 ht_stack_T **ht_stack,
7262 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007263{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007264 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007265
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007266 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007267 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007268 dict_T *dd = tv->vval.v_dict;
7269
Bram Moolenaara03f2332016-02-06 18:09:59 +01007270 if (dd != NULL && dd->dv_copyID != copyID)
7271 {
7272 /* Didn't see this dict yet. */
7273 dd->dv_copyID = copyID;
7274 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007275 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007276 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7277 }
7278 else
7279 {
7280 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7281 if (newitem == NULL)
7282 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007283 else
7284 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007285 newitem->ht = &dd->dv_hashtab;
7286 newitem->prev = *ht_stack;
7287 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007288 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007289 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007290 }
7291 }
7292 else if (tv->v_type == VAR_LIST)
7293 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007294 list_T *ll = tv->vval.v_list;
7295
Bram Moolenaara03f2332016-02-06 18:09:59 +01007296 if (ll != NULL && ll->lv_copyID != copyID)
7297 {
7298 /* Didn't see this list yet. */
7299 ll->lv_copyID = copyID;
7300 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007301 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007302 abort = set_ref_in_list(ll, copyID, ht_stack);
7303 }
7304 else
7305 {
7306 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007307 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007308 if (newitem == NULL)
7309 abort = TRUE;
7310 else
7311 {
7312 newitem->list = ll;
7313 newitem->prev = *list_stack;
7314 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007315 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007316 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007317 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007318 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007319 else if (tv->v_type == VAR_PARTIAL)
7320 {
7321 partial_T *pt = tv->vval.v_partial;
7322 int i;
7323
7324 /* A partial does not have a copyID, because it cannot contain itself.
7325 */
7326 if (pt != NULL)
7327 {
7328 if (pt->pt_dict != NULL)
7329 {
7330 typval_T dtv;
7331
7332 dtv.v_type = VAR_DICT;
7333 dtv.vval.v_dict = pt->pt_dict;
7334 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7335 }
7336
7337 for (i = 0; i < pt->pt_argc; ++i)
7338 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7339 ht_stack, list_stack);
7340 }
7341 }
7342#ifdef FEAT_JOB_CHANNEL
7343 else if (tv->v_type == VAR_JOB)
7344 {
7345 job_T *job = tv->vval.v_job;
7346 typval_T dtv;
7347
7348 if (job != NULL && job->jv_copyID != copyID)
7349 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007350 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007351 if (job->jv_channel != NULL)
7352 {
7353 dtv.v_type = VAR_CHANNEL;
7354 dtv.vval.v_channel = job->jv_channel;
7355 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7356 }
7357 if (job->jv_exit_partial != NULL)
7358 {
7359 dtv.v_type = VAR_PARTIAL;
7360 dtv.vval.v_partial = job->jv_exit_partial;
7361 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7362 }
7363 }
7364 }
7365 else if (tv->v_type == VAR_CHANNEL)
7366 {
7367 channel_T *ch =tv->vval.v_channel;
7368 int part;
7369 typval_T dtv;
7370 jsonq_T *jq;
7371 cbq_T *cq;
7372
7373 if (ch != NULL && ch->ch_copyID != copyID)
7374 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007375 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007376 for (part = PART_SOCK; part <= PART_IN; ++part)
7377 {
7378 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7379 jq = jq->jq_next)
7380 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7381 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7382 cq = cq->cq_next)
7383 if (cq->cq_partial != NULL)
7384 {
7385 dtv.v_type = VAR_PARTIAL;
7386 dtv.vval.v_partial = cq->cq_partial;
7387 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7388 }
7389 if (ch->ch_part[part].ch_partial != NULL)
7390 {
7391 dtv.v_type = VAR_PARTIAL;
7392 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7393 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7394 }
7395 }
7396 if (ch->ch_partial != NULL)
7397 {
7398 dtv.v_type = VAR_PARTIAL;
7399 dtv.vval.v_partial = ch->ch_partial;
7400 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7401 }
7402 if (ch->ch_close_partial != NULL)
7403 {
7404 dtv.v_type = VAR_PARTIAL;
7405 dtv.vval.v_partial = ch->ch_close_partial;
7406 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7407 }
7408 }
7409 }
7410#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007411 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007412}
7413
7414/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415 * Allocate an empty header for a dictionary.
7416 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007417 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007418dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419{
Bram Moolenaar33570922005-01-25 22:26:29 +00007420 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007421
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007423 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007424 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007425 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007426 if (first_dict != NULL)
7427 first_dict->dv_used_prev = d;
7428 d->dv_used_next = first_dict;
7429 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007430 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007431
Bram Moolenaar33570922005-01-25 22:26:29 +00007432 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007433 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007434 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007435 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007436 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007437 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007438 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439}
7440
7441/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007442 * Allocate an empty dict for a return value.
7443 * Returns OK or FAIL.
7444 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007446rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007447{
7448 dict_T *d = dict_alloc();
7449
7450 if (d == NULL)
7451 return FAIL;
7452
7453 rettv->vval.v_dict = d;
7454 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007455 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007456 ++d->dv_refcount;
7457 return OK;
7458}
7459
7460
7461/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462 * Unreference a Dictionary: decrement the reference count and free it when it
7463 * becomes zero.
7464 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007466dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007468 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007469 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470}
7471
7472/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007473 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007474 * Ignores the reference count.
7475 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007476 static void
7477dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007479 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007480 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007481 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007483 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007484 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007485 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488 if (!HASHITEM_EMPTY(hi))
7489 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007490 /* Remove the item before deleting it, just in case there is
7491 * something recursive causing trouble. */
7492 di = HI2DI(hi);
7493 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007494 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007495 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496 --todo;
7497 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007500}
7501
7502 static void
7503dict_free_dict(dict_T *d)
7504{
7505 /* Remove the dict from the list of dicts for garbage collection. */
7506 if (d->dv_used_prev == NULL)
7507 first_dict = d->dv_used_next;
7508 else
7509 d->dv_used_prev->dv_used_next = d->dv_used_next;
7510 if (d->dv_used_next != NULL)
7511 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 vim_free(d);
7513}
7514
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007515 void
7516dict_free(dict_T *d)
7517{
7518 if (!in_free_unref_items)
7519 {
7520 dict_free_contents(d);
7521 dict_free_dict(d);
7522 }
7523}
7524
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525/*
7526 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527 * The "key" is copied to the new item.
7528 * Note that the value of the item "di_tv" still needs to be initialized!
7529 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007531 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007532dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533{
Bram Moolenaar33570922005-01-25 22:26:29 +00007534 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007536 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007537 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007538 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007540 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007541 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007542 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543}
7544
7545/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007546 * Make a copy of a Dictionary item.
7547 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007549dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007550{
Bram Moolenaar33570922005-01-25 22:26:29 +00007551 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007553 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7554 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007555 if (di != NULL)
7556 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007558 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007559 copy_tv(&org->di_tv, &di->di_tv);
7560 }
7561 return di;
7562}
7563
7564/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 * Remove item "item" from Dictionary "dict" and free it.
7566 */
7567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007568dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007569{
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007571
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007573 if (HASHITEM_EMPTY(hi))
7574 EMSG2(_(e_intern2), "dictitem_remove()");
7575 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 dictitem_free(item);
7578}
7579
7580/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 * Free a dict item. Also clears the value.
7582 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007584dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007587 if (item->di_flags & DI_FLAGS_ALLOC)
7588 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589}
7590
7591/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007592 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7593 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007594 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007595 * Returns NULL when out of memory.
7596 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007598dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007599{
Bram Moolenaar33570922005-01-25 22:26:29 +00007600 dict_T *copy;
7601 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007602 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007603 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007604
7605 if (orig == NULL)
7606 return NULL;
7607
7608 copy = dict_alloc();
7609 if (copy != NULL)
7610 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007611 if (copyID != 0)
7612 {
7613 orig->dv_copyID = copyID;
7614 orig->dv_copydict = copy;
7615 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007616 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007617 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007619 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007620 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007621 --todo;
7622
7623 di = dictitem_alloc(hi->hi_key);
7624 if (di == NULL)
7625 break;
7626 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007627 {
7628 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7629 copyID) == FAIL)
7630 {
7631 vim_free(di);
7632 break;
7633 }
7634 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007635 else
7636 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7637 if (dict_add(copy, di) == FAIL)
7638 {
7639 dictitem_free(di);
7640 break;
7641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007644
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007646 if (todo > 0)
7647 {
7648 dict_unref(copy);
7649 copy = NULL;
7650 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007651 }
7652
7653 return copy;
7654}
7655
7656/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007658 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007660 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007661dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662{
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664}
7665
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007667 * Add a number or string entry to dictionary "d".
7668 * When "str" is NULL use number "nr", otherwise use "str".
7669 * Returns FAIL when out of memory and when key already exists.
7670 */
7671 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007672dict_add_nr_str(
7673 dict_T *d,
7674 char *key,
7675 long nr,
7676 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007677{
7678 dictitem_T *item;
7679
7680 item = dictitem_alloc((char_u *)key);
7681 if (item == NULL)
7682 return FAIL;
7683 item->di_tv.v_lock = 0;
7684 if (str == NULL)
7685 {
7686 item->di_tv.v_type = VAR_NUMBER;
7687 item->di_tv.vval.v_number = nr;
7688 }
7689 else
7690 {
7691 item->di_tv.v_type = VAR_STRING;
7692 item->di_tv.vval.v_string = vim_strsave(str);
7693 }
7694 if (dict_add(d, item) == FAIL)
7695 {
7696 dictitem_free(item);
7697 return FAIL;
7698 }
7699 return OK;
7700}
7701
7702/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007703 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007704 * Returns FAIL when out of memory and when key already exists.
7705 */
7706 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007707dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007708{
7709 dictitem_T *item;
7710
7711 item = dictitem_alloc((char_u *)key);
7712 if (item == NULL)
7713 return FAIL;
7714 item->di_tv.v_lock = 0;
7715 item->di_tv.v_type = VAR_LIST;
7716 item->di_tv.vval.v_list = list;
7717 if (dict_add(d, item) == FAIL)
7718 {
7719 dictitem_free(item);
7720 return FAIL;
7721 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007722 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007723 return OK;
7724}
7725
7726/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007727 * Get the number of items in a Dictionary.
7728 */
7729 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007730dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007731{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007732 if (d == NULL)
7733 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007734 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007735}
7736
7737/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738 * Find item "key[len]" in Dictionary "d".
7739 * If "len" is negative use strlen(key).
7740 * Returns NULL when not found.
7741 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007742 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007743dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007745#define AKEYLEN 200
7746 char_u buf[AKEYLEN];
7747 char_u *akey;
7748 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007749 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007751 if (len < 0)
7752 akey = key;
7753 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007754 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007755 tofree = akey = vim_strnsave(key, len);
7756 if (akey == NULL)
7757 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007758 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007759 else
7760 {
7761 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007762 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007763 akey = buf;
7764 }
7765
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007767 vim_free(tofree);
7768 if (HASHITEM_EMPTY(hi))
7769 return NULL;
7770 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771}
7772
7773/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007774 * Get a string item from a dictionary.
7775 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007776 * Returns NULL if the entry doesn't exist or out of memory.
7777 */
7778 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007779get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007780{
7781 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007782 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007783
7784 di = dict_find(d, key, -1);
7785 if (di == NULL)
7786 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007787 s = get_tv_string(&di->di_tv);
7788 if (save && s != NULL)
7789 s = vim_strsave(s);
7790 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007791}
7792
7793/*
7794 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007795 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007796 */
7797 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007798get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007799{
7800 dictitem_T *di;
7801
7802 di = dict_find(d, key, -1);
7803 if (di == NULL)
7804 return 0;
7805 return get_tv_number(&di->di_tv);
7806}
7807
7808/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007809 * Return an allocated string with the string representation of a Dictionary.
7810 * May return NULL.
7811 */
7812 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007813dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007814{
7815 garray_T ga;
7816 int first = TRUE;
7817 char_u *tofree;
7818 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007819 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007820 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007821 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007822 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007823
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007824 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007825 return NULL;
7826 ga_init2(&ga, (int)sizeof(char), 80);
7827 ga_append(&ga, '{');
7828
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007829 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007830 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007831 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007832 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007833 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007834 --todo;
7835
7836 if (first)
7837 first = FALSE;
7838 else
7839 ga_concat(&ga, (char_u *)", ");
7840
7841 tofree = string_quote(hi->hi_key, FALSE);
7842 if (tofree != NULL)
7843 {
7844 ga_concat(&ga, tofree);
7845 vim_free(tofree);
7846 }
7847 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007848 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007849 if (s != NULL)
7850 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007851 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007852 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007853 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007854 line_breakcheck();
7855
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007857 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007858 if (todo > 0)
7859 {
7860 vim_free(ga.ga_data);
7861 return NULL;
7862 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007863
7864 ga_append(&ga, '}');
7865 ga_append(&ga, NUL);
7866 return (char_u *)ga.ga_data;
7867}
7868
7869/*
7870 * Allocate a variable for a Dictionary and fill it from "*arg".
7871 * Return OK or FAIL. Returns NOTDONE for {expr}.
7872 */
7873 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007874get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007875{
Bram Moolenaar33570922005-01-25 22:26:29 +00007876 dict_T *d = NULL;
7877 typval_T tvkey;
7878 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007879 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007881 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007882 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007883
7884 /*
7885 * First check if it's not a curly-braces thing: {expr}.
7886 * Must do this without evaluating, otherwise a function may be called
7887 * twice. Unfortunately this means we need to call eval1() twice for the
7888 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007889 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007890 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007891 if (*start != '}')
7892 {
7893 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7894 return FAIL;
7895 if (*start == '}')
7896 return NOTDONE;
7897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007898
7899 if (evaluate)
7900 {
7901 d = dict_alloc();
7902 if (d == NULL)
7903 return FAIL;
7904 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007905 tvkey.v_type = VAR_UNKNOWN;
7906 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007907
7908 *arg = skipwhite(*arg + 1);
7909 while (**arg != '}' && **arg != NUL)
7910 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007911 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007912 goto failret;
7913 if (**arg != ':')
7914 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007915 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007916 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007917 goto failret;
7918 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007919 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007920 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007921 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007922 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007923 {
7924 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007925 clear_tv(&tvkey);
7926 goto failret;
7927 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007928 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929
7930 *arg = skipwhite(*arg + 1);
7931 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7932 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007933 if (evaluate)
7934 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 goto failret;
7936 }
7937 if (evaluate)
7938 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007939 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007940 if (item != NULL)
7941 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007942 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007943 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 clear_tv(&tv);
7945 goto failret;
7946 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007947 item = dictitem_alloc(key);
7948 clear_tv(&tvkey);
7949 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007951 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007952 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007953 if (dict_add(d, item) == FAIL)
7954 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 }
7956 }
7957
7958 if (**arg == '}')
7959 break;
7960 if (**arg != ',')
7961 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007962 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007963 goto failret;
7964 }
7965 *arg = skipwhite(*arg + 1);
7966 }
7967
7968 if (**arg != '}')
7969 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007970 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971failret:
7972 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007973 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007974 return FAIL;
7975 }
7976
7977 *arg = skipwhite(*arg + 1);
7978 if (evaluate)
7979 {
7980 rettv->v_type = VAR_DICT;
7981 rettv->vval.v_dict = d;
7982 ++d->dv_refcount;
7983 }
7984
7985 return OK;
7986}
7987
Bram Moolenaar17a13432016-01-24 14:22:10 +01007988 static char *
7989get_var_special_name(int nr)
7990{
7991 switch (nr)
7992 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007993 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007994 case VVAL_TRUE: return "v:true";
7995 case VVAL_NONE: return "v:none";
7996 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007997 }
7998 EMSG2(_(e_intern2), "get_var_special_name()");
7999 return "42";
8000}
8001
Bram Moolenaar8c711452005-01-14 21:53:12 +00008002/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008003 * Return a string with the string representation of a variable.
8004 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008005 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008007 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008008 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008009 */
8010 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008011echo_string(
8012 typval_T *tv,
8013 char_u **tofree,
8014 char_u *numbuf,
8015 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008016{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008017 static int recurse = 0;
8018 char_u *r = NULL;
8019
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008021 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008022 if (!did_echo_string_emsg)
8023 {
8024 /* Only give this message once for a recursive call to avoid
8025 * flooding the user with errors. And stop iterating over lists
8026 * and dicts. */
8027 did_echo_string_emsg = TRUE;
8028 EMSG(_("E724: variable nested too deep for displaying"));
8029 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008030 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008031 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008032 }
8033 ++recurse;
8034
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008035 switch (tv->v_type)
8036 {
8037 case VAR_FUNC:
8038 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008039 r = tv->vval.v_string;
8040 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008041
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008042 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008043 {
8044 partial_T *pt = tv->vval.v_partial;
8045 char_u *fname = string_quote(pt == NULL ? NULL
8046 : pt->pt_name, FALSE);
8047 garray_T ga;
8048 int i;
8049 char_u *tf;
8050
8051 ga_init2(&ga, 1, 100);
8052 ga_concat(&ga, (char_u *)"function(");
8053 if (fname != NULL)
8054 {
8055 ga_concat(&ga, fname);
8056 vim_free(fname);
8057 }
8058 if (pt != NULL && pt->pt_argc > 0)
8059 {
8060 ga_concat(&ga, (char_u *)", [");
8061 for (i = 0; i < pt->pt_argc; ++i)
8062 {
8063 if (i > 0)
8064 ga_concat(&ga, (char_u *)", ");
8065 ga_concat(&ga,
8066 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8067 vim_free(tf);
8068 }
8069 ga_concat(&ga, (char_u *)"]");
8070 }
8071 if (pt != NULL && pt->pt_dict != NULL)
8072 {
8073 typval_T dtv;
8074
8075 ga_concat(&ga, (char_u *)", ");
8076 dtv.v_type = VAR_DICT;
8077 dtv.vval.v_dict = pt->pt_dict;
8078 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8079 vim_free(tf);
8080 }
8081 ga_concat(&ga, (char_u *)")");
8082
8083 *tofree = ga.ga_data;
8084 r = *tofree;
8085 break;
8086 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008087
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008088 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008089 if (tv->vval.v_list == NULL)
8090 {
8091 *tofree = NULL;
8092 r = NULL;
8093 }
8094 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8095 {
8096 *tofree = NULL;
8097 r = (char_u *)"[...]";
8098 }
8099 else
8100 {
8101 tv->vval.v_list->lv_copyID = copyID;
8102 *tofree = list2string(tv, copyID);
8103 r = *tofree;
8104 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008105 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008106
Bram Moolenaar8c711452005-01-14 21:53:12 +00008107 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008108 if (tv->vval.v_dict == NULL)
8109 {
8110 *tofree = NULL;
8111 r = NULL;
8112 }
8113 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8114 {
8115 *tofree = NULL;
8116 r = (char_u *)"{...}";
8117 }
8118 else
8119 {
8120 tv->vval.v_dict->dv_copyID = copyID;
8121 *tofree = dict2string(tv, copyID);
8122 r = *tofree;
8123 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008124 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008125
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008126 case VAR_STRING:
8127 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008128 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008129 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008130 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008131 *tofree = NULL;
8132 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008133 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008134
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008135 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008136#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008137 *tofree = NULL;
8138 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8139 r = numbuf;
8140 break;
8141#endif
8142
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008143 case VAR_SPECIAL:
8144 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008145 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008146 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008147 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008148
Bram Moolenaar8502c702014-06-17 12:51:16 +02008149 if (--recurse == 0)
8150 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008151 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008152}
8153
8154/*
8155 * Return a string with the string representation of a variable.
8156 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8157 * "numbuf" is used for a number.
8158 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008159 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008160 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008161 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008162tv2string(
8163 typval_T *tv,
8164 char_u **tofree,
8165 char_u *numbuf,
8166 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008167{
8168 switch (tv->v_type)
8169 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008170 case VAR_FUNC:
8171 *tofree = string_quote(tv->vval.v_string, TRUE);
8172 return *tofree;
8173 case VAR_STRING:
8174 *tofree = string_quote(tv->vval.v_string, FALSE);
8175 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008176 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008177#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008178 *tofree = NULL;
8179 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8180 return numbuf;
8181#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008182 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008183 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008184 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008185 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008186 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008187 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008188 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008189 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008190 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008191 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008192 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008193}
8194
8195/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008196 * Return string "str" in ' quotes, doubling ' characters.
8197 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008198 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008199 */
8200 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008201string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008202{
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008204 char_u *p, *r, *s;
8205
Bram Moolenaar33570922005-01-25 22:26:29 +00008206 len = (function ? 13 : 3);
8207 if (str != NULL)
8208 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008209 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008210 for (p = str; *p != NUL; mb_ptr_adv(p))
8211 if (*p == '\'')
8212 ++len;
8213 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008214 s = r = alloc(len);
8215 if (r != NULL)
8216 {
8217 if (function)
8218 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008219 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008220 r += 10;
8221 }
8222 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008223 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008224 if (str != NULL)
8225 for (p = str; *p != NUL; )
8226 {
8227 if (*p == '\'')
8228 *r++ = '\'';
8229 MB_COPY_CHAR(p, r);
8230 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008231 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008232 if (function)
8233 *r++ = ')';
8234 *r++ = NUL;
8235 }
8236 return s;
8237}
8238
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008239#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008240/*
8241 * Convert the string "text" to a floating point number.
8242 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8243 * this always uses a decimal point.
8244 * Returns the length of the text that was consumed.
8245 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008246 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008247string2float(
8248 char_u *text,
8249 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008250{
8251 char *s = (char *)text;
8252 float_T f;
8253
8254 f = strtod(s, &s);
8255 *value = f;
8256 return (int)((char_u *)s - text);
8257}
8258#endif
8259
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 * Get the value of an environment variable.
8262 * "arg" is pointing to the '$'. It is advanced to after the name.
8263 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008264 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 */
8266 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008267get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268{
8269 char_u *string = NULL;
8270 int len;
8271 int cc;
8272 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008273 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274
8275 ++*arg;
8276 name = *arg;
8277 len = get_env_len(arg);
8278 if (evaluate)
8279 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008280 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008281 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008282
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008283 cc = name[len];
8284 name[len] = NUL;
8285 /* first try vim_getenv(), fast for normal environment vars */
8286 string = vim_getenv(name, &mustfree);
8287 if (string != NULL && *string != NUL)
8288 {
8289 if (!mustfree)
8290 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008292 else
8293 {
8294 if (mustfree)
8295 vim_free(string);
8296
8297 /* next try expanding things like $VIM and ${HOME} */
8298 string = expand_env_save(name - 1);
8299 if (string != NULL && *string == '$')
8300 {
8301 vim_free(string);
8302 string = NULL;
8303 }
8304 }
8305 name[len] = cc;
8306
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008307 rettv->v_type = VAR_STRING;
8308 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 }
8310
8311 return OK;
8312}
8313
8314/*
8315 * Array with names and number of arguments of all internal functions
8316 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8317 */
8318static struct fst
8319{
8320 char *f_name; /* function name */
8321 char f_min_argc; /* minimal number of arguments */
8322 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008323 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008324 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325} functions[] =
8326{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008327#ifdef FEAT_FLOAT
8328 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008329 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008330#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008331 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008332 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008333 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"append", 2, 2, f_append},
8335 {"argc", 0, 0, f_argc},
8336 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008337 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008338 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008339#ifdef FEAT_FLOAT
8340 {"asin", 1, 1, f_asin}, /* WJMc */
8341#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008342 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008343 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008344 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008345 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008346 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008347 {"assert_notequal", 2, 3, f_assert_notequal},
8348 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008349 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008350#ifdef FEAT_FLOAT
8351 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008352 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008355 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"bufexists", 1, 1, f_bufexists},
8357 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8358 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8359 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8360 {"buflisted", 1, 1, f_buflisted},
8361 {"bufloaded", 1, 1, f_bufloaded},
8362 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008363 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"bufwinnr", 1, 1, f_bufwinnr},
8365 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008366 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008367 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008368 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008369#ifdef FEAT_FLOAT
8370 {"ceil", 1, 1, f_ceil},
8371#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008372#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008373 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008374 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8375 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008376 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008377 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008378 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008379 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008380 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008381 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008382 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008383 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008384 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8385 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008386 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008387 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008388#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008389 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008390 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008392 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008394#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008395 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008396 {"complete_add", 1, 1, f_complete_add},
8397 {"complete_check", 0, 0, f_complete_check},
8398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008400 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008401#ifdef FEAT_FLOAT
8402 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008403 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008404#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008405 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008407 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008408 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008409 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008411 {"diff_filler", 1, 1, f_diff_filler},
8412 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008413 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008414 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008416 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"eventhandler", 0, 0, f_eventhandler},
8418 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008419 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008421#ifdef FEAT_FLOAT
8422 {"exp", 1, 1, f_exp},
8423#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008424 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008425 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008426 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8428 {"filereadable", 1, 1, f_filereadable},
8429 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008430 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008431 {"finddir", 1, 3, f_finddir},
8432 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008433#ifdef FEAT_FLOAT
8434 {"float2nr", 1, 1, f_float2nr},
8435 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008436 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008437#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008438 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {"fnamemodify", 2, 2, f_fnamemodify},
8440 {"foldclosed", 1, 1, f_foldclosed},
8441 {"foldclosedend", 1, 1, f_foldclosedend},
8442 {"foldlevel", 1, 1, f_foldlevel},
8443 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008444 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008446 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008447 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008448 {"garbagecollect_for_testing", 0, 0, f_garbagecollect_for_testing},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008449 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008450 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008451 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {"getchar", 0, 1, f_getchar},
8453 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008454 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {"getcmdline", 0, 0, f_getcmdline},
8456 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008457 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008458 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008459 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008460 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008461 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008462 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {"getfsize", 1, 1, f_getfsize},
8464 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008465 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008466 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008467 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008468 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008469 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008470 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008471 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008472 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008474 {"gettabvar", 2, 3, f_gettabvar},
8475 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {"getwinposx", 0, 0, f_getwinposx},
8477 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008478 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008479 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008480 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008481 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008483 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008484 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008485 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8487 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8488 {"histadd", 2, 2, f_histadd},
8489 {"histdel", 1, 2, f_histdel},
8490 {"histget", 1, 2, f_histget},
8491 {"histnr", 1, 1, f_histnr},
8492 {"hlID", 1, 1, f_hlID},
8493 {"hlexists", 1, 1, f_hlexists},
8494 {"hostname", 0, 0, f_hostname},
8495 {"iconv", 3, 3, f_iconv},
8496 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008497 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008498 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008500 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 {"inputrestore", 0, 0, f_inputrestore},
8502 {"inputsave", 0, 0, f_inputsave},
8503 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008504 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008505 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008507 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008508#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8509 {"isnan", 1, 1, f_isnan},
8510#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008511 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008512#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008513 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008514 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008515 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008516 {"job_start", 1, 2, f_job_start},
8517 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008518 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008519#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008520 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008521 {"js_decode", 1, 1, f_js_decode},
8522 {"js_encode", 1, 1, f_js_encode},
8523 {"json_decode", 1, 1, f_json_decode},
8524 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008525 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 {"libcall", 3, 3, f_libcall},
8529 {"libcallnr", 3, 3, f_libcallnr},
8530 {"line", 1, 1, f_line},
8531 {"line2byte", 1, 1, f_line2byte},
8532 {"lispindent", 1, 1, f_lispindent},
8533 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008534#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008535 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008536 {"log10", 1, 1, f_log10},
8537#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008538#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008539 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008540#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008541 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008542 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008543 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008544 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008545 {"matchadd", 2, 5, f_matchadd},
8546 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008547 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008548 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008549 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008550 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008551 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008552 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008553 {"max", 1, 1, f_max},
8554 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008555#ifdef vim_mkdir
8556 {"mkdir", 1, 3, f_mkdir},
8557#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008558 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008559#ifdef FEAT_MZSCHEME
8560 {"mzeval", 1, 1, f_mzeval},
8561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008563 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008564 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008565 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008566#ifdef FEAT_PERL
8567 {"perleval", 1, 1, f_perleval},
8568#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008569#ifdef FEAT_FLOAT
8570 {"pow", 2, 2, f_pow},
8571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008573 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008574 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008575#ifdef FEAT_PYTHON3
8576 {"py3eval", 1, 1, f_py3eval},
8577#endif
8578#ifdef FEAT_PYTHON
8579 {"pyeval", 1, 1, f_pyeval},
8580#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008581 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008582 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008583 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008584#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008585 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008586#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008587 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 {"remote_expr", 2, 3, f_remote_expr},
8589 {"remote_foreground", 1, 1, f_remote_foreground},
8590 {"remote_peek", 1, 2, f_remote_peek},
8591 {"remote_read", 1, 1, f_remote_read},
8592 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008593 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008595 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008597 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008598#ifdef FEAT_FLOAT
8599 {"round", 1, 1, f_round},
8600#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008601 {"screenattr", 2, 2, f_screenattr},
8602 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008603 {"screencol", 0, 0, f_screencol},
8604 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008605 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008606 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008607 {"searchpair", 3, 7, f_searchpair},
8608 {"searchpairpos", 3, 7, f_searchpairpos},
8609 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 {"server2client", 2, 2, f_server2client},
8611 {"serverlist", 0, 0, f_serverlist},
8612 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008613 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008615 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008617 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008618 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008619 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008620 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008622 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008623 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008625#ifdef FEAT_CRYPT
8626 {"sha256", 1, 1, f_sha256},
8627#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008628 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008629 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008631#ifdef FEAT_FLOAT
8632 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008633 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008634#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008635 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008636 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008637 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008638 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008639 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008640#ifdef FEAT_FLOAT
8641 {"sqrt", 1, 1, f_sqrt},
8642 {"str2float", 1, 1, f_str2float},
8643#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008644 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008645 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008646 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008647 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648#ifdef HAVE_STRFTIME
8649 {"strftime", 1, 2, f_strftime},
8650#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008651 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008652 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008653 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 {"strlen", 1, 1, f_strlen},
8655 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008656 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008658 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008659 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 {"substitute", 4, 4, f_substitute},
8661 {"synID", 3, 3, f_synID},
8662 {"synIDattr", 2, 3, f_synIDattr},
8663 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008664 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008665 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008666 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008667 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008668 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008669 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008670 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008671 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008672 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008673#ifdef FEAT_FLOAT
8674 {"tan", 1, 1, f_tan},
8675 {"tanh", 1, 1, f_tanh},
8676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008678 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008679#ifdef FEAT_TIMERS
8680 {"timer_start", 2, 3, f_timer_start},
8681 {"timer_stop", 1, 1, f_timer_stop},
8682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 {"tolower", 1, 1, f_tolower},
8684 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008685 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686#ifdef FEAT_FLOAT
8687 {"trunc", 1, 1, f_trunc},
8688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008690 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008691 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008692 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008693 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 {"virtcol", 1, 1, f_virtcol},
8695 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008696 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008697 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008698 {"win_getid", 0, 2, f_win_getid},
8699 {"win_gotoid", 1, 1, f_win_gotoid},
8700 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8701 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 {"winbufnr", 1, 1, f_winbufnr},
8703 {"wincol", 0, 0, f_wincol},
8704 {"winheight", 1, 1, f_winheight},
8705 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008706 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008708 {"winrestview", 1, 1, f_winrestview},
8709 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008711 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008712 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008713 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714};
8715
8716#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8717
8718/*
8719 * Function given to ExpandGeneric() to obtain the list of internal
8720 * or user defined function names.
8721 */
8722 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008723get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725 static int intidx = -1;
8726 char_u *name;
8727
8728 if (idx == 0)
8729 intidx = -1;
8730 if (intidx < 0)
8731 {
8732 name = get_user_func_name(xp, idx);
8733 if (name != NULL)
8734 return name;
8735 }
8736 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8737 {
8738 STRCPY(IObuff, functions[intidx].f_name);
8739 STRCAT(IObuff, "(");
8740 if (functions[intidx].f_max_argc == 0)
8741 STRCAT(IObuff, ")");
8742 return IObuff;
8743 }
8744
8745 return NULL;
8746}
8747
8748/*
8749 * Function given to ExpandGeneric() to obtain the list of internal or
8750 * user defined variable or function names.
8751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008753get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
8755 static int intidx = -1;
8756 char_u *name;
8757
8758 if (idx == 0)
8759 intidx = -1;
8760 if (intidx < 0)
8761 {
8762 name = get_function_name(xp, idx);
8763 if (name != NULL)
8764 return name;
8765 }
8766 return get_user_var_name(xp, ++intidx);
8767}
8768
8769#endif /* FEAT_CMDL_COMPL */
8770
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008771#if defined(EBCDIC) || defined(PROTO)
8772/*
8773 * Compare struct fst by function name.
8774 */
8775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008776compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008777{
8778 struct fst *p1 = (struct fst *)s1;
8779 struct fst *p2 = (struct fst *)s2;
8780
8781 return STRCMP(p1->f_name, p2->f_name);
8782}
8783
8784/*
8785 * Sort the function table by function name.
8786 * The sorting of the table above is ASCII dependant.
8787 * On machines using EBCDIC we have to sort it.
8788 */
8789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008790sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008791{
8792 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8793
8794 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8795}
8796#endif
8797
8798
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799/*
8800 * Find internal function in table above.
8801 * Return index, or -1 if not found
8802 */
8803 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008804find_internal_func(
8805 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806{
8807 int first = 0;
8808 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8809 int cmp;
8810 int x;
8811
8812 /*
8813 * Find the function name in the table. Binary search.
8814 */
8815 while (first <= last)
8816 {
8817 x = first + ((unsigned)(last - first) >> 1);
8818 cmp = STRCMP(name, functions[x].f_name);
8819 if (cmp < 0)
8820 last = x - 1;
8821 else if (cmp > 0)
8822 first = x + 1;
8823 else
8824 return x;
8825 }
8826 return -1;
8827}
8828
8829/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008830 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8831 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008832 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8833 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008834 */
8835 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008836deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008837{
Bram Moolenaar33570922005-01-25 22:26:29 +00008838 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008839 int cc;
8840
Bram Moolenaar65639032016-03-16 21:40:30 +01008841 if (partialp != NULL)
8842 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008843
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008844 cc = name[*lenp];
8845 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008846 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008847 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008848 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008849 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008851 {
8852 *lenp = 0;
8853 return (char_u *)""; /* just in case */
8854 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008855 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008857 }
8858
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008859 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8860 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008861 partial_T *pt = v->di_tv.vval.v_partial;
8862
8863 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008864 {
8865 *lenp = 0;
8866 return (char_u *)""; /* just in case */
8867 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008868 if (partialp != NULL)
8869 *partialp = pt;
8870 *lenp = (int)STRLEN(pt->pt_name);
8871 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008872 }
8873
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008874 return name;
8875}
8876
8877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 * Allocate a variable for the result of a function.
8879 * Return OK or FAIL.
8880 */
8881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008882get_func_tv(
8883 char_u *name, /* name of the function */
8884 int len, /* length of "name" */
8885 typval_T *rettv,
8886 char_u **arg, /* argument, pointing to the '(' */
8887 linenr_T firstline, /* first line of range */
8888 linenr_T lastline, /* last line of range */
8889 int *doesrange, /* return: function handled range */
8890 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008891 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008892 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893{
8894 char_u *argp;
8895 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008896 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 int argcount = 0; /* number of arguments found */
8898
8899 /*
8900 * Get the arguments.
8901 */
8902 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008903 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 {
8905 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8906 if (*argp == ')' || *argp == ',' || *argp == NUL)
8907 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8909 {
8910 ret = FAIL;
8911 break;
8912 }
8913 ++argcount;
8914 if (*argp != ',')
8915 break;
8916 }
8917 if (*argp == ')')
8918 ++argp;
8919 else
8920 ret = FAIL;
8921
8922 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008923 {
8924 int i = 0;
8925
8926 if (get_vim_var_nr(VV_TESTING))
8927 {
8928 /* Prepare for calling garbagecollect_for_testing(), need to know
8929 * what variables are used on the call stack. */
8930 if (funcargs.ga_itemsize == 0)
8931 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8932 for (i = 0; i < argcount; ++i)
8933 if (ga_grow(&funcargs, 1) == OK)
8934 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8935 &argvars[i];
8936 }
8937
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008939 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008940
8941 funcargs.ga_len -= i;
8942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 {
8945 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008946 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008947 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008948 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
8951 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
8954 *arg = skipwhite(argp);
8955 return ret;
8956}
8957
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008958#define ERROR_UNKNOWN 0
8959#define ERROR_TOOMANY 1
8960#define ERROR_TOOFEW 2
8961#define ERROR_SCRIPT 3
8962#define ERROR_DICT 4
8963#define ERROR_NONE 5
8964#define ERROR_OTHER 6
8965#define FLEN_FIXED 40
8966
8967/*
8968 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8969 * Change <SNR>123_name() to K_SNR 123_name().
8970 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8971 * (slow).
8972 */
8973 static char_u *
8974fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8975{
8976 int llen;
8977 char_u *fname;
8978 int i;
8979
8980 llen = eval_fname_script(name);
8981 if (llen > 0)
8982 {
8983 fname_buf[0] = K_SPECIAL;
8984 fname_buf[1] = KS_EXTRA;
8985 fname_buf[2] = (int)KE_SNR;
8986 i = 3;
8987 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8988 {
8989 if (current_SID <= 0)
8990 *error = ERROR_SCRIPT;
8991 else
8992 {
8993 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8994 i = (int)STRLEN(fname_buf);
8995 }
8996 }
8997 if (i + STRLEN(name + llen) < FLEN_FIXED)
8998 {
8999 STRCPY(fname_buf + i, name + llen);
9000 fname = fname_buf;
9001 }
9002 else
9003 {
9004 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9005 if (fname == NULL)
9006 *error = ERROR_OTHER;
9007 else
9008 {
9009 *tofree = fname;
9010 mch_memmove(fname, fname_buf, (size_t)i);
9011 STRCPY(fname + i, name + llen);
9012 }
9013 }
9014 }
9015 else
9016 fname = name;
9017 return fname;
9018}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
9020/*
9021 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009022 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009023 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009026call_func(
9027 char_u *funcname, /* name of the function */
9028 int len, /* length of "name" */
9029 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009030 int argcount_in, /* number of "argvars" */
9031 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009032 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009033 linenr_T firstline, /* first line of range */
9034 linenr_T lastline, /* last line of range */
9035 int *doesrange, /* return: function handled range */
9036 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009037 partial_T *partial, /* optional, can be NULL */
9038 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
9040 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 int error = ERROR_NONE;
9042 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009045 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009047 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009048 int argcount = argcount_in;
9049 typval_T *argvars = argvars_in;
9050 dict_T *selfdict = selfdict_in;
9051 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9052 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009053
9054 /* Make a copy of the name, if it comes from a funcref variable it could
9055 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009056 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009057 if (name == NULL)
9058 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009060 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061
9062 *doesrange = FALSE;
9063
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009064 if (partial != NULL)
9065 {
9066 if (partial->pt_dict != NULL)
9067 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009068 /* When the function has a partial with a dict and there is a dict
9069 * argument, use the dict argument. That is backwards compatible.
9070 */
9071 if (selfdict_in == NULL)
9072 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009073 }
9074 if (error == ERROR_NONE && partial->pt_argc > 0)
9075 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009076 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9077 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9078 for (i = 0; i < argcount_in; ++i)
9079 argv[i + argv_clear] = argvars_in[i];
9080 argvars = argv;
9081 argcount = partial->pt_argc + argcount_in;
9082 }
9083 }
9084
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
9086 /* execute the function if no errors detected and executing */
9087 if (evaluate && error == ERROR_NONE)
9088 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009089 char_u *rfname = fname;
9090
9091 /* Ignore "g:" before a function name. */
9092 if (fname[0] == 'g' && fname[1] == ':')
9093 rfname = fname + 2;
9094
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009095 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9096 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 error = ERROR_UNKNOWN;
9098
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009099 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 {
9101 /*
9102 * User defined function.
9103 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009104 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009105
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009107 /* Trigger FuncUndefined event, may load the function. */
9108 if (fp == NULL
9109 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009110 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009111 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009113 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009114 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 }
9116#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009117 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009118 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009119 {
9120 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009121 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009122 }
9123
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 if (fp != NULL)
9125 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009126 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009128 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009130 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009132 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009133 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 else
9135 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009136 int did_save_redo = FALSE;
9137
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 /*
9139 * Call the user function.
9140 * Save and restore search patterns, script variables and
9141 * redo buffer.
9142 */
9143 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009144#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009145 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009146#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009147 {
9148 saveRedobuff();
9149 did_save_redo = TRUE;
9150 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009151 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009153 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009154 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9155 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9156 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009157 /* Function was unreferenced while being used, free it
9158 * now. */
9159 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009160 if (did_save_redo)
9161 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 restore_search_patterns();
9163 error = ERROR_NONE;
9164 }
9165 }
9166 }
9167 else
9168 {
9169 /*
9170 * Find the function name in the table, call its implementation.
9171 */
9172 i = find_internal_func(fname);
9173 if (i >= 0)
9174 {
9175 if (argcount < functions[i].f_min_argc)
9176 error = ERROR_TOOFEW;
9177 else if (argcount > functions[i].f_max_argc)
9178 error = ERROR_TOOMANY;
9179 else
9180 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009181 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 error = ERROR_NONE;
9184 }
9185 }
9186 }
9187 /*
9188 * The function call (or "FuncUndefined" autocommand sequence) might
9189 * have been aborted by an error, an interrupt, or an explicitly thrown
9190 * exception that has not been caught so far. This situation can be
9191 * tested for by calling aborting(). For an error in an internal
9192 * function or for the "E132" error in call_user_func(), however, the
9193 * throw point at which the "force_abort" flag (temporarily reset by
9194 * emsg()) is normally updated has not been reached yet. We need to
9195 * update that flag first to make aborting() reliable.
9196 */
9197 update_force_abort();
9198 }
9199 if (error == ERROR_NONE)
9200 ret = OK;
9201
9202 /*
9203 * Report an error unless the argument evaluation or function call has been
9204 * cancelled due to an aborting error, an interrupt, or an exception.
9205 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009206 if (!aborting())
9207 {
9208 switch (error)
9209 {
9210 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009211 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009212 break;
9213 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009214 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009215 break;
9216 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009217 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009218 name);
9219 break;
9220 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009221 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009222 name);
9223 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009224 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009225 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009226 name);
9227 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009228 }
9229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009231 while (argv_clear > 0)
9232 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009233 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009234 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235
9236 return ret;
9237}
9238
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009239/*
9240 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009241 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009242 */
9243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009244emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009245{
9246 char_u *p;
9247
9248 if (*name == K_SPECIAL)
9249 p = concat_str((char_u *)"<SNR>", name + 3);
9250 else
9251 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009252 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009253 if (p != name)
9254 vim_free(p);
9255}
9256
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009257/*
9258 * Return TRUE for a non-zero Number and a non-empty String.
9259 */
9260 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009261non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009262{
9263 return ((argvars[0].v_type == VAR_NUMBER
9264 && argvars[0].vval.v_number != 0)
9265 || (argvars[0].v_type == VAR_STRING
9266 && argvars[0].vval.v_string != NULL
9267 && *argvars[0].vval.v_string != NUL));
9268}
9269
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270/*********************************************
9271 * Implementation of the built-in functions
9272 */
9273
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009274#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009275static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009276
9277/*
9278 * Get the float value of "argvars[0]" into "f".
9279 * Returns FAIL when the argument is not a Number or Float.
9280 */
9281 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009282get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009283{
9284 if (argvars[0].v_type == VAR_FLOAT)
9285 {
9286 *f = argvars[0].vval.v_float;
9287 return OK;
9288 }
9289 if (argvars[0].v_type == VAR_NUMBER)
9290 {
9291 *f = (float_T)argvars[0].vval.v_number;
9292 return OK;
9293 }
9294 EMSG(_("E808: Number or Float required"));
9295 return FAIL;
9296}
9297
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009298/*
9299 * "abs(expr)" function
9300 */
9301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009302f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009303{
9304 if (argvars[0].v_type == VAR_FLOAT)
9305 {
9306 rettv->v_type = VAR_FLOAT;
9307 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9308 }
9309 else
9310 {
9311 varnumber_T n;
9312 int error = FALSE;
9313
9314 n = get_tv_number_chk(&argvars[0], &error);
9315 if (error)
9316 rettv->vval.v_number = -1;
9317 else if (n > 0)
9318 rettv->vval.v_number = n;
9319 else
9320 rettv->vval.v_number = -n;
9321 }
9322}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009323
9324/*
9325 * "acos()" function
9326 */
9327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009328f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009329{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009330 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009331
9332 rettv->v_type = VAR_FLOAT;
9333 if (get_float_arg(argvars, &f) == OK)
9334 rettv->vval.v_float = acos(f);
9335 else
9336 rettv->vval.v_float = 0.0;
9337}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009338#endif
9339
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009341 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 */
9343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009344f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345{
Bram Moolenaar33570922005-01-25 22:26:29 +00009346 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009348 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009349 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009351 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009352 && !tv_check_lock(l->lv_lock,
9353 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009354 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009356 }
9357 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009358 EMSG(_(e_listreq));
9359}
9360
9361/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009362 * "alloc_fail(id, countdown, repeat)" function
9363 */
9364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009365f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009366{
9367 if (argvars[0].v_type != VAR_NUMBER
9368 || argvars[0].vval.v_number <= 0
9369 || argvars[1].v_type != VAR_NUMBER
9370 || argvars[1].vval.v_number < 0
9371 || argvars[2].v_type != VAR_NUMBER)
9372 EMSG(_(e_invarg));
9373 else
9374 {
9375 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009376 if (alloc_fail_id >= aid_last)
9377 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009378 alloc_fail_countdown = argvars[1].vval.v_number;
9379 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009380 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009381 }
9382}
9383
9384/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009385 * "and(expr, expr)" function
9386 */
9387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009388f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009389{
9390 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9391 & get_tv_number_chk(&argvars[1], NULL);
9392}
9393
9394/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009395 * "append(lnum, string/list)" function
9396 */
9397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009398f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009399{
9400 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009401 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009402 list_T *l = NULL;
9403 listitem_T *li = NULL;
9404 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009405 long added = 0;
9406
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009407 /* When coming here from Insert mode, sync undo, so that this can be
9408 * undone separately from what was previously inserted. */
9409 if (u_sync_once == 2)
9410 {
9411 u_sync_once = 1; /* notify that u_sync() was called */
9412 u_sync(TRUE);
9413 }
9414
Bram Moolenaar0d660222005-01-07 21:51:51 +00009415 lnum = get_tv_lnum(argvars);
9416 if (lnum >= 0
9417 && lnum <= curbuf->b_ml.ml_line_count
9418 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009419 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009420 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009421 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009422 l = argvars[1].vval.v_list;
9423 if (l == NULL)
9424 return;
9425 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009426 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009427 for (;;)
9428 {
9429 if (l == NULL)
9430 tv = &argvars[1]; /* append a string */
9431 else if (li == NULL)
9432 break; /* end of list */
9433 else
9434 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009435 line = get_tv_string_chk(tv);
9436 if (line == NULL) /* type error */
9437 {
9438 rettv->vval.v_number = 1; /* Failed */
9439 break;
9440 }
9441 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009442 ++added;
9443 if (l == NULL)
9444 break;
9445 li = li->li_next;
9446 }
9447
9448 appended_lines_mark(lnum, added);
9449 if (curwin->w_cursor.lnum > lnum)
9450 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009452 else
9453 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454}
9455
9456/*
9457 * "argc()" function
9458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009460f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463}
9464
9465/*
9466 * "argidx()" function
9467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009469f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472}
9473
9474/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009475 * "arglistid()" function
9476 */
9477 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009478f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009479{
9480 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009481
9482 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009483 wp = find_tabwin(&argvars[0], &argvars[1]);
9484 if (wp != NULL)
9485 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009486}
9487
9488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 * "argv(nr)" function
9490 */
9491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009492f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493{
9494 int idx;
9495
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009496 if (argvars[0].v_type != VAR_UNKNOWN)
9497 {
9498 idx = get_tv_number_chk(&argvars[0], NULL);
9499 if (idx >= 0 && idx < ARGCOUNT)
9500 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9501 else
9502 rettv->vval.v_string = NULL;
9503 rettv->v_type = VAR_STRING;
9504 }
9505 else if (rettv_list_alloc(rettv) == OK)
9506 for (idx = 0; idx < ARGCOUNT; ++idx)
9507 list_append_string(rettv->vval.v_list,
9508 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509}
9510
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009511typedef enum
9512{
9513 ASSERT_EQUAL,
9514 ASSERT_NOTEQUAL,
9515 ASSERT_MATCH,
9516 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009517 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009518} assert_type_T;
9519
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009520static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009521static 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 +01009522static void assert_error(garray_T *gap);
9523static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009524
9525/*
9526 * Prepare "gap" for an assert error and add the sourcing position.
9527 */
9528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009529prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009530{
9531 char buf[NUMBUFLEN];
9532
9533 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009534 if (sourcing_name != NULL)
9535 {
9536 ga_concat(gap, sourcing_name);
9537 if (sourcing_lnum > 0)
9538 ga_concat(gap, (char_u *)" ");
9539 }
9540 if (sourcing_lnum > 0)
9541 {
9542 sprintf(buf, "line %ld", (long)sourcing_lnum);
9543 ga_concat(gap, (char_u *)buf);
9544 }
9545 if (sourcing_name != NULL || sourcing_lnum > 0)
9546 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009547}
9548
9549/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009550 * Append "str" to "gap", escaping unprintable characters.
9551 * Changes NL to \n, CR to \r, etc.
9552 */
9553 static void
9554ga_concat_esc(garray_T *gap, char_u *str)
9555{
9556 char_u *p;
9557 char_u buf[NUMBUFLEN];
9558
Bram Moolenaarf1551962016-03-15 12:55:58 +01009559 if (str == NULL)
9560 {
9561 ga_concat(gap, (char_u *)"NULL");
9562 return;
9563 }
9564
Bram Moolenaar23689172016-02-15 22:37:37 +01009565 for (p = str; *p != NUL; ++p)
9566 switch (*p)
9567 {
9568 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9569 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9570 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9571 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9572 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9573 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9574 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9575 default:
9576 if (*p < ' ')
9577 {
9578 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9579 ga_concat(gap, buf);
9580 }
9581 else
9582 ga_append(gap, *p);
9583 break;
9584 }
9585}
9586
9587/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009588 * Fill "gap" with information about an assert error.
9589 */
9590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009591fill_assert_error(
9592 garray_T *gap,
9593 typval_T *opt_msg_tv,
9594 char_u *exp_str,
9595 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009596 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009597 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009598{
9599 char_u numbuf[NUMBUFLEN];
9600 char_u *tofree;
9601
9602 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9603 {
9604 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9605 vim_free(tofree);
9606 }
9607 else
9608 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009609 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009610 ga_concat(gap, (char_u *)"Pattern ");
9611 else
9612 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009613 if (exp_str == NULL)
9614 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009615 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009616 vim_free(tofree);
9617 }
9618 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009619 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009620 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009621 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009622 else if (atype == ASSERT_NOTMATCH)
9623 ga_concat(gap, (char_u *)" does match ");
9624 else if (atype == ASSERT_NOTEQUAL)
9625 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009626 else
9627 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009628 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009629 vim_free(tofree);
9630 }
9631}
Bram Moolenaar43345542015-11-29 17:35:35 +01009632
9633/*
9634 * Add an assert error to v:errors.
9635 */
9636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009637assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009638{
9639 struct vimvar *vp = &vimvars[VV_ERRORS];
9640
9641 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9642 /* Make sure v:errors is a list. */
9643 set_vim_var_list(VV_ERRORS, list_alloc());
9644 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9645}
9646
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009647 static void
9648assert_equal_common(typval_T *argvars, assert_type_T atype)
9649{
9650 garray_T ga;
9651
9652 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9653 != (atype == ASSERT_EQUAL))
9654 {
9655 prepare_assert_error(&ga);
9656 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9657 atype);
9658 assert_error(&ga);
9659 ga_clear(&ga);
9660 }
9661}
9662
Bram Moolenaar43345542015-11-29 17:35:35 +01009663/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009664 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009665 */
9666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009667f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009668{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009669 assert_equal_common(argvars, ASSERT_EQUAL);
9670}
Bram Moolenaar43345542015-11-29 17:35:35 +01009671
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009672/*
9673 * "assert_notequal(expected, actual[, msg])" function
9674 */
9675 static void
9676f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9677{
9678 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009679}
9680
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009681/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009682 * "assert_exception(string[, msg])" function
9683 */
9684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009685f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009686{
9687 garray_T ga;
9688 char *error;
9689
9690 error = (char *)get_tv_string_chk(&argvars[0]);
9691 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9692 {
9693 prepare_assert_error(&ga);
9694 ga_concat(&ga, (char_u *)"v:exception is not set");
9695 assert_error(&ga);
9696 ga_clear(&ga);
9697 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009698 else if (error != NULL
9699 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009700 {
9701 prepare_assert_error(&ga);
9702 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009703 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009704 assert_error(&ga);
9705 ga_clear(&ga);
9706 }
9707}
9708
9709/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009710 * "assert_fails(cmd [, error])" function
9711 */
9712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009713f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009714{
9715 char_u *cmd = get_tv_string_chk(&argvars[0]);
9716 garray_T ga;
9717
9718 called_emsg = FALSE;
9719 suppress_errthrow = TRUE;
9720 emsg_silent = TRUE;
9721 do_cmdline_cmd(cmd);
9722 if (!called_emsg)
9723 {
9724 prepare_assert_error(&ga);
9725 ga_concat(&ga, (char_u *)"command did not fail: ");
9726 ga_concat(&ga, cmd);
9727 assert_error(&ga);
9728 ga_clear(&ga);
9729 }
9730 else if (argvars[1].v_type != VAR_UNKNOWN)
9731 {
9732 char_u buf[NUMBUFLEN];
9733 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9734
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009735 if (error == NULL
9736 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009737 {
9738 prepare_assert_error(&ga);
9739 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009740 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009741 assert_error(&ga);
9742 ga_clear(&ga);
9743 }
9744 }
9745
9746 called_emsg = FALSE;
9747 suppress_errthrow = FALSE;
9748 emsg_silent = FALSE;
9749 emsg_on_display = FALSE;
9750 set_vim_var_string(VV_ERRMSG, NULL, 0);
9751}
9752
9753/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009754 * Common for assert_true() and assert_false().
9755 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009757assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009758{
9759 int error = FALSE;
9760 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009761
Bram Moolenaar37127922016-02-06 20:29:28 +01009762 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009763 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009764 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009765 if (argvars[0].v_type != VAR_NUMBER
9766 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9767 || error)
9768 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009769 prepare_assert_error(&ga);
9770 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009771 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009772 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009773 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009774 ga_clear(&ga);
9775 }
9776}
9777
9778/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009779 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009780 */
9781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009782f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009783{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009784 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009785}
9786
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009787 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009788assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009789{
9790 garray_T ga;
9791 char_u buf1[NUMBUFLEN];
9792 char_u buf2[NUMBUFLEN];
9793 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9794 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9795
Bram Moolenaar72188e92016-03-28 22:48:29 +02009796 if (pat == NULL || text == NULL)
9797 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009798 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009799 {
9800 prepare_assert_error(&ga);
9801 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009802 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009803 assert_error(&ga);
9804 ga_clear(&ga);
9805 }
9806}
9807
9808/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009809 * "assert_match(pattern, actual[, msg])" function
9810 */
9811 static void
9812f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9813{
9814 assert_match_common(argvars, ASSERT_MATCH);
9815}
9816
9817/*
9818 * "assert_notmatch(pattern, actual[, msg])" function
9819 */
9820 static void
9821f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9822{
9823 assert_match_common(argvars, ASSERT_NOTMATCH);
9824}
9825
9826/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009827 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009828 */
9829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009830f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009831{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009832 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009833}
9834
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009835#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009836/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009837 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009838 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009840f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009841{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009842 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009843
9844 rettv->v_type = VAR_FLOAT;
9845 if (get_float_arg(argvars, &f) == OK)
9846 rettv->vval.v_float = asin(f);
9847 else
9848 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009849}
9850
9851/*
9852 * "atan()" function
9853 */
9854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009855f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009856{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009857 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009858
9859 rettv->v_type = VAR_FLOAT;
9860 if (get_float_arg(argvars, &f) == OK)
9861 rettv->vval.v_float = atan(f);
9862 else
9863 rettv->vval.v_float = 0.0;
9864}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009865
9866/*
9867 * "atan2()" function
9868 */
9869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009870f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009871{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009872 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009873
9874 rettv->v_type = VAR_FLOAT;
9875 if (get_float_arg(argvars, &fx) == OK
9876 && get_float_arg(&argvars[1], &fy) == OK)
9877 rettv->vval.v_float = atan2(fx, fy);
9878 else
9879 rettv->vval.v_float = 0.0;
9880}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009881#endif
9882
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883/*
9884 * "browse(save, title, initdir, default)" function
9885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009887f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
9889#ifdef FEAT_BROWSE
9890 int save;
9891 char_u *title;
9892 char_u *initdir;
9893 char_u *defname;
9894 char_u buf[NUMBUFLEN];
9895 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009896 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898 save = get_tv_number_chk(&argvars[0], &error);
9899 title = get_tv_string_chk(&argvars[1]);
9900 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9901 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009903 if (error || title == NULL || initdir == NULL || defname == NULL)
9904 rettv->vval.v_string = NULL;
9905 else
9906 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009907 do_browse(save ? BROWSE_SAVE : 0,
9908 title, defname, NULL, initdir, NULL, curbuf);
9909#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009910 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009911#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009913}
9914
9915/*
9916 * "browsedir(title, initdir)" function
9917 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009919f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009920{
9921#ifdef FEAT_BROWSE
9922 char_u *title;
9923 char_u *initdir;
9924 char_u buf[NUMBUFLEN];
9925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009926 title = get_tv_string_chk(&argvars[0]);
9927 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009929 if (title == NULL || initdir == NULL)
9930 rettv->vval.v_string = NULL;
9931 else
9932 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009933 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938}
9939
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009940static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009941
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942/*
9943 * Find a buffer by number or exact name.
9944 */
9945 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009946find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947{
9948 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009950 if (avar->v_type == VAR_NUMBER)
9951 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009952 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009954 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009955 if (buf == NULL)
9956 {
9957 /* No full path name match, try a match with a URL or a "nofile"
9958 * buffer, these don't use the full path. */
9959 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9960 if (buf->b_fname != NULL
9961 && (path_with_url(buf->b_fname)
9962#ifdef FEAT_QUICKFIX
9963 || bt_nofile(buf)
9964#endif
9965 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009966 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009967 break;
9968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 }
9970 return buf;
9971}
9972
9973/*
9974 * "bufexists(expr)" function
9975 */
9976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009977f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980}
9981
9982/*
9983 * "buflisted(expr)" function
9984 */
9985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009986f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987{
9988 buf_T *buf;
9989
9990 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009991 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992}
9993
9994/*
9995 * "bufloaded(expr)" function
9996 */
9997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009998f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
10000 buf_T *buf;
10001
10002 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004}
10005
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010006 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010007buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 int save_magic;
10010 char_u *save_cpo;
10011 buf_T *buf;
10012
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10014 save_magic = p_magic;
10015 p_magic = TRUE;
10016 save_cpo = p_cpo;
10017 p_cpo = (char_u *)"";
10018
10019 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010020 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021
10022 p_magic = save_magic;
10023 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010024 return buf;
10025}
10026
10027/*
10028 * Get buffer by number or pattern.
10029 */
10030 static buf_T *
10031get_buf_tv(typval_T *tv, int curtab_only)
10032{
10033 char_u *name = tv->vval.v_string;
10034 buf_T *buf;
10035
10036 if (tv->v_type == VAR_NUMBER)
10037 return buflist_findnr((int)tv->vval.v_number);
10038 if (tv->v_type != VAR_STRING)
10039 return NULL;
10040 if (name == NULL || *name == NUL)
10041 return curbuf;
10042 if (name[0] == '$' && name[1] == NUL)
10043 return lastbuf;
10044
10045 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046
10047 /* If not found, try expanding the name, like done for bufexists(). */
10048 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
10051 return buf;
10052}
10053
10054/*
10055 * "bufname(expr)" function
10056 */
10057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010058f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059{
10060 buf_T *buf;
10061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010062 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010064 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010065 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010067 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010069 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 --emsg_off;
10071}
10072
10073/*
10074 * "bufnr(expr)" function
10075 */
10076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010077f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078{
10079 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010080 int error = FALSE;
10081 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010083 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010085 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010086 --emsg_off;
10087
10088 /* If the buffer isn't found and the second argument is not zero create a
10089 * new buffer. */
10090 if (buf == NULL
10091 && argvars[1].v_type != VAR_UNKNOWN
10092 && get_tv_number_chk(&argvars[1], &error) != 0
10093 && !error
10094 && (name = get_tv_string_chk(&argvars[0])) != NULL
10095 && !error)
10096 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10097
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102}
10103
10104/*
10105 * "bufwinnr(nr)" function
10106 */
10107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010108f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109{
10110#ifdef FEAT_WINDOWS
10111 win_T *wp;
10112 int winnr = 0;
10113#endif
10114 buf_T *buf;
10115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010116 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010118 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119#ifdef FEAT_WINDOWS
10120 for (wp = firstwin; wp; wp = wp->w_next)
10121 {
10122 ++winnr;
10123 if (wp->w_buffer == buf)
10124 break;
10125 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010126 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010128 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129#endif
10130 --emsg_off;
10131}
10132
10133/*
10134 * "byte2line(byte)" function
10135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010137f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138{
10139#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141#else
10142 long boff = 0;
10143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010144 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 (linenr_T)0, &boff);
10150#endif
10151}
10152
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010154byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010155{
10156#ifdef FEAT_MBYTE
10157 char_u *t;
10158#endif
10159 char_u *str;
10160 long idx;
10161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010162 str = get_tv_string_chk(&argvars[0]);
10163 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010165 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010166 return;
10167
10168#ifdef FEAT_MBYTE
10169 t = str;
10170 for ( ; idx > 0; idx--)
10171 {
10172 if (*t == NUL) /* EOL reached */
10173 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010174 if (enc_utf8 && comp)
10175 t += utf_ptr2len(t);
10176 else
10177 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010178 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010179 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010180#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010181 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010183#endif
10184}
10185
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010186/*
10187 * "byteidx()" function
10188 */
10189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010190f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010191{
10192 byteidx(argvars, rettv, FALSE);
10193}
10194
10195/*
10196 * "byteidxcomp()" function
10197 */
10198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010199f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010200{
10201 byteidx(argvars, rettv, TRUE);
10202}
10203
Bram Moolenaardb913952012-06-29 12:54:53 +020010204 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010205func_call(
10206 char_u *name,
10207 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010208 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010209 dict_T *selfdict,
10210 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010211{
10212 listitem_T *item;
10213 typval_T argv[MAX_FUNC_ARGS + 1];
10214 int argc = 0;
10215 int dummy;
10216 int r = 0;
10217
10218 for (item = args->vval.v_list->lv_first; item != NULL;
10219 item = item->li_next)
10220 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010221 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010222 {
10223 EMSG(_("E699: Too many arguments"));
10224 break;
10225 }
10226 /* Make a copy of each argument. This is needed to be able to set
10227 * v_lock to VAR_FIXED in the copy without changing the original list.
10228 */
10229 copy_tv(&item->li_tv, &argv[argc++]);
10230 }
10231
10232 if (item == NULL)
10233 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10234 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010235 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010236
10237 /* Free the arguments. */
10238 while (argc > 0)
10239 clear_tv(&argv[--argc]);
10240
10241 return r;
10242}
10243
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010244/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010245 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010246 */
10247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010248f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010249{
10250 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010251 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010252 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010253
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010254 if (argvars[1].v_type != VAR_LIST)
10255 {
10256 EMSG(_(e_listreq));
10257 return;
10258 }
10259 if (argvars[1].vval.v_list == NULL)
10260 return;
10261
10262 if (argvars[0].v_type == VAR_FUNC)
10263 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010264 else if (argvars[0].v_type == VAR_PARTIAL)
10265 {
10266 partial = argvars[0].vval.v_partial;
10267 func = partial->pt_name;
10268 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010269 else
10270 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010271 if (*func == NUL)
10272 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010273
Bram Moolenaare9a41262005-01-15 22:18:47 +000010274 if (argvars[2].v_type != VAR_UNKNOWN)
10275 {
10276 if (argvars[2].v_type != VAR_DICT)
10277 {
10278 EMSG(_(e_dictreq));
10279 return;
10280 }
10281 selfdict = argvars[2].vval.v_dict;
10282 }
10283
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010284 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010285}
10286
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010287#ifdef FEAT_FLOAT
10288/*
10289 * "ceil({float})" function
10290 */
10291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010292f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010293{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010294 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010295
10296 rettv->v_type = VAR_FLOAT;
10297 if (get_float_arg(argvars, &f) == OK)
10298 rettv->vval.v_float = ceil(f);
10299 else
10300 rettv->vval.v_float = 0.0;
10301}
10302#endif
10303
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010304#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010305/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010306 * "ch_close()" function
10307 */
10308 static void
10309f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10310{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010311 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010312
10313 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010314 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010315 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010316 channel_clear(channel);
10317 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010318}
10319
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010320/*
10321 * "ch_getbufnr()" function
10322 */
10323 static void
10324f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10325{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010326 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010327
10328 rettv->vval.v_number = -1;
10329 if (channel != NULL)
10330 {
10331 char_u *what = get_tv_string(&argvars[1]);
10332 int part;
10333
10334 if (STRCMP(what, "err") == 0)
10335 part = PART_ERR;
10336 else if (STRCMP(what, "out") == 0)
10337 part = PART_OUT;
10338 else if (STRCMP(what, "in") == 0)
10339 part = PART_IN;
10340 else
10341 part = PART_SOCK;
10342 if (channel->ch_part[part].ch_buffer != NULL)
10343 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10344 }
10345}
10346
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010347/*
10348 * "ch_getjob()" function
10349 */
10350 static void
10351f_ch_getjob(typval_T *argvars, typval_T *rettv)
10352{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010353 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010354
10355 if (channel != NULL)
10356 {
10357 rettv->v_type = VAR_JOB;
10358 rettv->vval.v_job = channel->ch_job;
10359 if (channel->ch_job != NULL)
10360 ++channel->ch_job->jv_refcount;
10361 }
10362}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010363
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010364/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010365 * "ch_info()" function
10366 */
10367 static void
10368f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10369{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010370 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010371
10372 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10373 channel_info(channel, rettv->vval.v_dict);
10374}
10375
10376/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010377 * "ch_log()" function
10378 */
10379 static void
10380f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10381{
10382 char_u *msg = get_tv_string(&argvars[0]);
10383 channel_T *channel = NULL;
10384
10385 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010386 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010387
10388 ch_log(channel, (char *)msg);
10389}
10390
10391/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010392 * "ch_logfile()" function
10393 */
10394 static void
10395f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10396{
10397 char_u *fname;
10398 char_u *opt = (char_u *)"";
10399 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010400
10401 fname = get_tv_string(&argvars[0]);
10402 if (argvars[1].v_type == VAR_STRING)
10403 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010404 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010405}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010406
10407/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010408 * "ch_open()" function
10409 */
10410 static void
10411f_ch_open(typval_T *argvars, typval_T *rettv)
10412{
Bram Moolenaar77073442016-02-13 23:23:53 +010010413 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010414 if (check_restricted() || check_secure())
10415 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010416 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010417}
10418
10419/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010420 * "ch_read()" function
10421 */
10422 static void
10423f_ch_read(typval_T *argvars, typval_T *rettv)
10424{
10425 common_channel_read(argvars, rettv, FALSE);
10426}
10427
10428/*
10429 * "ch_readraw()" function
10430 */
10431 static void
10432f_ch_readraw(typval_T *argvars, typval_T *rettv)
10433{
10434 common_channel_read(argvars, rettv, TRUE);
10435}
10436
10437/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010438 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010439 */
10440 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010441f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10442{
10443 ch_expr_common(argvars, rettv, TRUE);
10444}
10445
10446/*
10447 * "ch_sendexpr()" function
10448 */
10449 static void
10450f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10451{
10452 ch_expr_common(argvars, rettv, FALSE);
10453}
10454
10455/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010456 * "ch_evalraw()" function
10457 */
10458 static void
10459f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10460{
10461 ch_raw_common(argvars, rettv, TRUE);
10462}
10463
10464/*
10465 * "ch_sendraw()" function
10466 */
10467 static void
10468f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10469{
10470 ch_raw_common(argvars, rettv, FALSE);
10471}
10472
10473/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010474 * "ch_setoptions()" function
10475 */
10476 static void
10477f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10478{
10479 channel_T *channel;
10480 jobopt_T opt;
10481
Bram Moolenaar437905c2016-04-26 19:01:05 +020010482 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010483 if (channel == NULL)
10484 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010485 clear_job_options(&opt);
10486 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010487 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10488 channel_set_options(channel, &opt);
10489 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010490}
10491
10492/*
10493 * "ch_status()" function
10494 */
10495 static void
10496f_ch_status(typval_T *argvars, typval_T *rettv)
10497{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010498 channel_T *channel;
10499
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010500 /* return an empty string by default */
10501 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010502 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010503
Bram Moolenaar437905c2016-04-26 19:01:05 +020010504 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010505 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010506}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010507#endif
10508
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010509/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010510 * "changenr()" function
10511 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010513f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010514{
10515 rettv->vval.v_number = curbuf->b_u_seq_cur;
10516}
10517
10518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 * "char2nr(string)" function
10520 */
10521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010522f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523{
10524#ifdef FEAT_MBYTE
10525 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010526 {
10527 int utf8 = 0;
10528
10529 if (argvars[1].v_type != VAR_UNKNOWN)
10530 utf8 = get_tv_number_chk(&argvars[1], NULL);
10531
10532 if (utf8)
10533 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10534 else
10535 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 else
10538#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010539 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540}
10541
10542/*
10543 * "cindent(lnum)" function
10544 */
10545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010546f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547{
10548#ifdef FEAT_CINDENT
10549 pos_T pos;
10550 linenr_T lnum;
10551
10552 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10555 {
10556 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 curwin->w_cursor = pos;
10559 }
10560 else
10561#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010562 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563}
10564
10565/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010566 * "clearmatches()" function
10567 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010569f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010570{
10571#ifdef FEAT_SEARCH_EXTRA
10572 clear_matches(curwin);
10573#endif
10574}
10575
10576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 * "col(string)" function
10578 */
10579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010580f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581{
10582 colnr_T col = 0;
10583 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010584 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010586 fp = var2fpos(&argvars[0], FALSE, &fnum);
10587 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 {
10589 if (fp->col == MAXCOL)
10590 {
10591 /* '> can be MAXCOL, get the length of the line then */
10592 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010593 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 else
10595 col = MAXCOL;
10596 }
10597 else
10598 {
10599 col = fp->col + 1;
10600#ifdef FEAT_VIRTUALEDIT
10601 /* col(".") when the cursor is on the NUL at the end of the line
10602 * because of "coladd" can be seen as an extra column. */
10603 if (virtual_active() && fp == &curwin->w_cursor)
10604 {
10605 char_u *p = ml_get_cursor();
10606
10607 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10608 curwin->w_virtcol - curwin->w_cursor.coladd))
10609 {
10610# ifdef FEAT_MBYTE
10611 int l;
10612
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010613 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 col += l;
10615# else
10616 if (*p != NUL && p[1] == NUL)
10617 ++col;
10618# endif
10619 }
10620 }
10621#endif
10622 }
10623 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625}
10626
Bram Moolenaar572cb562005-08-05 21:35:02 +000010627#if defined(FEAT_INS_EXPAND)
10628/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010629 * "complete()" function
10630 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010632f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010633{
10634 int startcol;
10635
10636 if ((State & INSERT) == 0)
10637 {
10638 EMSG(_("E785: complete() can only be used in Insert mode"));
10639 return;
10640 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010641
10642 /* Check for undo allowed here, because if something was already inserted
10643 * the line was already saved for undo and this check isn't done. */
10644 if (!undo_allowed())
10645 return;
10646
Bram Moolenaarade00832006-03-10 21:46:58 +000010647 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10648 {
10649 EMSG(_(e_invarg));
10650 return;
10651 }
10652
10653 startcol = get_tv_number_chk(&argvars[0], NULL);
10654 if (startcol <= 0)
10655 return;
10656
10657 set_completion(startcol - 1, argvars[1].vval.v_list);
10658}
10659
10660/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010661 * "complete_add()" function
10662 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010664f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010665{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010666 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010667}
10668
10669/*
10670 * "complete_check()" function
10671 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010673f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010674{
10675 int saved = RedrawingDisabled;
10676
10677 RedrawingDisabled = 0;
10678 ins_compl_check_keys(0);
10679 rettv->vval.v_number = compl_interrupted;
10680 RedrawingDisabled = saved;
10681}
10682#endif
10683
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684/*
10685 * "confirm(message, buttons[, default [, type]])" function
10686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010688f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
10690#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10691 char_u *message;
10692 char_u *buttons = NULL;
10693 char_u buf[NUMBUFLEN];
10694 char_u buf2[NUMBUFLEN];
10695 int def = 1;
10696 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010697 char_u *typestr;
10698 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010700 message = get_tv_string_chk(&argvars[0]);
10701 if (message == NULL)
10702 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010703 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10706 if (buttons == NULL)
10707 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010708 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010711 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010713 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10714 if (typestr == NULL)
10715 error = TRUE;
10716 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010718 switch (TOUPPER_ASC(*typestr))
10719 {
10720 case 'E': type = VIM_ERROR; break;
10721 case 'Q': type = VIM_QUESTION; break;
10722 case 'I': type = VIM_INFO; break;
10723 case 'W': type = VIM_WARNING; break;
10724 case 'G': type = VIM_GENERIC; break;
10725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 }
10727 }
10728 }
10729 }
10730
10731 if (buttons == NULL || *buttons == NUL)
10732 buttons = (char_u *)_("&Ok");
10733
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010734 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010735 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010736 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737#endif
10738}
10739
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010740/*
10741 * "copy()" function
10742 */
10743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010744f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010745{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010746 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010747}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010749#ifdef FEAT_FLOAT
10750/*
10751 * "cos()" function
10752 */
10753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010754f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010755{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010756 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010757
10758 rettv->v_type = VAR_FLOAT;
10759 if (get_float_arg(argvars, &f) == OK)
10760 rettv->vval.v_float = cos(f);
10761 else
10762 rettv->vval.v_float = 0.0;
10763}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010764
10765/*
10766 * "cosh()" function
10767 */
10768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010769f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010770{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010771 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010772
10773 rettv->v_type = VAR_FLOAT;
10774 if (get_float_arg(argvars, &f) == OK)
10775 rettv->vval.v_float = cosh(f);
10776 else
10777 rettv->vval.v_float = 0.0;
10778}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010779#endif
10780
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010782 * "count()" function
10783 */
10784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010785f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010786{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010787 long n = 0;
10788 int ic = FALSE;
10789
Bram Moolenaare9a41262005-01-15 22:18:47 +000010790 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010791 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010792 listitem_T *li;
10793 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010794 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010795
Bram Moolenaare9a41262005-01-15 22:18:47 +000010796 if ((l = argvars[0].vval.v_list) != NULL)
10797 {
10798 li = l->lv_first;
10799 if (argvars[2].v_type != VAR_UNKNOWN)
10800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010801 int error = FALSE;
10802
10803 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010804 if (argvars[3].v_type != VAR_UNKNOWN)
10805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 idx = get_tv_number_chk(&argvars[3], &error);
10807 if (!error)
10808 {
10809 li = list_find(l, idx);
10810 if (li == NULL)
10811 EMSGN(_(e_listidx), idx);
10812 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010813 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010814 if (error)
10815 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 }
10817
10818 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010819 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820 ++n;
10821 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010823 else if (argvars[0].v_type == VAR_DICT)
10824 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010825 int todo;
10826 dict_T *d;
10827 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010828
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010829 if ((d = argvars[0].vval.v_dict) != NULL)
10830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010831 int error = FALSE;
10832
Bram Moolenaare9a41262005-01-15 22:18:47 +000010833 if (argvars[2].v_type != VAR_UNKNOWN)
10834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010835 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010836 if (argvars[3].v_type != VAR_UNKNOWN)
10837 EMSG(_(e_invarg));
10838 }
10839
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010840 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010841 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010842 {
10843 if (!HASHITEM_EMPTY(hi))
10844 {
10845 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010846 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010847 ++n;
10848 }
10849 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010850 }
10851 }
10852 else
10853 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010854 rettv->vval.v_number = n;
10855}
10856
10857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10859 *
10860 * Checks the existence of a cscope connection.
10861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010863f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864{
10865#ifdef FEAT_CSCOPE
10866 int num = 0;
10867 char_u *dbpath = NULL;
10868 char_u *prepend = NULL;
10869 char_u buf[NUMBUFLEN];
10870
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010871 if (argvars[0].v_type != VAR_UNKNOWN
10872 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 num = (int)get_tv_number(&argvars[0]);
10875 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010876 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010877 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 }
10879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881#endif
10882}
10883
10884/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010885 * "cursor(lnum, col)" function, or
10886 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010888 * Moves the cursor to the specified line and column.
10889 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010892f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893{
10894 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010895#ifdef FEAT_VIRTUALEDIT
10896 long coladd = 0;
10897#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010898 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010900 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010901 if (argvars[1].v_type == VAR_UNKNOWN)
10902 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010903 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010904 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010905
Bram Moolenaar493c1782014-05-28 14:34:46 +020010906 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010907 {
10908 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010909 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010910 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010911 line = pos.lnum;
10912 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010913#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010914 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010915#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010916 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010917 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010918 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010919 set_curswant = FALSE;
10920 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010921 }
10922 else
10923 {
10924 line = get_tv_lnum(argvars);
10925 col = get_tv_number_chk(&argvars[1], NULL);
10926#ifdef FEAT_VIRTUALEDIT
10927 if (argvars[2].v_type != VAR_UNKNOWN)
10928 coladd = get_tv_number_chk(&argvars[2], NULL);
10929#endif
10930 }
10931 if (line < 0 || col < 0
10932#ifdef FEAT_VIRTUALEDIT
10933 || coladd < 0
10934#endif
10935 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010936 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 if (line > 0)
10938 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 if (col > 0)
10940 curwin->w_cursor.col = col - 1;
10941#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010942 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943#endif
10944
10945 /* Make sure the cursor is in a valid position. */
10946 check_cursor();
10947#ifdef FEAT_MBYTE
10948 /* Correct cursor for multi-byte character. */
10949 if (has_mbyte)
10950 mb_adjust_cursor();
10951#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010952
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010953 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010954 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955}
10956
10957/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010958 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 */
10960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010961f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010963 int noref = 0;
10964
10965 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010966 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010967 if (noref < 0 || noref > 1)
10968 EMSG(_(e_invarg));
10969 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010970 {
10971 current_copyID += COPYID_INC;
10972 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974}
10975
10976/*
10977 * "delete()" function
10978 */
10979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010980f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010982 char_u nbuf[NUMBUFLEN];
10983 char_u *name;
10984 char_u *flags;
10985
10986 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010988 return;
10989
10990 name = get_tv_string(&argvars[0]);
10991 if (name == NULL || *name == NUL)
10992 {
10993 EMSG(_(e_invarg));
10994 return;
10995 }
10996
10997 if (argvars[1].v_type != VAR_UNKNOWN)
10998 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011000 flags = (char_u *)"";
11001
11002 if (*flags == NUL)
11003 /* delete a file */
11004 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11005 else if (STRCMP(flags, "d") == 0)
11006 /* delete an empty directory */
11007 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11008 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011009 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011010 rettv->vval.v_number = delete_recursive(name);
11011 else
11012 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013}
11014
11015/*
11016 * "did_filetype()" function
11017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011019f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020{
11021#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023#endif
11024}
11025
11026/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011027 * "diff_filler()" function
11028 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011030f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011031{
11032#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011034#endif
11035}
11036
11037/*
11038 * "diff_hlID()" function
11039 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011041f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011042{
11043#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011045 static linenr_T prev_lnum = 0;
11046 static int changedtick = 0;
11047 static int fnum = 0;
11048 static int change_start = 0;
11049 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011050 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011051 int filler_lines;
11052 int col;
11053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011054 if (lnum < 0) /* ignore type error in {lnum} arg */
11055 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011056 if (lnum != prev_lnum
11057 || changedtick != curbuf->b_changedtick
11058 || fnum != curbuf->b_fnum)
11059 {
11060 /* New line, buffer, change: need to get the values. */
11061 filler_lines = diff_check(curwin, lnum);
11062 if (filler_lines < 0)
11063 {
11064 if (filler_lines == -1)
11065 {
11066 change_start = MAXCOL;
11067 change_end = -1;
11068 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11069 hlID = HLF_ADD; /* added line */
11070 else
11071 hlID = HLF_CHD; /* changed line */
11072 }
11073 else
11074 hlID = HLF_ADD; /* added line */
11075 }
11076 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011077 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011078 prev_lnum = lnum;
11079 changedtick = curbuf->b_changedtick;
11080 fnum = curbuf->b_fnum;
11081 }
11082
11083 if (hlID == HLF_CHD || hlID == HLF_TXD)
11084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011085 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011086 if (col >= change_start && col <= change_end)
11087 hlID = HLF_TXD; /* changed text */
11088 else
11089 hlID = HLF_CHD; /* changed line */
11090 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011091 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011092#endif
11093}
11094
11095/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011096 * "disable_char_avail_for_testing({expr})" function
11097 */
11098 static void
11099f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11100{
11101 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11102}
11103
11104/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011105 * "empty({expr})" function
11106 */
11107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011108f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011109{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011110 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011111
11112 switch (argvars[0].v_type)
11113 {
11114 case VAR_STRING:
11115 case VAR_FUNC:
11116 n = argvars[0].vval.v_string == NULL
11117 || *argvars[0].vval.v_string == NUL;
11118 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011119 case VAR_PARTIAL:
11120 n = FALSE;
11121 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011122 case VAR_NUMBER:
11123 n = argvars[0].vval.v_number == 0;
11124 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011125 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011126#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011127 n = argvars[0].vval.v_float == 0.0;
11128 break;
11129#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011130 case VAR_LIST:
11131 n = argvars[0].vval.v_list == NULL
11132 || argvars[0].vval.v_list->lv_first == NULL;
11133 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011134 case VAR_DICT:
11135 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011136 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011137 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011138 case VAR_SPECIAL:
11139 n = argvars[0].vval.v_number != VVAL_TRUE;
11140 break;
11141
Bram Moolenaar835dc632016-02-07 14:27:38 +010011142 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011143#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011144 n = argvars[0].vval.v_job == NULL
11145 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11146 break;
11147#endif
11148 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011149#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011150 n = argvars[0].vval.v_channel == NULL
11151 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011152 break;
11153#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011154 case VAR_UNKNOWN:
11155 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11156 n = TRUE;
11157 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011158 }
11159
11160 rettv->vval.v_number = n;
11161}
11162
11163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 * "escape({string}, {chars})" function
11165 */
11166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011167f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168{
11169 char_u buf[NUMBUFLEN];
11170
Bram Moolenaar758711c2005-02-02 23:11:38 +000011171 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11172 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174}
11175
11176/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011177 * "eval()" function
11178 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011180f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011181{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011182 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 s = get_tv_string_chk(&argvars[0]);
11185 if (s != NULL)
11186 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011187
Bram Moolenaar615b9972015-01-14 17:15:05 +010011188 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011189 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11190 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011191 if (p != NULL && !aborting())
11192 EMSG2(_(e_invexpr2), p);
11193 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011195 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011197 else if (*s != NUL)
11198 EMSG(_(e_trailing));
11199}
11200
11201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 * "eventhandler()" function
11203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011205f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208}
11209
11210/*
11211 * "executable()" function
11212 */
11213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011214f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011216 char_u *name = get_tv_string(&argvars[0]);
11217
11218 /* Check in $PATH and also check directly if there is a directory name. */
11219 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11220 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011221}
11222
11223/*
11224 * "exepath()" function
11225 */
11226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011227f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011228{
11229 char_u *p = NULL;
11230
Bram Moolenaarb5971142015-03-21 17:32:19 +010011231 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011232 rettv->v_type = VAR_STRING;
11233 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234}
11235
11236/*
11237 * "exists()" function
11238 */
11239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011240f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241{
11242 char_u *p;
11243 char_u *name;
11244 int n = FALSE;
11245 int len = 0;
11246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 if (*p == '$') /* environment variable */
11249 {
11250 /* first try "normal" environment variables (fast) */
11251 if (mch_getenv(p + 1) != NULL)
11252 n = TRUE;
11253 else
11254 {
11255 /* try expanding things like $VIM and ${HOME} */
11256 p = expand_env_save(p);
11257 if (p != NULL && *p != '$')
11258 n = TRUE;
11259 vim_free(p);
11260 }
11261 }
11262 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011265 if (*skipwhite(p) != NUL)
11266 n = FALSE; /* trailing garbage */
11267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 else if (*p == '*') /* internal or user defined function */
11269 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011270 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 }
11272 else if (*p == ':')
11273 {
11274 n = cmd_exists(p + 1);
11275 }
11276 else if (*p == '#')
11277 {
11278#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011279 if (p[1] == '#')
11280 n = autocmd_supported(p + 2);
11281 else
11282 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283#endif
11284 }
11285 else /* internal variable */
11286 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011287 char_u *tofree;
11288 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011290 /* get_name_len() takes care of expanding curly braces */
11291 name = p;
11292 len = get_name_len(&p, &tofree, TRUE, FALSE);
11293 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011295 if (tofree != NULL)
11296 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011297 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011298 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011300 /* handle d.key, l[idx], f(expr) */
11301 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11302 if (n)
11303 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 }
11305 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011306 if (*p != NUL)
11307 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011309 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 }
11311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313}
11314
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011315#ifdef FEAT_FLOAT
11316/*
11317 * "exp()" function
11318 */
11319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011320f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011321{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011322 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011323
11324 rettv->v_type = VAR_FLOAT;
11325 if (get_float_arg(argvars, &f) == OK)
11326 rettv->vval.v_float = exp(f);
11327 else
11328 rettv->vval.v_float = 0.0;
11329}
11330#endif
11331
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332/*
11333 * "expand()" function
11334 */
11335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011336f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337{
11338 char_u *s;
11339 int len;
11340 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011341 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011344 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011347 if (argvars[1].v_type != VAR_UNKNOWN
11348 && argvars[2].v_type != VAR_UNKNOWN
11349 && get_tv_number_chk(&argvars[2], &error)
11350 && !error)
11351 {
11352 rettv->v_type = VAR_LIST;
11353 rettv->vval.v_list = NULL;
11354 }
11355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357 if (*s == '%' || *s == '#' || *s == '<')
11358 {
11359 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011360 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011362 if (rettv->v_type == VAR_LIST)
11363 {
11364 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11365 list_append_string(rettv->vval.v_list, result, -1);
11366 else
11367 vim_free(result);
11368 }
11369 else
11370 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 }
11372 else
11373 {
11374 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011375 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011376 if (argvars[1].v_type != VAR_UNKNOWN
11377 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011378 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011379 if (!error)
11380 {
11381 ExpandInit(&xpc);
11382 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011383 if (p_wic)
11384 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011385 if (rettv->v_type == VAR_STRING)
11386 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11387 options, WILD_ALL);
11388 else if (rettv_list_alloc(rettv) != FAIL)
11389 {
11390 int i;
11391
11392 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11393 for (i = 0; i < xpc.xp_numfiles; i++)
11394 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11395 ExpandCleanup(&xpc);
11396 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 }
11398 else
11399 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400 }
11401}
11402
11403/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011404 * Go over all entries in "d2" and add them to "d1".
11405 * When "action" is "error" then a duplicate key is an error.
11406 * When "action" is "force" then a duplicate key is overwritten.
11407 * Otherwise duplicate keys are ignored ("action" is "keep").
11408 */
11409 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011410dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011411{
11412 dictitem_T *di1;
11413 hashitem_T *hi2;
11414 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011415 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011416
11417 todo = (int)d2->dv_hashtab.ht_used;
11418 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11419 {
11420 if (!HASHITEM_EMPTY(hi2))
11421 {
11422 --todo;
11423 di1 = dict_find(d1, hi2->hi_key, -1);
11424 if (d1->dv_scope != 0)
11425 {
11426 /* Disallow replacing a builtin function in l: and g:.
11427 * Check the key to be valid when adding to any
11428 * scope. */
11429 if (d1->dv_scope == VAR_DEF_SCOPE
11430 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11431 && var_check_func_name(hi2->hi_key,
11432 di1 == NULL))
11433 break;
11434 if (!valid_varname(hi2->hi_key))
11435 break;
11436 }
11437 if (di1 == NULL)
11438 {
11439 di1 = dictitem_copy(HI2DI(hi2));
11440 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11441 dictitem_free(di1);
11442 }
11443 else if (*action == 'e')
11444 {
11445 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11446 break;
11447 }
11448 else if (*action == 'f' && HI2DI(hi2) != di1)
11449 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011450 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11451 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011452 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011453 clear_tv(&di1->di_tv);
11454 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11455 }
11456 }
11457 }
11458}
11459
11460/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011461 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011462 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011463 */
11464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011465f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011466{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011467 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011468
Bram Moolenaare9a41262005-01-15 22:18:47 +000011469 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011470 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011471 list_T *l1, *l2;
11472 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011473 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011474 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011475
Bram Moolenaare9a41262005-01-15 22:18:47 +000011476 l1 = argvars[0].vval.v_list;
11477 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011478 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011479 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011480 {
11481 if (argvars[2].v_type != VAR_UNKNOWN)
11482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011483 before = get_tv_number_chk(&argvars[2], &error);
11484 if (error)
11485 return; /* type error; errmsg already given */
11486
Bram Moolenaar758711c2005-02-02 23:11:38 +000011487 if (before == l1->lv_len)
11488 item = NULL;
11489 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011490 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011491 item = list_find(l1, before);
11492 if (item == NULL)
11493 {
11494 EMSGN(_(e_listidx), before);
11495 return;
11496 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011497 }
11498 }
11499 else
11500 item = NULL;
11501 list_extend(l1, l2, item);
11502
Bram Moolenaare9a41262005-01-15 22:18:47 +000011503 copy_tv(&argvars[0], rettv);
11504 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011505 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011506 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11507 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011508 dict_T *d1, *d2;
11509 char_u *action;
11510 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011511
11512 d1 = argvars[0].vval.v_dict;
11513 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011514 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011515 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011516 {
11517 /* Check the third argument. */
11518 if (argvars[2].v_type != VAR_UNKNOWN)
11519 {
11520 static char *(av[]) = {"keep", "force", "error"};
11521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011522 action = get_tv_string_chk(&argvars[2]);
11523 if (action == NULL)
11524 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011525 for (i = 0; i < 3; ++i)
11526 if (STRCMP(action, av[i]) == 0)
11527 break;
11528 if (i == 3)
11529 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011530 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011531 return;
11532 }
11533 }
11534 else
11535 action = (char_u *)"force";
11536
Bram Moolenaara9922d62013-05-30 13:01:18 +020011537 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011538
Bram Moolenaare9a41262005-01-15 22:18:47 +000011539 copy_tv(&argvars[0], rettv);
11540 }
11541 }
11542 else
11543 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011544}
11545
11546/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011547 * "feedkeys()" function
11548 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011550f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011551{
11552 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011553 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011554 char_u *keys, *flags;
11555 char_u nbuf[NUMBUFLEN];
11556 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011557 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011558 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011559 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011560
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011561 /* This is not allowed in the sandbox. If the commands would still be
11562 * executed in the sandbox it would be OK, but it probably happens later,
11563 * when "sandbox" is no longer set. */
11564 if (check_secure())
11565 return;
11566
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011567 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011568
11569 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011570 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011571 flags = get_tv_string_buf(&argvars[1], nbuf);
11572 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011573 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011574 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011575 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011576 case 'n': remap = FALSE; break;
11577 case 'm': remap = TRUE; break;
11578 case 't': typed = TRUE; break;
11579 case 'i': insert = TRUE; break;
11580 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011581 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011582 }
11583 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011584 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011585
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011586 if (*keys != NUL || execute)
11587 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011588 /* Need to escape K_SPECIAL and CSI before putting the string in the
11589 * typeahead buffer. */
11590 keys_esc = vim_strsave_escape_csi(keys);
11591 if (keys_esc != NULL)
11592 {
11593 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011594 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011595 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011596 if (vgetc_busy)
11597 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011598 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011599 {
11600 int save_msg_scroll = msg_scroll;
11601
11602 /* Avoid a 1 second delay when the keys start Insert mode. */
11603 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011604
Bram Moolenaar245c4102016-04-20 17:37:41 +020011605 if (!dangerous)
11606 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011607 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011608 if (!dangerous)
11609 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011610 msg_scroll |= save_msg_scroll;
11611 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011612 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011613 }
11614}
11615
11616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 * "filereadable()" function
11618 */
11619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011620f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011622 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 char_u *p;
11624 int n;
11625
Bram Moolenaarc236c162008-07-13 17:41:49 +000011626#ifndef O_NONBLOCK
11627# define O_NONBLOCK 0
11628#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011630 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11631 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 {
11633 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011634 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 }
11636 else
11637 n = FALSE;
11638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011639 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640}
11641
11642/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011643 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 * rights to write into.
11645 */
11646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011647f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011649 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650}
11651
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011652 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011653findfilendir(
11654 typval_T *argvars UNUSED,
11655 typval_T *rettv,
11656 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011657{
11658#ifdef FEAT_SEARCHPATH
11659 char_u *fname;
11660 char_u *fresult = NULL;
11661 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11662 char_u *p;
11663 char_u pathbuf[NUMBUFLEN];
11664 int count = 1;
11665 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011666 int error = FALSE;
11667#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011668
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011669 rettv->vval.v_string = NULL;
11670 rettv->v_type = VAR_STRING;
11671
11672#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011674
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011675 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011677 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11678 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011679 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011680 else
11681 {
11682 if (*p != NUL)
11683 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011685 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011686 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011688 }
11689
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011690 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11691 error = TRUE;
11692
11693 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 do
11696 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011697 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011698 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699 fresult = find_file_in_path_option(first ? fname : NULL,
11700 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011701 0, first, path,
11702 find_what,
11703 curbuf->b_ffname,
11704 find_what == FINDFILE_DIR
11705 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011706 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011707
11708 if (fresult != NULL && rettv->v_type == VAR_LIST)
11709 list_append_string(rettv->vval.v_list, fresult, -1);
11710
11711 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011712 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011713
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011714 if (rettv->v_type == VAR_STRING)
11715 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011716#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011717}
11718
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011719static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11720static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011721
11722/*
11723 * Implementation of map() and filter().
11724 */
11725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011726filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011727{
11728 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011729 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011730 listitem_T *li, *nli;
11731 list_T *l = NULL;
11732 dictitem_T *di;
11733 hashtab_T *ht;
11734 hashitem_T *hi;
11735 dict_T *d = NULL;
11736 typval_T save_val;
11737 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011738 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011739 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011740 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011741 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011742 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011743 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011744 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011745
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011747 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011748 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011749 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011750 return;
11751 }
11752 else if (argvars[0].v_type == VAR_DICT)
11753 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011754 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011755 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011756 return;
11757 }
11758 else
11759 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011760 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011761 return;
11762 }
11763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011764 expr = get_tv_string_buf_chk(&argvars[1], buf);
11765 /* On type errors, the preceding call has already displayed an error
11766 * message. Avoid a misleading error message for an empty string that
11767 * was not passed as argument. */
11768 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011770 prepare_vimvar(VV_VAL, &save_val);
11771 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011772
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011773 /* We reset "did_emsg" to be able to detect whether an error
11774 * occurred during evaluation of the expression. */
11775 save_did_emsg = did_emsg;
11776 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011777
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011778 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011779 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011781 vimvars[VV_KEY].vv_type = VAR_STRING;
11782
11783 ht = &d->dv_hashtab;
11784 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011785 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011788 if (!HASHITEM_EMPTY(hi))
11789 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011790 int r;
11791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011792 --todo;
11793 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011794 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011795 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11796 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011797 break;
11798 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011799 r = filter_map_one(&di->di_tv, expr, map, &rem);
11800 clear_tv(&vimvars[VV_KEY].vv_tv);
11801 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011802 break;
11803 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011804 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011805 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11806 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011807 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011809 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011810 }
11811 }
11812 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 }
11814 else
11815 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011816 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 for (li = l->lv_first; li != NULL; li = nli)
11819 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011820 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011821 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011823 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011824 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011825 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011826 break;
11827 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011828 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011829 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011830 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011831 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011832
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011833 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011834 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011835
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011836 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011837 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011838
11839 copy_tv(&argvars[0], rettv);
11840}
11841
11842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011843filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011844{
Bram Moolenaar33570922005-01-25 22:26:29 +000011845 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011846 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011847 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011848
Bram Moolenaar33570922005-01-25 22:26:29 +000011849 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011850 s = expr;
11851 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011852 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011853 if (*s != NUL) /* check for trailing chars after expr */
11854 {
11855 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011856 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011857 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011858 }
11859 if (map)
11860 {
11861 /* map(): replace the list item value */
11862 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011863 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011864 *tv = rettv;
11865 }
11866 else
11867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011868 int error = FALSE;
11869
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011871 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011872 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011873 /* On type error, nothing has been removed; return FAIL to stop the
11874 * loop. The error message was given by get_tv_number_chk(). */
11875 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011876 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011877 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011878 retval = OK;
11879theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011880 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011881 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011882}
11883
11884/*
11885 * "filter()" function
11886 */
11887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011888f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011889{
11890 filter_map(argvars, rettv, FALSE);
11891}
11892
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011893/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011894 * "finddir({fname}[, {path}[, {count}]])" function
11895 */
11896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011897f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011898{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011899 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011900}
11901
11902/*
11903 * "findfile({fname}[, {path}[, {count}]])" function
11904 */
11905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011906f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011907{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011908 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011909}
11910
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011911#ifdef FEAT_FLOAT
11912/*
11913 * "float2nr({float})" function
11914 */
11915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011916f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011917{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011918 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011919
11920 if (get_float_arg(argvars, &f) == OK)
11921 {
11922 if (f < -0x7fffffff)
11923 rettv->vval.v_number = -0x7fffffff;
11924 else if (f > 0x7fffffff)
11925 rettv->vval.v_number = 0x7fffffff;
11926 else
11927 rettv->vval.v_number = (varnumber_T)f;
11928 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011929}
11930
11931/*
11932 * "floor({float})" function
11933 */
11934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011935f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011936{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011937 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011938
11939 rettv->v_type = VAR_FLOAT;
11940 if (get_float_arg(argvars, &f) == OK)
11941 rettv->vval.v_float = floor(f);
11942 else
11943 rettv->vval.v_float = 0.0;
11944}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011945
11946/*
11947 * "fmod()" function
11948 */
11949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011950f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011951{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011952 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011953
11954 rettv->v_type = VAR_FLOAT;
11955 if (get_float_arg(argvars, &fx) == OK
11956 && get_float_arg(&argvars[1], &fy) == OK)
11957 rettv->vval.v_float = fmod(fx, fy);
11958 else
11959 rettv->vval.v_float = 0.0;
11960}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011961#endif
11962
Bram Moolenaar0d660222005-01-07 21:51:51 +000011963/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011964 * "fnameescape({string})" function
11965 */
11966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011967f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011968{
11969 rettv->vval.v_string = vim_strsave_fnameescape(
11970 get_tv_string(&argvars[0]), FALSE);
11971 rettv->v_type = VAR_STRING;
11972}
11973
11974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 * "fnamemodify({fname}, {mods})" function
11976 */
11977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011978f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979{
11980 char_u *fname;
11981 char_u *mods;
11982 int usedlen = 0;
11983 int len;
11984 char_u *fbuf = NULL;
11985 char_u buf[NUMBUFLEN];
11986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011987 fname = get_tv_string_chk(&argvars[0]);
11988 mods = get_tv_string_buf_chk(&argvars[1], buf);
11989 if (fname == NULL || mods == NULL)
11990 fname = NULL;
11991 else
11992 {
11993 len = (int)STRLEN(fname);
11994 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011999 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 vim_free(fbuf);
12003}
12004
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012005static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006
12007/*
12008 * "foldclosed()" function
12009 */
12010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012011foldclosed_both(
12012 typval_T *argvars UNUSED,
12013 typval_T *rettv,
12014 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015{
12016#ifdef FEAT_FOLDING
12017 linenr_T lnum;
12018 linenr_T first, last;
12019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012020 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12022 {
12023 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12024 {
12025 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 return;
12030 }
12031 }
12032#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034}
12035
12036/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012037 * "foldclosed()" function
12038 */
12039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012040f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012041{
12042 foldclosed_both(argvars, rettv, FALSE);
12043}
12044
12045/*
12046 * "foldclosedend()" function
12047 */
12048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012049f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012050{
12051 foldclosed_both(argvars, rettv, TRUE);
12052}
12053
12054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 * "foldlevel()" function
12056 */
12057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012058f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059{
12060#ifdef FEAT_FOLDING
12061 linenr_T lnum;
12062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012065 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067}
12068
12069/*
12070 * "foldtext()" function
12071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012073f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074{
12075#ifdef FEAT_FOLDING
12076 linenr_T lnum;
12077 char_u *s;
12078 char_u *r;
12079 int len;
12080 char *txt;
12081#endif
12082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083 rettv->v_type = VAR_STRING;
12084 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012086 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12087 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12088 <= curbuf->b_ml.ml_line_count
12089 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090 {
12091 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012092 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12093 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 {
12095 if (!linewhite(lnum))
12096 break;
12097 ++lnum;
12098 }
12099
12100 /* Find interesting text in this line. */
12101 s = skipwhite(ml_get(lnum));
12102 /* skip C comment-start */
12103 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012106 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012107 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012108 {
12109 s = skipwhite(ml_get(lnum + 1));
12110 if (*s == '*')
12111 s = skipwhite(s + 1);
12112 }
12113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114 txt = _("+-%s%3ld lines: ");
12115 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012116 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117 + 20 /* for %3ld */
12118 + STRLEN(s))); /* concatenated */
12119 if (r != NULL)
12120 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012121 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12122 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12123 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124 len = (int)STRLEN(r);
12125 STRCAT(r, s);
12126 /* remove 'foldmarker' and 'commentstring' */
12127 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129 }
12130 }
12131#endif
12132}
12133
12134/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012135 * "foldtextresult(lnum)" function
12136 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012138f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012139{
12140#ifdef FEAT_FOLDING
12141 linenr_T lnum;
12142 char_u *text;
12143 char_u buf[51];
12144 foldinfo_T foldinfo;
12145 int fold_count;
12146#endif
12147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148 rettv->v_type = VAR_STRING;
12149 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012150#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012151 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012152 /* treat illegal types and illegal string values for {lnum} the same */
12153 if (lnum < 0)
12154 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012155 fold_count = foldedCount(curwin, lnum, &foldinfo);
12156 if (fold_count > 0)
12157 {
12158 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12159 &foldinfo, buf);
12160 if (text == buf)
12161 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012162 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012163 }
12164#endif
12165}
12166
12167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 * "foreground()" function
12169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012171f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173#ifdef FEAT_GUI
12174 if (gui.in_use)
12175 gui_mch_set_foreground();
12176#else
12177# ifdef WIN32
12178 win32_set_foreground();
12179# endif
12180#endif
12181}
12182
12183/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012184 * "function()" function
12185 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012187f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012188{
12189 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012190 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012191 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012192 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012193
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012194 if (argvars[0].v_type == VAR_FUNC)
12195 {
12196 /* function(MyFunc, [arg], dict) */
12197 s = argvars[0].vval.v_string;
12198 }
12199 else if (argvars[0].v_type == VAR_PARTIAL
12200 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012201 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012202 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012203 arg_pt = argvars[0].vval.v_partial;
12204 s = arg_pt->pt_name;
12205 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012206 else
12207 {
12208 /* function('MyFunc', [arg], dict) */
12209 s = get_tv_string(&argvars[0]);
12210 use_string = TRUE;
12211 }
12212
12213 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012214 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012215 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012216 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12217 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012218 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012219 else
12220 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012221 int dict_idx = 0;
12222 int arg_idx = 0;
12223 list_T *list = NULL;
12224
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012225 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012226 {
12227 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012228 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012229
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012230 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12231 * also be called from another script. Using trans_function_name()
12232 * would also work, but some plugins depend on the name being
12233 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012234 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012235 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12236 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012237 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012238 STRCPY(name, sid_buf);
12239 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012240 }
12241 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012242 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012243 name = vim_strsave(s);
12244
12245 if (argvars[1].v_type != VAR_UNKNOWN)
12246 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012247 if (argvars[2].v_type != VAR_UNKNOWN)
12248 {
12249 /* function(name, [args], dict) */
12250 arg_idx = 1;
12251 dict_idx = 2;
12252 }
12253 else if (argvars[1].v_type == VAR_DICT)
12254 /* function(name, dict) */
12255 dict_idx = 1;
12256 else
12257 /* function(name, [args]) */
12258 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012259 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012260 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012261 if (argvars[dict_idx].v_type != VAR_DICT)
12262 {
12263 EMSG(_("E922: expected a dict"));
12264 vim_free(name);
12265 return;
12266 }
12267 if (argvars[dict_idx].vval.v_dict == NULL)
12268 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012269 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012270 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012271 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012272 if (argvars[arg_idx].v_type != VAR_LIST)
12273 {
12274 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12275 vim_free(name);
12276 return;
12277 }
12278 list = argvars[arg_idx].vval.v_list;
12279 if (list == NULL || list->lv_len == 0)
12280 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012281 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012282 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012283 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012284 {
12285 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012286
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012287 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012288 if (pt == NULL)
12289 vim_free(name);
12290 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012291 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012292 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012293 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012294 listitem_T *li;
12295 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012296 int arg_len = 0;
12297 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012298
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012299 if (arg_pt != NULL)
12300 arg_len = arg_pt->pt_argc;
12301 if (list != NULL)
12302 lv_len = list->lv_len;
12303 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012304 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012305 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012306 if (pt->pt_argv == NULL)
12307 {
12308 vim_free(pt);
12309 vim_free(name);
12310 return;
12311 }
12312 else
12313 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012314 for (i = 0; i < arg_len; i++)
12315 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12316 if (lv_len > 0)
12317 for (li = list->lv_first; li != NULL;
12318 li = li->li_next)
12319 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012320 }
12321 }
12322
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012323 /* For "function(dict.func, [], dict)" and "func" is a partial
12324 * use "dict". That is backwards compatible. */
12325 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012326 {
12327 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12328 ++pt->pt_dict->dv_refcount;
12329 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012330 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012331 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012332 pt->pt_dict = arg_pt->pt_dict;
12333 if (pt->pt_dict != NULL)
12334 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012335 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012336
12337 pt->pt_refcount = 1;
12338 pt->pt_name = name;
12339 func_ref(pt->pt_name);
12340 }
12341 rettv->v_type = VAR_PARTIAL;
12342 rettv->vval.v_partial = pt;
12343 }
12344 else
12345 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012346 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012347 rettv->v_type = VAR_FUNC;
12348 rettv->vval.v_string = name;
12349 func_ref(name);
12350 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012351 }
12352}
12353
12354/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012355 * "garbagecollect()" function
12356 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012358f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012359{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012360 /* This is postponed until we are back at the toplevel, because we may be
12361 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12362 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012363
12364 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12365 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012366}
12367
12368/*
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +020012369 * "garbagecollect_for_testing()" function
12370 */
12371 static void
12372f_garbagecollect_for_testing(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12373{
12374 /* This is dangerous, any Lists and Dicts used internally may be freed
12375 * while still in use. */
12376 garbage_collect(TRUE);
12377}
12378
12379/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012380 * "get()" function
12381 */
12382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012383f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012384{
Bram Moolenaar33570922005-01-25 22:26:29 +000012385 listitem_T *li;
12386 list_T *l;
12387 dictitem_T *di;
12388 dict_T *d;
12389 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012390
Bram Moolenaare9a41262005-01-15 22:18:47 +000012391 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012392 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012393 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012395 int error = FALSE;
12396
12397 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12398 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012399 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012400 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012401 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012402 else if (argvars[0].v_type == VAR_DICT)
12403 {
12404 if ((d = argvars[0].vval.v_dict) != NULL)
12405 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012406 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012407 if (di != NULL)
12408 tv = &di->di_tv;
12409 }
12410 }
12411 else
12412 EMSG2(_(e_listdictarg), "get()");
12413
12414 if (tv == NULL)
12415 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012416 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012417 copy_tv(&argvars[2], rettv);
12418 }
12419 else
12420 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012421}
12422
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012423static 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 +000012424
12425/*
12426 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012427 * Return a range (from start to end) of lines in rettv from the specified
12428 * buffer.
12429 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012430 */
12431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012432get_buffer_lines(
12433 buf_T *buf,
12434 linenr_T start,
12435 linenr_T end,
12436 int retlist,
12437 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012438{
12439 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012440
Bram Moolenaar959a1432013-12-14 12:17:38 +010012441 rettv->v_type = VAR_STRING;
12442 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012443 if (retlist && rettv_list_alloc(rettv) == FAIL)
12444 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012445
12446 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12447 return;
12448
12449 if (!retlist)
12450 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012451 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12452 p = ml_get_buf(buf, start, FALSE);
12453 else
12454 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012455 rettv->vval.v_string = vim_strsave(p);
12456 }
12457 else
12458 {
12459 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012460 return;
12461
12462 if (start < 1)
12463 start = 1;
12464 if (end > buf->b_ml.ml_line_count)
12465 end = buf->b_ml.ml_line_count;
12466 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012467 if (list_append_string(rettv->vval.v_list,
12468 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012469 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012470 }
12471}
12472
12473/*
12474 * "getbufline()" function
12475 */
12476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012477f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012478{
12479 linenr_T lnum;
12480 linenr_T end;
12481 buf_T *buf;
12482
12483 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12484 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012485 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012486 --emsg_off;
12487
Bram Moolenaar661b1822005-07-28 22:36:45 +000012488 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012489 if (argvars[2].v_type == VAR_UNKNOWN)
12490 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012491 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012492 end = get_tv_lnum_buf(&argvars[2], buf);
12493
Bram Moolenaar342337a2005-07-21 21:11:17 +000012494 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012495}
12496
Bram Moolenaar0d660222005-01-07 21:51:51 +000012497/*
12498 * "getbufvar()" function
12499 */
12500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012501f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012502{
12503 buf_T *buf;
12504 buf_T *save_curbuf;
12505 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012506 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012507 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12510 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012512 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012513
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012514 rettv->v_type = VAR_STRING;
12515 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516
12517 if (buf != NULL && varname != NULL)
12518 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012519 /* set curbuf to be our buf, temporarily */
12520 save_curbuf = curbuf;
12521 curbuf = buf;
12522
Bram Moolenaar0d660222005-01-07 21:51:51 +000012523 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012524 {
12525 if (get_option_tv(&varname, rettv, TRUE) == OK)
12526 done = TRUE;
12527 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012528 else if (STRCMP(varname, "changedtick") == 0)
12529 {
12530 rettv->v_type = VAR_NUMBER;
12531 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012532 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012533 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012534 else
12535 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012536 /* Look up the variable. */
12537 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12538 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12539 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012541 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012542 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012543 done = TRUE;
12544 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012545 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012546
12547 /* restore previous notion of curbuf */
12548 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012549 }
12550
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012551 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12552 /* use the default value */
12553 copy_tv(&argvars[2], rettv);
12554
Bram Moolenaar0d660222005-01-07 21:51:51 +000012555 --emsg_off;
12556}
12557
12558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559 * "getchar()" function
12560 */
12561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012562f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563{
12564 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012565 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012567 /* Position the cursor. Needed after a message that ends in a space. */
12568 windgoto(msg_row, msg_col);
12569
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570 ++no_mapping;
12571 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012572 for (;;)
12573 {
12574 if (argvars[0].v_type == VAR_UNKNOWN)
12575 /* getchar(): blocking wait. */
12576 n = safe_vgetc();
12577 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12578 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012579 n = vpeekc_any();
12580 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012581 /* illegal argument or getchar(0) and no char avail: return zero */
12582 n = 0;
12583 else
12584 /* getchar(0) and char avail: return char */
12585 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012586
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012587 if (n == K_IGNORE)
12588 continue;
12589 break;
12590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591 --no_mapping;
12592 --allow_keys;
12593
Bram Moolenaar219b8702006-11-01 14:32:36 +000012594 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12595 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12596 vimvars[VV_MOUSE_COL].vv_nr = 0;
12597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012598 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599 if (IS_SPECIAL(n) || mod_mask != 0)
12600 {
12601 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12602 int i = 0;
12603
12604 /* Turn a special key into three bytes, plus modifier. */
12605 if (mod_mask != 0)
12606 {
12607 temp[i++] = K_SPECIAL;
12608 temp[i++] = KS_MODIFIER;
12609 temp[i++] = mod_mask;
12610 }
12611 if (IS_SPECIAL(n))
12612 {
12613 temp[i++] = K_SPECIAL;
12614 temp[i++] = K_SECOND(n);
12615 temp[i++] = K_THIRD(n);
12616 }
12617#ifdef FEAT_MBYTE
12618 else if (has_mbyte)
12619 i += (*mb_char2bytes)(n, temp + i);
12620#endif
12621 else
12622 temp[i++] = n;
12623 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012624 rettv->v_type = VAR_STRING;
12625 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012626
12627#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012628 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012629 {
12630 int row = mouse_row;
12631 int col = mouse_col;
12632 win_T *win;
12633 linenr_T lnum;
12634# ifdef FEAT_WINDOWS
12635 win_T *wp;
12636# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012637 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012638
12639 if (row >= 0 && col >= 0)
12640 {
12641 /* Find the window at the mouse coordinates and compute the
12642 * text position. */
12643 win = mouse_find_win(&row, &col);
12644 (void)mouse_comp_pos(win, &row, &col, &lnum);
12645# ifdef FEAT_WINDOWS
12646 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012647 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012648# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012649 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012650 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12651 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12652 }
12653 }
12654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655 }
12656}
12657
12658/*
12659 * "getcharmod()" function
12660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012662f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012664 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665}
12666
12667/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012668 * "getcharsearch()" function
12669 */
12670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012671f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012672{
12673 if (rettv_dict_alloc(rettv) != FAIL)
12674 {
12675 dict_T *dict = rettv->vval.v_dict;
12676
12677 dict_add_nr_str(dict, "char", 0L, last_csearch());
12678 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12679 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12680 }
12681}
12682
12683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684 * "getcmdline()" function
12685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012687f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689 rettv->v_type = VAR_STRING;
12690 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691}
12692
12693/*
12694 * "getcmdpos()" function
12695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012697f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012699 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700}
12701
12702/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012703 * "getcmdtype()" function
12704 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012706f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012707{
12708 rettv->v_type = VAR_STRING;
12709 rettv->vval.v_string = alloc(2);
12710 if (rettv->vval.v_string != NULL)
12711 {
12712 rettv->vval.v_string[0] = get_cmdline_type();
12713 rettv->vval.v_string[1] = NUL;
12714 }
12715}
12716
12717/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012718 * "getcmdwintype()" function
12719 */
12720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012721f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012722{
12723 rettv->v_type = VAR_STRING;
12724 rettv->vval.v_string = NULL;
12725#ifdef FEAT_CMDWIN
12726 rettv->vval.v_string = alloc(2);
12727 if (rettv->vval.v_string != NULL)
12728 {
12729 rettv->vval.v_string[0] = cmdwin_type;
12730 rettv->vval.v_string[1] = NUL;
12731 }
12732#endif
12733}
12734
12735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 * "getcwd()" function
12737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012739f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012741 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012742 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012744 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012745 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012746
12747 wp = find_tabwin(&argvars[0], &argvars[1]);
12748 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012750 if (wp->w_localdir != NULL)
12751 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12752 else if(globaldir != NULL)
12753 rettv->vval.v_string = vim_strsave(globaldir);
12754 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012755 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012756 cwd = alloc(MAXPATHL);
12757 if (cwd != NULL)
12758 {
12759 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12760 rettv->vval.v_string = vim_strsave(cwd);
12761 vim_free(cwd);
12762 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012763 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012764#ifdef BACKSLASH_IN_FILENAME
12765 if (rettv->vval.v_string != NULL)
12766 slash_adjust(rettv->vval.v_string);
12767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 }
12769}
12770
12771/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012772 * "getfontname()" function
12773 */
12774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012775f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777 rettv->v_type = VAR_STRING;
12778 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012779#ifdef FEAT_GUI
12780 if (gui.in_use)
12781 {
12782 GuiFont font;
12783 char_u *name = NULL;
12784
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012785 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012786 {
12787 /* Get the "Normal" font. Either the name saved by
12788 * hl_set_font_name() or from the font ID. */
12789 font = gui.norm_font;
12790 name = hl_get_font_name();
12791 }
12792 else
12793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012795 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12796 return;
12797 font = gui_mch_get_font(name, FALSE);
12798 if (font == NOFONT)
12799 return; /* Invalid font name, return empty string. */
12800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012802 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012803 gui_mch_free_font(font);
12804 }
12805#endif
12806}
12807
12808/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012809 * "getfperm({fname})" function
12810 */
12811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012812f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012813{
12814 char_u *fname;
12815 struct stat st;
12816 char_u *perm = NULL;
12817 char_u flags[] = "rwx";
12818 int i;
12819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012823 if (mch_stat((char *)fname, &st) >= 0)
12824 {
12825 perm = vim_strsave((char_u *)"---------");
12826 if (perm != NULL)
12827 {
12828 for (i = 0; i < 9; i++)
12829 {
12830 if (st.st_mode & (1 << (8 - i)))
12831 perm[i] = flags[i % 3];
12832 }
12833 }
12834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012836}
12837
12838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 * "getfsize({fname})" function
12840 */
12841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012842f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843{
12844 char_u *fname;
12845 struct stat st;
12846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850
12851 if (mch_stat((char *)fname, &st) >= 0)
12852 {
12853 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012858
12859 /* non-perfect check for overflow */
12860 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12861 rettv->vval.v_number = -2;
12862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 }
12864 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866}
12867
12868/*
12869 * "getftime({fname})" function
12870 */
12871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012872f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873{
12874 char_u *fname;
12875 struct stat st;
12876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878
12879 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012880 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012882 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883}
12884
12885/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012886 * "getftype({fname})" function
12887 */
12888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012889f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012890{
12891 char_u *fname;
12892 struct stat st;
12893 char_u *type = NULL;
12894 char *t;
12895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012899 if (mch_lstat((char *)fname, &st) >= 0)
12900 {
12901#ifdef S_ISREG
12902 if (S_ISREG(st.st_mode))
12903 t = "file";
12904 else if (S_ISDIR(st.st_mode))
12905 t = "dir";
12906# ifdef S_ISLNK
12907 else if (S_ISLNK(st.st_mode))
12908 t = "link";
12909# endif
12910# ifdef S_ISBLK
12911 else if (S_ISBLK(st.st_mode))
12912 t = "bdev";
12913# endif
12914# ifdef S_ISCHR
12915 else if (S_ISCHR(st.st_mode))
12916 t = "cdev";
12917# endif
12918# ifdef S_ISFIFO
12919 else if (S_ISFIFO(st.st_mode))
12920 t = "fifo";
12921# endif
12922# ifdef S_ISSOCK
12923 else if (S_ISSOCK(st.st_mode))
12924 t = "fifo";
12925# endif
12926 else
12927 t = "other";
12928#else
12929# ifdef S_IFMT
12930 switch (st.st_mode & S_IFMT)
12931 {
12932 case S_IFREG: t = "file"; break;
12933 case S_IFDIR: t = "dir"; break;
12934# ifdef S_IFLNK
12935 case S_IFLNK: t = "link"; break;
12936# endif
12937# ifdef S_IFBLK
12938 case S_IFBLK: t = "bdev"; break;
12939# endif
12940# ifdef S_IFCHR
12941 case S_IFCHR: t = "cdev"; break;
12942# endif
12943# ifdef S_IFIFO
12944 case S_IFIFO: t = "fifo"; break;
12945# endif
12946# ifdef S_IFSOCK
12947 case S_IFSOCK: t = "socket"; break;
12948# endif
12949 default: t = "other";
12950 }
12951# else
12952 if (mch_isdir(fname))
12953 t = "dir";
12954 else
12955 t = "file";
12956# endif
12957#endif
12958 type = vim_strsave((char_u *)t);
12959 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012961}
12962
12963/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012964 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012965 */
12966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012967f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012968{
12969 linenr_T lnum;
12970 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012971 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012972
12973 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012974 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012975 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012976 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012977 retlist = FALSE;
12978 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012979 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012980 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012981 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012982 retlist = TRUE;
12983 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012984
Bram Moolenaar342337a2005-07-21 21:11:17 +000012985 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012986}
12987
12988/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012989 * "getmatches()" function
12990 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012992f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012993{
12994#ifdef FEAT_SEARCH_EXTRA
12995 dict_T *dict;
12996 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012997 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012998
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012999 if (rettv_list_alloc(rettv) == OK)
13000 {
13001 while (cur != NULL)
13002 {
13003 dict = dict_alloc();
13004 if (dict == NULL)
13005 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013006 if (cur->match.regprog == NULL)
13007 {
13008 /* match added with matchaddpos() */
13009 for (i = 0; i < MAXPOSMATCH; ++i)
13010 {
13011 llpos_T *llpos;
13012 char buf[6];
13013 list_T *l;
13014
13015 llpos = &cur->pos.pos[i];
13016 if (llpos->lnum == 0)
13017 break;
13018 l = list_alloc();
13019 if (l == NULL)
13020 break;
13021 list_append_number(l, (varnumber_T)llpos->lnum);
13022 if (llpos->col > 0)
13023 {
13024 list_append_number(l, (varnumber_T)llpos->col);
13025 list_append_number(l, (varnumber_T)llpos->len);
13026 }
13027 sprintf(buf, "pos%d", i + 1);
13028 dict_add_list(dict, buf, l);
13029 }
13030 }
13031 else
13032 {
13033 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13034 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013035 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013036 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13037 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013038# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013039 if (cur->conceal_char)
13040 {
13041 char_u buf[MB_MAXBYTES + 1];
13042
13043 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13044 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13045 }
13046# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013047 list_append_dict(rettv->vval.v_list, dict);
13048 cur = cur->next;
13049 }
13050 }
13051#endif
13052}
13053
13054/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013055 * "getpid()" function
13056 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013058f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013059{
13060 rettv->vval.v_number = mch_get_pid();
13061}
13062
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013064getpos_both(
13065 typval_T *argvars,
13066 typval_T *rettv,
13067 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013068{
Bram Moolenaara5525202006-03-02 22:52:09 +000013069 pos_T *fp;
13070 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013071 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013072
13073 if (rettv_list_alloc(rettv) == OK)
13074 {
13075 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013076 if (getcurpos)
13077 fp = &curwin->w_cursor;
13078 else
13079 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013080 if (fnum != -1)
13081 list_append_number(l, (varnumber_T)fnum);
13082 else
13083 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013084 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13085 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013086 list_append_number(l, (fp != NULL)
13087 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013088 : (varnumber_T)0);
13089 list_append_number(l,
13090#ifdef FEAT_VIRTUALEDIT
13091 (fp != NULL) ? (varnumber_T)fp->coladd :
13092#endif
13093 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013094 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013095 {
13096 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013097 list_append_number(l, curwin->w_curswant == MAXCOL ?
13098 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013099 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013100 }
13101 else
13102 rettv->vval.v_number = FALSE;
13103}
13104
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013105
13106/*
13107 * "getcurpos()" function
13108 */
13109 static void
13110f_getcurpos(typval_T *argvars, typval_T *rettv)
13111{
13112 getpos_both(argvars, rettv, TRUE);
13113}
13114
13115/*
13116 * "getpos(string)" function
13117 */
13118 static void
13119f_getpos(typval_T *argvars, typval_T *rettv)
13120{
13121 getpos_both(argvars, rettv, FALSE);
13122}
13123
Bram Moolenaara5525202006-03-02 22:52:09 +000013124/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013125 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013126 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013128f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013129{
13130#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013131 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013132#endif
13133
Bram Moolenaar2641f772005-03-25 21:58:17 +000013134#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013135 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013136 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013137 wp = NULL;
13138 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13139 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013140 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013141 if (wp == NULL)
13142 return;
13143 }
13144
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013145 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013146 }
13147#endif
13148}
13149
13150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151 * "getreg()" function
13152 */
13153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013154f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155{
13156 char_u *strregname;
13157 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013158 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013159 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013160 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013162 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013164 strregname = get_tv_string_chk(&argvars[0]);
13165 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013166 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013168 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013169 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13170 return_list = get_tv_number_chk(&argvars[2], &error);
13171 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013174 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013175
13176 if (error)
13177 return;
13178
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179 regname = (strregname == NULL ? '"' : *strregname);
13180 if (regname == 0)
13181 regname = '"';
13182
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013183 if (return_list)
13184 {
13185 rettv->v_type = VAR_LIST;
13186 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13187 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013188 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013189 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013190 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013191 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013192 }
13193 else
13194 {
13195 rettv->v_type = VAR_STRING;
13196 rettv->vval.v_string = get_reg_contents(regname,
13197 arg2 ? GREG_EXPR_SRC : 0);
13198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199}
13200
13201/*
13202 * "getregtype()" function
13203 */
13204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013205f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206{
13207 char_u *strregname;
13208 int regname;
13209 char_u buf[NUMBUFLEN + 2];
13210 long reglen = 0;
13211
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013212 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013213 {
13214 strregname = get_tv_string_chk(&argvars[0]);
13215 if (strregname == NULL) /* type error; errmsg already given */
13216 {
13217 rettv->v_type = VAR_STRING;
13218 rettv->vval.v_string = NULL;
13219 return;
13220 }
13221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 else
13223 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013224 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225
13226 regname = (strregname == NULL ? '"' : *strregname);
13227 if (regname == 0)
13228 regname = '"';
13229
13230 buf[0] = NUL;
13231 buf[1] = NUL;
13232 switch (get_reg_type(regname, &reglen))
13233 {
13234 case MLINE: buf[0] = 'V'; break;
13235 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 case MBLOCK:
13237 buf[0] = Ctrl_V;
13238 sprintf((char *)buf + 1, "%ld", reglen + 1);
13239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 rettv->v_type = VAR_STRING;
13242 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243}
13244
13245/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013246 * "gettabvar()" function
13247 */
13248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013249f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013250{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013251 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013252 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013253 dictitem_T *v;
13254 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013255 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013256
13257 rettv->v_type = VAR_STRING;
13258 rettv->vval.v_string = NULL;
13259
13260 varname = get_tv_string_chk(&argvars[1]);
13261 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13262 if (tp != NULL && varname != NULL)
13263 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013264 /* Set tp to be our tabpage, temporarily. Also set the window to the
13265 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013266 if (switch_win(&oldcurwin, &oldtabpage,
13267 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013268 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013269 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013270 /* look up the variable */
13271 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13272 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13273 if (v != NULL)
13274 {
13275 copy_tv(&v->di_tv, rettv);
13276 done = TRUE;
13277 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013278 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013279
13280 /* restore previous notion of curwin */
13281 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013282 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013283
13284 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013285 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013286}
13287
13288/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013289 * "gettabwinvar()" function
13290 */
13291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013292f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013293{
13294 getwinvar(argvars, rettv, 1);
13295}
13296
13297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 * "getwinposx()" function
13299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013301f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013303 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304#ifdef FEAT_GUI
13305 if (gui.in_use)
13306 {
13307 int x, y;
13308
13309 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013310 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311 }
13312#endif
13313}
13314
13315/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013316 * "win_findbuf()" function
13317 */
13318 static void
13319f_win_findbuf(typval_T *argvars, typval_T *rettv)
13320{
13321 if (rettv_list_alloc(rettv) != FAIL)
13322 win_findbuf(argvars, rettv->vval.v_list);
13323}
13324
13325/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013326 * "win_getid()" function
13327 */
13328 static void
13329f_win_getid(typval_T *argvars, typval_T *rettv)
13330{
13331 rettv->vval.v_number = win_getid(argvars);
13332}
13333
13334/*
13335 * "win_gotoid()" function
13336 */
13337 static void
13338f_win_gotoid(typval_T *argvars, typval_T *rettv)
13339{
13340 rettv->vval.v_number = win_gotoid(argvars);
13341}
13342
13343/*
13344 * "win_id2tabwin()" function
13345 */
13346 static void
13347f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13348{
13349 if (rettv_list_alloc(rettv) != FAIL)
13350 win_id2tabwin(argvars, rettv->vval.v_list);
13351}
13352
13353/*
13354 * "win_id2win()" function
13355 */
13356 static void
13357f_win_id2win(typval_T *argvars, typval_T *rettv)
13358{
13359 rettv->vval.v_number = win_id2win(argvars);
13360}
13361
13362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363 * "getwinposy()" function
13364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013366f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369#ifdef FEAT_GUI
13370 if (gui.in_use)
13371 {
13372 int x, y;
13373
13374 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013375 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376 }
13377#endif
13378}
13379
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013380/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013381 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013382 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013383 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013384find_win_by_nr(
13385 typval_T *vp,
13386 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013387{
13388#ifdef FEAT_WINDOWS
13389 win_T *wp;
13390#endif
13391 int nr;
13392
13393 nr = get_tv_number_chk(vp, NULL);
13394
13395#ifdef FEAT_WINDOWS
13396 if (nr < 0)
13397 return NULL;
13398 if (nr == 0)
13399 return curwin;
13400
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013401 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13402 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013403 if (--nr <= 0)
13404 break;
13405 return wp;
13406#else
13407 if (nr == 0 || nr == 1)
13408 return curwin;
13409 return NULL;
13410#endif
13411}
13412
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013414 * Find window specified by "wvp" in tabpage "tvp".
13415 */
13416 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013417find_tabwin(
13418 typval_T *wvp, /* VAR_UNKNOWN for current window */
13419 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013420{
13421 win_T *wp = NULL;
13422 tabpage_T *tp = NULL;
13423 long n;
13424
13425 if (wvp->v_type != VAR_UNKNOWN)
13426 {
13427 if (tvp->v_type != VAR_UNKNOWN)
13428 {
13429 n = get_tv_number(tvp);
13430 if (n >= 0)
13431 tp = find_tabpage(n);
13432 }
13433 else
13434 tp = curtab;
13435
13436 if (tp != NULL)
13437 wp = find_win_by_nr(wvp, tp);
13438 }
13439 else
13440 wp = curwin;
13441
13442 return wp;
13443}
13444
13445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 * "getwinvar()" function
13447 */
13448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013449f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013451 getwinvar(argvars, rettv, 0);
13452}
13453
13454/*
13455 * getwinvar() and gettabwinvar()
13456 */
13457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013458getwinvar(
13459 typval_T *argvars,
13460 typval_T *rettv,
13461 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013462{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013463 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013465 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013466 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013467 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013468#ifdef FEAT_WINDOWS
13469 win_T *oldcurwin;
13470 tabpage_T *oldtabpage;
13471 int need_switch_win;
13472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013474#ifdef FEAT_WINDOWS
13475 if (off == 1)
13476 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13477 else
13478 tp = curtab;
13479#endif
13480 win = find_win_by_nr(&argvars[off], tp);
13481 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013482 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013484 rettv->v_type = VAR_STRING;
13485 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486
13487 if (win != NULL && varname != NULL)
13488 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013489#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013490 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013491 * otherwise the window is not valid. Only do this when needed,
13492 * autocommands get blocked. */
13493 need_switch_win = !(tp == curtab && win == curwin);
13494 if (!need_switch_win
13495 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13496#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013497 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013498 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013499 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013500 if (get_option_tv(&varname, rettv, 1) == OK)
13501 done = TRUE;
13502 }
13503 else
13504 {
13505 /* Look up the variable. */
13506 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13507 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13508 varname, FALSE);
13509 if (v != NULL)
13510 {
13511 copy_tv(&v->di_tv, rettv);
13512 done = TRUE;
13513 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013516
Bram Moolenaarba117c22015-09-29 16:53:22 +020013517#ifdef FEAT_WINDOWS
13518 if (need_switch_win)
13519 /* restore previous notion of curwin */
13520 restore_win(oldcurwin, oldtabpage, TRUE);
13521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522 }
13523
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013524 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13525 /* use the default return value */
13526 copy_tv(&argvars[off + 2], rettv);
13527
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 --emsg_off;
13529}
13530
13531/*
13532 * "glob()" function
13533 */
13534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013535f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013537 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013539 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013541 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013542 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013543 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013544 if (argvars[1].v_type != VAR_UNKNOWN)
13545 {
13546 if (get_tv_number_chk(&argvars[1], &error))
13547 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013548 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013549 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013550 if (get_tv_number_chk(&argvars[2], &error))
13551 {
13552 rettv->v_type = VAR_LIST;
13553 rettv->vval.v_list = NULL;
13554 }
13555 if (argvars[3].v_type != VAR_UNKNOWN
13556 && get_tv_number_chk(&argvars[3], &error))
13557 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013558 }
13559 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013560 if (!error)
13561 {
13562 ExpandInit(&xpc);
13563 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013564 if (p_wic)
13565 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013566 if (rettv->v_type == VAR_STRING)
13567 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013568 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013569 else if (rettv_list_alloc(rettv) != FAIL)
13570 {
13571 int i;
13572
13573 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13574 NULL, options, WILD_ALL_KEEP);
13575 for (i = 0; i < xpc.xp_numfiles; i++)
13576 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13577
13578 ExpandCleanup(&xpc);
13579 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013580 }
13581 else
13582 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583}
13584
13585/*
13586 * "globpath()" function
13587 */
13588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013589f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013591 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013593 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013594 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013595 garray_T ga;
13596 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013598 /* When the optional second argument is non-zero, don't remove matches
13599 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013600 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013601 if (argvars[2].v_type != VAR_UNKNOWN)
13602 {
13603 if (get_tv_number_chk(&argvars[2], &error))
13604 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013605 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013606 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013607 if (get_tv_number_chk(&argvars[3], &error))
13608 {
13609 rettv->v_type = VAR_LIST;
13610 rettv->vval.v_list = NULL;
13611 }
13612 if (argvars[4].v_type != VAR_UNKNOWN
13613 && get_tv_number_chk(&argvars[4], &error))
13614 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013615 }
13616 }
13617 if (file != NULL && !error)
13618 {
13619 ga_init2(&ga, (int)sizeof(char_u *), 10);
13620 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13621 if (rettv->v_type == VAR_STRING)
13622 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13623 else if (rettv_list_alloc(rettv) != FAIL)
13624 for (i = 0; i < ga.ga_len; ++i)
13625 list_append_string(rettv->vval.v_list,
13626 ((char_u **)(ga.ga_data))[i], -1);
13627 ga_clear_strings(&ga);
13628 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013629 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013630 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631}
13632
13633/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013634 * "glob2regpat()" function
13635 */
13636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013637f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013638{
13639 char_u *pat = get_tv_string_chk(&argvars[0]);
13640
13641 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013642 rettv->vval.v_string = (pat == NULL)
13643 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013644}
13645
13646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 * "has()" function
13648 */
13649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013650f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651{
13652 int i;
13653 char_u *name;
13654 int n = FALSE;
13655 static char *(has_list[]) =
13656 {
13657#ifdef AMIGA
13658 "amiga",
13659# ifdef FEAT_ARP
13660 "arp",
13661# endif
13662#endif
13663#ifdef __BEOS__
13664 "beos",
13665#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013666#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 "mac",
13668#endif
13669#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013670 "macunix", /* built with 'darwin' enabled */
13671#endif
13672#if defined(__APPLE__) && __APPLE__ == 1
13673 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675#ifdef __QNX__
13676 "qnx",
13677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678#ifdef UNIX
13679 "unix",
13680#endif
13681#ifdef VMS
13682 "vms",
13683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684#ifdef WIN32
13685 "win32",
13686#endif
13687#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13688 "win32unix",
13689#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013690#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 "win64",
13692#endif
13693#ifdef EBCDIC
13694 "ebcdic",
13695#endif
13696#ifndef CASE_INSENSITIVE_FILENAME
13697 "fname_case",
13698#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013699#ifdef HAVE_ACL
13700 "acl",
13701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702#ifdef FEAT_ARABIC
13703 "arabic",
13704#endif
13705#ifdef FEAT_AUTOCMD
13706 "autocmd",
13707#endif
13708#ifdef FEAT_BEVAL
13709 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013710# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13711 "balloon_multiline",
13712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713#endif
13714#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13715 "builtin_terms",
13716# ifdef ALL_BUILTIN_TCAPS
13717 "all_builtin_terms",
13718# endif
13719#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013720#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13721 || defined(FEAT_GUI_W32) \
13722 || defined(FEAT_GUI_MOTIF))
13723 "browsefilter",
13724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725#ifdef FEAT_BYTEOFF
13726 "byte_offset",
13727#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013728#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013729 "channel",
13730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731#ifdef FEAT_CINDENT
13732 "cindent",
13733#endif
13734#ifdef FEAT_CLIENTSERVER
13735 "clientserver",
13736#endif
13737#ifdef FEAT_CLIPBOARD
13738 "clipboard",
13739#endif
13740#ifdef FEAT_CMDL_COMPL
13741 "cmdline_compl",
13742#endif
13743#ifdef FEAT_CMDHIST
13744 "cmdline_hist",
13745#endif
13746#ifdef FEAT_COMMENTS
13747 "comments",
13748#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013749#ifdef FEAT_CONCEAL
13750 "conceal",
13751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752#ifdef FEAT_CRYPT
13753 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013754 "crypt-blowfish",
13755 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756#endif
13757#ifdef FEAT_CSCOPE
13758 "cscope",
13759#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013760#ifdef FEAT_CURSORBIND
13761 "cursorbind",
13762#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013763#ifdef CURSOR_SHAPE
13764 "cursorshape",
13765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766#ifdef DEBUG
13767 "debug",
13768#endif
13769#ifdef FEAT_CON_DIALOG
13770 "dialog_con",
13771#endif
13772#ifdef FEAT_GUI_DIALOG
13773 "dialog_gui",
13774#endif
13775#ifdef FEAT_DIFF
13776 "diff",
13777#endif
13778#ifdef FEAT_DIGRAPHS
13779 "digraphs",
13780#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013781#ifdef FEAT_DIRECTX
13782 "directx",
13783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784#ifdef FEAT_DND
13785 "dnd",
13786#endif
13787#ifdef FEAT_EMACS_TAGS
13788 "emacs_tags",
13789#endif
13790 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013791 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792#ifdef FEAT_SEARCH_EXTRA
13793 "extra_search",
13794#endif
13795#ifdef FEAT_FKMAP
13796 "farsi",
13797#endif
13798#ifdef FEAT_SEARCHPATH
13799 "file_in_path",
13800#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013801#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013802 "filterpipe",
13803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804#ifdef FEAT_FIND_ID
13805 "find_in_path",
13806#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013807#ifdef FEAT_FLOAT
13808 "float",
13809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810#ifdef FEAT_FOLDING
13811 "folding",
13812#endif
13813#ifdef FEAT_FOOTER
13814 "footer",
13815#endif
13816#if !defined(USE_SYSTEM) && defined(UNIX)
13817 "fork",
13818#endif
13819#ifdef FEAT_GETTEXT
13820 "gettext",
13821#endif
13822#ifdef FEAT_GUI
13823 "gui",
13824#endif
13825#ifdef FEAT_GUI_ATHENA
13826# ifdef FEAT_GUI_NEXTAW
13827 "gui_neXtaw",
13828# else
13829 "gui_athena",
13830# endif
13831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832#ifdef FEAT_GUI_GTK
13833 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013834# ifdef USE_GTK3
13835 "gui_gtk3",
13836# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013840#ifdef FEAT_GUI_GNOME
13841 "gui_gnome",
13842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843#ifdef FEAT_GUI_MAC
13844 "gui_mac",
13845#endif
13846#ifdef FEAT_GUI_MOTIF
13847 "gui_motif",
13848#endif
13849#ifdef FEAT_GUI_PHOTON
13850 "gui_photon",
13851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852#ifdef FEAT_GUI_W32
13853 "gui_win32",
13854#endif
13855#ifdef FEAT_HANGULIN
13856 "hangul_input",
13857#endif
13858#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13859 "iconv",
13860#endif
13861#ifdef FEAT_INS_EXPAND
13862 "insert_expand",
13863#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013864#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013865 "job",
13866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867#ifdef FEAT_JUMPLIST
13868 "jumplist",
13869#endif
13870#ifdef FEAT_KEYMAP
13871 "keymap",
13872#endif
13873#ifdef FEAT_LANGMAP
13874 "langmap",
13875#endif
13876#ifdef FEAT_LIBCALL
13877 "libcall",
13878#endif
13879#ifdef FEAT_LINEBREAK
13880 "linebreak",
13881#endif
13882#ifdef FEAT_LISP
13883 "lispindent",
13884#endif
13885#ifdef FEAT_LISTCMDS
13886 "listcmds",
13887#endif
13888#ifdef FEAT_LOCALMAP
13889 "localmap",
13890#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013891#ifdef FEAT_LUA
13892# ifndef DYNAMIC_LUA
13893 "lua",
13894# endif
13895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896#ifdef FEAT_MENU
13897 "menu",
13898#endif
13899#ifdef FEAT_SESSION
13900 "mksession",
13901#endif
13902#ifdef FEAT_MODIFY_FNAME
13903 "modify_fname",
13904#endif
13905#ifdef FEAT_MOUSE
13906 "mouse",
13907#endif
13908#ifdef FEAT_MOUSESHAPE
13909 "mouseshape",
13910#endif
13911#if defined(UNIX) || defined(VMS)
13912# ifdef FEAT_MOUSE_DEC
13913 "mouse_dec",
13914# endif
13915# ifdef FEAT_MOUSE_GPM
13916 "mouse_gpm",
13917# endif
13918# ifdef FEAT_MOUSE_JSB
13919 "mouse_jsbterm",
13920# endif
13921# ifdef FEAT_MOUSE_NET
13922 "mouse_netterm",
13923# endif
13924# ifdef FEAT_MOUSE_PTERM
13925 "mouse_pterm",
13926# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013927# ifdef FEAT_MOUSE_SGR
13928 "mouse_sgr",
13929# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013930# ifdef FEAT_SYSMOUSE
13931 "mouse_sysmouse",
13932# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013933# ifdef FEAT_MOUSE_URXVT
13934 "mouse_urxvt",
13935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936# ifdef FEAT_MOUSE_XTERM
13937 "mouse_xterm",
13938# endif
13939#endif
13940#ifdef FEAT_MBYTE
13941 "multi_byte",
13942#endif
13943#ifdef FEAT_MBYTE_IME
13944 "multi_byte_ime",
13945#endif
13946#ifdef FEAT_MULTI_LANG
13947 "multi_lang",
13948#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013949#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013950#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013951 "mzscheme",
13952#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954#ifdef FEAT_OLE
13955 "ole",
13956#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013957 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958#ifdef FEAT_PATH_EXTRA
13959 "path_extra",
13960#endif
13961#ifdef FEAT_PERL
13962#ifndef DYNAMIC_PERL
13963 "perl",
13964#endif
13965#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013966#ifdef FEAT_PERSISTENT_UNDO
13967 "persistent_undo",
13968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969#ifdef FEAT_PYTHON
13970#ifndef DYNAMIC_PYTHON
13971 "python",
13972#endif
13973#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013974#ifdef FEAT_PYTHON3
13975#ifndef DYNAMIC_PYTHON3
13976 "python3",
13977#endif
13978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979#ifdef FEAT_POSTSCRIPT
13980 "postscript",
13981#endif
13982#ifdef FEAT_PRINTER
13983 "printer",
13984#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013985#ifdef FEAT_PROFILE
13986 "profile",
13987#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013988#ifdef FEAT_RELTIME
13989 "reltime",
13990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991#ifdef FEAT_QUICKFIX
13992 "quickfix",
13993#endif
13994#ifdef FEAT_RIGHTLEFT
13995 "rightleft",
13996#endif
13997#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13998 "ruby",
13999#endif
14000#ifdef FEAT_SCROLLBIND
14001 "scrollbind",
14002#endif
14003#ifdef FEAT_CMDL_INFO
14004 "showcmd",
14005 "cmdline_info",
14006#endif
14007#ifdef FEAT_SIGNS
14008 "signs",
14009#endif
14010#ifdef FEAT_SMARTINDENT
14011 "smartindent",
14012#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014013#ifdef STARTUPTIME
14014 "startuptime",
14015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016#ifdef FEAT_STL_OPT
14017 "statusline",
14018#endif
14019#ifdef FEAT_SUN_WORKSHOP
14020 "sun_workshop",
14021#endif
14022#ifdef FEAT_NETBEANS_INTG
14023 "netbeans_intg",
14024#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014025#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014026 "spell",
14027#endif
14028#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 "syntax",
14030#endif
14031#if defined(USE_SYSTEM) || !defined(UNIX)
14032 "system",
14033#endif
14034#ifdef FEAT_TAG_BINS
14035 "tag_binary",
14036#endif
14037#ifdef FEAT_TAG_OLDSTATIC
14038 "tag_old_static",
14039#endif
14040#ifdef FEAT_TAG_ANYWHITE
14041 "tag_any_white",
14042#endif
14043#ifdef FEAT_TCL
14044# ifndef DYNAMIC_TCL
14045 "tcl",
14046# endif
14047#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014048#ifdef FEAT_TERMGUICOLORS
14049 "termguicolors",
14050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051#ifdef TERMINFO
14052 "terminfo",
14053#endif
14054#ifdef FEAT_TERMRESPONSE
14055 "termresponse",
14056#endif
14057#ifdef FEAT_TEXTOBJ
14058 "textobjects",
14059#endif
14060#ifdef HAVE_TGETENT
14061 "tgetent",
14062#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014063#ifdef FEAT_TIMERS
14064 "timers",
14065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066#ifdef FEAT_TITLE
14067 "title",
14068#endif
14069#ifdef FEAT_TOOLBAR
14070 "toolbar",
14071#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014072#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14073 "unnamedplus",
14074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075#ifdef FEAT_USR_CMDS
14076 "user-commands", /* was accidentally included in 5.4 */
14077 "user_commands",
14078#endif
14079#ifdef FEAT_VIMINFO
14080 "viminfo",
14081#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014082#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083 "vertsplit",
14084#endif
14085#ifdef FEAT_VIRTUALEDIT
14086 "virtualedit",
14087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089#ifdef FEAT_VISUALEXTRA
14090 "visualextra",
14091#endif
14092#ifdef FEAT_VREPLACE
14093 "vreplace",
14094#endif
14095#ifdef FEAT_WILDIGN
14096 "wildignore",
14097#endif
14098#ifdef FEAT_WILDMENU
14099 "wildmenu",
14100#endif
14101#ifdef FEAT_WINDOWS
14102 "windows",
14103#endif
14104#ifdef FEAT_WAK
14105 "winaltkeys",
14106#endif
14107#ifdef FEAT_WRITEBACKUP
14108 "writebackup",
14109#endif
14110#ifdef FEAT_XIM
14111 "xim",
14112#endif
14113#ifdef FEAT_XFONTSET
14114 "xfontset",
14115#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014116#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014117 "xpm",
14118 "xpm_w32", /* for backward compatibility */
14119#else
14120# if defined(HAVE_XPM)
14121 "xpm",
14122# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124#ifdef USE_XSMP
14125 "xsmp",
14126#endif
14127#ifdef USE_XSMP_INTERACT
14128 "xsmp_interact",
14129#endif
14130#ifdef FEAT_XCLIPBOARD
14131 "xterm_clipboard",
14132#endif
14133#ifdef FEAT_XTERM_SAVE
14134 "xterm_save",
14135#endif
14136#if defined(UNIX) && defined(FEAT_X11)
14137 "X11",
14138#endif
14139 NULL
14140 };
14141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014142 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 for (i = 0; has_list[i] != NULL; ++i)
14144 if (STRICMP(name, has_list[i]) == 0)
14145 {
14146 n = TRUE;
14147 break;
14148 }
14149
14150 if (n == FALSE)
14151 {
14152 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014153 {
14154 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014155 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014156 && vim_isdigit(name[6])
14157 && vim_isdigit(name[8])
14158 && vim_isdigit(name[10]))
14159 {
14160 int major = atoi((char *)name + 6);
14161 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014162
14163 /* Expect "patch-9.9.01234". */
14164 n = (major < VIM_VERSION_MAJOR
14165 || (major == VIM_VERSION_MAJOR
14166 && (minor < VIM_VERSION_MINOR
14167 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014168 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014169 }
14170 else
14171 n = has_patch(atoi((char *)name + 5));
14172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 else if (STRICMP(name, "vim_starting") == 0)
14174 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014175#ifdef FEAT_MBYTE
14176 else if (STRICMP(name, "multi_byte_encoding") == 0)
14177 n = has_mbyte;
14178#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014179#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14180 else if (STRICMP(name, "balloon_multiline") == 0)
14181 n = multiline_balloon_available();
14182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183#ifdef DYNAMIC_TCL
14184 else if (STRICMP(name, "tcl") == 0)
14185 n = tcl_enabled(FALSE);
14186#endif
14187#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14188 else if (STRICMP(name, "iconv") == 0)
14189 n = iconv_enabled(FALSE);
14190#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014191#ifdef DYNAMIC_LUA
14192 else if (STRICMP(name, "lua") == 0)
14193 n = lua_enabled(FALSE);
14194#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014195#ifdef DYNAMIC_MZSCHEME
14196 else if (STRICMP(name, "mzscheme") == 0)
14197 n = mzscheme_enabled(FALSE);
14198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199#ifdef DYNAMIC_RUBY
14200 else if (STRICMP(name, "ruby") == 0)
14201 n = ruby_enabled(FALSE);
14202#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014203#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204#ifdef DYNAMIC_PYTHON
14205 else if (STRICMP(name, "python") == 0)
14206 n = python_enabled(FALSE);
14207#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014208#endif
14209#ifdef FEAT_PYTHON3
14210#ifdef DYNAMIC_PYTHON3
14211 else if (STRICMP(name, "python3") == 0)
14212 n = python3_enabled(FALSE);
14213#endif
14214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215#ifdef DYNAMIC_PERL
14216 else if (STRICMP(name, "perl") == 0)
14217 n = perl_enabled(FALSE);
14218#endif
14219#ifdef FEAT_GUI
14220 else if (STRICMP(name, "gui_running") == 0)
14221 n = (gui.in_use || gui.starting);
14222# ifdef FEAT_GUI_W32
14223 else if (STRICMP(name, "gui_win32s") == 0)
14224 n = gui_is_win32s();
14225# endif
14226# ifdef FEAT_BROWSE
14227 else if (STRICMP(name, "browse") == 0)
14228 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14229# endif
14230#endif
14231#ifdef FEAT_SYN_HL
14232 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014233 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234#endif
14235#if defined(WIN3264)
14236 else if (STRICMP(name, "win95") == 0)
14237 n = mch_windows95();
14238#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014239#ifdef FEAT_NETBEANS_INTG
14240 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014241 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 }
14244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014245 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246}
14247
14248/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014249 * "has_key()" function
14250 */
14251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014252f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014253{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014254 if (argvars[0].v_type != VAR_DICT)
14255 {
14256 EMSG(_(e_dictreq));
14257 return;
14258 }
14259 if (argvars[0].vval.v_dict == NULL)
14260 return;
14261
14262 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014263 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014264}
14265
14266/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014267 * "haslocaldir()" function
14268 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014270f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014271{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014272 win_T *wp = NULL;
14273
14274 wp = find_tabwin(&argvars[0], &argvars[1]);
14275 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014276}
14277
14278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 * "hasmapto()" function
14280 */
14281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014282f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283{
14284 char_u *name;
14285 char_u *mode;
14286 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014287 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014289 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014290 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 mode = (char_u *)"nvo";
14292 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014294 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014295 if (argvars[2].v_type != VAR_UNKNOWN)
14296 abbr = get_tv_number(&argvars[2]);
14297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298
Bram Moolenaar2c932302006-03-18 21:42:09 +000014299 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014300 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014302 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303}
14304
14305/*
14306 * "histadd()" function
14307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014309f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310{
14311#ifdef FEAT_CMDHIST
14312 int histype;
14313 char_u *str;
14314 char_u buf[NUMBUFLEN];
14315#endif
14316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 if (check_restricted() || check_secure())
14319 return;
14320#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014321 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14322 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 if (histype >= 0)
14324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014325 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 if (*str != NUL)
14327 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014328 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014330 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 return;
14332 }
14333 }
14334#endif
14335}
14336
14337/*
14338 * "histdel()" function
14339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014341f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342{
14343#ifdef FEAT_CMDHIST
14344 int n;
14345 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014346 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14349 if (str == NULL)
14350 n = 0;
14351 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014353 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014354 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014356 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014357 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 else
14359 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014361 get_tv_string_buf(&argvars[1], buf));
14362 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363#endif
14364}
14365
14366/*
14367 * "histget()" function
14368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014370f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371{
14372#ifdef FEAT_CMDHIST
14373 int type;
14374 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14378 if (str == NULL)
14379 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014381 {
14382 type = get_histtype(str);
14383 if (argvars[1].v_type == VAR_UNKNOWN)
14384 idx = get_history_idx(type);
14385 else
14386 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14387 /* -1 on type error */
14388 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394}
14395
14396/*
14397 * "histnr()" function
14398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014400f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401{
14402 int i;
14403
14404#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014405 char_u *history = get_tv_string_chk(&argvars[0]);
14406
14407 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 if (i >= HIST_CMD && i < HIST_COUNT)
14409 i = get_history_idx(i);
14410 else
14411#endif
14412 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014413 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414}
14415
14416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417 * "highlightID(name)" function
14418 */
14419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014420f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014422 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423}
14424
14425/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014426 * "highlight_exists()" function
14427 */
14428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014429f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014430{
14431 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14432}
14433
14434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 * "hostname()" function
14436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014438f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439{
14440 char_u hostname[256];
14441
14442 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014443 rettv->v_type = VAR_STRING;
14444 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445}
14446
14447/*
14448 * iconv() function
14449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014451f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452{
14453#ifdef FEAT_MBYTE
14454 char_u buf1[NUMBUFLEN];
14455 char_u buf2[NUMBUFLEN];
14456 char_u *from, *to, *str;
14457 vimconv_T vimconv;
14458#endif
14459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460 rettv->v_type = VAR_STRING;
14461 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462
14463#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 str = get_tv_string(&argvars[0]);
14465 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14466 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 vimconv.vc_type = CONV_NONE;
14468 convert_setup(&vimconv, from, to);
14469
14470 /* If the encodings are equal, no conversion needed. */
14471 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014472 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475
14476 convert_setup(&vimconv, NULL, NULL);
14477 vim_free(from);
14478 vim_free(to);
14479#endif
14480}
14481
14482/*
14483 * "indent()" function
14484 */
14485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014486f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487{
14488 linenr_T lnum;
14489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014492 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014494 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495}
14496
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014497/*
14498 * "index()" function
14499 */
14500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014501f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014502{
Bram Moolenaar33570922005-01-25 22:26:29 +000014503 list_T *l;
14504 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014505 long idx = 0;
14506 int ic = FALSE;
14507
14508 rettv->vval.v_number = -1;
14509 if (argvars[0].v_type != VAR_LIST)
14510 {
14511 EMSG(_(e_listreq));
14512 return;
14513 }
14514 l = argvars[0].vval.v_list;
14515 if (l != NULL)
14516 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014517 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014518 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014520 int error = FALSE;
14521
Bram Moolenaar758711c2005-02-02 23:11:38 +000014522 /* Start at specified item. Use the cached index that list_find()
14523 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014524 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014525 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014526 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014527 ic = get_tv_number_chk(&argvars[3], &error);
14528 if (error)
14529 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014531
Bram Moolenaar758711c2005-02-02 23:11:38 +000014532 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014533 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014534 {
14535 rettv->vval.v_number = idx;
14536 break;
14537 }
14538 }
14539}
14540
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541static int inputsecret_flag = 0;
14542
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014543static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014544
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014546 * This function is used by f_input() and f_inputdialog() functions. The third
14547 * argument to f_input() specifies the type of completion to use at the
14548 * prompt. The third argument to f_inputdialog() specifies the value to return
14549 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 */
14551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014552get_user_input(
14553 typval_T *argvars,
14554 typval_T *rettv,
14555 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014557 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 char_u *p = NULL;
14559 int c;
14560 char_u buf[NUMBUFLEN];
14561 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014562 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014563 int xp_type = EXPAND_NOTHING;
14564 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014567 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568
14569#ifdef NO_CONSOLE_INPUT
14570 /* While starting up, there is no place to enter text. */
14571 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573#endif
14574
14575 cmd_silent = FALSE; /* Want to see the prompt. */
14576 if (prompt != NULL)
14577 {
14578 /* Only the part of the message after the last NL is considered as
14579 * prompt for the command line */
14580 p = vim_strrchr(prompt, '\n');
14581 if (p == NULL)
14582 p = prompt;
14583 else
14584 {
14585 ++p;
14586 c = *p;
14587 *p = NUL;
14588 msg_start();
14589 msg_clr_eos();
14590 msg_puts_attr(prompt, echo_attr);
14591 msg_didout = FALSE;
14592 msg_starthere();
14593 *p = c;
14594 }
14595 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014597 if (argvars[1].v_type != VAR_UNKNOWN)
14598 {
14599 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14600 if (defstr != NULL)
14601 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014603 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014604 {
14605 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014606 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014607 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014608
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014609 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014610 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014611
Bram Moolenaar4463f292005-09-25 22:20:24 +000014612 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14613 if (xp_name == NULL)
14614 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014615
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014616 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014617
Bram Moolenaar4463f292005-09-25 22:20:24 +000014618 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14619 &xp_arg) == FAIL)
14620 return;
14621 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014622 }
14623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014624 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014625 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014626 int save_ex_normal_busy = ex_normal_busy;
14627 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014628 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014629 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14630 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014631 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014632 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014633 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014634 && argvars[1].v_type != VAR_UNKNOWN
14635 && argvars[2].v_type != VAR_UNKNOWN)
14636 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14637 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014638
14639 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014641 /* since the user typed this, no need to wait for return */
14642 need_wait_return = FALSE;
14643 msg_didout = FALSE;
14644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645 cmd_silent = cmd_silent_save;
14646}
14647
14648/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014649 * "input()" function
14650 * Also handles inputsecret() when inputsecret is set.
14651 */
14652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014653f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014654{
14655 get_user_input(argvars, rettv, FALSE);
14656}
14657
14658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 * "inputdialog()" function
14660 */
14661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014662f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663{
14664#if defined(FEAT_GUI_TEXTDIALOG)
14665 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14666 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14667 {
14668 char_u *message;
14669 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014670 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014672 message = get_tv_string_chk(&argvars[0]);
14673 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014674 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014675 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676 else
14677 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014678 if (message != NULL && defstr != NULL
14679 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014680 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682 else
14683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014684 if (message != NULL && defstr != NULL
14685 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014686 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 rettv->vval.v_string = vim_strsave(
14688 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014692 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693 }
14694 else
14695#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014696 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697}
14698
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014699/*
14700 * "inputlist()" function
14701 */
14702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014703f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014704{
14705 listitem_T *li;
14706 int selected;
14707 int mouse_used;
14708
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014709#ifdef NO_CONSOLE_INPUT
14710 /* While starting up, there is no place to enter text. */
14711 if (no_console_input())
14712 return;
14713#endif
14714 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14715 {
14716 EMSG2(_(e_listarg), "inputlist()");
14717 return;
14718 }
14719
14720 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014721 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014722 lines_left = Rows; /* avoid more prompt */
14723 msg_scroll = TRUE;
14724 msg_clr_eos();
14725
14726 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14727 {
14728 msg_puts(get_tv_string(&li->li_tv));
14729 msg_putchar('\n');
14730 }
14731
14732 /* Ask for choice. */
14733 selected = prompt_for_number(&mouse_used);
14734 if (mouse_used)
14735 selected -= lines_left;
14736
14737 rettv->vval.v_number = selected;
14738}
14739
14740
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14742
14743/*
14744 * "inputrestore()" function
14745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014747f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748{
14749 if (ga_userinput.ga_len > 0)
14750 {
14751 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14753 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014754 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 }
14756 else if (p_verbose > 1)
14757 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014758 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014759 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 }
14761}
14762
14763/*
14764 * "inputsave()" function
14765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014767f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014769 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770 if (ga_grow(&ga_userinput, 1) == OK)
14771 {
14772 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14773 + ga_userinput.ga_len);
14774 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014775 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776 }
14777 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014778 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779}
14780
14781/*
14782 * "inputsecret()" function
14783 */
14784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014785f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786{
14787 ++cmdline_star;
14788 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014789 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 --cmdline_star;
14791 --inputsecret_flag;
14792}
14793
14794/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014795 * "insert()" function
14796 */
14797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014798f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014799{
14800 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014801 listitem_T *item;
14802 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014803 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014804
14805 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014806 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014807 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014808 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014809 {
14810 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014811 before = get_tv_number_chk(&argvars[2], &error);
14812 if (error)
14813 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014814
Bram Moolenaar758711c2005-02-02 23:11:38 +000014815 if (before == l->lv_len)
14816 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014817 else
14818 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014819 item = list_find(l, before);
14820 if (item == NULL)
14821 {
14822 EMSGN(_(e_listidx), before);
14823 l = NULL;
14824 }
14825 }
14826 if (l != NULL)
14827 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014828 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014829 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014830 }
14831 }
14832}
14833
14834/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014835 * "invert(expr)" function
14836 */
14837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014838f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014839{
14840 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14841}
14842
14843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 * "isdirectory()" function
14845 */
14846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014847f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014849 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850}
14851
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014852/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014853 * "islocked()" function
14854 */
14855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014856f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014857{
14858 lval_T lv;
14859 char_u *end;
14860 dictitem_T *di;
14861
14862 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014863 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14864 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014865 if (end != NULL && lv.ll_name != NULL)
14866 {
14867 if (*end != NUL)
14868 EMSG(_(e_trailing));
14869 else
14870 {
14871 if (lv.ll_tv == NULL)
14872 {
14873 if (check_changedtick(lv.ll_name))
14874 rettv->vval.v_number = 1; /* always locked */
14875 else
14876 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014877 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014878 if (di != NULL)
14879 {
14880 /* Consider a variable locked when:
14881 * 1. the variable itself is locked
14882 * 2. the value of the variable is locked.
14883 * 3. the List or Dict value is locked.
14884 */
14885 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14886 || tv_islocked(&di->di_tv));
14887 }
14888 }
14889 }
14890 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014891 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014892 else if (lv.ll_newkey != NULL)
14893 EMSG2(_(e_dictkey), lv.ll_newkey);
14894 else if (lv.ll_list != NULL)
14895 /* List item. */
14896 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14897 else
14898 /* Dictionary item. */
14899 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14900 }
14901 }
14902
14903 clear_lval(&lv);
14904}
14905
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014906#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14907/*
14908 * "isnan()" function
14909 */
14910 static void
14911f_isnan(typval_T *argvars, typval_T *rettv)
14912{
14913 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14914 && isnan(argvars[0].vval.v_float);
14915}
14916#endif
14917
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014918static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014919
14920/*
14921 * Turn a dict into a list:
14922 * "what" == 0: list of keys
14923 * "what" == 1: list of values
14924 * "what" == 2: list of items
14925 */
14926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014927dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014928{
Bram Moolenaar33570922005-01-25 22:26:29 +000014929 list_T *l2;
14930 dictitem_T *di;
14931 hashitem_T *hi;
14932 listitem_T *li;
14933 listitem_T *li2;
14934 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014935 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014936
Bram Moolenaar8c711452005-01-14 21:53:12 +000014937 if (argvars[0].v_type != VAR_DICT)
14938 {
14939 EMSG(_(e_dictreq));
14940 return;
14941 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014942 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014943 return;
14944
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014945 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014946 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014947
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014948 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014949 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014950 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014951 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014952 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014953 --todo;
14954 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014955
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014956 li = listitem_alloc();
14957 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014958 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014959 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014960
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014961 if (what == 0)
14962 {
14963 /* keys() */
14964 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014965 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014966 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14967 }
14968 else if (what == 1)
14969 {
14970 /* values() */
14971 copy_tv(&di->di_tv, &li->li_tv);
14972 }
14973 else
14974 {
14975 /* items() */
14976 l2 = list_alloc();
14977 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014978 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014979 li->li_tv.vval.v_list = l2;
14980 if (l2 == NULL)
14981 break;
14982 ++l2->lv_refcount;
14983
14984 li2 = listitem_alloc();
14985 if (li2 == NULL)
14986 break;
14987 list_append(l2, li2);
14988 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014989 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014990 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14991
14992 li2 = listitem_alloc();
14993 if (li2 == NULL)
14994 break;
14995 list_append(l2, li2);
14996 copy_tv(&di->di_tv, &li2->li_tv);
14997 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014998 }
14999 }
15000}
15001
15002/*
15003 * "items(dict)" function
15004 */
15005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015006f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015007{
15008 dict_list(argvars, rettv, 2);
15009}
15010
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015011#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015012/*
15013 * Get the job from the argument.
15014 * Returns NULL if the job is invalid.
15015 */
15016 static job_T *
15017get_job_arg(typval_T *tv)
15018{
15019 job_T *job;
15020
15021 if (tv->v_type != VAR_JOB)
15022 {
15023 EMSG2(_(e_invarg2), get_tv_string(tv));
15024 return NULL;
15025 }
15026 job = tv->vval.v_job;
15027
15028 if (job == NULL)
15029 EMSG(_("E916: not a valid job"));
15030 return job;
15031}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015032
Bram Moolenaar835dc632016-02-07 14:27:38 +010015033/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015034 * "job_getchannel()" function
15035 */
15036 static void
15037f_job_getchannel(typval_T *argvars, typval_T *rettv)
15038{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015039 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015040
Bram Moolenaar65edff82016-02-21 16:40:11 +010015041 if (job != NULL)
15042 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015043 rettv->v_type = VAR_CHANNEL;
15044 rettv->vval.v_channel = job->jv_channel;
15045 if (job->jv_channel != NULL)
15046 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015047 }
15048}
15049
15050/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015051 * "job_info()" function
15052 */
15053 static void
15054f_job_info(typval_T *argvars, typval_T *rettv)
15055{
15056 job_T *job = get_job_arg(&argvars[0]);
15057
15058 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15059 job_info(job, rettv->vval.v_dict);
15060}
15061
15062/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015063 * "job_setoptions()" function
15064 */
15065 static void
15066f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15067{
15068 job_T *job = get_job_arg(&argvars[0]);
15069 jobopt_T opt;
15070
15071 if (job == NULL)
15072 return;
15073 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015074 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15075 job_set_options(job, &opt);
15076 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015077}
15078
15079/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015080 * "job_start()" function
15081 */
15082 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015083f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015084{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015085 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015086 if (check_restricted() || check_secure())
15087 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015088 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015089}
15090
15091/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015092 * "job_status()" function
15093 */
15094 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015095f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015096{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015097 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015098
Bram Moolenaar65edff82016-02-21 16:40:11 +010015099 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015100 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015101 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015102 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015103 }
15104}
15105
15106/*
15107 * "job_stop()" function
15108 */
15109 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015110f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015111{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015112 job_T *job = get_job_arg(&argvars[0]);
15113
15114 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015115 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015116}
15117#endif
15118
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015120 * "join()" function
15121 */
15122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015123f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015124{
15125 garray_T ga;
15126 char_u *sep;
15127
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015128 if (argvars[0].v_type != VAR_LIST)
15129 {
15130 EMSG(_(e_listreq));
15131 return;
15132 }
15133 if (argvars[0].vval.v_list == NULL)
15134 return;
15135 if (argvars[1].v_type == VAR_UNKNOWN)
15136 sep = (char_u *)" ";
15137 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015138 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015139
15140 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015141
15142 if (sep != NULL)
15143 {
15144 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015145 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015146 ga_append(&ga, NUL);
15147 rettv->vval.v_string = (char_u *)ga.ga_data;
15148 }
15149 else
15150 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015151}
15152
15153/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015154 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015155 */
15156 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015157f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015158{
15159 js_read_T reader;
15160
15161 reader.js_buf = get_tv_string(&argvars[0]);
15162 reader.js_fill = NULL;
15163 reader.js_used = 0;
15164 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15165 EMSG(_(e_invarg));
15166}
15167
15168/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015169 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015170 */
15171 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015172f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015173{
15174 rettv->v_type = VAR_STRING;
15175 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15176}
15177
15178/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015179 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015180 */
15181 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015182f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015183{
15184 js_read_T reader;
15185
15186 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015187 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015188 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015189 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015190 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015191}
15192
15193/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015194 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015195 */
15196 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015197f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015198{
15199 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015200 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015201}
15202
15203/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015204 * "keys()" function
15205 */
15206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015207f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015208{
15209 dict_list(argvars, rettv, 0);
15210}
15211
15212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015213 * "last_buffer_nr()" function.
15214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015216f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217{
15218 int n = 0;
15219 buf_T *buf;
15220
15221 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15222 if (n < buf->b_fnum)
15223 n = buf->b_fnum;
15224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015225 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015226}
15227
15228/*
15229 * "len()" function
15230 */
15231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015232f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015233{
15234 switch (argvars[0].v_type)
15235 {
15236 case VAR_STRING:
15237 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015238 rettv->vval.v_number = (varnumber_T)STRLEN(
15239 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015240 break;
15241 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015242 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015243 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015244 case VAR_DICT:
15245 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15246 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015247 case VAR_UNKNOWN:
15248 case VAR_SPECIAL:
15249 case VAR_FLOAT:
15250 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015251 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015252 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015253 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015254 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015255 break;
15256 }
15257}
15258
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015259static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015260
15261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015262libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015263{
15264#ifdef FEAT_LIBCALL
15265 char_u *string_in;
15266 char_u **string_result;
15267 int nr_result;
15268#endif
15269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015270 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015271 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015272 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015273
15274 if (check_restricted() || check_secure())
15275 return;
15276
15277#ifdef FEAT_LIBCALL
15278 /* The first two args must be strings, otherwise its meaningless */
15279 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15280 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015281 string_in = NULL;
15282 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015283 string_in = argvars[2].vval.v_string;
15284 if (type == VAR_NUMBER)
15285 string_result = NULL;
15286 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015287 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015288 if (mch_libcall(argvars[0].vval.v_string,
15289 argvars[1].vval.v_string,
15290 string_in,
15291 argvars[2].vval.v_number,
15292 string_result,
15293 &nr_result) == OK
15294 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015295 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015296 }
15297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298}
15299
15300/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015301 * "libcall()" function
15302 */
15303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015304f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015305{
15306 libcall_common(argvars, rettv, VAR_STRING);
15307}
15308
15309/*
15310 * "libcallnr()" function
15311 */
15312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015313f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015314{
15315 libcall_common(argvars, rettv, VAR_NUMBER);
15316}
15317
15318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 * "line(string)" function
15320 */
15321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015322f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323{
15324 linenr_T lnum = 0;
15325 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015326 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015328 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329 if (fp != NULL)
15330 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015331 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332}
15333
15334/*
15335 * "line2byte(lnum)" function
15336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015338f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339{
15340#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015341 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342#else
15343 linenr_T lnum;
15344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015345 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015347 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015349 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15350 if (rettv->vval.v_number >= 0)
15351 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352#endif
15353}
15354
15355/*
15356 * "lispindent(lnum)" function
15357 */
15358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015359f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360{
15361#ifdef FEAT_LISP
15362 pos_T pos;
15363 linenr_T lnum;
15364
15365 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15368 {
15369 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015370 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 curwin->w_cursor = pos;
15372 }
15373 else
15374#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015375 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376}
15377
15378/*
15379 * "localtime()" function
15380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015382f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385}
15386
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015387static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388
15389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015390get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391{
15392 char_u *keys;
15393 char_u *which;
15394 char_u buf[NUMBUFLEN];
15395 char_u *keys_buf = NULL;
15396 char_u *rhs;
15397 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015398 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015399 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015400 mapblock_T *mp;
15401 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402
15403 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404 rettv->v_type = VAR_STRING;
15405 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015407 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 if (*keys == NUL)
15409 return;
15410
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015411 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015413 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015414 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015415 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015416 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015417 if (argvars[3].v_type != VAR_UNKNOWN)
15418 get_dict = get_tv_number(&argvars[3]);
15419 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 else
15422 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015423 if (which == NULL)
15424 return;
15425
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426 mode = get_map_mode(&which, 0);
15427
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015428 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015429 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015431
15432 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015434 /* Return a string. */
15435 if (rhs != NULL)
15436 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437
Bram Moolenaarbd743252010-10-20 21:23:33 +020015438 }
15439 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15440 {
15441 /* Return a dictionary. */
15442 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15443 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15444 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445
Bram Moolenaarbd743252010-10-20 21:23:33 +020015446 dict_add_nr_str(dict, "lhs", 0L, lhs);
15447 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15448 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15449 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15450 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15451 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15452 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015453 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015454 dict_add_nr_str(dict, "mode", 0L, mapmode);
15455
15456 vim_free(lhs);
15457 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 }
15459}
15460
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015461#ifdef FEAT_FLOAT
15462/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015463 * "log()" function
15464 */
15465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015466f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015467{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015468 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015469
15470 rettv->v_type = VAR_FLOAT;
15471 if (get_float_arg(argvars, &f) == OK)
15472 rettv->vval.v_float = log(f);
15473 else
15474 rettv->vval.v_float = 0.0;
15475}
15476
15477/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015478 * "log10()" function
15479 */
15480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015481f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015482{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015483 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015484
15485 rettv->v_type = VAR_FLOAT;
15486 if (get_float_arg(argvars, &f) == OK)
15487 rettv->vval.v_float = log10(f);
15488 else
15489 rettv->vval.v_float = 0.0;
15490}
15491#endif
15492
Bram Moolenaar1dced572012-04-05 16:54:08 +020015493#ifdef FEAT_LUA
15494/*
15495 * "luaeval()" function
15496 */
15497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015498f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015499{
15500 char_u *str;
15501 char_u buf[NUMBUFLEN];
15502
15503 str = get_tv_string_buf(&argvars[0], buf);
15504 do_luaeval(str, argvars + 1, rettv);
15505}
15506#endif
15507
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015509 * "map()" function
15510 */
15511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015512f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015513{
15514 filter_map(argvars, rettv, TRUE);
15515}
15516
15517/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015518 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 */
15520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015521f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015523 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524}
15525
15526/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015527 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528 */
15529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015530f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015532 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533}
15534
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015535static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536
15537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015538find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015540 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015541 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015542 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 char_u *pat;
15544 regmatch_T regmatch;
15545 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015546 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 char_u *save_cpo;
15548 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015549 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015550 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015551 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015552 list_T *l = NULL;
15553 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015554 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015555 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556
15557 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15558 save_cpo = p_cpo;
15559 p_cpo = (char_u *)"";
15560
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015561 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015562 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015563 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015564 /* type 3: return empty list when there are no matches.
15565 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015566 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015567 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015568 if (type == 4
15569 && (list_append_string(rettv->vval.v_list,
15570 (char_u *)"", 0) == FAIL
15571 || list_append_number(rettv->vval.v_list,
15572 (varnumber_T)-1) == FAIL
15573 || list_append_number(rettv->vval.v_list,
15574 (varnumber_T)-1) == FAIL
15575 || list_append_number(rettv->vval.v_list,
15576 (varnumber_T)-1) == FAIL))
15577 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015578 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015579 rettv->vval.v_list = NULL;
15580 goto theend;
15581 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015582 }
15583 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015585 rettv->v_type = VAR_STRING;
15586 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015589 if (argvars[0].v_type == VAR_LIST)
15590 {
15591 if ((l = argvars[0].vval.v_list) == NULL)
15592 goto theend;
15593 li = l->lv_first;
15594 }
15595 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015596 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015597 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015598 len = (long)STRLEN(str);
15599 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015601 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15602 if (pat == NULL)
15603 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015604
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015605 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015607 int error = FALSE;
15608
15609 start = get_tv_number_chk(&argvars[2], &error);
15610 if (error)
15611 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015612 if (l != NULL)
15613 {
15614 li = list_find(l, start);
15615 if (li == NULL)
15616 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015617 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015618 }
15619 else
15620 {
15621 if (start < 0)
15622 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015623 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015624 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015625 /* When "count" argument is there ignore matches before "start",
15626 * otherwise skip part of the string. Differs when pattern is "^"
15627 * or "\<". */
15628 if (argvars[3].v_type != VAR_UNKNOWN)
15629 startcol = start;
15630 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015631 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015632 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015633 len -= start;
15634 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015635 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015636
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015637 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015638 nth = get_tv_number_chk(&argvars[3], &error);
15639 if (error)
15640 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 }
15642
15643 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15644 if (regmatch.regprog != NULL)
15645 {
15646 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015647
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015648 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015649 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015650 if (l != NULL)
15651 {
15652 if (li == NULL)
15653 {
15654 match = FALSE;
15655 break;
15656 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015657 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015658 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015659 if (str == NULL)
15660 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015661 }
15662
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015663 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015664
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015665 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015666 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015667 if (l == NULL && !match)
15668 break;
15669
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015670 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015671 if (l != NULL)
15672 {
15673 li = li->li_next;
15674 ++idx;
15675 }
15676 else
15677 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015678#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015679 startcol = (colnr_T)(regmatch.startp[0]
15680 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015681#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015682 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015683#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015684 if (startcol > (colnr_T)len
15685 || str + startcol <= regmatch.startp[0])
15686 {
15687 match = FALSE;
15688 break;
15689 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015690 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015691 }
15692
15693 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015695 if (type == 4)
15696 {
15697 listitem_T *li1 = rettv->vval.v_list->lv_first;
15698 listitem_T *li2 = li1->li_next;
15699 listitem_T *li3 = li2->li_next;
15700 listitem_T *li4 = li3->li_next;
15701
15702 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15703 (int)(regmatch.endp[0] - regmatch.startp[0]));
15704 li3->li_tv.vval.v_number =
15705 (varnumber_T)(regmatch.startp[0] - expr);
15706 li4->li_tv.vval.v_number =
15707 (varnumber_T)(regmatch.endp[0] - expr);
15708 if (l != NULL)
15709 li2->li_tv.vval.v_number = (varnumber_T)idx;
15710 }
15711 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015712 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015713 int i;
15714
15715 /* return list with matched string and submatches */
15716 for (i = 0; i < NSUBEXP; ++i)
15717 {
15718 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015719 {
15720 if (list_append_string(rettv->vval.v_list,
15721 (char_u *)"", 0) == FAIL)
15722 break;
15723 }
15724 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015725 regmatch.startp[i],
15726 (int)(regmatch.endp[i] - regmatch.startp[i]))
15727 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015728 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015729 }
15730 }
15731 else if (type == 2)
15732 {
15733 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015734 if (l != NULL)
15735 copy_tv(&li->li_tv, rettv);
15736 else
15737 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015739 }
15740 else if (l != NULL)
15741 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 else
15743 {
15744 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015745 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 (varnumber_T)(regmatch.startp[0] - str);
15747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015748 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015750 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751 }
15752 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015753 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 }
15755
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015756 if (type == 4 && l == NULL)
15757 /* matchstrpos() without a list: drop the second item. */
15758 listitem_remove(rettv->vval.v_list,
15759 rettv->vval.v_list->lv_first->li_next);
15760
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015762 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763 p_cpo = save_cpo;
15764}
15765
15766/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015767 * "match()" function
15768 */
15769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015770f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015771{
15772 find_some_match(argvars, rettv, 1);
15773}
15774
15775/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015776 * "matchadd()" function
15777 */
15778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015779f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015780{
15781#ifdef FEAT_SEARCH_EXTRA
15782 char_u buf[NUMBUFLEN];
15783 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15784 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15785 int prio = 10; /* default priority */
15786 int id = -1;
15787 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015788 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015789
15790 rettv->vval.v_number = -1;
15791
15792 if (grp == NULL || pat == NULL)
15793 return;
15794 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015795 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015796 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015797 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015798 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015799 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015800 if (argvars[4].v_type != VAR_UNKNOWN)
15801 {
15802 if (argvars[4].v_type != VAR_DICT)
15803 {
15804 EMSG(_(e_dictreq));
15805 return;
15806 }
15807 if (dict_find(argvars[4].vval.v_dict,
15808 (char_u *)"conceal", -1) != NULL)
15809 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15810 (char_u *)"conceal", FALSE);
15811 }
15812 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015813 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015814 if (error == TRUE)
15815 return;
15816 if (id >= 1 && id <= 3)
15817 {
15818 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15819 return;
15820 }
15821
Bram Moolenaar6561d522015-07-21 15:48:27 +020015822 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15823 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015824#endif
15825}
15826
15827/*
15828 * "matchaddpos()" function
15829 */
15830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015831f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015832{
15833#ifdef FEAT_SEARCH_EXTRA
15834 char_u buf[NUMBUFLEN];
15835 char_u *group;
15836 int prio = 10;
15837 int id = -1;
15838 int error = FALSE;
15839 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015840 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015841
15842 rettv->vval.v_number = -1;
15843
15844 group = get_tv_string_buf_chk(&argvars[0], buf);
15845 if (group == NULL)
15846 return;
15847
15848 if (argvars[1].v_type != VAR_LIST)
15849 {
15850 EMSG2(_(e_listarg), "matchaddpos()");
15851 return;
15852 }
15853 l = argvars[1].vval.v_list;
15854 if (l == NULL)
15855 return;
15856
15857 if (argvars[2].v_type != VAR_UNKNOWN)
15858 {
15859 prio = get_tv_number_chk(&argvars[2], &error);
15860 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015861 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015862 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015863 if (argvars[4].v_type != VAR_UNKNOWN)
15864 {
15865 if (argvars[4].v_type != VAR_DICT)
15866 {
15867 EMSG(_(e_dictreq));
15868 return;
15869 }
15870 if (dict_find(argvars[4].vval.v_dict,
15871 (char_u *)"conceal", -1) != NULL)
15872 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15873 (char_u *)"conceal", FALSE);
15874 }
15875 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015876 }
15877 if (error == TRUE)
15878 return;
15879
15880 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15881 if (id == 1 || id == 2)
15882 {
15883 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15884 return;
15885 }
15886
Bram Moolenaar6561d522015-07-21 15:48:27 +020015887 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15888 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015889#endif
15890}
15891
15892/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015893 * "matcharg()" function
15894 */
15895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015896f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015897{
15898 if (rettv_list_alloc(rettv) == OK)
15899 {
15900#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015901 int id = get_tv_number(&argvars[0]);
15902 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015903
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015904 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015905 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015906 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15907 {
15908 list_append_string(rettv->vval.v_list,
15909 syn_id2name(m->hlg_id), -1);
15910 list_append_string(rettv->vval.v_list, m->pattern, -1);
15911 }
15912 else
15913 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015914 list_append_string(rettv->vval.v_list, NULL, -1);
15915 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015916 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015917 }
15918#endif
15919 }
15920}
15921
15922/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015923 * "matchdelete()" function
15924 */
15925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015926f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015927{
15928#ifdef FEAT_SEARCH_EXTRA
15929 rettv->vval.v_number = match_delete(curwin,
15930 (int)get_tv_number(&argvars[0]), TRUE);
15931#endif
15932}
15933
15934/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015935 * "matchend()" function
15936 */
15937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015938f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015939{
15940 find_some_match(argvars, rettv, 0);
15941}
15942
15943/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015944 * "matchlist()" function
15945 */
15946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015947f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015948{
15949 find_some_match(argvars, rettv, 3);
15950}
15951
15952/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015953 * "matchstr()" function
15954 */
15955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015956f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015957{
15958 find_some_match(argvars, rettv, 2);
15959}
15960
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015961/*
15962 * "matchstrpos()" function
15963 */
15964 static void
15965f_matchstrpos(typval_T *argvars, typval_T *rettv)
15966{
15967 find_some_match(argvars, rettv, 4);
15968}
15969
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015970static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015971
15972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015973max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015974{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015975 long n = 0;
15976 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015977 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015978
15979 if (argvars[0].v_type == VAR_LIST)
15980 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015981 list_T *l;
15982 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015983
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015984 l = argvars[0].vval.v_list;
15985 if (l != NULL)
15986 {
15987 li = l->lv_first;
15988 if (li != NULL)
15989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015990 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015991 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015992 {
15993 li = li->li_next;
15994 if (li == NULL)
15995 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015996 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015997 if (domax ? i > n : i < n)
15998 n = i;
15999 }
16000 }
16001 }
16002 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016003 else if (argvars[0].v_type == VAR_DICT)
16004 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016005 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016006 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016007 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016008 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016009
16010 d = argvars[0].vval.v_dict;
16011 if (d != NULL)
16012 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016013 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016014 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016016 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016017 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016018 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016019 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016020 if (first)
16021 {
16022 n = i;
16023 first = FALSE;
16024 }
16025 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016026 n = i;
16027 }
16028 }
16029 }
16030 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016031 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016032 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016033 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016034}
16035
16036/*
16037 * "max()" function
16038 */
16039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016040f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016041{
16042 max_min(argvars, rettv, TRUE);
16043}
16044
16045/*
16046 * "min()" function
16047 */
16048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016049f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016050{
16051 max_min(argvars, rettv, FALSE);
16052}
16053
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016054static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016055
16056/*
16057 * Create the directory in which "dir" is located, and higher levels when
16058 * needed.
16059 */
16060 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016061mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016062{
16063 char_u *p;
16064 char_u *updir;
16065 int r = FAIL;
16066
16067 /* Get end of directory name in "dir".
16068 * We're done when it's "/" or "c:/". */
16069 p = gettail_sep(dir);
16070 if (p <= get_past_head(dir))
16071 return OK;
16072
16073 /* If the directory exists we're done. Otherwise: create it.*/
16074 updir = vim_strnsave(dir, (int)(p - dir));
16075 if (updir == NULL)
16076 return FAIL;
16077 if (mch_isdir(updir))
16078 r = OK;
16079 else if (mkdir_recurse(updir, prot) == OK)
16080 r = vim_mkdir_emsg(updir, prot);
16081 vim_free(updir);
16082 return r;
16083}
16084
16085#ifdef vim_mkdir
16086/*
16087 * "mkdir()" function
16088 */
16089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016090f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016091{
16092 char_u *dir;
16093 char_u buf[NUMBUFLEN];
16094 int prot = 0755;
16095
16096 rettv->vval.v_number = FAIL;
16097 if (check_restricted() || check_secure())
16098 return;
16099
16100 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016101 if (*dir == NUL)
16102 rettv->vval.v_number = FAIL;
16103 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016104 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016105 if (*gettail(dir) == NUL)
16106 /* remove trailing slashes */
16107 *gettail_sep(dir) = NUL;
16108
16109 if (argvars[1].v_type != VAR_UNKNOWN)
16110 {
16111 if (argvars[2].v_type != VAR_UNKNOWN)
16112 prot = get_tv_number_chk(&argvars[2], NULL);
16113 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16114 mkdir_recurse(dir, prot);
16115 }
16116 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016117 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016118}
16119#endif
16120
Bram Moolenaar0d660222005-01-07 21:51:51 +000016121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 * "mode()" function
16123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016125f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016127 char_u buf[3];
16128
16129 buf[1] = NUL;
16130 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 if (VIsual_active)
16133 {
16134 if (VIsual_select)
16135 buf[0] = VIsual_mode + 's' - 'v';
16136 else
16137 buf[0] = VIsual_mode;
16138 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016139 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016140 || State == CONFIRM)
16141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016143 if (State == ASKMORE)
16144 buf[1] = 'm';
16145 else if (State == CONFIRM)
16146 buf[1] = '?';
16147 }
16148 else if (State == EXTERNCMD)
16149 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150 else if (State & INSERT)
16151 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016152#ifdef FEAT_VREPLACE
16153 if (State & VREPLACE_FLAG)
16154 {
16155 buf[0] = 'R';
16156 buf[1] = 'v';
16157 }
16158 else
16159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 if (State & REPLACE_FLAG)
16161 buf[0] = 'R';
16162 else
16163 buf[0] = 'i';
16164 }
16165 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016168 if (exmode_active)
16169 buf[1] = 'v';
16170 }
16171 else if (exmode_active)
16172 {
16173 buf[0] = 'c';
16174 buf[1] = 'e';
16175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016179 if (finish_op)
16180 buf[1] = 'o';
16181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016183 /* Clear out the minor mode when the argument is not a non-zero number or
16184 * non-empty string. */
16185 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016186 buf[1] = NUL;
16187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016188 rettv->vval.v_string = vim_strsave(buf);
16189 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190}
16191
Bram Moolenaar429fa852013-04-15 12:27:36 +020016192#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016193/*
16194 * "mzeval()" function
16195 */
16196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016197f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016198{
16199 char_u *str;
16200 char_u buf[NUMBUFLEN];
16201
16202 str = get_tv_string_buf(&argvars[0], buf);
16203 do_mzeval(str, rettv);
16204}
Bram Moolenaar75676462013-01-30 14:55:42 +010016205
16206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016207mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016208{
16209 typval_T argvars[3];
16210
16211 argvars[0].v_type = VAR_STRING;
16212 argvars[0].vval.v_string = name;
16213 copy_tv(args, &argvars[1]);
16214 argvars[2].v_type = VAR_UNKNOWN;
16215 f_call(argvars, rettv);
16216 clear_tv(&argvars[1]);
16217}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016218#endif
16219
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016221 * "nextnonblank()" function
16222 */
16223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016224f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016225{
16226 linenr_T lnum;
16227
16228 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016230 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016231 {
16232 lnum = 0;
16233 break;
16234 }
16235 if (*skipwhite(ml_get(lnum)) != NUL)
16236 break;
16237 }
16238 rettv->vval.v_number = lnum;
16239}
16240
16241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 * "nr2char()" function
16243 */
16244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016245f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246{
16247 char_u buf[NUMBUFLEN];
16248
16249#ifdef FEAT_MBYTE
16250 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016251 {
16252 int utf8 = 0;
16253
16254 if (argvars[1].v_type != VAR_UNKNOWN)
16255 utf8 = get_tv_number_chk(&argvars[1], NULL);
16256 if (utf8)
16257 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16258 else
16259 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 else
16262#endif
16263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016264 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265 buf[1] = NUL;
16266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016267 rettv->v_type = VAR_STRING;
16268 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016269}
16270
16271/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016272 * "or(expr, expr)" function
16273 */
16274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016275f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016276{
16277 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16278 | get_tv_number_chk(&argvars[1], NULL);
16279}
16280
16281/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016282 * "pathshorten()" function
16283 */
16284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016285f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016286{
16287 char_u *p;
16288
16289 rettv->v_type = VAR_STRING;
16290 p = get_tv_string_chk(&argvars[0]);
16291 if (p == NULL)
16292 rettv->vval.v_string = NULL;
16293 else
16294 {
16295 p = vim_strsave(p);
16296 rettv->vval.v_string = p;
16297 if (p != NULL)
16298 shorten_dir(p);
16299 }
16300}
16301
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016302#ifdef FEAT_PERL
16303/*
16304 * "perleval()" function
16305 */
16306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016307f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016308{
16309 char_u *str;
16310 char_u buf[NUMBUFLEN];
16311
16312 str = get_tv_string_buf(&argvars[0], buf);
16313 do_perleval(str, rettv);
16314}
16315#endif
16316
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016317#ifdef FEAT_FLOAT
16318/*
16319 * "pow()" function
16320 */
16321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016322f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016323{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016324 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016325
16326 rettv->v_type = VAR_FLOAT;
16327 if (get_float_arg(argvars, &fx) == OK
16328 && get_float_arg(&argvars[1], &fy) == OK)
16329 rettv->vval.v_float = pow(fx, fy);
16330 else
16331 rettv->vval.v_float = 0.0;
16332}
16333#endif
16334
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016335/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016336 * "prevnonblank()" function
16337 */
16338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016339f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016340{
16341 linenr_T lnum;
16342
16343 lnum = get_tv_lnum(argvars);
16344 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16345 lnum = 0;
16346 else
16347 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16348 --lnum;
16349 rettv->vval.v_number = lnum;
16350}
16351
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016352/* This dummy va_list is here because:
16353 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16354 * - locally in the function results in a "used before set" warning
16355 * - using va_start() to initialize it gives "function with fixed args" error */
16356static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016357
Bram Moolenaar8c711452005-01-14 21:53:12 +000016358/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016359 * "printf()" function
16360 */
16361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016362f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016363{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016364 char_u buf[NUMBUFLEN];
16365 int len;
16366 char_u *s;
16367 int saved_did_emsg = did_emsg;
16368 char *fmt;
16369
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016370 rettv->v_type = VAR_STRING;
16371 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016372
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016373 /* Get the required length, allocate the buffer and do it for real. */
16374 did_emsg = FALSE;
16375 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16376 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16377 if (!did_emsg)
16378 {
16379 s = alloc(len + 1);
16380 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016381 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016382 rettv->vval.v_string = s;
16383 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016384 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016385 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016386 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016387}
16388
16389/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016390 * "pumvisible()" function
16391 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016393f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016394{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016395#ifdef FEAT_INS_EXPAND
16396 if (pum_visible())
16397 rettv->vval.v_number = 1;
16398#endif
16399}
16400
Bram Moolenaardb913952012-06-29 12:54:53 +020016401#ifdef FEAT_PYTHON3
16402/*
16403 * "py3eval()" function
16404 */
16405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016406f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016407{
16408 char_u *str;
16409 char_u buf[NUMBUFLEN];
16410
16411 str = get_tv_string_buf(&argvars[0], buf);
16412 do_py3eval(str, rettv);
16413}
16414#endif
16415
16416#ifdef FEAT_PYTHON
16417/*
16418 * "pyeval()" function
16419 */
16420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016421f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016422{
16423 char_u *str;
16424 char_u buf[NUMBUFLEN];
16425
16426 str = get_tv_string_buf(&argvars[0], buf);
16427 do_pyeval(str, rettv);
16428}
16429#endif
16430
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016431/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016432 * "range()" function
16433 */
16434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016435f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016436{
16437 long start;
16438 long end;
16439 long stride = 1;
16440 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016443 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016444 if (argvars[1].v_type == VAR_UNKNOWN)
16445 {
16446 end = start - 1;
16447 start = 0;
16448 }
16449 else
16450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016451 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016452 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016453 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016454 }
16455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016456 if (error)
16457 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016458 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016459 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016460 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016461 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016462 else
16463 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016464 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016465 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016466 if (list_append_number(rettv->vval.v_list,
16467 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016468 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016469 }
16470}
16471
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016472/*
16473 * "readfile()" function
16474 */
16475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016476f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016477{
16478 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016479 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016480 char_u *fname;
16481 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016482 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16483 int io_size = sizeof(buf);
16484 int readlen; /* size of last fread() */
16485 char_u *prev = NULL; /* previously read bytes, if any */
16486 long prevlen = 0; /* length of data in prev */
16487 long prevsize = 0; /* size of prev buffer */
16488 long maxline = MAXLNUM;
16489 long cnt = 0;
16490 char_u *p; /* position in buf */
16491 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016492
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016493 if (argvars[1].v_type != VAR_UNKNOWN)
16494 {
16495 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16496 binary = TRUE;
16497 if (argvars[2].v_type != VAR_UNKNOWN)
16498 maxline = get_tv_number(&argvars[2]);
16499 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016500
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016501 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016502 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016503
16504 /* Always open the file in binary mode, library functions have a mind of
16505 * their own about CR-LF conversion. */
16506 fname = get_tv_string(&argvars[0]);
16507 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16508 {
16509 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16510 return;
16511 }
16512
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016513 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016514 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016515 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016516
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016517 /* This for loop processes what was read, but is also entered at end
16518 * of file so that either:
16519 * - an incomplete line gets written
16520 * - a "binary" file gets an empty line at the end if it ends in a
16521 * newline. */
16522 for (p = buf, start = buf;
16523 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16524 ++p)
16525 {
16526 if (*p == '\n' || readlen <= 0)
16527 {
16528 listitem_T *li;
16529 char_u *s = NULL;
16530 long_u len = p - start;
16531
16532 /* Finished a line. Remove CRs before NL. */
16533 if (readlen > 0 && !binary)
16534 {
16535 while (len > 0 && start[len - 1] == '\r')
16536 --len;
16537 /* removal may cross back to the "prev" string */
16538 if (len == 0)
16539 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16540 --prevlen;
16541 }
16542 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016543 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016544 else
16545 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016546 /* Change "prev" buffer to be the right size. This way
16547 * the bytes are only copied once, and very long lines are
16548 * allocated only once. */
16549 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016550 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016551 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016552 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016553 prev = NULL; /* the list will own the string */
16554 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016555 }
16556 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016557 if (s == NULL)
16558 {
16559 do_outofmem_msg((long_u) prevlen + len + 1);
16560 failed = TRUE;
16561 break;
16562 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016563
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016564 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016565 {
16566 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016567 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016568 break;
16569 }
16570 li->li_tv.v_type = VAR_STRING;
16571 li->li_tv.v_lock = 0;
16572 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016573 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016574
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016575 start = p + 1; /* step over newline */
16576 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016577 break;
16578 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016579 else if (*p == NUL)
16580 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016581#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016582 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16583 * when finding the BF and check the previous two bytes. */
16584 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016585 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016586 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16587 * + 1, these may be in the "prev" string. */
16588 char_u back1 = p >= buf + 1 ? p[-1]
16589 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16590 char_u back2 = p >= buf + 2 ? p[-2]
16591 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16592 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016593
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016594 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016595 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016596 char_u *dest = p - 2;
16597
16598 /* Usually a BOM is at the beginning of a file, and so at
16599 * the beginning of a line; then we can just step over it.
16600 */
16601 if (start == dest)
16602 start = p + 1;
16603 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016604 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016605 /* have to shuffle buf to close gap */
16606 int adjust_prevlen = 0;
16607
16608 if (dest < buf)
16609 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016610 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016611 dest = buf;
16612 }
16613 if (readlen > p - buf + 1)
16614 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16615 readlen -= 3 - adjust_prevlen;
16616 prevlen -= adjust_prevlen;
16617 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016618 }
16619 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016620 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016621#endif
16622 } /* for */
16623
16624 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16625 break;
16626 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016627 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016628 /* There's part of a line in buf, store it in "prev". */
16629 if (p - start + prevlen >= prevsize)
16630 {
16631 /* need bigger "prev" buffer */
16632 char_u *newprev;
16633
16634 /* A common use case is ordinary text files and "prev" gets a
16635 * fragment of a line, so the first allocation is made
16636 * small, to avoid repeatedly 'allocing' large and
16637 * 'reallocing' small. */
16638 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016639 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016640 else
16641 {
16642 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016643 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016644 prevsize = grow50pc > growmin ? grow50pc : growmin;
16645 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016646 newprev = prev == NULL ? alloc(prevsize)
16647 : vim_realloc(prev, prevsize);
16648 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016649 {
16650 do_outofmem_msg((long_u)prevsize);
16651 failed = TRUE;
16652 break;
16653 }
16654 prev = newprev;
16655 }
16656 /* Add the line part to end of "prev". */
16657 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016658 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016659 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016660 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016661
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016662 /*
16663 * For a negative line count use only the lines at the end of the file,
16664 * free the rest.
16665 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016666 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016667 while (cnt > -maxline)
16668 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016669 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016670 --cnt;
16671 }
16672
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016673 if (failed)
16674 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016675 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016676 /* readfile doc says an empty list is returned on error */
16677 rettv->vval.v_list = list_alloc();
16678 }
16679
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016680 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016681 fclose(fd);
16682}
16683
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016684#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016685static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016686
16687/*
16688 * Convert a List to proftime_T.
16689 * Return FAIL when there is something wrong.
16690 */
16691 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016692list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016693{
16694 long n1, n2;
16695 int error = FALSE;
16696
16697 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16698 || arg->vval.v_list->lv_len != 2)
16699 return FAIL;
16700 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16701 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16702# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016703 tm->HighPart = n1;
16704 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016705# else
16706 tm->tv_sec = n1;
16707 tm->tv_usec = n2;
16708# endif
16709 return error ? FAIL : OK;
16710}
16711#endif /* FEAT_RELTIME */
16712
16713/*
16714 * "reltime()" function
16715 */
16716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016717f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016718{
16719#ifdef FEAT_RELTIME
16720 proftime_T res;
16721 proftime_T start;
16722
16723 if (argvars[0].v_type == VAR_UNKNOWN)
16724 {
16725 /* No arguments: get current time. */
16726 profile_start(&res);
16727 }
16728 else if (argvars[1].v_type == VAR_UNKNOWN)
16729 {
16730 if (list2proftime(&argvars[0], &res) == FAIL)
16731 return;
16732 profile_end(&res);
16733 }
16734 else
16735 {
16736 /* Two arguments: compute the difference. */
16737 if (list2proftime(&argvars[0], &start) == FAIL
16738 || list2proftime(&argvars[1], &res) == FAIL)
16739 return;
16740 profile_sub(&res, &start);
16741 }
16742
16743 if (rettv_list_alloc(rettv) == OK)
16744 {
16745 long n1, n2;
16746
16747# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016748 n1 = res.HighPart;
16749 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016750# else
16751 n1 = res.tv_sec;
16752 n2 = res.tv_usec;
16753# endif
16754 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16755 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16756 }
16757#endif
16758}
16759
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016760#ifdef FEAT_FLOAT
16761/*
16762 * "reltimefloat()" function
16763 */
16764 static void
16765f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16766{
16767# ifdef FEAT_RELTIME
16768 proftime_T tm;
16769# endif
16770
16771 rettv->v_type = VAR_FLOAT;
16772 rettv->vval.v_float = 0;
16773# ifdef FEAT_RELTIME
16774 if (list2proftime(&argvars[0], &tm) == OK)
16775 rettv->vval.v_float = profile_float(&tm);
16776# endif
16777}
16778#endif
16779
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016780/*
16781 * "reltimestr()" function
16782 */
16783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016784f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016785{
16786#ifdef FEAT_RELTIME
16787 proftime_T tm;
16788#endif
16789
16790 rettv->v_type = VAR_STRING;
16791 rettv->vval.v_string = NULL;
16792#ifdef FEAT_RELTIME
16793 if (list2proftime(&argvars[0], &tm) == OK)
16794 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16795#endif
16796}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016797
Bram Moolenaar0d660222005-01-07 21:51:51 +000016798#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016799static void make_connection(void);
16800static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016801
16802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016803make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016804{
16805 if (X_DISPLAY == NULL
16806# ifdef FEAT_GUI
16807 && !gui.in_use
16808# endif
16809 )
16810 {
16811 x_force_connect = TRUE;
16812 setup_term_clip();
16813 x_force_connect = FALSE;
16814 }
16815}
16816
16817 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016818check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016819{
16820 make_connection();
16821 if (X_DISPLAY == NULL)
16822 {
16823 EMSG(_("E240: No connection to Vim server"));
16824 return FAIL;
16825 }
16826 return OK;
16827}
16828#endif
16829
16830#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016832remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016833{
16834 char_u *server_name;
16835 char_u *keys;
16836 char_u *r = NULL;
16837 char_u buf[NUMBUFLEN];
16838# ifdef WIN32
16839 HWND w;
16840# else
16841 Window w;
16842# endif
16843
16844 if (check_restricted() || check_secure())
16845 return;
16846
16847# ifdef FEAT_X11
16848 if (check_connection() == FAIL)
16849 return;
16850# endif
16851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016852 server_name = get_tv_string_chk(&argvars[0]);
16853 if (server_name == NULL)
16854 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016855 keys = get_tv_string_buf(&argvars[1], buf);
16856# ifdef WIN32
16857 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16858# else
16859 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16860 < 0)
16861# endif
16862 {
16863 if (r != NULL)
16864 EMSG(r); /* sending worked but evaluation failed */
16865 else
16866 EMSG2(_("E241: Unable to send to %s"), server_name);
16867 return;
16868 }
16869
16870 rettv->vval.v_string = r;
16871
16872 if (argvars[2].v_type != VAR_UNKNOWN)
16873 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016874 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016875 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016876 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016878 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016879 v.di_tv.v_type = VAR_STRING;
16880 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016881 idvar = get_tv_string_chk(&argvars[2]);
16882 if (idvar != NULL)
16883 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016884 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016885 }
16886}
16887#endif
16888
16889/*
16890 * "remote_expr()" function
16891 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016893f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016894{
16895 rettv->v_type = VAR_STRING;
16896 rettv->vval.v_string = NULL;
16897#ifdef FEAT_CLIENTSERVER
16898 remote_common(argvars, rettv, TRUE);
16899#endif
16900}
16901
16902/*
16903 * "remote_foreground()" function
16904 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016906f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016907{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016908#ifdef FEAT_CLIENTSERVER
16909# ifdef WIN32
16910 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016911 {
16912 char_u *server_name = get_tv_string_chk(&argvars[0]);
16913
16914 if (server_name != NULL)
16915 serverForeground(server_name);
16916 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917# else
16918 /* Send a foreground() expression to the server. */
16919 argvars[1].v_type = VAR_STRING;
16920 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16921 argvars[2].v_type = VAR_UNKNOWN;
16922 remote_common(argvars, rettv, TRUE);
16923 vim_free(argvars[1].vval.v_string);
16924# endif
16925#endif
16926}
16927
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016929f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930{
16931#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016932 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 char_u *s = NULL;
16934# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016935 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016937 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938
16939 if (check_restricted() || check_secure())
16940 {
16941 rettv->vval.v_number = -1;
16942 return;
16943 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016944 serverid = get_tv_string_chk(&argvars[0]);
16945 if (serverid == NULL)
16946 {
16947 rettv->vval.v_number = -1;
16948 return; /* type error; errmsg already given */
16949 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016950# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016951 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 if (n == 0)
16953 rettv->vval.v_number = -1;
16954 else
16955 {
16956 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16957 rettv->vval.v_number = (s != NULL);
16958 }
16959# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 if (check_connection() == FAIL)
16961 return;
16962
16963 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016964 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965# endif
16966
16967 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016969 char_u *retvar;
16970
Bram Moolenaar33570922005-01-25 22:26:29 +000016971 v.di_tv.v_type = VAR_STRING;
16972 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016973 retvar = get_tv_string_chk(&argvars[1]);
16974 if (retvar != NULL)
16975 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016976 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977 }
16978#else
16979 rettv->vval.v_number = -1;
16980#endif
16981}
16982
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016984f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985{
16986 char_u *r = NULL;
16987
16988#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016989 char_u *serverid = get_tv_string_chk(&argvars[0]);
16990
16991 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 {
16993# ifdef WIN32
16994 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016995 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016996
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016997 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998 if (n != 0)
16999 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17000 if (r == NULL)
17001# else
17002 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017003 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004# endif
17005 EMSG(_("E277: Unable to read a server reply"));
17006 }
17007#endif
17008 rettv->v_type = VAR_STRING;
17009 rettv->vval.v_string = r;
17010}
17011
17012/*
17013 * "remote_send()" function
17014 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017016f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017{
17018 rettv->v_type = VAR_STRING;
17019 rettv->vval.v_string = NULL;
17020#ifdef FEAT_CLIENTSERVER
17021 remote_common(argvars, rettv, FALSE);
17022#endif
17023}
17024
17025/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017026 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017027 */
17028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017029f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017030{
Bram Moolenaar33570922005-01-25 22:26:29 +000017031 list_T *l;
17032 listitem_T *item, *item2;
17033 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017034 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017035 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017036 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017037 dict_T *d;
17038 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017039 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017040
Bram Moolenaar8c711452005-01-14 21:53:12 +000017041 if (argvars[0].v_type == VAR_DICT)
17042 {
17043 if (argvars[2].v_type != VAR_UNKNOWN)
17044 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017045 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017046 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017048 key = get_tv_string_chk(&argvars[1]);
17049 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017051 di = dict_find(d, key, -1);
17052 if (di == NULL)
17053 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017054 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17055 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017056 {
17057 *rettv = di->di_tv;
17058 init_tv(&di->di_tv);
17059 dictitem_remove(d, di);
17060 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017062 }
17063 }
17064 else if (argvars[0].v_type != VAR_LIST)
17065 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017066 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017067 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017069 int error = FALSE;
17070
17071 idx = get_tv_number_chk(&argvars[1], &error);
17072 if (error)
17073 ; /* type error: do nothing, errmsg already given */
17074 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017075 EMSGN(_(e_listidx), idx);
17076 else
17077 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017078 if (argvars[2].v_type == VAR_UNKNOWN)
17079 {
17080 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017081 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017082 *rettv = item->li_tv;
17083 vim_free(item);
17084 }
17085 else
17086 {
17087 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017088 end = get_tv_number_chk(&argvars[2], &error);
17089 if (error)
17090 ; /* type error: do nothing */
17091 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017092 EMSGN(_(e_listidx), end);
17093 else
17094 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017095 int cnt = 0;
17096
17097 for (li = item; li != NULL; li = li->li_next)
17098 {
17099 ++cnt;
17100 if (li == item2)
17101 break;
17102 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017103 if (li == NULL) /* didn't find "item2" after "item" */
17104 EMSG(_(e_invrange));
17105 else
17106 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017107 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017108 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017109 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017110 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017111 l->lv_first = item;
17112 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017113 item->li_prev = NULL;
17114 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017115 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017116 }
17117 }
17118 }
17119 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017120 }
17121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122}
17123
17124/*
17125 * "rename({from}, {to})" function
17126 */
17127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017128f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017129{
17130 char_u buf[NUMBUFLEN];
17131
17132 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017135 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17136 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137}
17138
17139/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017140 * "repeat()" function
17141 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017143f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017144{
17145 char_u *p;
17146 int n;
17147 int slen;
17148 int len;
17149 char_u *r;
17150 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017151
17152 n = get_tv_number(&argvars[1]);
17153 if (argvars[0].v_type == VAR_LIST)
17154 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017155 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017156 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017157 if (list_extend(rettv->vval.v_list,
17158 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017159 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017160 }
17161 else
17162 {
17163 p = get_tv_string(&argvars[0]);
17164 rettv->v_type = VAR_STRING;
17165 rettv->vval.v_string = NULL;
17166
17167 slen = (int)STRLEN(p);
17168 len = slen * n;
17169 if (len <= 0)
17170 return;
17171
17172 r = alloc(len + 1);
17173 if (r != NULL)
17174 {
17175 for (i = 0; i < n; i++)
17176 mch_memmove(r + i * slen, p, (size_t)slen);
17177 r[len] = NUL;
17178 }
17179
17180 rettv->vval.v_string = r;
17181 }
17182}
17183
17184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 * "resolve()" function
17186 */
17187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017188f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189{
17190 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017191#ifdef HAVE_READLINK
17192 char_u *buf = NULL;
17193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017195 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196#ifdef FEAT_SHORTCUT
17197 {
17198 char_u *v = NULL;
17199
17200 v = mch_resolve_shortcut(p);
17201 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017202 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017204 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 }
17206#else
17207# ifdef HAVE_READLINK
17208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 char_u *cpy;
17210 int len;
17211 char_u *remain = NULL;
17212 char_u *q;
17213 int is_relative_to_current = FALSE;
17214 int has_trailing_pathsep = FALSE;
17215 int limit = 100;
17216
17217 p = vim_strsave(p);
17218
17219 if (p[0] == '.' && (vim_ispathsep(p[1])
17220 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17221 is_relative_to_current = TRUE;
17222
17223 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017224 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017227 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229
17230 q = getnextcomp(p);
17231 if (*q != NUL)
17232 {
17233 /* Separate the first path component in "p", and keep the
17234 * remainder (beginning with the path separator). */
17235 remain = vim_strsave(q - 1);
17236 q[-1] = NUL;
17237 }
17238
Bram Moolenaard9462e32011-04-11 21:35:11 +020017239 buf = alloc(MAXPATHL + 1);
17240 if (buf == NULL)
17241 goto fail;
17242
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 for (;;)
17244 {
17245 for (;;)
17246 {
17247 len = readlink((char *)p, (char *)buf, MAXPATHL);
17248 if (len <= 0)
17249 break;
17250 buf[len] = NUL;
17251
17252 if (limit-- == 0)
17253 {
17254 vim_free(p);
17255 vim_free(remain);
17256 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017257 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 goto fail;
17259 }
17260
17261 /* Ensure that the result will have a trailing path separator
17262 * if the argument has one. */
17263 if (remain == NULL && has_trailing_pathsep)
17264 add_pathsep(buf);
17265
17266 /* Separate the first path component in the link value and
17267 * concatenate the remainders. */
17268 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17269 if (*q != NUL)
17270 {
17271 if (remain == NULL)
17272 remain = vim_strsave(q - 1);
17273 else
17274 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017275 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 if (cpy != NULL)
17277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278 vim_free(remain);
17279 remain = cpy;
17280 }
17281 }
17282 q[-1] = NUL;
17283 }
17284
17285 q = gettail(p);
17286 if (q > p && *q == NUL)
17287 {
17288 /* Ignore trailing path separator. */
17289 q[-1] = NUL;
17290 q = gettail(p);
17291 }
17292 if (q > p && !mch_isFullName(buf))
17293 {
17294 /* symlink is relative to directory of argument */
17295 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17296 if (cpy != NULL)
17297 {
17298 STRCPY(cpy, p);
17299 STRCPY(gettail(cpy), buf);
17300 vim_free(p);
17301 p = cpy;
17302 }
17303 }
17304 else
17305 {
17306 vim_free(p);
17307 p = vim_strsave(buf);
17308 }
17309 }
17310
17311 if (remain == NULL)
17312 break;
17313
17314 /* Append the first path component of "remain" to "p". */
17315 q = getnextcomp(remain + 1);
17316 len = q - remain - (*q != NUL);
17317 cpy = vim_strnsave(p, STRLEN(p) + len);
17318 if (cpy != NULL)
17319 {
17320 STRNCAT(cpy, remain, len);
17321 vim_free(p);
17322 p = cpy;
17323 }
17324 /* Shorten "remain". */
17325 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017326 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 else
17328 {
17329 vim_free(remain);
17330 remain = NULL;
17331 }
17332 }
17333
17334 /* If the result is a relative path name, make it explicitly relative to
17335 * the current directory if and only if the argument had this form. */
17336 if (!vim_ispathsep(*p))
17337 {
17338 if (is_relative_to_current
17339 && *p != NUL
17340 && !(p[0] == '.'
17341 && (p[1] == NUL
17342 || vim_ispathsep(p[1])
17343 || (p[1] == '.'
17344 && (p[2] == NUL
17345 || vim_ispathsep(p[2]))))))
17346 {
17347 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017348 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 if (cpy != NULL)
17350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 vim_free(p);
17352 p = cpy;
17353 }
17354 }
17355 else if (!is_relative_to_current)
17356 {
17357 /* Strip leading "./". */
17358 q = p;
17359 while (q[0] == '.' && vim_ispathsep(q[1]))
17360 q += 2;
17361 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017362 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 }
17364 }
17365
17366 /* Ensure that the result will have no trailing path separator
17367 * if the argument had none. But keep "/" or "//". */
17368 if (!has_trailing_pathsep)
17369 {
17370 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017371 if (after_pathsep(p, q))
17372 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 }
17374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 }
17377# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017378 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379# endif
17380#endif
17381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017382 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383
17384#ifdef HAVE_READLINK
17385fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017386 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389}
17390
17391/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017392 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 */
17394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017395f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396{
Bram Moolenaar33570922005-01-25 22:26:29 +000017397 list_T *l;
17398 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399
Bram Moolenaar0d660222005-01-07 21:51:51 +000017400 if (argvars[0].v_type != VAR_LIST)
17401 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017402 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017403 && !tv_check_lock(l->lv_lock,
17404 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017405 {
17406 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017407 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017408 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017409 while (li != NULL)
17410 {
17411 ni = li->li_prev;
17412 list_append(l, li);
17413 li = ni;
17414 }
17415 rettv->vval.v_list = l;
17416 rettv->v_type = VAR_LIST;
17417 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017418 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420}
17421
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017422#define SP_NOMOVE 0x01 /* don't move cursor */
17423#define SP_REPEAT 0x02 /* repeat to find outer pair */
17424#define SP_RETCOUNT 0x04 /* return matchcount */
17425#define SP_SETPCMARK 0x08 /* set previous context mark */
17426#define SP_START 0x10 /* accept match at start position */
17427#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17428#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017429#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017430
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017431static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017432
17433/*
17434 * Get flags for a search function.
17435 * Possibly sets "p_ws".
17436 * Returns BACKWARD, FORWARD or zero (for an error).
17437 */
17438 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017439get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017440{
17441 int dir = FORWARD;
17442 char_u *flags;
17443 char_u nbuf[NUMBUFLEN];
17444 int mask;
17445
17446 if (varp->v_type != VAR_UNKNOWN)
17447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017448 flags = get_tv_string_buf_chk(varp, nbuf);
17449 if (flags == NULL)
17450 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017451 while (*flags != NUL)
17452 {
17453 switch (*flags)
17454 {
17455 case 'b': dir = BACKWARD; break;
17456 case 'w': p_ws = TRUE; break;
17457 case 'W': p_ws = FALSE; break;
17458 default: mask = 0;
17459 if (flagsp != NULL)
17460 switch (*flags)
17461 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017462 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017463 case 'e': mask = SP_END; break;
17464 case 'm': mask = SP_RETCOUNT; break;
17465 case 'n': mask = SP_NOMOVE; break;
17466 case 'p': mask = SP_SUBPAT; break;
17467 case 'r': mask = SP_REPEAT; break;
17468 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017469 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017470 }
17471 if (mask == 0)
17472 {
17473 EMSG2(_(e_invarg2), flags);
17474 dir = 0;
17475 }
17476 else
17477 *flagsp |= mask;
17478 }
17479 if (dir == 0)
17480 break;
17481 ++flags;
17482 }
17483 }
17484 return dir;
17485}
17486
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017488 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017490 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017491search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017493 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 char_u *pat;
17495 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017496 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 int save_p_ws = p_ws;
17498 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017499 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017500 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017501 proftime_T tm;
17502#ifdef FEAT_RELTIME
17503 long time_limit = 0;
17504#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017505 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017506 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017508 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017509 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017510 if (dir == 0)
17511 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017512 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017513 if (flags & SP_START)
17514 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017515 if (flags & SP_END)
17516 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017517 if (flags & SP_COLUMN)
17518 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017519
Bram Moolenaar76929292008-01-06 19:07:36 +000017520 /* Optional arguments: line number to stop searching and timeout. */
17521 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017522 {
17523 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17524 if (lnum_stop < 0)
17525 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017526#ifdef FEAT_RELTIME
17527 if (argvars[3].v_type != VAR_UNKNOWN)
17528 {
17529 time_limit = get_tv_number_chk(&argvars[3], NULL);
17530 if (time_limit < 0)
17531 goto theend;
17532 }
17533#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017534 }
17535
Bram Moolenaar76929292008-01-06 19:07:36 +000017536#ifdef FEAT_RELTIME
17537 /* Set the time limit, if there is one. */
17538 profile_setlimit(time_limit, &tm);
17539#endif
17540
Bram Moolenaar231334e2005-07-25 20:46:57 +000017541 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017542 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017543 * Check to make sure only those flags are set.
17544 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17545 * flags cannot be set. Check for that condition also.
17546 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017547 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017548 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017550 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017551 goto theend;
17552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017554 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017555 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017556 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017557 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017559 if (flags & SP_SUBPAT)
17560 retval = subpatnum;
17561 else
17562 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017563 if (flags & SP_SETPCMARK)
17564 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017566 if (match_pos != NULL)
17567 {
17568 /* Store the match cursor position */
17569 match_pos->lnum = pos.lnum;
17570 match_pos->col = pos.col + 1;
17571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 /* "/$" will put the cursor after the end of the line, may need to
17573 * correct that here */
17574 check_cursor();
17575 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017576
17577 /* If 'n' flag is used: restore cursor position. */
17578 if (flags & SP_NOMOVE)
17579 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017580 else
17581 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017582theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017584
17585 return retval;
17586}
17587
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017588#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017589
17590/*
17591 * round() is not in C90, use ceil() or floor() instead.
17592 */
17593 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017594vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017595{
17596 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17597}
17598
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017599/*
17600 * "round({float})" function
17601 */
17602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017603f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017604{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017605 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017606
17607 rettv->v_type = VAR_FLOAT;
17608 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017609 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017610 else
17611 rettv->vval.v_float = 0.0;
17612}
17613#endif
17614
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017615/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017616 * "screenattr()" function
17617 */
17618 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017619f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017620{
17621 int row;
17622 int col;
17623 int c;
17624
17625 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17626 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17627 if (row < 0 || row >= screen_Rows
17628 || col < 0 || col >= screen_Columns)
17629 c = -1;
17630 else
17631 c = ScreenAttrs[LineOffset[row] + col];
17632 rettv->vval.v_number = c;
17633}
17634
17635/*
17636 * "screenchar()" function
17637 */
17638 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017639f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017640{
17641 int row;
17642 int col;
17643 int off;
17644 int c;
17645
17646 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17647 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17648 if (row < 0 || row >= screen_Rows
17649 || col < 0 || col >= screen_Columns)
17650 c = -1;
17651 else
17652 {
17653 off = LineOffset[row] + col;
17654#ifdef FEAT_MBYTE
17655 if (enc_utf8 && ScreenLinesUC[off] != 0)
17656 c = ScreenLinesUC[off];
17657 else
17658#endif
17659 c = ScreenLines[off];
17660 }
17661 rettv->vval.v_number = c;
17662}
17663
17664/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017665 * "screencol()" function
17666 *
17667 * First column is 1 to be consistent with virtcol().
17668 */
17669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017670f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017671{
17672 rettv->vval.v_number = screen_screencol() + 1;
17673}
17674
17675/*
17676 * "screenrow()" function
17677 */
17678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017679f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017680{
17681 rettv->vval.v_number = screen_screenrow() + 1;
17682}
17683
17684/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017685 * "search()" function
17686 */
17687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017688f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017689{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017690 int flags = 0;
17691
17692 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693}
17694
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017696 * "searchdecl()" function
17697 */
17698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017699f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017700{
17701 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017702 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017703 int error = FALSE;
17704 char_u *name;
17705
17706 rettv->vval.v_number = 1; /* default: FAIL */
17707
17708 name = get_tv_string_chk(&argvars[0]);
17709 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017710 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017711 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017712 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17713 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17714 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017715 if (!error && name != NULL)
17716 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017717 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017718}
17719
17720/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017721 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017724searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725{
17726 char_u *spat, *mpat, *epat;
17727 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 int dir;
17730 int flags = 0;
17731 char_u nbuf1[NUMBUFLEN];
17732 char_u nbuf2[NUMBUFLEN];
17733 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017734 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017735 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017736 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017739 spat = get_tv_string_chk(&argvars[0]);
17740 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17741 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17742 if (spat == NULL || mpat == NULL || epat == NULL)
17743 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745 /* Handle the optional fourth argument: flags */
17746 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017747 if (dir == 0)
17748 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017749
17750 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017751 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17752 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017753 if ((flags & (SP_END | SP_SUBPAT)) != 0
17754 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017755 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017756 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017757 goto theend;
17758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017760 /* Using 'r' implies 'W', otherwise it doesn't work. */
17761 if (flags & SP_REPEAT)
17762 p_ws = FALSE;
17763
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017764 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017765 if (argvars[3].v_type == VAR_UNKNOWN
17766 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 skip = (char_u *)"";
17768 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017770 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017771 if (argvars[5].v_type != VAR_UNKNOWN)
17772 {
17773 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17774 if (lnum_stop < 0)
17775 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017776#ifdef FEAT_RELTIME
17777 if (argvars[6].v_type != VAR_UNKNOWN)
17778 {
17779 time_limit = get_tv_number_chk(&argvars[6], NULL);
17780 if (time_limit < 0)
17781 goto theend;
17782 }
17783#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017784 }
17785 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017786 if (skip == NULL)
17787 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017789 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017790 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017791
17792theend:
17793 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017794
17795 return retval;
17796}
17797
17798/*
17799 * "searchpair()" function
17800 */
17801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017802f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017803{
17804 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17805}
17806
17807/*
17808 * "searchpairpos()" function
17809 */
17810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017811f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017812{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017813 pos_T match_pos;
17814 int lnum = 0;
17815 int col = 0;
17816
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017817 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017818 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017819
17820 if (searchpair_cmn(argvars, &match_pos) > 0)
17821 {
17822 lnum = match_pos.lnum;
17823 col = match_pos.col;
17824 }
17825
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017826 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17827 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017828}
17829
17830/*
17831 * Search for a start/middle/end thing.
17832 * Used by searchpair(), see its documentation for the details.
17833 * Returns 0 or -1 for no match,
17834 */
17835 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017836do_searchpair(
17837 char_u *spat, /* start pattern */
17838 char_u *mpat, /* middle pattern */
17839 char_u *epat, /* end pattern */
17840 int dir, /* BACKWARD or FORWARD */
17841 char_u *skip, /* skip expression */
17842 int flags, /* SP_SETPCMARK and other SP_ values */
17843 pos_T *match_pos,
17844 linenr_T lnum_stop, /* stop at this line if not zero */
17845 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017846{
17847 char_u *save_cpo;
17848 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17849 long retval = 0;
17850 pos_T pos;
17851 pos_T firstpos;
17852 pos_T foundpos;
17853 pos_T save_cursor;
17854 pos_T save_pos;
17855 int n;
17856 int r;
17857 int nest = 1;
17858 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017859 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017860 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017861
17862 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17863 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017864 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017865
Bram Moolenaar76929292008-01-06 19:07:36 +000017866#ifdef FEAT_RELTIME
17867 /* Set the time limit, if there is one. */
17868 profile_setlimit(time_limit, &tm);
17869#endif
17870
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017871 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17872 * start/middle/end (pat3, for the top pair). */
17873 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17874 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17875 if (pat2 == NULL || pat3 == NULL)
17876 goto theend;
17877 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17878 if (*mpat == NUL)
17879 STRCPY(pat3, pat2);
17880 else
17881 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17882 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017883 if (flags & SP_START)
17884 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017885
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 save_cursor = curwin->w_cursor;
17887 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017888 clearpos(&firstpos);
17889 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 pat = pat3;
17891 for (;;)
17892 {
17893 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017894 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17896 /* didn't find it or found the first match again: FAIL */
17897 break;
17898
17899 if (firstpos.lnum == 0)
17900 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017901 if (equalpos(pos, foundpos))
17902 {
17903 /* Found the same position again. Can happen with a pattern that
17904 * has "\zs" at the end and searching backwards. Advance one
17905 * character and try again. */
17906 if (dir == BACKWARD)
17907 decl(&pos);
17908 else
17909 incl(&pos);
17910 }
17911 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017913 /* clear the start flag to avoid getting stuck here */
17914 options &= ~SEARCH_START;
17915
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 /* If the skip pattern matches, ignore this match. */
17917 if (*skip != NUL)
17918 {
17919 save_pos = curwin->w_cursor;
17920 curwin->w_cursor = pos;
17921 r = eval_to_bool(skip, &err, NULL, FALSE);
17922 curwin->w_cursor = save_pos;
17923 if (err)
17924 {
17925 /* Evaluating {skip} caused an error, break here. */
17926 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017927 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 break;
17929 }
17930 if (r)
17931 continue;
17932 }
17933
17934 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17935 {
17936 /* Found end when searching backwards or start when searching
17937 * forward: nested pair. */
17938 ++nest;
17939 pat = pat2; /* nested, don't search for middle */
17940 }
17941 else
17942 {
17943 /* Found end when searching forward or start when searching
17944 * backward: end of (nested) pair; or found middle in outer pair. */
17945 if (--nest == 1)
17946 pat = pat3; /* outer level, search for middle */
17947 }
17948
17949 if (nest == 0)
17950 {
17951 /* Found the match: return matchcount or line number. */
17952 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017953 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017955 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017956 if (flags & SP_SETPCMARK)
17957 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 curwin->w_cursor = pos;
17959 if (!(flags & SP_REPEAT))
17960 break;
17961 nest = 1; /* search for next unmatched */
17962 }
17963 }
17964
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017965 if (match_pos != NULL)
17966 {
17967 /* Store the match cursor position */
17968 match_pos->lnum = curwin->w_cursor.lnum;
17969 match_pos->col = curwin->w_cursor.col + 1;
17970 }
17971
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017973 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 curwin->w_cursor = save_cursor;
17975
17976theend:
17977 vim_free(pat2);
17978 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017979 if (p_cpo == empty_option)
17980 p_cpo = save_cpo;
17981 else
17982 /* Darn, evaluating the {skip} expression changed the value. */
17983 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017984
17985 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986}
17987
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017988/*
17989 * "searchpos()" function
17990 */
17991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017992f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017993{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017994 pos_T match_pos;
17995 int lnum = 0;
17996 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017997 int n;
17998 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017999
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018000 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018001 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018002
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018003 n = search_cmn(argvars, &match_pos, &flags);
18004 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018005 {
18006 lnum = match_pos.lnum;
18007 col = match_pos.col;
18008 }
18009
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018010 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18011 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018012 if (flags & SP_SUBPAT)
18013 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018014}
18015
Bram Moolenaar0d660222005-01-07 21:51:51 +000018016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018017f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018019#ifdef FEAT_CLIENTSERVER
18020 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018021 char_u *server = get_tv_string_chk(&argvars[0]);
18022 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023
Bram Moolenaar0d660222005-01-07 21:51:51 +000018024 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018025 if (server == NULL || reply == NULL)
18026 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018027 if (check_restricted() || check_secure())
18028 return;
18029# ifdef FEAT_X11
18030 if (check_connection() == FAIL)
18031 return;
18032# endif
18033
18034 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018036 EMSG(_("E258: Unable to send to client"));
18037 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018039 rettv->vval.v_number = 0;
18040#else
18041 rettv->vval.v_number = -1;
18042#endif
18043}
18044
Bram Moolenaar0d660222005-01-07 21:51:51 +000018045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018046f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018047{
18048 char_u *r = NULL;
18049
18050#ifdef FEAT_CLIENTSERVER
18051# ifdef WIN32
18052 r = serverGetVimNames();
18053# else
18054 make_connection();
18055 if (X_DISPLAY != NULL)
18056 r = serverGetVimNames(X_DISPLAY);
18057# endif
18058#endif
18059 rettv->v_type = VAR_STRING;
18060 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061}
18062
18063/*
18064 * "setbufvar()" function
18065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018067f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068{
18069 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018072 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 char_u nbuf[NUMBUFLEN];
18074
18075 if (check_restricted() || check_secure())
18076 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018077 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18078 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018079 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 varp = &argvars[2];
18081
18082 if (buf != NULL && varname != NULL && varp != NULL)
18083 {
18084 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086
18087 if (*varname == '&')
18088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018089 long numval;
18090 char_u *strval;
18091 int error = FALSE;
18092
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018094 numval = get_tv_number_chk(varp, &error);
18095 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018096 if (!error && strval != NULL)
18097 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 }
18099 else
18100 {
18101 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18102 if (bufvarname != NULL)
18103 {
18104 STRCPY(bufvarname, "b:");
18105 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018106 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 vim_free(bufvarname);
18108 }
18109 }
18110
18111 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114}
18115
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018117f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018118{
18119 dict_T *d;
18120 dictitem_T *di;
18121 char_u *csearch;
18122
18123 if (argvars[0].v_type != VAR_DICT)
18124 {
18125 EMSG(_(e_dictreq));
18126 return;
18127 }
18128
18129 if ((d = argvars[0].vval.v_dict) != NULL)
18130 {
18131 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18132 if (csearch != NULL)
18133 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018134#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018135 if (enc_utf8)
18136 {
18137 int pcc[MAX_MCO];
18138 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018139
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018140 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18141 }
18142 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018143#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018144 set_last_csearch(PTR2CHAR(csearch),
18145 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018146 }
18147
18148 di = dict_find(d, (char_u *)"forward", -1);
18149 if (di != NULL)
18150 set_csearch_direction(get_tv_number(&di->di_tv)
18151 ? FORWARD : BACKWARD);
18152
18153 di = dict_find(d, (char_u *)"until", -1);
18154 if (di != NULL)
18155 set_csearch_until(!!get_tv_number(&di->di_tv));
18156 }
18157}
18158
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159/*
18160 * "setcmdpos()" function
18161 */
18162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018163f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018165 int pos = (int)get_tv_number(&argvars[0]) - 1;
18166
18167 if (pos >= 0)
18168 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169}
18170
18171/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018172 * "setfperm({fname}, {mode})" function
18173 */
18174 static void
18175f_setfperm(typval_T *argvars, typval_T *rettv)
18176{
18177 char_u *fname;
18178 char_u modebuf[NUMBUFLEN];
18179 char_u *mode_str;
18180 int i;
18181 int mask;
18182 int mode = 0;
18183
18184 rettv->vval.v_number = 0;
18185 fname = get_tv_string_chk(&argvars[0]);
18186 if (fname == NULL)
18187 return;
18188 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18189 if (mode_str == NULL)
18190 return;
18191 if (STRLEN(mode_str) != 9)
18192 {
18193 EMSG2(_(e_invarg2), mode_str);
18194 return;
18195 }
18196
18197 mask = 1;
18198 for (i = 8; i >= 0; --i)
18199 {
18200 if (mode_str[i] != '-')
18201 mode |= mask;
18202 mask = mask << 1;
18203 }
18204 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18205}
18206
18207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 * "setline()" function
18209 */
18210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018211f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212{
18213 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018214 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018215 list_T *l = NULL;
18216 listitem_T *li = NULL;
18217 long added = 0;
18218 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018220 lnum = get_tv_lnum(&argvars[0]);
18221 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018223 l = argvars[1].vval.v_list;
18224 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018226 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018227 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018228
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018229 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018230 for (;;)
18231 {
18232 if (l != NULL)
18233 {
18234 /* list argument, get next string */
18235 if (li == NULL)
18236 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018237 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018238 li = li->li_next;
18239 }
18240
18241 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018242 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018243 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018244
18245 /* When coming here from Insert mode, sync undo, so that this can be
18246 * undone separately from what was previously inserted. */
18247 if (u_sync_once == 2)
18248 {
18249 u_sync_once = 1; /* notify that u_sync() was called */
18250 u_sync(TRUE);
18251 }
18252
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018253 if (lnum <= curbuf->b_ml.ml_line_count)
18254 {
18255 /* existing line, replace it */
18256 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18257 {
18258 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018259 if (lnum == curwin->w_cursor.lnum)
18260 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018261 rettv->vval.v_number = 0; /* OK */
18262 }
18263 }
18264 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18265 {
18266 /* lnum is one past the last line, append the line */
18267 ++added;
18268 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18269 rettv->vval.v_number = 0; /* OK */
18270 }
18271
18272 if (l == NULL) /* only one string argument */
18273 break;
18274 ++lnum;
18275 }
18276
18277 if (added > 0)
18278 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279}
18280
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018281static 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 +000018282
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018284 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018285 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018287set_qf_ll_list(
18288 win_T *wp UNUSED,
18289 typval_T *list_arg UNUSED,
18290 typval_T *action_arg UNUSED,
18291 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018292{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018293#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018294 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018295 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018296 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018297#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018298
Bram Moolenaar2641f772005-03-25 21:58:17 +000018299 rettv->vval.v_number = -1;
18300
18301#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018302 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018303 EMSG(_(e_listreq));
18304 else
18305 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018306 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018307
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018308 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018309 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018310 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018311 if (act == NULL)
18312 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018313 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018314 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018315 else
18316 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018317 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018318 else if (action_arg->v_type == VAR_UNKNOWN)
18319 action = ' ';
18320 else
18321 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018322
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018323 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018324 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018325 rettv->vval.v_number = 0;
18326 }
18327#endif
18328}
18329
18330/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018331 * "setloclist()" function
18332 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018334f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018335{
18336 win_T *win;
18337
18338 rettv->vval.v_number = -1;
18339
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018340 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018341 if (win != NULL)
18342 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18343}
18344
18345/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018346 * "setmatches()" function
18347 */
18348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018349f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018350{
18351#ifdef FEAT_SEARCH_EXTRA
18352 list_T *l;
18353 listitem_T *li;
18354 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018355 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018356
18357 rettv->vval.v_number = -1;
18358 if (argvars[0].v_type != VAR_LIST)
18359 {
18360 EMSG(_(e_listreq));
18361 return;
18362 }
18363 if ((l = argvars[0].vval.v_list) != NULL)
18364 {
18365
18366 /* To some extent make sure that we are dealing with a list from
18367 * "getmatches()". */
18368 li = l->lv_first;
18369 while (li != NULL)
18370 {
18371 if (li->li_tv.v_type != VAR_DICT
18372 || (d = li->li_tv.vval.v_dict) == NULL)
18373 {
18374 EMSG(_(e_invarg));
18375 return;
18376 }
18377 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018378 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18379 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018380 && dict_find(d, (char_u *)"priority", -1) != NULL
18381 && dict_find(d, (char_u *)"id", -1) != NULL))
18382 {
18383 EMSG(_(e_invarg));
18384 return;
18385 }
18386 li = li->li_next;
18387 }
18388
18389 clear_matches(curwin);
18390 li = l->lv_first;
18391 while (li != NULL)
18392 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018393 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018394 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018395 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018396 char_u *group;
18397 int priority;
18398 int id;
18399 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018400
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018401 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018402 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18403 {
18404 if (s == NULL)
18405 {
18406 s = list_alloc();
18407 if (s == NULL)
18408 return;
18409 }
18410
18411 /* match from matchaddpos() */
18412 for (i = 1; i < 9; i++)
18413 {
18414 sprintf((char *)buf, (char *)"pos%d", i);
18415 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18416 {
18417 if (di->di_tv.v_type != VAR_LIST)
18418 return;
18419
18420 list_append_tv(s, &di->di_tv);
18421 s->lv_refcount++;
18422 }
18423 else
18424 break;
18425 }
18426 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018427
18428 group = get_dict_string(d, (char_u *)"group", FALSE);
18429 priority = (int)get_dict_number(d, (char_u *)"priority");
18430 id = (int)get_dict_number(d, (char_u *)"id");
18431 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18432 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18433 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018434 if (i == 0)
18435 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018436 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018437 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018438 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018439 }
18440 else
18441 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018442 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018443 list_unref(s);
18444 s = NULL;
18445 }
18446
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018447 li = li->li_next;
18448 }
18449 rettv->vval.v_number = 0;
18450 }
18451#endif
18452}
18453
18454/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018455 * "setpos()" function
18456 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018458f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018459{
18460 pos_T pos;
18461 int fnum;
18462 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018463 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018464
Bram Moolenaar08250432008-02-13 11:42:46 +000018465 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018466 name = get_tv_string_chk(argvars);
18467 if (name != NULL)
18468 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018469 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018470 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018471 if (--pos.col < 0)
18472 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018473 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018474 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018475 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018476 if (fnum == curbuf->b_fnum)
18477 {
18478 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018479 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018480 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018481 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018482 curwin->w_set_curswant = FALSE;
18483 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018484 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018485 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018486 }
18487 else
18488 EMSG(_(e_invarg));
18489 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018490 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18491 {
18492 /* set mark */
18493 if (setmark_pos(name[1], &pos, fnum) == OK)
18494 rettv->vval.v_number = 0;
18495 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018496 else
18497 EMSG(_(e_invarg));
18498 }
18499 }
18500}
18501
18502/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018503 * "setqflist()" function
18504 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018506f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018507{
18508 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18509}
18510
18511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 * "setreg()" function
18513 */
18514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018515f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516{
18517 int regname;
18518 char_u *strregname;
18519 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018520 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 int append;
18522 char_u yank_type;
18523 long block_len;
18524
18525 block_len = -1;
18526 yank_type = MAUTO;
18527 append = FALSE;
18528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018529 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018530 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018532 if (strregname == NULL)
18533 return; /* type error; errmsg already given */
18534 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535 if (regname == 0 || regname == '@')
18536 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018538 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018540 stropt = get_tv_string_chk(&argvars[2]);
18541 if (stropt == NULL)
18542 return; /* type error */
18543 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 switch (*stropt)
18545 {
18546 case 'a': case 'A': /* append */
18547 append = TRUE;
18548 break;
18549 case 'v': case 'c': /* character-wise selection */
18550 yank_type = MCHAR;
18551 break;
18552 case 'V': case 'l': /* line-wise selection */
18553 yank_type = MLINE;
18554 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 case 'b': case Ctrl_V: /* block-wise selection */
18556 yank_type = MBLOCK;
18557 if (VIM_ISDIGIT(stropt[1]))
18558 {
18559 ++stropt;
18560 block_len = getdigits(&stropt) - 1;
18561 --stropt;
18562 }
18563 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 }
18565 }
18566
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018567 if (argvars[1].v_type == VAR_LIST)
18568 {
18569 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018570 char_u **allocval;
18571 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018572 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018573 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018574 int len = argvars[1].vval.v_list->lv_len;
18575 listitem_T *li;
18576
Bram Moolenaar7d647822014-04-05 21:28:56 +020018577 /* First half: use for pointers to result lines; second half: use for
18578 * pointers to allocated copies. */
18579 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018580 if (lstval == NULL)
18581 return;
18582 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018583 allocval = lstval + len + 2;
18584 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018585
18586 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18587 li = li->li_next)
18588 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018589 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018590 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018591 goto free_lstval;
18592 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018593 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018594 /* Need to make a copy, next get_tv_string_buf_chk() will
18595 * overwrite the string. */
18596 strval = vim_strsave(buf);
18597 if (strval == NULL)
18598 goto free_lstval;
18599 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018600 }
18601 *curval++ = strval;
18602 }
18603 *curval++ = NULL;
18604
18605 write_reg_contents_lst(regname, lstval, -1,
18606 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018607free_lstval:
18608 while (curallocval > allocval)
18609 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018610 vim_free(lstval);
18611 }
18612 else
18613 {
18614 strval = get_tv_string_chk(&argvars[1]);
18615 if (strval == NULL)
18616 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018617 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018620 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621}
18622
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018623/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018624 * "settabvar()" function
18625 */
18626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018627f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018628{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018629#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018630 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018631 tabpage_T *tp;
18632#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018633 char_u *varname, *tabvarname;
18634 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018635
18636 rettv->vval.v_number = 0;
18637
18638 if (check_restricted() || check_secure())
18639 return;
18640
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018641#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018642 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018643#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018644 varname = get_tv_string_chk(&argvars[1]);
18645 varp = &argvars[2];
18646
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018647 if (varname != NULL && varp != NULL
18648#ifdef FEAT_WINDOWS
18649 && tp != NULL
18650#endif
18651 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018652 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018653#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018654 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018655 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018656#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018657
18658 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18659 if (tabvarname != NULL)
18660 {
18661 STRCPY(tabvarname, "t:");
18662 STRCPY(tabvarname + 2, varname);
18663 set_var(tabvarname, varp, TRUE);
18664 vim_free(tabvarname);
18665 }
18666
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018667#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018668 /* Restore current tabpage */
18669 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018670 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018671#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018672 }
18673}
18674
18675/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018676 * "settabwinvar()" function
18677 */
18678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018679f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018680{
18681 setwinvar(argvars, rettv, 1);
18682}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683
18684/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018685 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018688f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018690 setwinvar(argvars, rettv, 0);
18691}
18692
18693/*
18694 * "setwinvar()" and "settabwinvar()" functions
18695 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018696
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018698setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018699{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700 win_T *win;
18701#ifdef FEAT_WINDOWS
18702 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018703 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018704 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705#endif
18706 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018707 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018709 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710
18711 if (check_restricted() || check_secure())
18712 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018713
18714#ifdef FEAT_WINDOWS
18715 if (off == 1)
18716 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18717 else
18718 tp = curtab;
18719#endif
18720 win = find_win_by_nr(&argvars[off], tp);
18721 varname = get_tv_string_chk(&argvars[off + 1]);
18722 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723
18724 if (win != NULL && varname != NULL && varp != NULL)
18725 {
18726#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018727 need_switch_win = !(tp == curtab && win == curwin);
18728 if (!need_switch_win
18729 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018732 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018734 long numval;
18735 char_u *strval;
18736 int error = FALSE;
18737
18738 ++varname;
18739 numval = get_tv_number_chk(varp, &error);
18740 strval = get_tv_string_buf_chk(varp, nbuf);
18741 if (!error && strval != NULL)
18742 set_option_value(varname, numval, strval, OPT_LOCAL);
18743 }
18744 else
18745 {
18746 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18747 if (winvarname != NULL)
18748 {
18749 STRCPY(winvarname, "w:");
18750 STRCPY(winvarname + 2, varname);
18751 set_var(winvarname, varp, TRUE);
18752 vim_free(winvarname);
18753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 }
18755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018757 if (need_switch_win)
18758 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759#endif
18760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761}
18762
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018763#ifdef FEAT_CRYPT
18764/*
18765 * "sha256({string})" function
18766 */
18767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018768f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018769{
18770 char_u *p;
18771
18772 p = get_tv_string(&argvars[0]);
18773 rettv->vval.v_string = vim_strsave(
18774 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18775 rettv->v_type = VAR_STRING;
18776}
18777#endif /* FEAT_CRYPT */
18778
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018780 * "shellescape({string})" function
18781 */
18782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018783f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018784{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018785 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018786 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018787 rettv->v_type = VAR_STRING;
18788}
18789
18790/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018791 * shiftwidth() function
18792 */
18793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018794f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018795{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018796 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018797}
18798
18799/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018800 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 */
18802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018803f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018805 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806
Bram Moolenaar0d660222005-01-07 21:51:51 +000018807 p = get_tv_string(&argvars[0]);
18808 rettv->vval.v_string = vim_strsave(p);
18809 simplify_filename(rettv->vval.v_string); /* simplify in place */
18810 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811}
18812
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018813#ifdef FEAT_FLOAT
18814/*
18815 * "sin()" function
18816 */
18817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018818f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018819{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018820 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018821
18822 rettv->v_type = VAR_FLOAT;
18823 if (get_float_arg(argvars, &f) == OK)
18824 rettv->vval.v_float = sin(f);
18825 else
18826 rettv->vval.v_float = 0.0;
18827}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018828
18829/*
18830 * "sinh()" function
18831 */
18832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018833f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018834{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018835 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018836
18837 rettv->v_type = VAR_FLOAT;
18838 if (get_float_arg(argvars, &f) == OK)
18839 rettv->vval.v_float = sinh(f);
18840 else
18841 rettv->vval.v_float = 0.0;
18842}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018843#endif
18844
Bram Moolenaar0d660222005-01-07 21:51:51 +000018845static int
18846#ifdef __BORLANDC__
18847 _RTLENTRYF
18848#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018849 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018850static int
18851#ifdef __BORLANDC__
18852 _RTLENTRYF
18853#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018854 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018855
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018856/* struct used in the array that's given to qsort() */
18857typedef struct
18858{
18859 listitem_T *item;
18860 int idx;
18861} sortItem_T;
18862
Bram Moolenaar0b962472016-02-22 22:51:33 +010018863/* struct storing information about current sort */
18864typedef struct
18865{
18866 int item_compare_ic;
18867 int item_compare_numeric;
18868 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018869#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018870 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018871#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018872 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018873 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018874 dict_T *item_compare_selfdict;
18875 int item_compare_func_err;
18876 int item_compare_keep_zero;
18877} sortinfo_T;
18878static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018879static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018880#define ITEM_COMPARE_FAIL 999
18881
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018883 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018885 static int
18886#ifdef __BORLANDC__
18887_RTLENTRYF
18888#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018889item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018891 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018892 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018893 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018894 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018895 int res;
18896 char_u numbuf1[NUMBUFLEN];
18897 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018899 si1 = (sortItem_T *)s1;
18900 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018901 tv1 = &si1->item->li_tv;
18902 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018903
Bram Moolenaar0b962472016-02-22 22:51:33 +010018904 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018905 {
18906 long v1 = get_tv_number(tv1);
18907 long v2 = get_tv_number(tv2);
18908
18909 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18910 }
18911
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018912#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018913 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018914 {
18915 float_T v1 = get_tv_float(tv1);
18916 float_T v2 = get_tv_float(tv2);
18917
18918 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18919 }
18920#endif
18921
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018922 /* tv2string() puts quotes around a string and allocates memory. Don't do
18923 * that for string variables. Use a single quote when comparing with a
18924 * non-string to do what the docs promise. */
18925 if (tv1->v_type == VAR_STRING)
18926 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018927 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018928 p1 = (char_u *)"'";
18929 else
18930 p1 = tv1->vval.v_string;
18931 }
18932 else
18933 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18934 if (tv2->v_type == VAR_STRING)
18935 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018936 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018937 p2 = (char_u *)"'";
18938 else
18939 p2 = tv2->vval.v_string;
18940 }
18941 else
18942 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018943 if (p1 == NULL)
18944 p1 = (char_u *)"";
18945 if (p2 == NULL)
18946 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018947 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018948 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018949 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018950 res = STRICMP(p1, p2);
18951 else
18952 res = STRCMP(p1, p2);
18953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018955 {
18956 double n1, n2;
18957 n1 = strtod((char *)p1, (char **)&p1);
18958 n2 = strtod((char *)p2, (char **)&p2);
18959 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18960 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018961
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018962 /* When the result would be zero, compare the item indexes. Makes the
18963 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018964 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018965 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018966
Bram Moolenaar0d660222005-01-07 21:51:51 +000018967 vim_free(tofree1);
18968 vim_free(tofree2);
18969 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970}
18971
18972 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018973#ifdef __BORLANDC__
18974_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018976item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018977{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018978 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018979 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018980 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018981 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018982 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018983 char_u *func_name;
18984 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018986 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018987 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018988 return 0;
18989
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018990 si1 = (sortItem_T *)s1;
18991 si2 = (sortItem_T *)s2;
18992
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018993 if (partial == NULL)
18994 func_name = sortinfo->item_compare_func;
18995 else
18996 func_name = partial->pt_name;
18997
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018998 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018999 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019000 copy_tv(&si1->item->li_tv, &argv[0]);
19001 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019002
19003 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019004 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019005 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019006 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019007 clear_tv(&argv[0]);
19008 clear_tv(&argv[1]);
19009
19010 if (res == FAIL)
19011 res = ITEM_COMPARE_FAIL;
19012 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019013 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19014 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019015 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019016 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019017
19018 /* When the result would be zero, compare the pointers themselves. Makes
19019 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019020 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019021 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019022
Bram Moolenaar0d660222005-01-07 21:51:51 +000019023 return res;
19024}
19025
19026/*
19027 * "sort({list})" function
19028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019030do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031{
Bram Moolenaar33570922005-01-25 22:26:29 +000019032 list_T *l;
19033 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019034 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019035 sortinfo_T *old_sortinfo;
19036 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019037 long len;
19038 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039
Bram Moolenaar0b962472016-02-22 22:51:33 +010019040 /* Pointer to current info struct used in compare function. Save and
19041 * restore the current one for nested calls. */
19042 old_sortinfo = sortinfo;
19043 sortinfo = &info;
19044
Bram Moolenaar0d660222005-01-07 21:51:51 +000019045 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019046 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 else
19048 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019049 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019050 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019051 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19052 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019053 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019054 rettv->vval.v_list = l;
19055 rettv->v_type = VAR_LIST;
19056 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057
Bram Moolenaar0d660222005-01-07 21:51:51 +000019058 len = list_len(l);
19059 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019060 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061
Bram Moolenaar0b962472016-02-22 22:51:33 +010019062 info.item_compare_ic = FALSE;
19063 info.item_compare_numeric = FALSE;
19064 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019065#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019066 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019067#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019068 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019069 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019070 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019071 if (argvars[1].v_type != VAR_UNKNOWN)
19072 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019073 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019074 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019075 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019076 else if (argvars[1].v_type == VAR_PARTIAL)
19077 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019078 else
19079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019080 int error = FALSE;
19081
19082 i = get_tv_number_chk(&argvars[1], &error);
19083 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019084 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019085 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019086 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019087 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019088 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019089 else if (i != 0)
19090 {
19091 EMSG(_(e_invarg));
19092 goto theend;
19093 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019094 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019095 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019096 if (*info.item_compare_func == NUL)
19097 {
19098 /* empty string means default sort */
19099 info.item_compare_func = NULL;
19100 }
19101 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019102 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019103 info.item_compare_func = NULL;
19104 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019105 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019106 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019107 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019108 info.item_compare_func = NULL;
19109 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019110 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019111#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019112 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019113 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 info.item_compare_func = NULL;
19115 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019116 }
19117#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019118 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019119 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019120 info.item_compare_func = NULL;
19121 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019122 }
19123 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019124 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019125
19126 if (argvars[2].v_type != VAR_UNKNOWN)
19127 {
19128 /* optional third argument: {dict} */
19129 if (argvars[2].v_type != VAR_DICT)
19130 {
19131 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019132 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019133 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019134 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019135 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137
Bram Moolenaar0d660222005-01-07 21:51:51 +000019138 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019139 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019140 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019141 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142
Bram Moolenaar327aa022014-03-25 18:24:23 +010019143 i = 0;
19144 if (sort)
19145 {
19146 /* sort(): ptrs will be the list to sort */
19147 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019148 {
19149 ptrs[i].item = li;
19150 ptrs[i].idx = i;
19151 ++i;
19152 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019153
Bram Moolenaar0b962472016-02-22 22:51:33 +010019154 info.item_compare_func_err = FALSE;
19155 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019156 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019157 if ((info.item_compare_func != NULL
19158 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019159 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019160 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019161 EMSG(_("E702: Sort compare function failed"));
19162 else
19163 {
19164 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019165 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019166 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019167 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019168 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019169
Bram Moolenaar0b962472016-02-22 22:51:33 +010019170 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019171 {
19172 /* Clear the List and append the items in sorted order. */
19173 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19174 l->lv_len = 0;
19175 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019176 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019177 }
19178 }
19179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019181 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019182 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019183
19184 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019185 info.item_compare_func_err = FALSE;
19186 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019187 item_compare_func_ptr = info.item_compare_func != NULL
19188 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019189 ? item_compare2 : item_compare;
19190
19191 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19192 li = li->li_next)
19193 {
19194 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19195 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019196 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019197 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019198 {
19199 EMSG(_("E882: Uniq compare function failed"));
19200 break;
19201 }
19202 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019203
Bram Moolenaar0b962472016-02-22 22:51:33 +010019204 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019205 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019206 while (--i >= 0)
19207 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019208 li = ptrs[i].item->li_next;
19209 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019210 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019211 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019212 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019213 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019214 list_fix_watch(l, li);
19215 listitem_free(li);
19216 l->lv_len--;
19217 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019218 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019219 }
19220
19221 vim_free(ptrs);
19222 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019223theend:
19224 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019225}
19226
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019227/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019228 * "sort({list})" function
19229 */
19230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019231f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019232{
19233 do_sort_uniq(argvars, rettv, TRUE);
19234}
19235
19236/*
19237 * "uniq({list})" function
19238 */
19239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019240f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019241{
19242 do_sort_uniq(argvars, rettv, FALSE);
19243}
19244
19245/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019246 * "soundfold({word})" function
19247 */
19248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019249f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019250{
19251 char_u *s;
19252
19253 rettv->v_type = VAR_STRING;
19254 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019255#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019256 rettv->vval.v_string = eval_soundfold(s);
19257#else
19258 rettv->vval.v_string = vim_strsave(s);
19259#endif
19260}
19261
19262/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019263 * "spellbadword()" function
19264 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019266f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019267{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019268 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019269 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019270 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019271
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019272 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019273 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019274
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019275#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019276 if (argvars[0].v_type == VAR_UNKNOWN)
19277 {
19278 /* Find the start and length of the badly spelled word. */
19279 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19280 if (len != 0)
19281 word = ml_get_cursor();
19282 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019283 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019284 {
19285 char_u *str = get_tv_string_chk(&argvars[0]);
19286 int capcol = -1;
19287
19288 if (str != NULL)
19289 {
19290 /* Check the argument for spelling. */
19291 while (*str != NUL)
19292 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019293 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019294 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019295 {
19296 word = str;
19297 break;
19298 }
19299 str += len;
19300 }
19301 }
19302 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019303#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019304
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019305 list_append_string(rettv->vval.v_list, word, len);
19306 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019307 attr == HLF_SPB ? "bad" :
19308 attr == HLF_SPR ? "rare" :
19309 attr == HLF_SPL ? "local" :
19310 attr == HLF_SPC ? "caps" :
19311 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019312}
19313
19314/*
19315 * "spellsuggest()" function
19316 */
19317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019318f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019319{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019320#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019321 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019322 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019323 int maxcount;
19324 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019325 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019326 listitem_T *li;
19327 int need_capital = FALSE;
19328#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019329
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019330 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019331 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019332
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019333#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019334 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019335 {
19336 str = get_tv_string(&argvars[0]);
19337 if (argvars[1].v_type != VAR_UNKNOWN)
19338 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019339 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019340 if (maxcount <= 0)
19341 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019342 if (argvars[2].v_type != VAR_UNKNOWN)
19343 {
19344 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19345 if (typeerr)
19346 return;
19347 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019348 }
19349 else
19350 maxcount = 25;
19351
Bram Moolenaar4770d092006-01-12 23:22:24 +000019352 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019353
19354 for (i = 0; i < ga.ga_len; ++i)
19355 {
19356 str = ((char_u **)ga.ga_data)[i];
19357
19358 li = listitem_alloc();
19359 if (li == NULL)
19360 vim_free(str);
19361 else
19362 {
19363 li->li_tv.v_type = VAR_STRING;
19364 li->li_tv.v_lock = 0;
19365 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019366 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019367 }
19368 }
19369 ga_clear(&ga);
19370 }
19371#endif
19372}
19373
Bram Moolenaar0d660222005-01-07 21:51:51 +000019374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019375f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019376{
19377 char_u *str;
19378 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019379 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019380 regmatch_T regmatch;
19381 char_u patbuf[NUMBUFLEN];
19382 char_u *save_cpo;
19383 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019384 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019385 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019386 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019387
19388 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19389 save_cpo = p_cpo;
19390 p_cpo = (char_u *)"";
19391
19392 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019393 if (argvars[1].v_type != VAR_UNKNOWN)
19394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019395 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19396 if (pat == NULL)
19397 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019398 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019399 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019400 }
19401 if (pat == NULL || *pat == NUL)
19402 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019403
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019404 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019406 if (typeerr)
19407 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408
Bram Moolenaar0d660222005-01-07 21:51:51 +000019409 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19410 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019412 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019413 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019414 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019415 if (*str == NUL)
19416 match = FALSE; /* empty item at the end */
19417 else
19418 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019419 if (match)
19420 end = regmatch.startp[0];
19421 else
19422 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019423 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19424 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019425 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019426 if (list_append_string(rettv->vval.v_list, str,
19427 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019428 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019429 }
19430 if (!match)
19431 break;
19432 /* Advance to just after the match. */
19433 if (regmatch.endp[0] > str)
19434 col = 0;
19435 else
19436 {
19437 /* Don't get stuck at the same match. */
19438#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019439 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019440#else
19441 col = 1;
19442#endif
19443 }
19444 str = regmatch.endp[0];
19445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446
Bram Moolenaar473de612013-06-08 18:19:48 +020019447 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449
Bram Moolenaar0d660222005-01-07 21:51:51 +000019450 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451}
19452
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019453#ifdef FEAT_FLOAT
19454/*
19455 * "sqrt()" function
19456 */
19457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019458f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019459{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019460 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019461
19462 rettv->v_type = VAR_FLOAT;
19463 if (get_float_arg(argvars, &f) == OK)
19464 rettv->vval.v_float = sqrt(f);
19465 else
19466 rettv->vval.v_float = 0.0;
19467}
19468
19469/*
19470 * "str2float()" function
19471 */
19472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019473f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019474{
19475 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19476
19477 if (*p == '+')
19478 p = skipwhite(p + 1);
19479 (void)string2float(p, &rettv->vval.v_float);
19480 rettv->v_type = VAR_FLOAT;
19481}
19482#endif
19483
Bram Moolenaar2c932302006-03-18 21:42:09 +000019484/*
19485 * "str2nr()" function
19486 */
19487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019488f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019489{
19490 int base = 10;
19491 char_u *p;
19492 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019493 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019494
19495 if (argvars[1].v_type != VAR_UNKNOWN)
19496 {
19497 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019498 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019499 {
19500 EMSG(_(e_invarg));
19501 return;
19502 }
19503 }
19504
19505 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019506 if (*p == '+')
19507 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019508 switch (base)
19509 {
19510 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19511 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19512 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19513 default: what = 0;
19514 }
19515 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019516 rettv->vval.v_number = n;
19517}
19518
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519#ifdef HAVE_STRFTIME
19520/*
19521 * "strftime({format}[, {time}])" function
19522 */
19523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019524f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525{
19526 char_u result_buf[256];
19527 struct tm *curtime;
19528 time_t seconds;
19529 char_u *p;
19530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019531 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019533 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019534 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 seconds = time(NULL);
19536 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019537 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 curtime = localtime(&seconds);
19539 /* MSVC returns NULL for an invalid value of seconds. */
19540 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019541 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 else
19543 {
19544# ifdef FEAT_MBYTE
19545 vimconv_T conv;
19546 char_u *enc;
19547
19548 conv.vc_type = CONV_NONE;
19549 enc = enc_locale();
19550 convert_setup(&conv, p_enc, enc);
19551 if (conv.vc_type != CONV_NONE)
19552 p = string_convert(&conv, p, NULL);
19553# endif
19554 if (p != NULL)
19555 (void)strftime((char *)result_buf, sizeof(result_buf),
19556 (char *)p, curtime);
19557 else
19558 result_buf[0] = NUL;
19559
19560# ifdef FEAT_MBYTE
19561 if (conv.vc_type != CONV_NONE)
19562 vim_free(p);
19563 convert_setup(&conv, enc, p_enc);
19564 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019565 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566 else
19567# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019568 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569
19570# ifdef FEAT_MBYTE
19571 /* Release conversion descriptors */
19572 convert_setup(&conv, NULL, NULL);
19573 vim_free(enc);
19574# endif
19575 }
19576}
19577#endif
19578
19579/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019580 * "strgetchar()" function
19581 */
19582 static void
19583f_strgetchar(typval_T *argvars, typval_T *rettv)
19584{
19585 char_u *str;
19586 int len;
19587 int error = FALSE;
19588 int charidx;
19589
19590 rettv->vval.v_number = -1;
19591 str = get_tv_string_chk(&argvars[0]);
19592 if (str == NULL)
19593 return;
19594 len = (int)STRLEN(str);
19595 charidx = get_tv_number_chk(&argvars[1], &error);
19596 if (error)
19597 return;
19598#ifdef FEAT_MBYTE
19599 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019600 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019601
19602 while (charidx >= 0 && byteidx < len)
19603 {
19604 if (charidx == 0)
19605 {
19606 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19607 break;
19608 }
19609 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019610 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019611 }
19612 }
19613#else
19614 if (charidx < len)
19615 rettv->vval.v_number = str[charidx];
19616#endif
19617}
19618
19619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620 * "stridx()" function
19621 */
19622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019623f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624{
19625 char_u buf[NUMBUFLEN];
19626 char_u *needle;
19627 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019628 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019630 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019632 needle = get_tv_string_chk(&argvars[1]);
19633 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019634 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019635 if (needle == NULL || haystack == NULL)
19636 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637
Bram Moolenaar33570922005-01-25 22:26:29 +000019638 if (argvars[2].v_type != VAR_UNKNOWN)
19639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019640 int error = FALSE;
19641
19642 start_idx = get_tv_number_chk(&argvars[2], &error);
19643 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019645 if (start_idx >= 0)
19646 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 }
19648
19649 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19650 if (pos != NULL)
19651 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652}
19653
19654/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019655 * "string()" function
19656 */
19657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019658f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019659{
19660 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019661 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019663 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019664 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19665 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019666 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019667 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019668 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669}
19670
19671/*
19672 * "strlen()" function
19673 */
19674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019675f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019677 rettv->vval.v_number = (varnumber_T)(STRLEN(
19678 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679}
19680
19681/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019682 * "strchars()" function
19683 */
19684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019685f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019686{
19687 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019688 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019689#ifdef FEAT_MBYTE
19690 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019691 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019692#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019693
19694 if (argvars[1].v_type != VAR_UNKNOWN)
19695 skipcc = get_tv_number_chk(&argvars[1], NULL);
19696 if (skipcc < 0 || skipcc > 1)
19697 EMSG(_(e_invarg));
19698 else
19699 {
19700#ifdef FEAT_MBYTE
19701 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19702 while (*s != NUL)
19703 {
19704 func_mb_ptr2char_adv(&s);
19705 ++len;
19706 }
19707 rettv->vval.v_number = len;
19708#else
19709 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19710#endif
19711 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019712}
19713
19714/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019715 * "strdisplaywidth()" function
19716 */
19717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019718f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019719{
19720 char_u *s = get_tv_string(&argvars[0]);
19721 int col = 0;
19722
19723 if (argvars[1].v_type != VAR_UNKNOWN)
19724 col = get_tv_number(&argvars[1]);
19725
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019726 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019727}
19728
19729/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019730 * "strwidth()" function
19731 */
19732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019733f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019734{
19735 char_u *s = get_tv_string(&argvars[0]);
19736
19737 rettv->vval.v_number = (varnumber_T)(
19738#ifdef FEAT_MBYTE
19739 mb_string2cells(s, -1)
19740#else
19741 STRLEN(s)
19742#endif
19743 );
19744}
19745
19746/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019747 * "strcharpart()" function
19748 */
19749 static void
19750f_strcharpart(typval_T *argvars, typval_T *rettv)
19751{
19752#ifdef FEAT_MBYTE
19753 char_u *p;
19754 int nchar;
19755 int nbyte = 0;
19756 int charlen;
19757 int len = 0;
19758 int slen;
19759 int error = FALSE;
19760
19761 p = get_tv_string(&argvars[0]);
19762 slen = (int)STRLEN(p);
19763
19764 nchar = get_tv_number_chk(&argvars[1], &error);
19765 if (!error)
19766 {
19767 if (nchar > 0)
19768 while (nchar > 0 && nbyte < slen)
19769 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019770 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019771 --nchar;
19772 }
19773 else
19774 nbyte = nchar;
19775 if (argvars[2].v_type != VAR_UNKNOWN)
19776 {
19777 charlen = get_tv_number(&argvars[2]);
19778 while (charlen > 0 && nbyte + len < slen)
19779 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019780 int off = nbyte + len;
19781
19782 if (off < 0)
19783 len += 1;
19784 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019785 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019786 --charlen;
19787 }
19788 }
19789 else
19790 len = slen - nbyte; /* default: all bytes that are available. */
19791 }
19792
19793 /*
19794 * Only return the overlap between the specified part and the actual
19795 * string.
19796 */
19797 if (nbyte < 0)
19798 {
19799 len += nbyte;
19800 nbyte = 0;
19801 }
19802 else if (nbyte > slen)
19803 nbyte = slen;
19804 if (len < 0)
19805 len = 0;
19806 else if (nbyte + len > slen)
19807 len = slen - nbyte;
19808
19809 rettv->v_type = VAR_STRING;
19810 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19811#else
19812 f_strpart(argvars, rettv);
19813#endif
19814}
19815
19816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 * "strpart()" function
19818 */
19819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019820f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821{
19822 char_u *p;
19823 int n;
19824 int len;
19825 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019826 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829 slen = (int)STRLEN(p);
19830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019831 n = get_tv_number_chk(&argvars[1], &error);
19832 if (error)
19833 len = 0;
19834 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019835 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 else
19837 len = slen - n; /* default len: all bytes that are available. */
19838
19839 /*
19840 * Only return the overlap between the specified part and the actual
19841 * string.
19842 */
19843 if (n < 0)
19844 {
19845 len += n;
19846 n = 0;
19847 }
19848 else if (n > slen)
19849 n = slen;
19850 if (len < 0)
19851 len = 0;
19852 else if (n + len > slen)
19853 len = slen - n;
19854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019855 rettv->v_type = VAR_STRING;
19856 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857}
19858
19859/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019860 * "strridx()" function
19861 */
19862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019863f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019864{
19865 char_u buf[NUMBUFLEN];
19866 char_u *needle;
19867 char_u *haystack;
19868 char_u *rest;
19869 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019870 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019871
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019872 needle = get_tv_string_chk(&argvars[1]);
19873 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019874
19875 rettv->vval.v_number = -1;
19876 if (needle == NULL || haystack == NULL)
19877 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019878
19879 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019880 if (argvars[2].v_type != VAR_UNKNOWN)
19881 {
19882 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019883 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019884 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019885 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019886 }
19887 else
19888 end_idx = haystack_len;
19889
Bram Moolenaar0d660222005-01-07 21:51:51 +000019890 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019891 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019892 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019893 lastmatch = haystack + end_idx;
19894 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019895 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019896 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019897 for (rest = haystack; *rest != '\0'; ++rest)
19898 {
19899 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019900 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019901 break;
19902 lastmatch = rest;
19903 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019904 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019905
19906 if (lastmatch == NULL)
19907 rettv->vval.v_number = -1;
19908 else
19909 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19910}
19911
19912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 * "strtrans()" function
19914 */
19915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019916f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019918 rettv->v_type = VAR_STRING;
19919 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920}
19921
19922/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019923 * "submatch()" function
19924 */
19925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019926f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019927{
Bram Moolenaar41571762014-04-02 19:00:58 +020019928 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019929 int no;
19930 int retList = 0;
19931
19932 no = (int)get_tv_number_chk(&argvars[0], &error);
19933 if (error)
19934 return;
19935 error = FALSE;
19936 if (argvars[1].v_type != VAR_UNKNOWN)
19937 retList = get_tv_number_chk(&argvars[1], &error);
19938 if (error)
19939 return;
19940
19941 if (retList == 0)
19942 {
19943 rettv->v_type = VAR_STRING;
19944 rettv->vval.v_string = reg_submatch(no);
19945 }
19946 else
19947 {
19948 rettv->v_type = VAR_LIST;
19949 rettv->vval.v_list = reg_submatch_list(no);
19950 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019951}
19952
19953/*
19954 * "substitute()" function
19955 */
19956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019957f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019958{
19959 char_u patbuf[NUMBUFLEN];
19960 char_u subbuf[NUMBUFLEN];
19961 char_u flagsbuf[NUMBUFLEN];
19962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019963 char_u *str = get_tv_string_chk(&argvars[0]);
19964 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19965 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19966 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19967
Bram Moolenaar0d660222005-01-07 21:51:51 +000019968 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019969 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19970 rettv->vval.v_string = NULL;
19971 else
19972 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019973}
19974
19975/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019976 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019979f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980{
19981 int id = 0;
19982#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019983 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984 long col;
19985 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019986 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019988 lnum = get_tv_lnum(argvars); /* -1 on type error */
19989 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19990 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019992 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019993 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019994 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995#endif
19996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019997 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998}
19999
20000/*
20001 * "synIDattr(id, what [, mode])" function
20002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020004f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005{
20006 char_u *p = NULL;
20007#ifdef FEAT_SYN_HL
20008 int id;
20009 char_u *what;
20010 char_u *mode;
20011 char_u modebuf[NUMBUFLEN];
20012 int modec;
20013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020014 id = get_tv_number(&argvars[0]);
20015 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020016 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020020 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 modec = 0; /* replace invalid with current */
20022 }
20023 else
20024 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020025#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020026 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 modec = 'g';
20028 else
20029#endif
20030 if (t_colors > 1)
20031 modec = 'c';
20032 else
20033 modec = 't';
20034 }
20035
20036
20037 switch (TOLOWER_ASC(what[0]))
20038 {
20039 case 'b':
20040 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20041 p = highlight_color(id, what, modec);
20042 else /* bold */
20043 p = highlight_has_attr(id, HL_BOLD, modec);
20044 break;
20045
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020046 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 p = highlight_color(id, what, modec);
20048 break;
20049
20050 case 'i':
20051 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20052 p = highlight_has_attr(id, HL_INVERSE, modec);
20053 else /* italic */
20054 p = highlight_has_attr(id, HL_ITALIC, modec);
20055 break;
20056
20057 case 'n': /* name */
20058 p = get_highlight_name(NULL, id - 1);
20059 break;
20060
20061 case 'r': /* reverse */
20062 p = highlight_has_attr(id, HL_INVERSE, modec);
20063 break;
20064
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020065 case 's':
20066 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20067 p = highlight_color(id, what, modec);
20068 else /* standout */
20069 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070 break;
20071
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020072 case 'u':
20073 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20074 /* underline */
20075 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20076 else
20077 /* undercurl */
20078 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 break;
20080 }
20081
20082 if (p != NULL)
20083 p = vim_strsave(p);
20084#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020085 rettv->v_type = VAR_STRING;
20086 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087}
20088
20089/*
20090 * "synIDtrans(id)" function
20091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020093f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094{
20095 int id;
20096
20097#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099
20100 if (id > 0)
20101 id = syn_get_final_id(id);
20102 else
20103#endif
20104 id = 0;
20105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020106 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107}
20108
20109/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020110 * "synconcealed(lnum, col)" function
20111 */
20112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020113f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020114{
20115#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20116 long lnum;
20117 long col;
20118 int syntax_flags = 0;
20119 int cchar;
20120 int matchid = 0;
20121 char_u str[NUMBUFLEN];
20122#endif
20123
20124 rettv->v_type = VAR_LIST;
20125 rettv->vval.v_list = NULL;
20126
20127#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20128 lnum = get_tv_lnum(argvars); /* -1 on type error */
20129 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20130
20131 vim_memset(str, NUL, sizeof(str));
20132
20133 if (rettv_list_alloc(rettv) != FAIL)
20134 {
20135 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20136 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20137 && curwin->w_p_cole > 0)
20138 {
20139 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20140 syntax_flags = get_syntax_info(&matchid);
20141
20142 /* get the conceal character */
20143 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20144 {
20145 cchar = syn_get_sub_char();
20146 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20147 cchar = lcs_conceal;
20148 if (cchar != NUL)
20149 {
20150# ifdef FEAT_MBYTE
20151 if (has_mbyte)
20152 (*mb_char2bytes)(cchar, str);
20153 else
20154# endif
20155 str[0] = cchar;
20156 }
20157 }
20158 }
20159
20160 list_append_number(rettv->vval.v_list,
20161 (syntax_flags & HL_CONCEAL) != 0);
20162 /* -1 to auto-determine strlen */
20163 list_append_string(rettv->vval.v_list, str, -1);
20164 list_append_number(rettv->vval.v_list, matchid);
20165 }
20166#endif
20167}
20168
20169/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020170 * "synstack(lnum, col)" function
20171 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020173f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020174{
20175#ifdef FEAT_SYN_HL
20176 long lnum;
20177 long col;
20178 int i;
20179 int id;
20180#endif
20181
20182 rettv->v_type = VAR_LIST;
20183 rettv->vval.v_list = NULL;
20184
20185#ifdef FEAT_SYN_HL
20186 lnum = get_tv_lnum(argvars); /* -1 on type error */
20187 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20188
20189 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020190 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020191 && rettv_list_alloc(rettv) != FAIL)
20192 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020193 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020194 for (i = 0; ; ++i)
20195 {
20196 id = syn_get_stack_item(i);
20197 if (id < 0)
20198 break;
20199 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20200 break;
20201 }
20202 }
20203#endif
20204}
20205
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020207get_cmd_output_as_rettv(
20208 typval_T *argvars,
20209 typval_T *rettv,
20210 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020212 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020214 char_u *infile = NULL;
20215 char_u buf[NUMBUFLEN];
20216 int err = FALSE;
20217 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020218 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020219 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020221 rettv->v_type = VAR_STRING;
20222 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020223 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020224 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020225
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020226 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020227 {
20228 /*
20229 * Write the string to a temp file, to be used for input of the shell
20230 * command.
20231 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020232 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020233 {
20234 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020235 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020236 }
20237
20238 fd = mch_fopen((char *)infile, WRITEBIN);
20239 if (fd == NULL)
20240 {
20241 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020242 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020243 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020244 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020245 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020246 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20247 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020248 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020249 else
20250 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020251 size_t len;
20252
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020253 p = get_tv_string_buf_chk(&argvars[1], buf);
20254 if (p == NULL)
20255 {
20256 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020257 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020258 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020259 len = STRLEN(p);
20260 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020261 err = TRUE;
20262 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020263 if (fclose(fd) != 0)
20264 err = TRUE;
20265 if (err)
20266 {
20267 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020268 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020269 }
20270 }
20271
Bram Moolenaar52a72462014-08-29 15:53:52 +020020272 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20273 * echoes typeahead, that messes up the display. */
20274 if (!msg_silent)
20275 flags += SHELL_COOKED;
20276
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020277 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020279 int len;
20280 listitem_T *li;
20281 char_u *s = NULL;
20282 char_u *start;
20283 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020284 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285
Bram Moolenaar52a72462014-08-29 15:53:52 +020020286 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020287 if (res == NULL)
20288 goto errret;
20289
20290 list = list_alloc();
20291 if (list == NULL)
20292 goto errret;
20293
20294 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020296 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020297 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020298 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020299 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020300
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020301 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020302 if (s == NULL)
20303 goto errret;
20304
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020305 for (p = s; start < end; ++p, ++start)
20306 *p = *start == NUL ? NL : *start;
20307 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020308
20309 li = listitem_alloc();
20310 if (li == NULL)
20311 {
20312 vim_free(s);
20313 goto errret;
20314 }
20315 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020316 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020317 li->li_tv.vval.v_string = s;
20318 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020320
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020321 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020322 rettv->v_type = VAR_LIST;
20323 rettv->vval.v_list = list;
20324 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020326 else
20327 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020328 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020329#ifdef USE_CR
20330 /* translate <CR> into <NL> */
20331 if (res != NULL)
20332 {
20333 char_u *s;
20334
20335 for (s = res; *s; ++s)
20336 {
20337 if (*s == CAR)
20338 *s = NL;
20339 }
20340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341#else
20342# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020343 /* translate <CR><NL> into <NL> */
20344 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020346 char_u *s, *d;
20347
20348 d = res;
20349 for (s = res; *s; ++s)
20350 {
20351 if (s[0] == CAR && s[1] == NL)
20352 ++s;
20353 *d++ = *s;
20354 }
20355 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357# endif
20358#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020359 rettv->vval.v_string = res;
20360 res = NULL;
20361 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020362
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020363errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020364 if (infile != NULL)
20365 {
20366 mch_remove(infile);
20367 vim_free(infile);
20368 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020369 if (res != NULL)
20370 vim_free(res);
20371 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020372 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020373}
20374
20375/*
20376 * "system()" function
20377 */
20378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020379f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020380{
20381 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20382}
20383
20384/*
20385 * "systemlist()" function
20386 */
20387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020388f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020389{
20390 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391}
20392
20393/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020394 * "tabpagebuflist()" function
20395 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020397f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020398{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020399#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020400 tabpage_T *tp;
20401 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020402
20403 if (argvars[0].v_type == VAR_UNKNOWN)
20404 wp = firstwin;
20405 else
20406 {
20407 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20408 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020409 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020410 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020411 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020412 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020413 for (; wp != NULL; wp = wp->w_next)
20414 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020415 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020416 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020417 }
20418#endif
20419}
20420
20421
20422/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020423 * "tabpagenr()" function
20424 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020426f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020427{
20428 int nr = 1;
20429#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020430 char_u *arg;
20431
20432 if (argvars[0].v_type != VAR_UNKNOWN)
20433 {
20434 arg = get_tv_string_chk(&argvars[0]);
20435 nr = 0;
20436 if (arg != NULL)
20437 {
20438 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020439 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020440 else
20441 EMSG2(_(e_invexpr2), arg);
20442 }
20443 }
20444 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020445 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020446#endif
20447 rettv->vval.v_number = nr;
20448}
20449
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020450
20451#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020452static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020453
20454/*
20455 * Common code for tabpagewinnr() and winnr().
20456 */
20457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020458get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020459{
20460 win_T *twin;
20461 int nr = 1;
20462 win_T *wp;
20463 char_u *arg;
20464
20465 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20466 if (argvar->v_type != VAR_UNKNOWN)
20467 {
20468 arg = get_tv_string_chk(argvar);
20469 if (arg == NULL)
20470 nr = 0; /* type error; errmsg already given */
20471 else if (STRCMP(arg, "$") == 0)
20472 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20473 else if (STRCMP(arg, "#") == 0)
20474 {
20475 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20476 if (twin == NULL)
20477 nr = 0;
20478 }
20479 else
20480 {
20481 EMSG2(_(e_invexpr2), arg);
20482 nr = 0;
20483 }
20484 }
20485
20486 if (nr > 0)
20487 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20488 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020489 {
20490 if (wp == NULL)
20491 {
20492 /* didn't find it in this tabpage */
20493 nr = 0;
20494 break;
20495 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020496 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020497 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020498 return nr;
20499}
20500#endif
20501
20502/*
20503 * "tabpagewinnr()" function
20504 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020506f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020507{
20508 int nr = 1;
20509#ifdef FEAT_WINDOWS
20510 tabpage_T *tp;
20511
20512 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20513 if (tp == NULL)
20514 nr = 0;
20515 else
20516 nr = get_winnr(tp, &argvars[1]);
20517#endif
20518 rettv->vval.v_number = nr;
20519}
20520
20521
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020522/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020523 * "tagfiles()" function
20524 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020526f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020527{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020528 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020529 tagname_T tn;
20530 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020531
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020532 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020533 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020534 fname = alloc(MAXPATHL);
20535 if (fname == NULL)
20536 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020537
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020538 for (first = TRUE; ; first = FALSE)
20539 if (get_tagfname(&tn, first, fname) == FAIL
20540 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020541 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020542 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020543 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020544}
20545
20546/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020547 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020548 */
20549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020550f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020551{
20552 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020553
20554 tag_pattern = get_tv_string(&argvars[0]);
20555
20556 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020557 if (*tag_pattern == NUL)
20558 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020559
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020560 if (rettv_list_alloc(rettv) == OK)
20561 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020562}
20563
20564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 * "tempname()" function
20566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020568f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569{
20570 static int x = 'A';
20571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020572 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020573 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574
20575 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20576 * names. Skip 'I' and 'O', they are used for shell redirection. */
20577 do
20578 {
20579 if (x == 'Z')
20580 x = '0';
20581 else if (x == '9')
20582 x = 'A';
20583 else
20584 {
20585#ifdef EBCDIC
20586 if (x == 'I')
20587 x = 'J';
20588 else if (x == 'R')
20589 x = 'S';
20590 else
20591#endif
20592 ++x;
20593 }
20594 } while (x == 'I' || x == 'O');
20595}
20596
20597/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020598 * "test(list)" function: Just checking the walls...
20599 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020601f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020602{
20603 /* Used for unit testing. Change the code below to your liking. */
20604#if 0
20605 listitem_T *li;
20606 list_T *l;
20607 char_u *bad, *good;
20608
20609 if (argvars[0].v_type != VAR_LIST)
20610 return;
20611 l = argvars[0].vval.v_list;
20612 if (l == NULL)
20613 return;
20614 li = l->lv_first;
20615 if (li == NULL)
20616 return;
20617 bad = get_tv_string(&li->li_tv);
20618 li = li->li_next;
20619 if (li == NULL)
20620 return;
20621 good = get_tv_string(&li->li_tv);
20622 rettv->vval.v_number = test_edit_score(bad, good);
20623#endif
20624}
20625
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020626#ifdef FEAT_FLOAT
20627/*
20628 * "tan()" function
20629 */
20630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020631f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020632{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020633 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020634
20635 rettv->v_type = VAR_FLOAT;
20636 if (get_float_arg(argvars, &f) == OK)
20637 rettv->vval.v_float = tan(f);
20638 else
20639 rettv->vval.v_float = 0.0;
20640}
20641
20642/*
20643 * "tanh()" function
20644 */
20645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020646f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020647{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020648 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020649
20650 rettv->v_type = VAR_FLOAT;
20651 if (get_float_arg(argvars, &f) == OK)
20652 rettv->vval.v_float = tanh(f);
20653 else
20654 rettv->vval.v_float = 0.0;
20655}
20656#endif
20657
Bram Moolenaar975b5272016-03-15 23:10:59 +010020658#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20659/*
20660 * Get a callback from "arg". It can be a Funcref or a function name.
20661 * When "arg" is zero return an empty string.
20662 * Return NULL for an invalid argument.
20663 */
20664 char_u *
20665get_callback(typval_T *arg, partial_T **pp)
20666{
20667 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20668 {
20669 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020670 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020671 return (*pp)->pt_name;
20672 }
20673 *pp = NULL;
20674 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20675 return arg->vval.v_string;
20676 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20677 return (char_u *)"";
20678 EMSG(_("E921: Invalid callback argument"));
20679 return NULL;
20680}
20681#endif
20682
20683#ifdef FEAT_TIMERS
20684/*
20685 * "timer_start(time, callback [, options])" function
20686 */
20687 static void
20688f_timer_start(typval_T *argvars, typval_T *rettv)
20689{
20690 long msec = get_tv_number(&argvars[0]);
20691 timer_T *timer;
20692 int repeat = 0;
20693 char_u *callback;
20694 dict_T *dict;
20695
Bram Moolenaar38499922016-04-22 20:46:52 +020020696 if (check_secure())
20697 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020698 if (argvars[2].v_type != VAR_UNKNOWN)
20699 {
20700 if (argvars[2].v_type != VAR_DICT
20701 || (dict = argvars[2].vval.v_dict) == NULL)
20702 {
20703 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20704 return;
20705 }
20706 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20707 repeat = get_dict_number(dict, (char_u *)"repeat");
20708 }
20709
20710 timer = create_timer(msec, repeat);
20711 callback = get_callback(&argvars[1], &timer->tr_partial);
20712 if (callback == NULL)
20713 {
20714 stop_timer(timer);
20715 rettv->vval.v_number = -1;
20716 }
20717 else
20718 {
20719 timer->tr_callback = vim_strsave(callback);
20720 rettv->vval.v_number = timer->tr_id;
20721 }
20722}
20723
20724/*
20725 * "timer_stop(timer)" function
20726 */
20727 static void
20728f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20729{
20730 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20731
20732 if (timer != NULL)
20733 stop_timer(timer);
20734}
20735#endif
20736
Bram Moolenaard52d9742005-08-21 22:20:28 +000020737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 * "tolower(string)" function
20739 */
20740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020741f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020742{
20743 char_u *p;
20744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020745 p = vim_strsave(get_tv_string(&argvars[0]));
20746 rettv->v_type = VAR_STRING;
20747 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748
20749 if (p != NULL)
20750 while (*p != NUL)
20751 {
20752#ifdef FEAT_MBYTE
20753 int l;
20754
20755 if (enc_utf8)
20756 {
20757 int c, lc;
20758
20759 c = utf_ptr2char(p);
20760 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020761 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 /* TODO: reallocate string when byte count changes. */
20763 if (utf_char2len(lc) == l)
20764 utf_char2bytes(lc, p);
20765 p += l;
20766 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020767 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768 p += l; /* skip multi-byte character */
20769 else
20770#endif
20771 {
20772 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20773 ++p;
20774 }
20775 }
20776}
20777
20778/*
20779 * "toupper(string)" function
20780 */
20781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020782f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020784 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020785 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786}
20787
20788/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020789 * "tr(string, fromstr, tostr)" function
20790 */
20791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020792f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020793{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020794 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020795 char_u *fromstr;
20796 char_u *tostr;
20797 char_u *p;
20798#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020799 int inlen;
20800 int fromlen;
20801 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020802 int idx;
20803 char_u *cpstr;
20804 int cplen;
20805 int first = TRUE;
20806#endif
20807 char_u buf[NUMBUFLEN];
20808 char_u buf2[NUMBUFLEN];
20809 garray_T ga;
20810
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020811 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020812 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20813 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020814
20815 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020816 rettv->v_type = VAR_STRING;
20817 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020818 if (fromstr == NULL || tostr == NULL)
20819 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020820 ga_init2(&ga, (int)sizeof(char), 80);
20821
20822#ifdef FEAT_MBYTE
20823 if (!has_mbyte)
20824#endif
20825 /* not multi-byte: fromstr and tostr must be the same length */
20826 if (STRLEN(fromstr) != STRLEN(tostr))
20827 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020828#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020829error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020830#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020831 EMSG2(_(e_invarg2), fromstr);
20832 ga_clear(&ga);
20833 return;
20834 }
20835
20836 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020837 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020838 {
20839#ifdef FEAT_MBYTE
20840 if (has_mbyte)
20841 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020842 inlen = (*mb_ptr2len)(in_str);
20843 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020844 cplen = inlen;
20845 idx = 0;
20846 for (p = fromstr; *p != NUL; p += fromlen)
20847 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020848 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020849 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020850 {
20851 for (p = tostr; *p != NUL; p += tolen)
20852 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020853 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020854 if (idx-- == 0)
20855 {
20856 cplen = tolen;
20857 cpstr = p;
20858 break;
20859 }
20860 }
20861 if (*p == NUL) /* tostr is shorter than fromstr */
20862 goto error;
20863 break;
20864 }
20865 ++idx;
20866 }
20867
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020868 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020869 {
20870 /* Check that fromstr and tostr have the same number of
20871 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020872 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020873 first = FALSE;
20874 for (p = tostr; *p != NUL; p += tolen)
20875 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020876 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020877 --idx;
20878 }
20879 if (idx != 0)
20880 goto error;
20881 }
20882
Bram Moolenaarcde88542015-08-11 19:14:00 +020020883 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020884 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020885 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020886
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020887 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020888 }
20889 else
20890#endif
20891 {
20892 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020893 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020894 if (p != NULL)
20895 ga_append(&ga, tostr[p - fromstr]);
20896 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020897 ga_append(&ga, *in_str);
20898 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020899 }
20900 }
20901
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020902 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020903 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020904 ga_append(&ga, NUL);
20905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020906 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020907}
20908
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020909#ifdef FEAT_FLOAT
20910/*
20911 * "trunc({float})" function
20912 */
20913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020914f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020915{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020916 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020917
20918 rettv->v_type = VAR_FLOAT;
20919 if (get_float_arg(argvars, &f) == OK)
20920 /* trunc() is not in C90, use floor() or ceil() instead. */
20921 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20922 else
20923 rettv->vval.v_float = 0.0;
20924}
20925#endif
20926
Bram Moolenaar8299df92004-07-10 09:47:34 +000020927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 * "type(expr)" function
20929 */
20930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020931f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020933 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020934
20935 switch (argvars[0].v_type)
20936 {
20937 case VAR_NUMBER: n = 0; break;
20938 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020939 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020940 case VAR_FUNC: n = 2; break;
20941 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020942 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020943 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020944 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020945 if (argvars[0].vval.v_number == VVAL_FALSE
20946 || argvars[0].vval.v_number == VVAL_TRUE)
20947 n = 6;
20948 else
20949 n = 7;
20950 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020951 case VAR_JOB: n = 8; break;
20952 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020953 case VAR_UNKNOWN:
20954 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20955 n = -1;
20956 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020957 }
20958 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959}
20960
20961/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020962 * "undofile(name)" function
20963 */
20964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020965f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020966{
20967 rettv->v_type = VAR_STRING;
20968#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020969 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020970 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020971
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020972 if (*fname == NUL)
20973 {
20974 /* If there is no file name there will be no undo file. */
20975 rettv->vval.v_string = NULL;
20976 }
20977 else
20978 {
20979 char_u *ffname = FullName_save(fname, FALSE);
20980
20981 if (ffname != NULL)
20982 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20983 vim_free(ffname);
20984 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020985 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020986#else
20987 rettv->vval.v_string = NULL;
20988#endif
20989}
20990
20991/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020992 * "undotree()" function
20993 */
20994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020995f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020996{
20997 if (rettv_dict_alloc(rettv) == OK)
20998 {
20999 dict_T *dict = rettv->vval.v_dict;
21000 list_T *list;
21001
Bram Moolenaar730cde92010-06-27 05:18:54 +020021002 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021003 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021004 dict_add_nr_str(dict, "save_last",
21005 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021006 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21007 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021008 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021009
21010 list = list_alloc();
21011 if (list != NULL)
21012 {
21013 u_eval_tree(curbuf->b_u_oldhead, list);
21014 dict_add_list(dict, "entries", list);
21015 }
21016 }
21017}
21018
21019/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021020 * "values(dict)" function
21021 */
21022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021023f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021024{
21025 dict_list(argvars, rettv, 1);
21026}
21027
21028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 * "virtcol(string)" function
21030 */
21031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021032f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033{
21034 colnr_T vcol = 0;
21035 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021036 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021038 fp = var2fpos(&argvars[0], FALSE, &fnum);
21039 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21040 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 {
21042 getvvcol(curwin, fp, NULL, NULL, &vcol);
21043 ++vcol;
21044 }
21045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021046 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047}
21048
21049/*
21050 * "visualmode()" function
21051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021053f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 char_u str[2];
21056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021057 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058 str[0] = curbuf->b_visual_mode_eval;
21059 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021060 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061
21062 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021063 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065}
21066
21067/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021068 * "wildmenumode()" function
21069 */
21070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021071f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021072{
21073#ifdef FEAT_WILDMENU
21074 if (wild_menu_showing)
21075 rettv->vval.v_number = 1;
21076#endif
21077}
21078
21079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080 * "winbufnr(nr)" function
21081 */
21082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021083f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084{
21085 win_T *wp;
21086
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021087 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021089 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021091 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092}
21093
21094/*
21095 * "wincol()" function
21096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021098f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099{
21100 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021101 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102}
21103
21104/*
21105 * "winheight(nr)" function
21106 */
21107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021108f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109{
21110 win_T *wp;
21111
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021112 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021114 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021116 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117}
21118
21119/*
21120 * "winline()" function
21121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021123f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124{
21125 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021126 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127}
21128
21129/*
21130 * "winnr()" function
21131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021133f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134{
21135 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021136
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021138 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021140 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141}
21142
21143/*
21144 * "winrestcmd()" function
21145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021147f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148{
21149#ifdef FEAT_WINDOWS
21150 win_T *wp;
21151 int winnr = 1;
21152 garray_T ga;
21153 char_u buf[50];
21154
21155 ga_init2(&ga, (int)sizeof(char), 70);
21156 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21157 {
21158 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21159 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21161 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 ++winnr;
21163 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021164 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021166 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021168 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021170 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171}
21172
21173/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021174 * "winrestview()" function
21175 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021177f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021178{
21179 dict_T *dict;
21180
21181 if (argvars[0].v_type != VAR_DICT
21182 || (dict = argvars[0].vval.v_dict) == NULL)
21183 EMSG(_(e_invarg));
21184 else
21185 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021186 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21187 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21188 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21189 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021190#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021191 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21192 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021193#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021194 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21195 {
21196 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21197 curwin->w_set_curswant = FALSE;
21198 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021199
Bram Moolenaar82c25852014-05-28 16:47:16 +020021200 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21201 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021202#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021203 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21204 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021205#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021206 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21207 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21208 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21209 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021210
21211 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021212 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021213# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021214 win_new_width(curwin, W_WIDTH(curwin));
21215# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021216 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021217
Bram Moolenaarb851a962014-10-31 15:45:52 +010021218 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021219 curwin->w_topline = 1;
21220 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21221 curwin->w_topline = curbuf->b_ml.ml_line_count;
21222#ifdef FEAT_DIFF
21223 check_topfill(curwin, TRUE);
21224#endif
21225 }
21226}
21227
21228/*
21229 * "winsaveview()" function
21230 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021232f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021233{
21234 dict_T *dict;
21235
Bram Moolenaara800b422010-06-27 01:15:55 +020021236 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021237 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021238 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021239
21240 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21241 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21242#ifdef FEAT_VIRTUALEDIT
21243 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21244#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021245 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021246 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21247
21248 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21249#ifdef FEAT_DIFF
21250 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21251#endif
21252 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21253 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21254}
21255
21256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257 * "winwidth(nr)" function
21258 */
21259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021260f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261{
21262 win_T *wp;
21263
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021264 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021266 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021268#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021269 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021271 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272#endif
21273}
21274
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021276 * "wordcount()" function
21277 */
21278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021279f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021280{
21281 if (rettv_dict_alloc(rettv) == FAIL)
21282 return;
21283 cursor_pos_info(rettv->vval.v_dict);
21284}
21285
21286/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021287 * Write list of strings to file
21288 */
21289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021290write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021291{
21292 listitem_T *li;
21293 int c;
21294 int ret = OK;
21295 char_u *s;
21296
21297 for (li = list->lv_first; li != NULL; li = li->li_next)
21298 {
21299 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21300 {
21301 if (*s == '\n')
21302 c = putc(NUL, fd);
21303 else
21304 c = putc(*s, fd);
21305 if (c == EOF)
21306 {
21307 ret = FAIL;
21308 break;
21309 }
21310 }
21311 if (!binary || li->li_next != NULL)
21312 if (putc('\n', fd) == EOF)
21313 {
21314 ret = FAIL;
21315 break;
21316 }
21317 if (ret == FAIL)
21318 {
21319 EMSG(_(e_write));
21320 break;
21321 }
21322 }
21323 return ret;
21324}
21325
21326/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021327 * "writefile()" function
21328 */
21329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021330f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021331{
21332 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021333 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021334 char_u *fname;
21335 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021336 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021337
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021338 if (check_restricted() || check_secure())
21339 return;
21340
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021341 if (argvars[0].v_type != VAR_LIST)
21342 {
21343 EMSG2(_(e_listarg), "writefile()");
21344 return;
21345 }
21346 if (argvars[0].vval.v_list == NULL)
21347 return;
21348
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021349 if (argvars[2].v_type != VAR_UNKNOWN)
21350 {
21351 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21352 binary = TRUE;
21353 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21354 append = TRUE;
21355 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021356
21357 /* Always open the file in binary mode, library functions have a mind of
21358 * their own about CR-LF conversion. */
21359 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021360 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21361 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021362 {
21363 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21364 ret = -1;
21365 }
21366 else
21367 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021368 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21369 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021370 fclose(fd);
21371 }
21372
21373 rettv->vval.v_number = ret;
21374}
21375
21376/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021377 * "xor(expr, expr)" function
21378 */
21379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021380f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021381{
21382 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21383 ^ get_tv_number_chk(&argvars[1], NULL);
21384}
21385
21386
21387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021389 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 */
21391 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021392var2fpos(
21393 typval_T *varp,
21394 int dollar_lnum, /* TRUE when $ is last line */
21395 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021397 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021399 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400
Bram Moolenaara5525202006-03-02 22:52:09 +000021401 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021402 if (varp->v_type == VAR_LIST)
21403 {
21404 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021405 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021406 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021407 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021408
21409 l = varp->vval.v_list;
21410 if (l == NULL)
21411 return NULL;
21412
21413 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021414 pos.lnum = list_find_nr(l, 0L, &error);
21415 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021416 return NULL; /* invalid line number */
21417
21418 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021419 pos.col = list_find_nr(l, 1L, &error);
21420 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021421 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021422 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021423
21424 /* We accept "$" for the column number: last column. */
21425 li = list_find(l, 1L);
21426 if (li != NULL && li->li_tv.v_type == VAR_STRING
21427 && li->li_tv.vval.v_string != NULL
21428 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21429 pos.col = len + 1;
21430
Bram Moolenaara5525202006-03-02 22:52:09 +000021431 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021432 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021433 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021434 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021435
Bram Moolenaara5525202006-03-02 22:52:09 +000021436#ifdef FEAT_VIRTUALEDIT
21437 /* Get the virtual offset. Defaults to zero. */
21438 pos.coladd = list_find_nr(l, 2L, &error);
21439 if (error)
21440 pos.coladd = 0;
21441#endif
21442
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021443 return &pos;
21444 }
21445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021446 name = get_tv_string_chk(varp);
21447 if (name == NULL)
21448 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021449 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021451 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21452 {
21453 if (VIsual_active)
21454 return &VIsual;
21455 return &curwin->w_cursor;
21456 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021457 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021459 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21461 return NULL;
21462 return pp;
21463 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021464
21465#ifdef FEAT_VIRTUALEDIT
21466 pos.coladd = 0;
21467#endif
21468
Bram Moolenaar477933c2007-07-17 14:32:23 +000021469 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021470 {
21471 pos.col = 0;
21472 if (name[1] == '0') /* "w0": first visible line */
21473 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021474 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021475 pos.lnum = curwin->w_topline;
21476 return &pos;
21477 }
21478 else if (name[1] == '$') /* "w$": last visible line */
21479 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021480 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021481 pos.lnum = curwin->w_botline - 1;
21482 return &pos;
21483 }
21484 }
21485 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021487 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488 {
21489 pos.lnum = curbuf->b_ml.ml_line_count;
21490 pos.col = 0;
21491 }
21492 else
21493 {
21494 pos.lnum = curwin->w_cursor.lnum;
21495 pos.col = (colnr_T)STRLEN(ml_get_curline());
21496 }
21497 return &pos;
21498 }
21499 return NULL;
21500}
21501
21502/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021503 * Convert list in "arg" into a position and optional file number.
21504 * When "fnump" is NULL there is no file number, only 3 items.
21505 * Note that the column is passed on as-is, the caller may want to decrement
21506 * it to use 1 for the first column.
21507 * Return FAIL when conversion is not possible, doesn't check the position for
21508 * validity.
21509 */
21510 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021511list2fpos(
21512 typval_T *arg,
21513 pos_T *posp,
21514 int *fnump,
21515 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021516{
21517 list_T *l = arg->vval.v_list;
21518 long i = 0;
21519 long n;
21520
Bram Moolenaar493c1782014-05-28 14:34:46 +020021521 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21522 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021523 if (arg->v_type != VAR_LIST
21524 || l == NULL
21525 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021526 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021527 return FAIL;
21528
21529 if (fnump != NULL)
21530 {
21531 n = list_find_nr(l, i++, NULL); /* fnum */
21532 if (n < 0)
21533 return FAIL;
21534 if (n == 0)
21535 n = curbuf->b_fnum; /* current buffer */
21536 *fnump = n;
21537 }
21538
21539 n = list_find_nr(l, i++, NULL); /* lnum */
21540 if (n < 0)
21541 return FAIL;
21542 posp->lnum = n;
21543
21544 n = list_find_nr(l, i++, NULL); /* col */
21545 if (n < 0)
21546 return FAIL;
21547 posp->col = n;
21548
21549#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021550 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021551 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021552 posp->coladd = 0;
21553 else
21554 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021555#endif
21556
Bram Moolenaar493c1782014-05-28 14:34:46 +020021557 if (curswantp != NULL)
21558 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21559
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021560 return OK;
21561}
21562
21563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 * Get the length of an environment variable name.
21565 * Advance "arg" to the first character after the name.
21566 * Return 0 for error.
21567 */
21568 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021569get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570{
21571 char_u *p;
21572 int len;
21573
21574 for (p = *arg; vim_isIDc(*p); ++p)
21575 ;
21576 if (p == *arg) /* no name found */
21577 return 0;
21578
21579 len = (int)(p - *arg);
21580 *arg = p;
21581 return len;
21582}
21583
21584/*
21585 * Get the length of the name of a function or internal variable.
21586 * "arg" is advanced to the first non-white character after the name.
21587 * Return 0 if something is wrong.
21588 */
21589 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021590get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591{
21592 char_u *p;
21593 int len;
21594
21595 /* Find the end of the name. */
21596 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021597 {
21598 if (*p == ':')
21599 {
21600 /* "s:" is start of "s:var", but "n:" is not and can be used in
21601 * slice "[n:]". Also "xx:" is not a namespace. */
21602 len = (int)(p - *arg);
21603 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21604 || len > 1)
21605 break;
21606 }
21607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 if (p == *arg) /* no name found */
21609 return 0;
21610
21611 len = (int)(p - *arg);
21612 *arg = skipwhite(p);
21613
21614 return len;
21615}
21616
21617/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021618 * Get the length of the name of a variable or function.
21619 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021621 * Return -1 if curly braces expansion failed.
21622 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 * If the name contains 'magic' {}'s, expand them and return the
21624 * expanded name in an allocated string via 'alias' - caller must free.
21625 */
21626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021627get_name_len(
21628 char_u **arg,
21629 char_u **alias,
21630 int evaluate,
21631 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632{
21633 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 char_u *p;
21635 char_u *expr_start;
21636 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637
21638 *alias = NULL; /* default to no alias */
21639
21640 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21641 && (*arg)[2] == (int)KE_SNR)
21642 {
21643 /* hard coded <SNR>, already translated */
21644 *arg += 3;
21645 return get_id_len(arg) + 3;
21646 }
21647 len = eval_fname_script(*arg);
21648 if (len > 0)
21649 {
21650 /* literal "<SID>", "s:" or "<SNR>" */
21651 *arg += len;
21652 }
21653
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021655 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021657 p = find_name_end(*arg, &expr_start, &expr_end,
21658 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 if (expr_start != NULL)
21660 {
21661 char_u *temp_string;
21662
21663 if (!evaluate)
21664 {
21665 len += (int)(p - *arg);
21666 *arg = skipwhite(p);
21667 return len;
21668 }
21669
21670 /*
21671 * Include any <SID> etc in the expanded string:
21672 * Thus the -len here.
21673 */
21674 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21675 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021676 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 *alias = temp_string;
21678 *arg = skipwhite(p);
21679 return (int)STRLEN(temp_string);
21680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681
21682 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021683 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 EMSG2(_(e_invexpr2), *arg);
21685
21686 return len;
21687}
21688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021689/*
21690 * Find the end of a variable or function name, taking care of magic braces.
21691 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21692 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021693 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021694 * Return a pointer to just after the name. Equal to "arg" if there is no
21695 * valid name.
21696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021698find_name_end(
21699 char_u *arg,
21700 char_u **expr_start,
21701 char_u **expr_end,
21702 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021704 int mb_nest = 0;
21705 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021707 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021709 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021711 *expr_start = NULL;
21712 *expr_end = NULL;
21713 }
21714
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021715 /* Quick check for valid starting character. */
21716 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21717 return arg;
21718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021719 for (p = arg; *p != NUL
21720 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021721 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021722 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021723 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021724 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021725 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021726 if (*p == '\'')
21727 {
21728 /* skip over 'string' to avoid counting [ and ] inside it. */
21729 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21730 ;
21731 if (*p == NUL)
21732 break;
21733 }
21734 else if (*p == '"')
21735 {
21736 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21737 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21738 if (*p == '\\' && p[1] != NUL)
21739 ++p;
21740 if (*p == NUL)
21741 break;
21742 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021743 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21744 {
21745 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021746 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021747 len = (int)(p - arg);
21748 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021749 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021750 break;
21751 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021753 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021755 if (*p == '[')
21756 ++br_nest;
21757 else if (*p == ']')
21758 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021761 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021763 if (*p == '{')
21764 {
21765 mb_nest++;
21766 if (expr_start != NULL && *expr_start == NULL)
21767 *expr_start = p;
21768 }
21769 else if (*p == '}')
21770 {
21771 mb_nest--;
21772 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21773 *expr_end = p;
21774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 }
21777
21778 return p;
21779}
21780
21781/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021782 * Expands out the 'magic' {}'s in a variable/function name.
21783 * Note that this can call itself recursively, to deal with
21784 * constructs like foo{bar}{baz}{bam}
21785 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21786 * "in_start" ^
21787 * "expr_start" ^
21788 * "expr_end" ^
21789 * "in_end" ^
21790 *
21791 * Returns a new allocated string, which the caller must free.
21792 * Returns NULL for failure.
21793 */
21794 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021795make_expanded_name(
21796 char_u *in_start,
21797 char_u *expr_start,
21798 char_u *expr_end,
21799 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021800{
21801 char_u c1;
21802 char_u *retval = NULL;
21803 char_u *temp_result;
21804 char_u *nextcmd = NULL;
21805
21806 if (expr_end == NULL || in_end == NULL)
21807 return NULL;
21808 *expr_start = NUL;
21809 *expr_end = NUL;
21810 c1 = *in_end;
21811 *in_end = NUL;
21812
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021813 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021814 if (temp_result != NULL && nextcmd == NULL)
21815 {
21816 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21817 + (in_end - expr_end) + 1));
21818 if (retval != NULL)
21819 {
21820 STRCPY(retval, in_start);
21821 STRCAT(retval, temp_result);
21822 STRCAT(retval, expr_end + 1);
21823 }
21824 }
21825 vim_free(temp_result);
21826
21827 *in_end = c1; /* put char back for error messages */
21828 *expr_start = '{';
21829 *expr_end = '}';
21830
21831 if (retval != NULL)
21832 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021833 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021834 if (expr_start != NULL)
21835 {
21836 /* Further expansion! */
21837 temp_result = make_expanded_name(retval, expr_start,
21838 expr_end, temp_result);
21839 vim_free(retval);
21840 retval = temp_result;
21841 }
21842 }
21843
21844 return retval;
21845}
21846
21847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021849 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 */
21851 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021852eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021854 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21855}
21856
21857/*
21858 * Return TRUE if character "c" can be used as the first character in a
21859 * variable or function name (excluding '{' and '}').
21860 */
21861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021862eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021863{
21864 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865}
21866
21867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 * Set number v: variable to "val".
21869 */
21870 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021871set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021873 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874}
21875
21876/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021877 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 */
21879 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021880get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021882 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883}
21884
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021885/*
21886 * Get string v: variable value. Uses a static buffer, can only be used once.
21887 */
21888 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021889get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021890{
21891 return get_tv_string(&vimvars[idx].vv_tv);
21892}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021893
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021895 * Get List v: variable value. Caller must take care of reference count when
21896 * needed.
21897 */
21898 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021899get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021900{
21901 return vimvars[idx].vv_list;
21902}
21903
21904/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021905 * Set v:char to character "c".
21906 */
21907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021908set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021909{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021910 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021911
21912#ifdef FEAT_MBYTE
21913 if (has_mbyte)
21914 buf[(*mb_char2bytes)(c, buf)] = NUL;
21915 else
21916#endif
21917 {
21918 buf[0] = c;
21919 buf[1] = NUL;
21920 }
21921 set_vim_var_string(VV_CHAR, buf, -1);
21922}
21923
21924/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021925 * Set v:count to "count" and v:count1 to "count1".
21926 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927 */
21928 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021929set_vcount(
21930 long count,
21931 long count1,
21932 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021934 if (set_prevcount)
21935 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021936 vimvars[VV_COUNT].vv_nr = count;
21937 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938}
21939
21940/*
21941 * Set string v: variable to a copy of "val".
21942 */
21943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021944set_vim_var_string(
21945 int idx,
21946 char_u *val,
21947 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948{
Bram Moolenaara542c682016-01-31 16:28:04 +010021949 clear_tv(&vimvars[idx].vv_di.di_tv);
21950 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021952 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021954 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021956 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957}
21958
21959/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021960 * Set List v: variable to "val".
21961 */
21962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021963set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021964{
Bram Moolenaara542c682016-01-31 16:28:04 +010021965 clear_tv(&vimvars[idx].vv_di.di_tv);
21966 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021967 vimvars[idx].vv_list = val;
21968 if (val != NULL)
21969 ++val->lv_refcount;
21970}
21971
21972/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021973 * Set Dictionary v: variable to "val".
21974 */
21975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021976set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021977{
21978 int todo;
21979 hashitem_T *hi;
21980
Bram Moolenaara542c682016-01-31 16:28:04 +010021981 clear_tv(&vimvars[idx].vv_di.di_tv);
21982 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021983 vimvars[idx].vv_dict = val;
21984 if (val != NULL)
21985 {
21986 ++val->dv_refcount;
21987
21988 /* Set readonly */
21989 todo = (int)val->dv_hashtab.ht_used;
21990 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21991 {
21992 if (HASHITEM_EMPTY(hi))
21993 continue;
21994 --todo;
21995 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21996 }
21997 }
21998}
21999
22000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 * Set v:register if needed.
22002 */
22003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022004set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005{
22006 char_u regname;
22007
22008 if (c == 0 || c == ' ')
22009 regname = '"';
22010 else
22011 regname = c;
22012 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022013 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 set_vim_var_string(VV_REG, &regname, 1);
22015}
22016
22017/*
22018 * Get or set v:exception. If "oldval" == NULL, return the current value.
22019 * Otherwise, restore the value to "oldval" and return NULL.
22020 * Must always be called in pairs to save and restore v:exception! Does not
22021 * take care of memory allocations.
22022 */
22023 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022024v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025{
22026 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022027 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028
Bram Moolenaare9a41262005-01-15 22:18:47 +000022029 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 return NULL;
22031}
22032
22033/*
22034 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22035 * Otherwise, restore the value to "oldval" and return NULL.
22036 * Must always be called in pairs to save and restore v:throwpoint! Does not
22037 * take care of memory allocations.
22038 */
22039 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022040v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041{
22042 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022043 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044
Bram Moolenaare9a41262005-01-15 22:18:47 +000022045 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 return NULL;
22047}
22048
22049#if defined(FEAT_AUTOCMD) || defined(PROTO)
22050/*
22051 * Set v:cmdarg.
22052 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22053 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22054 * Must always be called in pairs!
22055 */
22056 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022057set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058{
22059 char_u *oldval;
22060 char_u *newval;
22061 unsigned len;
22062
Bram Moolenaare9a41262005-01-15 22:18:47 +000022063 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022064 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022066 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022067 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022068 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069 }
22070
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022071 if (eap->force_bin == FORCE_BIN)
22072 len = 6;
22073 else if (eap->force_bin == FORCE_NOBIN)
22074 len = 8;
22075 else
22076 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022077
22078 if (eap->read_edit)
22079 len += 7;
22080
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022081 if (eap->force_ff != 0)
22082 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22083# ifdef FEAT_MBYTE
22084 if (eap->force_enc != 0)
22085 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022086 if (eap->bad_char != 0)
22087 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022088# endif
22089
22090 newval = alloc(len + 1);
22091 if (newval == NULL)
22092 return NULL;
22093
22094 if (eap->force_bin == FORCE_BIN)
22095 sprintf((char *)newval, " ++bin");
22096 else if (eap->force_bin == FORCE_NOBIN)
22097 sprintf((char *)newval, " ++nobin");
22098 else
22099 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022100
22101 if (eap->read_edit)
22102 STRCAT(newval, " ++edit");
22103
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022104 if (eap->force_ff != 0)
22105 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22106 eap->cmd + eap->force_ff);
22107# ifdef FEAT_MBYTE
22108 if (eap->force_enc != 0)
22109 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22110 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022111 if (eap->bad_char == BAD_KEEP)
22112 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22113 else if (eap->bad_char == BAD_DROP)
22114 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22115 else if (eap->bad_char != 0)
22116 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022117# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022118 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022119 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120}
22121#endif
22122
22123/*
22124 * Get the value of internal variable "name".
22125 * Return OK or FAIL.
22126 */
22127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022128get_var_tv(
22129 char_u *name,
22130 int len, /* length of "name" */
22131 typval_T *rettv, /* NULL when only checking existence */
22132 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22133 int verbose, /* may give error message */
22134 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135{
22136 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022137 typval_T *tv = NULL;
22138 typval_T atv;
22139 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022140 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141
22142 /* truncate the name, so that we can use strcmp() */
22143 cc = name[len];
22144 name[len] = NUL;
22145
22146 /*
22147 * Check for "b:changedtick".
22148 */
22149 if (STRCMP(name, "b:changedtick") == 0)
22150 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022151 atv.v_type = VAR_NUMBER;
22152 atv.vval.v_number = curbuf->b_changedtick;
22153 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 }
22155
22156 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 * Check for user-defined variables.
22158 */
22159 else
22160 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022161 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022163 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022164 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022165 if (dip != NULL)
22166 *dip = v;
22167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 }
22169
Bram Moolenaare9a41262005-01-15 22:18:47 +000022170 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022172 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022173 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 ret = FAIL;
22175 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022176 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022177 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178
22179 name[len] = cc;
22180
22181 return ret;
22182}
22183
22184/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022185 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22186 * Also handle function call with Funcref variable: func(expr)
22187 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22188 */
22189 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022190handle_subscript(
22191 char_u **arg,
22192 typval_T *rettv,
22193 int evaluate, /* do more than finding the end */
22194 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022195{
22196 int ret = OK;
22197 dict_T *selfdict = NULL;
22198 char_u *s;
22199 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022200 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022201
22202 while (ret == OK
22203 && (**arg == '['
22204 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022205 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22206 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022207 && !vim_iswhite(*(*arg - 1)))
22208 {
22209 if (**arg == '(')
22210 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022211 partial_T *pt = NULL;
22212
Bram Moolenaard9fba312005-06-26 22:34:35 +000022213 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022214 if (evaluate)
22215 {
22216 functv = *rettv;
22217 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022218
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022219 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022220 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022221 {
22222 pt = functv.vval.v_partial;
22223 s = pt->pt_name;
22224 }
22225 else
22226 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022227 }
22228 else
22229 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022230 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022231 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022232 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022233
22234 /* Clear the funcref afterwards, so that deleting it while
22235 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022236 if (evaluate)
22237 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022238
22239 /* Stop the expression evaluation when immediately aborting on
22240 * error, or when an interrupt occurred or an exception was thrown
22241 * but not caught. */
22242 if (aborting())
22243 {
22244 if (ret == OK)
22245 clear_tv(rettv);
22246 ret = FAIL;
22247 }
22248 dict_unref(selfdict);
22249 selfdict = NULL;
22250 }
22251 else /* **arg == '[' || **arg == '.' */
22252 {
22253 dict_unref(selfdict);
22254 if (rettv->v_type == VAR_DICT)
22255 {
22256 selfdict = rettv->vval.v_dict;
22257 if (selfdict != NULL)
22258 ++selfdict->dv_refcount;
22259 }
22260 else
22261 selfdict = NULL;
22262 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22263 {
22264 clear_tv(rettv);
22265 ret = FAIL;
22266 }
22267 }
22268 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022269
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022270 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
22271 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022272 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022273 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22274 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022275 char_u *tofree = NULL;
22276 ufunc_T *fp;
22277 char_u fname_buf[FLEN_FIXED + 1];
22278 int error;
22279
22280 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022281 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022282 fp = find_func(fname);
22283 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022284
22285 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010022286 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022287 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022288 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22289
22290 if (pt != NULL)
22291 {
22292 pt->pt_refcount = 1;
22293 pt->pt_dict = selfdict;
22294 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022295 if (rettv->v_type == VAR_FUNC)
22296 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022297 /* Just a function: Take over the function name and use
22298 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022299 pt->pt_name = rettv->vval.v_string;
22300 }
22301 else
22302 {
22303 partial_T *ret_pt = rettv->vval.v_partial;
22304 int i;
22305
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022306 /* Partial: copy the function name, use selfdict and copy
22307 * args. Can't take over name or args, the partial might
22308 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022309 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022310 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022311 if (ret_pt->pt_argc > 0)
22312 {
22313 pt->pt_argv = (typval_T *)alloc(
22314 sizeof(typval_T) * ret_pt->pt_argc);
22315 if (pt->pt_argv == NULL)
22316 /* out of memory: drop the arguments */
22317 pt->pt_argc = 0;
22318 else
22319 {
22320 pt->pt_argc = ret_pt->pt_argc;
22321 for (i = 0; i < pt->pt_argc; i++)
22322 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22323 }
22324 }
22325 partial_unref(ret_pt);
22326 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022327 rettv->v_type = VAR_PARTIAL;
22328 rettv->vval.v_partial = pt;
22329 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022330 }
22331 }
22332
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022333 dict_unref(selfdict);
22334 return ret;
22335}
22336
22337/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022338 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022339 * value).
22340 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022341 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022342alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022343{
Bram Moolenaar33570922005-01-25 22:26:29 +000022344 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022345}
22346
22347/*
22348 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 * The string "s" must have been allocated, it is consumed.
22350 * Return NULL for out of memory, the variable otherwise.
22351 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022352 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022353alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354{
Bram Moolenaar33570922005-01-25 22:26:29 +000022355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022357 rettv = alloc_tv();
22358 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022360 rettv->v_type = VAR_STRING;
22361 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 }
22363 else
22364 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022365 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366}
22367
22368/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022369 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022371 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022372free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373{
22374 if (varp != NULL)
22375 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022376 switch (varp->v_type)
22377 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022378 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022379 func_unref(varp->vval.v_string);
22380 /*FALLTHROUGH*/
22381 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022382 vim_free(varp->vval.v_string);
22383 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022384 case VAR_PARTIAL:
22385 partial_unref(varp->vval.v_partial);
22386 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022387 case VAR_LIST:
22388 list_unref(varp->vval.v_list);
22389 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022390 case VAR_DICT:
22391 dict_unref(varp->vval.v_dict);
22392 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022393 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022394#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022395 job_unref(varp->vval.v_job);
22396 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022397#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022398 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022399#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022400 channel_unref(varp->vval.v_channel);
22401 break;
22402#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022403 case VAR_NUMBER:
22404 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022405 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022406 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022407 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 vim_free(varp);
22410 }
22411}
22412
22413/*
22414 * Free the memory for a variable value and set the value to NULL or 0.
22415 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022416 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022417clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418{
22419 if (varp != NULL)
22420 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022421 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022423 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022424 func_unref(varp->vval.v_string);
22425 /*FALLTHROUGH*/
22426 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022427 vim_free(varp->vval.v_string);
22428 varp->vval.v_string = NULL;
22429 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022430 case VAR_PARTIAL:
22431 partial_unref(varp->vval.v_partial);
22432 varp->vval.v_partial = NULL;
22433 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022434 case VAR_LIST:
22435 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022436 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022437 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022438 case VAR_DICT:
22439 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022440 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022441 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022442 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022443 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022444 varp->vval.v_number = 0;
22445 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022446 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022447#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022448 varp->vval.v_float = 0.0;
22449 break;
22450#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022451 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022452#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022453 job_unref(varp->vval.v_job);
22454 varp->vval.v_job = NULL;
22455#endif
22456 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022457 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022458#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022459 channel_unref(varp->vval.v_channel);
22460 varp->vval.v_channel = NULL;
22461#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022462 case VAR_UNKNOWN:
22463 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022465 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 }
22467}
22468
22469/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022470 * Set the value of a variable to NULL without freeing items.
22471 */
22472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022473init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022474{
22475 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022476 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022477}
22478
22479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480 * Get the number value of a variable.
22481 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022482 * For incompatible types, return 0.
22483 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22484 * caller of incompatible types: it sets *denote to TRUE if "denote"
22485 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022487 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022488get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022490 int error = FALSE;
22491
22492 return get_tv_number_chk(varp, &error); /* return 0L on error */
22493}
22494
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022495 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022496get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022497{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022498 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022500 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022502 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022503 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022504 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022505#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022506 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022507 break;
22508#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022509 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022510 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022511 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022512 break;
22513 case VAR_STRING:
22514 if (varp->vval.v_string != NULL)
22515 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022516 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022517 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022518 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022519 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022520 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022521 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022522 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022523 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022524 case VAR_SPECIAL:
22525 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22526 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022527 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022528#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022529 EMSG(_("E910: Using a Job as a Number"));
22530 break;
22531#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022532 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022533#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022534 EMSG(_("E913: Using a Channel as a Number"));
22535 break;
22536#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022537 case VAR_UNKNOWN:
22538 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022539 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022541 if (denote == NULL) /* useful for values that must be unsigned */
22542 n = -1;
22543 else
22544 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022545 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546}
22547
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022548#ifdef FEAT_FLOAT
22549 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022550get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022551{
22552 switch (varp->v_type)
22553 {
22554 case VAR_NUMBER:
22555 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022556 case VAR_FLOAT:
22557 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022558 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022559 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022560 EMSG(_("E891: Using a Funcref as a Float"));
22561 break;
22562 case VAR_STRING:
22563 EMSG(_("E892: Using a String as a Float"));
22564 break;
22565 case VAR_LIST:
22566 EMSG(_("E893: Using a List as a Float"));
22567 break;
22568 case VAR_DICT:
22569 EMSG(_("E894: Using a Dictionary as a Float"));
22570 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022571 case VAR_SPECIAL:
22572 EMSG(_("E907: Using a special value as a Float"));
22573 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022574 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022575# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022576 EMSG(_("E911: Using a Job as a Float"));
22577 break;
22578# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022579 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022580# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022581 EMSG(_("E914: Using a Channel as a Float"));
22582 break;
22583# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022584 case VAR_UNKNOWN:
22585 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022586 break;
22587 }
22588 return 0;
22589}
22590#endif
22591
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022593 * Get the lnum from the first argument.
22594 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022595 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 */
22597 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022598get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599{
Bram Moolenaar33570922005-01-25 22:26:29 +000022600 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 linenr_T lnum;
22602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022603 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 if (lnum == 0) /* no valid number, try using line() */
22605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022606 rettv.v_type = VAR_NUMBER;
22607 f_line(argvars, &rettv);
22608 lnum = rettv.vval.v_number;
22609 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 }
22611 return lnum;
22612}
22613
22614/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022615 * Get the lnum from the first argument.
22616 * Also accepts "$", then "buf" is used.
22617 * Returns 0 on error.
22618 */
22619 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022620get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022621{
22622 if (argvars[0].v_type == VAR_STRING
22623 && argvars[0].vval.v_string != NULL
22624 && argvars[0].vval.v_string[0] == '$'
22625 && buf != NULL)
22626 return buf->b_ml.ml_line_count;
22627 return get_tv_number_chk(&argvars[0], NULL);
22628}
22629
22630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 * Get the string value of a variable.
22632 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022633 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22634 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635 * If the String variable has never been set, return an empty string.
22636 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022637 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22638 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022640 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022641get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642{
22643 static char_u mybuf[NUMBUFLEN];
22644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022645 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646}
22647
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022648 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022649get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022651 char_u *res = get_tv_string_buf_chk(varp, buf);
22652
22653 return res != NULL ? res : (char_u *)"";
22654}
22655
Bram Moolenaar7d647822014-04-05 21:28:56 +020022656/*
22657 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22658 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022659 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022660get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022661{
22662 static char_u mybuf[NUMBUFLEN];
22663
22664 return get_tv_string_buf_chk(varp, mybuf);
22665}
22666
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022667 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022668get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022669{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022670 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022672 case VAR_NUMBER:
22673 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22674 return buf;
22675 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022676 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022677 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022678 break;
22679 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022680 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022681 break;
22682 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022683 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022684 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022685 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022686#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022687 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022688 break;
22689#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022690 case VAR_STRING:
22691 if (varp->vval.v_string != NULL)
22692 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022693 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022694 case VAR_SPECIAL:
22695 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22696 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022697 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022698#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022699 {
22700 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022701 char *status;
22702
22703 if (job == NULL)
22704 return (char_u *)"no process";
22705 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022706 : job->jv_status == JOB_ENDED ? "dead"
22707 : "run";
22708# ifdef UNIX
22709 vim_snprintf((char *)buf, NUMBUFLEN,
22710 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022711# elif defined(WIN32)
22712 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022713 "process %ld %s",
22714 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022715 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022716# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022717 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022718 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22719# endif
22720 return buf;
22721 }
22722#endif
22723 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022724 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022725#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022726 {
22727 channel_T *channel = varp->vval.v_channel;
22728 char *status = channel_status(channel);
22729
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022730 if (channel == NULL)
22731 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22732 else
22733 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022734 "channel %d %s", channel->ch_id, status);
22735 return buf;
22736 }
22737#endif
22738 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022739 case VAR_UNKNOWN:
22740 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022741 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022743 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744}
22745
22746/*
22747 * Find variable "name" in the list of variables.
22748 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022749 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022750 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022751 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022753 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022754find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022757 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758
Bram Moolenaara7043832005-01-21 11:56:39 +000022759 ht = find_var_ht(name, &varname);
22760 if (htp != NULL)
22761 *htp = ht;
22762 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022764 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765}
22766
22767/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022768 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022769 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022771 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022772find_var_in_ht(
22773 hashtab_T *ht,
22774 int htname,
22775 char_u *varname,
22776 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022777{
Bram Moolenaar33570922005-01-25 22:26:29 +000022778 hashitem_T *hi;
22779
22780 if (*varname == NUL)
22781 {
22782 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022783 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022784 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022785 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022786 case 'g': return &globvars_var;
22787 case 'v': return &vimvars_var;
22788 case 'b': return &curbuf->b_bufvar;
22789 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022790#ifdef FEAT_WINDOWS
22791 case 't': return &curtab->tp_winvar;
22792#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022793 case 'l': return current_funccal == NULL
22794 ? NULL : &current_funccal->l_vars_var;
22795 case 'a': return current_funccal == NULL
22796 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022797 }
22798 return NULL;
22799 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022800
22801 hi = hash_find(ht, varname);
22802 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022803 {
22804 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022805 * worked find the variable again. Don't auto-load a script if it was
22806 * loaded already, otherwise it would be loaded every time when
22807 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022808 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022809 {
22810 /* Note: script_autoload() may make "hi" invalid. It must either
22811 * be obtained again or not used. */
22812 if (!script_autoload(varname, FALSE) || aborting())
22813 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022814 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022815 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022816 if (HASHITEM_EMPTY(hi))
22817 return NULL;
22818 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022819 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022820}
22821
22822/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022823 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022824 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022825 * Set "varname" to the start of name without ':'.
22826 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022827 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022828find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022830 hashitem_T *hi;
22831
Bram Moolenaar73627d02015-08-11 15:46:09 +020022832 if (name[0] == NUL)
22833 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834 if (name[1] != ':')
22835 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022836 /* The name must not start with a colon or #. */
22837 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 return NULL;
22839 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022840
22841 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022842 hi = hash_find(&compat_hashtab, name);
22843 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022844 return &compat_hashtab;
22845
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022847 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022848 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 }
22850 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022851 if (*name == 'g') /* global variable */
22852 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022853 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22854 */
22855 if (vim_strchr(name + 2, ':') != NULL
22856 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022857 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022859 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022861 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022862#ifdef FEAT_WINDOWS
22863 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022864 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022865#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022866 if (*name == 'v') /* v: variable */
22867 return &vimvarht;
22868 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022869 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022870 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022871 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 if (*name == 's' /* script variable */
22873 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22874 return &SCRIPT_VARS(current_SID);
22875 return NULL;
22876}
22877
22878/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022879 * Get function call environment based on bactrace debug level
22880 */
22881 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022882get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022883{
22884 int i;
22885 funccall_T *funccal;
22886 funccall_T *temp_funccal;
22887
22888 funccal = current_funccal;
22889 if (debug_backtrace_level > 0)
22890 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022891 for (i = 0; i < debug_backtrace_level; i++)
22892 {
22893 temp_funccal = funccal->caller;
22894 if (temp_funccal)
22895 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022896 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022897 /* backtrace level overflow. reset to max */
22898 debug_backtrace_level = i;
22899 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022900 }
22901 return funccal;
22902}
22903
22904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022906 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 * Returns NULL when it doesn't exist.
22908 */
22909 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022910get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911{
Bram Moolenaar33570922005-01-25 22:26:29 +000022912 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022914 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 if (v == NULL)
22916 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022917 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918}
22919
22920/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022921 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 * sourcing this script and when executing functions defined in the script.
22923 */
22924 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022925new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926{
Bram Moolenaara7043832005-01-21 11:56:39 +000022927 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022928 hashtab_T *ht;
22929 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022930
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22932 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022933 /* Re-allocating ga_data means that an ht_array pointing to
22934 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022935 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022936 for (i = 1; i <= ga_scripts.ga_len; ++i)
22937 {
22938 ht = &SCRIPT_VARS(i);
22939 if (ht->ht_mask == HT_INIT_SIZE - 1)
22940 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022941 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022942 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022943 }
22944
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945 while (ga_scripts.ga_len < id)
22946 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022947 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022948 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022949 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 }
22952 }
22953}
22954
22955/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022956 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22957 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958 */
22959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022960init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961{
Bram Moolenaar33570922005-01-25 22:26:29 +000022962 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022963 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022964 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022965 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022966 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022967 dict_var->di_tv.vval.v_dict = dict;
22968 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022969 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022970 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22971 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972}
22973
22974/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022975 * Unreference a dictionary initialized by init_var_dict().
22976 */
22977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022978unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022979{
22980 /* Now the dict needs to be freed if no one else is using it, go back to
22981 * normal reference counting. */
22982 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22983 dict_unref(dict);
22984}
22985
22986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022988 * Frees all allocated variables and the value they contain.
22989 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990 */
22991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022992vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022993{
22994 vars_clear_ext(ht, TRUE);
22995}
22996
22997/*
22998 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22999 */
23000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023001vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002{
Bram Moolenaara7043832005-01-21 11:56:39 +000023003 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023004 hashitem_T *hi;
23005 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006
Bram Moolenaar33570922005-01-25 22:26:29 +000023007 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023008 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023009 for (hi = ht->ht_array; todo > 0; ++hi)
23010 {
23011 if (!HASHITEM_EMPTY(hi))
23012 {
23013 --todo;
23014
Bram Moolenaar33570922005-01-25 22:26:29 +000023015 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023016 * ht_array might change then. hash_clear() takes care of it
23017 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023018 v = HI2DI(hi);
23019 if (free_val)
23020 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023021 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023022 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023023 }
23024 }
23025 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023026 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027}
23028
Bram Moolenaara7043832005-01-21 11:56:39 +000023029/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023030 * Delete a variable from hashtab "ht" at item "hi".
23031 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023034delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035{
Bram Moolenaar33570922005-01-25 22:26:29 +000023036 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023037
23038 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023039 clear_tv(&di->di_tv);
23040 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041}
23042
23043/*
23044 * List the value of one internal variable.
23045 */
23046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023047list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023048{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023049 char_u *tofree;
23050 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023051 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023052
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023053 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023054 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023055 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023056 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057}
23058
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023060list_one_var_a(
23061 char_u *prefix,
23062 char_u *name,
23063 int type,
23064 char_u *string,
23065 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066{
Bram Moolenaar31859182007-08-14 20:41:13 +000023067 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23068 msg_start();
23069 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 if (name != NULL) /* "a:" vars don't have a name stored */
23071 msg_puts(name);
23072 msg_putchar(' ');
23073 msg_advance(22);
23074 if (type == VAR_NUMBER)
23075 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023076 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023077 msg_putchar('*');
23078 else if (type == VAR_LIST)
23079 {
23080 msg_putchar('[');
23081 if (*string == '[')
23082 ++string;
23083 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023084 else if (type == VAR_DICT)
23085 {
23086 msg_putchar('{');
23087 if (*string == '{')
23088 ++string;
23089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 else
23091 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023092
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023094
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023095 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023096 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023097 if (*first)
23098 {
23099 msg_clr_eos();
23100 *first = FALSE;
23101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102}
23103
23104/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023105 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106 * If the variable already exists, the value is updated.
23107 * Otherwise the variable is created.
23108 */
23109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023110set_var(
23111 char_u *name,
23112 typval_T *tv,
23113 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114{
Bram Moolenaar33570922005-01-25 22:26:29 +000023115 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023117 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023119 ht = find_var_ht(name, &varname);
23120 if (ht == NULL || *varname == NUL)
23121 {
23122 EMSG2(_(e_illvar), name);
23123 return;
23124 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023125 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023126
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023127 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23128 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023129 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023130
Bram Moolenaar33570922005-01-25 22:26:29 +000023131 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023133 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023134 if (var_check_ro(v->di_flags, name, FALSE)
23135 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023136 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023137
23138 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023139 * Handle setting internal v: variables separately where needed to
23140 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023141 */
23142 if (ht == &vimvarht)
23143 {
23144 if (v->di_tv.v_type == VAR_STRING)
23145 {
23146 vim_free(v->di_tv.vval.v_string);
23147 if (copy || tv->v_type != VAR_STRING)
23148 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23149 else
23150 {
23151 /* Take over the string to avoid an extra alloc/free. */
23152 v->di_tv.vval.v_string = tv->vval.v_string;
23153 tv->vval.v_string = NULL;
23154 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023155 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023156 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023157 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023158 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023159 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023160 if (STRCMP(varname, "searchforward") == 0)
23161 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023162#ifdef FEAT_SEARCH_EXTRA
23163 else if (STRCMP(varname, "hlsearch") == 0)
23164 {
23165 no_hlsearch = !v->di_tv.vval.v_number;
23166 redraw_all_later(SOME_VALID);
23167 }
23168#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023169 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023170 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023171 else if (v->di_tv.v_type != tv->v_type)
23172 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023173 }
23174
23175 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 }
23177 else /* add a new variable */
23178 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023179 /* Can't add "v:" variable. */
23180 if (ht == &vimvarht)
23181 {
23182 EMSG2(_(e_illvar), name);
23183 return;
23184 }
23185
Bram Moolenaar92124a32005-06-17 22:03:40 +000023186 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023187 if (!valid_varname(varname))
23188 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023189
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023190 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23191 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023192 if (v == NULL)
23193 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023194 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023195 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023197 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 return;
23199 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023200 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023202
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023203 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023204 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023205 else
23206 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023207 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023208 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023209 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211}
23212
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023213/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023214 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023215 * Also give an error message.
23216 */
23217 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023218var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023219{
23220 if (flags & DI_FLAGS_RO)
23221 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023222 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023223 return TRUE;
23224 }
23225 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23226 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023227 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023228 return TRUE;
23229 }
23230 return FALSE;
23231}
23232
23233/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023234 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23235 * Also give an error message.
23236 */
23237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023238var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023239{
23240 if (flags & DI_FLAGS_FIX)
23241 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023242 EMSG2(_("E795: Cannot delete variable %s"),
23243 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023244 return TRUE;
23245 }
23246 return FALSE;
23247}
23248
23249/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023250 * Check if a funcref is assigned to a valid variable name.
23251 * Return TRUE and give an error if not.
23252 */
23253 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023254var_check_func_name(
23255 char_u *name, /* points to start of variable name */
23256 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023257{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023258 /* Allow for w: b: s: and t:. */
23259 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023260 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23261 ? name[2] : name[0]))
23262 {
23263 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23264 name);
23265 return TRUE;
23266 }
23267 /* Don't allow hiding a function. When "v" is not NULL we might be
23268 * assigning another function to the same var, the type is checked
23269 * below. */
23270 if (new_var && function_exists(name))
23271 {
23272 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23273 name);
23274 return TRUE;
23275 }
23276 return FALSE;
23277}
23278
23279/*
23280 * Check if a variable name is valid.
23281 * Return FALSE and give an error if not.
23282 */
23283 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023284valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023285{
23286 char_u *p;
23287
23288 for (p = varname; *p != NUL; ++p)
23289 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23290 && *p != AUTOLOAD_CHAR)
23291 {
23292 EMSG2(_(e_illvar), varname);
23293 return FALSE;
23294 }
23295 return TRUE;
23296}
23297
23298/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023299 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023300 * Also give an error message, using "name" or _("name") when use_gettext is
23301 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023302 */
23303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023304tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023305{
23306 if (lock & VAR_LOCKED)
23307 {
23308 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023309 name == NULL ? (char_u *)_("Unknown")
23310 : use_gettext ? (char_u *)_(name)
23311 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023312 return TRUE;
23313 }
23314 if (lock & VAR_FIXED)
23315 {
23316 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023317 name == NULL ? (char_u *)_("Unknown")
23318 : use_gettext ? (char_u *)_(name)
23319 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023320 return TRUE;
23321 }
23322 return FALSE;
23323}
23324
23325/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023326 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023327 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023328 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023329 * It is OK for "from" and "to" to point to the same item. This is used to
23330 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023331 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023332 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023333copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023334{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023335 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023336 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023337 switch (from->v_type)
23338 {
23339 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023340 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023341 to->vval.v_number = from->vval.v_number;
23342 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023343 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023344#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023345 to->vval.v_float = from->vval.v_float;
23346 break;
23347#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023348 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023349#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023350 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023351 if (to->vval.v_job != NULL)
23352 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023353 break;
23354#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023355 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023356#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023357 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023358 if (to->vval.v_channel != NULL)
23359 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023360 break;
23361#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023362 case VAR_STRING:
23363 case VAR_FUNC:
23364 if (from->vval.v_string == NULL)
23365 to->vval.v_string = NULL;
23366 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023367 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023368 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023369 if (from->v_type == VAR_FUNC)
23370 func_ref(to->vval.v_string);
23371 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023372 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023373 case VAR_PARTIAL:
23374 if (from->vval.v_partial == NULL)
23375 to->vval.v_partial = NULL;
23376 else
23377 {
23378 to->vval.v_partial = from->vval.v_partial;
23379 ++to->vval.v_partial->pt_refcount;
23380 }
23381 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023382 case VAR_LIST:
23383 if (from->vval.v_list == NULL)
23384 to->vval.v_list = NULL;
23385 else
23386 {
23387 to->vval.v_list = from->vval.v_list;
23388 ++to->vval.v_list->lv_refcount;
23389 }
23390 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023391 case VAR_DICT:
23392 if (from->vval.v_dict == NULL)
23393 to->vval.v_dict = NULL;
23394 else
23395 {
23396 to->vval.v_dict = from->vval.v_dict;
23397 ++to->vval.v_dict->dv_refcount;
23398 }
23399 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023400 case VAR_UNKNOWN:
23401 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023402 break;
23403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404}
23405
23406/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023407 * Make a copy of an item.
23408 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023409 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23410 * reference to an already copied list/dict can be used.
23411 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023412 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023413 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023414item_copy(
23415 typval_T *from,
23416 typval_T *to,
23417 int deep,
23418 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023419{
23420 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023421 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023422
Bram Moolenaar33570922005-01-25 22:26:29 +000023423 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023424 {
23425 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023426 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023427 }
23428 ++recurse;
23429
23430 switch (from->v_type)
23431 {
23432 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023433 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023434 case VAR_STRING:
23435 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023436 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023437 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023438 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023439 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023440 copy_tv(from, to);
23441 break;
23442 case VAR_LIST:
23443 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023444 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023445 if (from->vval.v_list == NULL)
23446 to->vval.v_list = NULL;
23447 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23448 {
23449 /* use the copy made earlier */
23450 to->vval.v_list = from->vval.v_list->lv_copylist;
23451 ++to->vval.v_list->lv_refcount;
23452 }
23453 else
23454 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23455 if (to->vval.v_list == NULL)
23456 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023457 break;
23458 case VAR_DICT:
23459 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023460 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023461 if (from->vval.v_dict == NULL)
23462 to->vval.v_dict = NULL;
23463 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23464 {
23465 /* use the copy made earlier */
23466 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23467 ++to->vval.v_dict->dv_refcount;
23468 }
23469 else
23470 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23471 if (to->vval.v_dict == NULL)
23472 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023473 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023474 case VAR_UNKNOWN:
23475 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023476 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023477 }
23478 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023479 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023480}
23481
23482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 * ":echo expr1 ..." print each argument separated with a space, add a
23484 * newline at the end.
23485 * ":echon expr1 ..." print each argument plain.
23486 */
23487 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023488ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489{
23490 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023491 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023492 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493 char_u *p;
23494 int needclr = TRUE;
23495 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023496 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497
23498 if (eap->skip)
23499 ++emsg_skip;
23500 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23501 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023502 /* If eval1() causes an error message the text from the command may
23503 * still need to be cleared. E.g., "echo 22,44". */
23504 need_clr_eos = needclr;
23505
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023507 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508 {
23509 /*
23510 * Report the invalid expression unless the expression evaluation
23511 * has been cancelled due to an aborting error, an interrupt, or an
23512 * exception.
23513 */
23514 if (!aborting())
23515 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023516 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 break;
23518 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023519 need_clr_eos = FALSE;
23520
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 if (!eap->skip)
23522 {
23523 if (atstart)
23524 {
23525 atstart = FALSE;
23526 /* Call msg_start() after eval1(), evaluating the expression
23527 * may cause a message to appear. */
23528 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023529 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023530 /* Mark the saved text as finishing the line, so that what
23531 * follows is displayed on a new line when scrolling back
23532 * at the more prompt. */
23533 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 }
23537 else if (eap->cmdidx == CMD_echo)
23538 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023539 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023540 if (p != NULL)
23541 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023543 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023545 if (*p != TAB && needclr)
23546 {
23547 /* remove any text still there from the command */
23548 msg_clr_eos();
23549 needclr = FALSE;
23550 }
23551 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 }
23553 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023554 {
23555#ifdef FEAT_MBYTE
23556 if (has_mbyte)
23557 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023558 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023559
23560 (void)msg_outtrans_len_attr(p, i, echo_attr);
23561 p += i - 1;
23562 }
23563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023565 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023568 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023570 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571 arg = skipwhite(arg);
23572 }
23573 eap->nextcmd = check_nextcmd(arg);
23574
23575 if (eap->skip)
23576 --emsg_skip;
23577 else
23578 {
23579 /* remove text that may still be there from the command */
23580 if (needclr)
23581 msg_clr_eos();
23582 if (eap->cmdidx == CMD_echo)
23583 msg_end();
23584 }
23585}
23586
23587/*
23588 * ":echohl {name}".
23589 */
23590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023591ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592{
23593 int id;
23594
23595 id = syn_name2id(eap->arg);
23596 if (id == 0)
23597 echo_attr = 0;
23598 else
23599 echo_attr = syn_id2attr(id);
23600}
23601
23602/*
23603 * ":execute expr1 ..." execute the result of an expression.
23604 * ":echomsg expr1 ..." Print a message
23605 * ":echoerr expr1 ..." Print an error
23606 * Each gets spaces around each argument and a newline at the end for
23607 * echo commands
23608 */
23609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023610ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611{
23612 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023613 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 int ret = OK;
23615 char_u *p;
23616 garray_T ga;
23617 int len;
23618 int save_did_emsg;
23619
23620 ga_init2(&ga, 1, 80);
23621
23622 if (eap->skip)
23623 ++emsg_skip;
23624 while (*arg != NUL && *arg != '|' && *arg != '\n')
23625 {
23626 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023627 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 {
23629 /*
23630 * Report the invalid expression unless the expression evaluation
23631 * has been cancelled due to an aborting error, an interrupt, or an
23632 * exception.
23633 */
23634 if (!aborting())
23635 EMSG2(_(e_invexpr2), p);
23636 ret = FAIL;
23637 break;
23638 }
23639
23640 if (!eap->skip)
23641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023642 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 len = (int)STRLEN(p);
23644 if (ga_grow(&ga, len + 2) == FAIL)
23645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023646 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 ret = FAIL;
23648 break;
23649 }
23650 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 ga.ga_len += len;
23654 }
23655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023656 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 arg = skipwhite(arg);
23658 }
23659
23660 if (ret != FAIL && ga.ga_data != NULL)
23661 {
23662 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023663 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023665 out_flush();
23666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 else if (eap->cmdidx == CMD_echoerr)
23668 {
23669 /* We don't want to abort following commands, restore did_emsg. */
23670 save_did_emsg = did_emsg;
23671 EMSG((char_u *)ga.ga_data);
23672 if (!force_abort)
23673 did_emsg = save_did_emsg;
23674 }
23675 else if (eap->cmdidx == CMD_execute)
23676 do_cmdline((char_u *)ga.ga_data,
23677 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23678 }
23679
23680 ga_clear(&ga);
23681
23682 if (eap->skip)
23683 --emsg_skip;
23684
23685 eap->nextcmd = check_nextcmd(arg);
23686}
23687
23688/*
23689 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23690 * "arg" points to the "&" or '+' when called, to "option" when returning.
23691 * Returns NULL when no option name found. Otherwise pointer to the char
23692 * after the option name.
23693 */
23694 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023695find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696{
23697 char_u *p = *arg;
23698
23699 ++p;
23700 if (*p == 'g' && p[1] == ':')
23701 {
23702 *opt_flags = OPT_GLOBAL;
23703 p += 2;
23704 }
23705 else if (*p == 'l' && p[1] == ':')
23706 {
23707 *opt_flags = OPT_LOCAL;
23708 p += 2;
23709 }
23710 else
23711 *opt_flags = 0;
23712
23713 if (!ASCII_ISALPHA(*p))
23714 return NULL;
23715 *arg = p;
23716
23717 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23718 p += 4; /* termcap option */
23719 else
23720 while (ASCII_ISALPHA(*p))
23721 ++p;
23722 return p;
23723}
23724
23725/*
23726 * ":function"
23727 */
23728 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023729ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730{
23731 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023732 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733 int j;
23734 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023736 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737 char_u *name = NULL;
23738 char_u *p;
23739 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023740 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741 garray_T newargs;
23742 garray_T newlines;
23743 int varargs = FALSE;
23744 int mustend = FALSE;
23745 int flags = 0;
23746 ufunc_T *fp;
23747 int indent;
23748 int nesting;
23749 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023750 dictitem_T *v;
23751 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023752 static int func_nr = 0; /* number for nameless function */
23753 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023754 hashtab_T *ht;
23755 int todo;
23756 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023757 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023758
23759 /*
23760 * ":function" without argument: list functions.
23761 */
23762 if (ends_excmd(*eap->arg))
23763 {
23764 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023765 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023766 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023767 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023768 {
23769 if (!HASHITEM_EMPTY(hi))
23770 {
23771 --todo;
23772 fp = HI2UF(hi);
23773 if (!isdigit(*fp->uf_name))
23774 list_func_head(fp, FALSE);
23775 }
23776 }
23777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023778 eap->nextcmd = check_nextcmd(eap->arg);
23779 return;
23780 }
23781
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023782 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023783 * ":function /pat": list functions matching pattern.
23784 */
23785 if (*eap->arg == '/')
23786 {
23787 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23788 if (!eap->skip)
23789 {
23790 regmatch_T regmatch;
23791
23792 c = *p;
23793 *p = NUL;
23794 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23795 *p = c;
23796 if (regmatch.regprog != NULL)
23797 {
23798 regmatch.rm_ic = p_ic;
23799
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023800 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023801 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23802 {
23803 if (!HASHITEM_EMPTY(hi))
23804 {
23805 --todo;
23806 fp = HI2UF(hi);
23807 if (!isdigit(*fp->uf_name)
23808 && vim_regexec(&regmatch, fp->uf_name, 0))
23809 list_func_head(fp, FALSE);
23810 }
23811 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023812 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023813 }
23814 }
23815 if (*p == '/')
23816 ++p;
23817 eap->nextcmd = check_nextcmd(p);
23818 return;
23819 }
23820
23821 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023822 * Get the function name. There are these situations:
23823 * func normal function name
23824 * "name" == func, "fudi.fd_dict" == NULL
23825 * dict.func new dictionary entry
23826 * "name" == NULL, "fudi.fd_dict" set,
23827 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23828 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023829 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023830 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23831 * dict.func existing dict entry that's not a Funcref
23832 * "name" == NULL, "fudi.fd_dict" set,
23833 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023834 * s:func script-local function name
23835 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023838 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023839 paren = (vim_strchr(p, '(') != NULL);
23840 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841 {
23842 /*
23843 * Return on an invalid expression in braces, unless the expression
23844 * evaluation has been cancelled due to an aborting error, an
23845 * interrupt, or an exception.
23846 */
23847 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023848 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023849 if (!eap->skip && fudi.fd_newkey != NULL)
23850 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023851 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 else
23855 eap->skip = TRUE;
23856 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023857
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858 /* An error in a function call during evaluation of an expression in magic
23859 * braces should not cause the function not to be defined. */
23860 saved_did_emsg = did_emsg;
23861 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862
23863 /*
23864 * ":function func" with only function name: list function.
23865 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023866 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867 {
23868 if (!ends_excmd(*skipwhite(p)))
23869 {
23870 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023871 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 }
23873 eap->nextcmd = check_nextcmd(p);
23874 if (eap->nextcmd != NULL)
23875 *p = NUL;
23876 if (!eap->skip && !got_int)
23877 {
23878 fp = find_func(name);
23879 if (fp != NULL)
23880 {
23881 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023882 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023884 if (FUNCLINE(fp, j) == NULL)
23885 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 msg_putchar('\n');
23887 msg_outnum((long)(j + 1));
23888 if (j < 9)
23889 msg_putchar(' ');
23890 if (j < 99)
23891 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023892 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893 out_flush(); /* show a line at a time */
23894 ui_breakcheck();
23895 }
23896 if (!got_int)
23897 {
23898 msg_putchar('\n');
23899 msg_puts((char_u *)" endfunction");
23900 }
23901 }
23902 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023903 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023905 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 }
23907
23908 /*
23909 * ":function name(arg1, arg2)" Define function.
23910 */
23911 p = skipwhite(p);
23912 if (*p != '(')
23913 {
23914 if (!eap->skip)
23915 {
23916 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023917 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918 }
23919 /* attempt to continue by skipping some text */
23920 if (vim_strchr(p, '(') != NULL)
23921 p = vim_strchr(p, '(');
23922 }
23923 p = skipwhite(p + 1);
23924
23925 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23926 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23927
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023928 if (!eap->skip)
23929 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023930 /* Check the name of the function. Unless it's a dictionary function
23931 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023932 if (name != NULL)
23933 arg = name;
23934 else
23935 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023936 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023937 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23938 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023939 {
23940 if (*arg == K_SPECIAL)
23941 j = 3;
23942 else
23943 j = 0;
23944 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23945 : eval_isnamec(arg[j])))
23946 ++j;
23947 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023948 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023949 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023950 /* Disallow using the g: dict. */
23951 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23952 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023953 }
23954
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 /*
23956 * Isolate the arguments: "arg1, arg2, ...)"
23957 */
23958 while (*p != ')')
23959 {
23960 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23961 {
23962 varargs = TRUE;
23963 p += 3;
23964 mustend = TRUE;
23965 }
23966 else
23967 {
23968 arg = p;
23969 while (ASCII_ISALNUM(*p) || *p == '_')
23970 ++p;
23971 if (arg == p || isdigit(*arg)
23972 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23973 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23974 {
23975 if (!eap->skip)
23976 EMSG2(_("E125: Illegal argument: %s"), arg);
23977 break;
23978 }
23979 if (ga_grow(&newargs, 1) == FAIL)
23980 goto erret;
23981 c = *p;
23982 *p = NUL;
23983 arg = vim_strsave(arg);
23984 if (arg == NULL)
23985 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023986
23987 /* Check for duplicate argument name. */
23988 for (i = 0; i < newargs.ga_len; ++i)
23989 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23990 {
23991 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023992 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023993 goto erret;
23994 }
23995
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23997 *p = c;
23998 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 if (*p == ',')
24000 ++p;
24001 else
24002 mustend = TRUE;
24003 }
24004 p = skipwhite(p);
24005 if (mustend && *p != ')')
24006 {
24007 if (!eap->skip)
24008 EMSG2(_(e_invarg2), eap->arg);
24009 break;
24010 }
24011 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024012 if (*p != ')')
24013 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 ++p; /* skip the ')' */
24015
Bram Moolenaare9a41262005-01-15 22:18:47 +000024016 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024017 for (;;)
24018 {
24019 p = skipwhite(p);
24020 if (STRNCMP(p, "range", 5) == 0)
24021 {
24022 flags |= FC_RANGE;
24023 p += 5;
24024 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024025 else if (STRNCMP(p, "dict", 4) == 0)
24026 {
24027 flags |= FC_DICT;
24028 p += 4;
24029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030 else if (STRNCMP(p, "abort", 5) == 0)
24031 {
24032 flags |= FC_ABORT;
24033 p += 5;
24034 }
24035 else
24036 break;
24037 }
24038
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024039 /* When there is a line break use what follows for the function body.
24040 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24041 if (*p == '\n')
24042 line_arg = p + 1;
24043 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044 EMSG(_(e_trailing));
24045
24046 /*
24047 * Read the body of the function, until ":endfunction" is found.
24048 */
24049 if (KeyTyped)
24050 {
24051 /* Check if the function already exists, don't let the user type the
24052 * whole function before telling him it doesn't work! For a script we
24053 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024054 if (!eap->skip && !eap->forceit)
24055 {
24056 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24057 EMSG(_(e_funcdict));
24058 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024059 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024062 if (!eap->skip && did_emsg)
24063 goto erret;
24064
Bram Moolenaar071d4272004-06-13 20:20:40 +000024065 msg_putchar('\n'); /* don't overwrite the function name */
24066 cmdline_row = msg_row;
24067 }
24068
24069 indent = 2;
24070 nesting = 0;
24071 for (;;)
24072 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024073 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024074 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024075 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024076 saved_wait_return = FALSE;
24077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024079 sourcing_lnum_off = sourcing_lnum;
24080
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024081 if (line_arg != NULL)
24082 {
24083 /* Use eap->arg, split up in parts by line breaks. */
24084 theline = line_arg;
24085 p = vim_strchr(theline, '\n');
24086 if (p == NULL)
24087 line_arg += STRLEN(line_arg);
24088 else
24089 {
24090 *p = NUL;
24091 line_arg = p + 1;
24092 }
24093 }
24094 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095 theline = getcmdline(':', 0L, indent);
24096 else
24097 theline = eap->getline(':', eap->cookie, indent);
24098 if (KeyTyped)
24099 lines_left = Rows - 1;
24100 if (theline == NULL)
24101 {
24102 EMSG(_("E126: Missing :endfunction"));
24103 goto erret;
24104 }
24105
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024106 /* Detect line continuation: sourcing_lnum increased more than one. */
24107 if (sourcing_lnum > sourcing_lnum_off + 1)
24108 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24109 else
24110 sourcing_lnum_off = 0;
24111
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 if (skip_until != NULL)
24113 {
24114 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24115 * don't check for ":endfunc". */
24116 if (STRCMP(theline, skip_until) == 0)
24117 {
24118 vim_free(skip_until);
24119 skip_until = NULL;
24120 }
24121 }
24122 else
24123 {
24124 /* skip ':' and blanks*/
24125 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24126 ;
24127
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024128 /* Check for "endfunction". */
24129 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024131 if (line_arg == NULL)
24132 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 break;
24134 }
24135
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024136 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137 * at "end". */
24138 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24139 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024140 else if (STRNCMP(p, "if", 2) == 0
24141 || STRNCMP(p, "wh", 2) == 0
24142 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 || STRNCMP(p, "try", 3) == 0)
24144 indent += 2;
24145
24146 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024147 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024149 if (*p == '!')
24150 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024152 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024153 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024155 ++nesting;
24156 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 }
24158 }
24159
24160 /* Check for ":append" or ":insert". */
24161 p = skip_range(p, NULL);
24162 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24163 || (p[0] == 'i'
24164 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24165 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24166 skip_until = vim_strsave((char_u *)".");
24167
24168 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24169 arg = skipwhite(skiptowhite(p));
24170 if (arg[0] == '<' && arg[1] =='<'
24171 && ((p[0] == 'p' && p[1] == 'y'
24172 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24173 || (p[0] == 'p' && p[1] == 'e'
24174 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24175 || (p[0] == 't' && p[1] == 'c'
24176 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024177 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24178 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24180 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024181 || (p[0] == 'm' && p[1] == 'z'
24182 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 ))
24184 {
24185 /* ":python <<" continues until a dot, like ":append" */
24186 p = skipwhite(arg + 2);
24187 if (*p == NUL)
24188 skip_until = vim_strsave((char_u *)".");
24189 else
24190 skip_until = vim_strsave(p);
24191 }
24192 }
24193
24194 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024195 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024196 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024197 if (line_arg == NULL)
24198 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024200 }
24201
24202 /* Copy the line to newly allocated memory. get_one_sourceline()
24203 * allocates 250 bytes per line, this saves 80% on average. The cost
24204 * is an extra alloc/free. */
24205 p = vim_strsave(theline);
24206 if (p != NULL)
24207 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024208 if (line_arg == NULL)
24209 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024210 theline = p;
24211 }
24212
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024213 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24214
24215 /* Add NULL lines for continuation lines, so that the line count is
24216 * equal to the index in the growarray. */
24217 while (sourcing_lnum_off-- > 0)
24218 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024219
24220 /* Check for end of eap->arg. */
24221 if (line_arg != NULL && *line_arg == NUL)
24222 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223 }
24224
24225 /* Don't define the function when skipping commands or when an error was
24226 * detected. */
24227 if (eap->skip || did_emsg)
24228 goto erret;
24229
24230 /*
24231 * If there are no errors, add the function
24232 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024233 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024234 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024235 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024236 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024237 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024238 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024239 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024240 goto erret;
24241 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024242
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024243 fp = find_func(name);
24244 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024246 if (!eap->forceit)
24247 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024248 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024249 goto erret;
24250 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024251 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024252 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024253 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024254 name);
24255 goto erret;
24256 }
24257 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024258 ga_clear_strings(&(fp->uf_args));
24259 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024260 vim_free(name);
24261 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 }
24264 else
24265 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024266 char numbuf[20];
24267
24268 fp = NULL;
24269 if (fudi.fd_newkey == NULL && !eap->forceit)
24270 {
24271 EMSG(_(e_funcdict));
24272 goto erret;
24273 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024274 if (fudi.fd_di == NULL)
24275 {
24276 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024277 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024278 goto erret;
24279 }
24280 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024281 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024282 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024283
24284 /* Give the function a sequential number. Can only be used with a
24285 * Funcref! */
24286 vim_free(name);
24287 sprintf(numbuf, "%d", ++func_nr);
24288 name = vim_strsave((char_u *)numbuf);
24289 if (name == NULL)
24290 goto erret;
24291 }
24292
24293 if (fp == NULL)
24294 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024295 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024296 {
24297 int slen, plen;
24298 char_u *scriptname;
24299
24300 /* Check that the autoload name matches the script name. */
24301 j = FAIL;
24302 if (sourcing_name != NULL)
24303 {
24304 scriptname = autoload_name(name);
24305 if (scriptname != NULL)
24306 {
24307 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024308 plen = (int)STRLEN(p);
24309 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024310 if (slen > plen && fnamecmp(p,
24311 sourcing_name + slen - plen) == 0)
24312 j = OK;
24313 vim_free(scriptname);
24314 }
24315 }
24316 if (j == FAIL)
24317 {
24318 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24319 goto erret;
24320 }
24321 }
24322
24323 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324 if (fp == NULL)
24325 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024326
24327 if (fudi.fd_dict != NULL)
24328 {
24329 if (fudi.fd_di == NULL)
24330 {
24331 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024332 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024333 if (fudi.fd_di == NULL)
24334 {
24335 vim_free(fp);
24336 goto erret;
24337 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024338 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24339 {
24340 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024341 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024342 goto erret;
24343 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024344 }
24345 else
24346 /* overwrite existing dict entry */
24347 clear_tv(&fudi.fd_di->di_tv);
24348 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024349 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024350 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024351 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024352
24353 /* behave like "dict" was used */
24354 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024355 }
24356
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024358 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024359 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24360 {
24361 vim_free(fp);
24362 goto erret;
24363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024365 fp->uf_args = newargs;
24366 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024367#ifdef FEAT_PROFILE
24368 fp->uf_tml_count = NULL;
24369 fp->uf_tml_total = NULL;
24370 fp->uf_tml_self = NULL;
24371 fp->uf_profiling = FALSE;
24372 if (prof_def_func())
24373 func_do_profile(fp);
24374#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024375 fp->uf_varargs = varargs;
24376 fp->uf_flags = flags;
24377 fp->uf_calls = 0;
24378 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024379 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380
24381erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024382 ga_clear_strings(&newargs);
24383 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024384ret_free:
24385 vim_free(skip_until);
24386 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024389 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024390}
24391
24392/*
24393 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024394 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024396 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024397 * TFN_INT: internal function name OK
24398 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024399 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400 * Advances "pp" to just after the function name (if no error).
24401 */
24402 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024403trans_function_name(
24404 char_u **pp,
24405 int skip, /* only find the end, don't evaluate */
24406 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024407 funcdict_T *fdp, /* return: info about dictionary used */
24408 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024410 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024411 char_u *start;
24412 char_u *end;
24413 int lead;
24414 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024415 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024416 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024417
24418 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024419 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024420 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024421
24422 /* Check for hard coded <SNR>: already translated function ID (from a user
24423 * command). */
24424 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24425 && (*pp)[2] == (int)KE_SNR)
24426 {
24427 *pp += 3;
24428 len = get_id_len(pp) + 3;
24429 return vim_strnsave(start, len);
24430 }
24431
24432 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24433 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024435 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024437
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024438 /* Note that TFN_ flags use the same values as GLV_ flags. */
24439 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024440 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024441 if (end == start)
24442 {
24443 if (!skip)
24444 EMSG(_("E129: Function name required"));
24445 goto theend;
24446 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024447 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024448 {
24449 /*
24450 * Report an invalid expression in braces, unless the expression
24451 * evaluation has been cancelled due to an aborting error, an
24452 * interrupt, or an exception.
24453 */
24454 if (!aborting())
24455 {
24456 if (end != NULL)
24457 EMSG2(_(e_invarg2), start);
24458 }
24459 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024460 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024461 goto theend;
24462 }
24463
24464 if (lv.ll_tv != NULL)
24465 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024466 if (fdp != NULL)
24467 {
24468 fdp->fd_dict = lv.ll_dict;
24469 fdp->fd_newkey = lv.ll_newkey;
24470 lv.ll_newkey = NULL;
24471 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024472 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024473 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24474 {
24475 name = vim_strsave(lv.ll_tv->vval.v_string);
24476 *pp = end;
24477 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024478 else if (lv.ll_tv->v_type == VAR_PARTIAL
24479 && lv.ll_tv->vval.v_partial != NULL)
24480 {
24481 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24482 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024483 if (partial != NULL)
24484 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024485 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024486 else
24487 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024488 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24489 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024490 EMSG(_(e_funcref));
24491 else
24492 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024493 name = NULL;
24494 }
24495 goto theend;
24496 }
24497
24498 if (lv.ll_name == NULL)
24499 {
24500 /* Error found, but continue after the function name. */
24501 *pp = end;
24502 goto theend;
24503 }
24504
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024505 /* Check if the name is a Funcref. If so, use the value. */
24506 if (lv.ll_exp_name != NULL)
24507 {
24508 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024509 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024510 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024511 if (name == lv.ll_exp_name)
24512 name = NULL;
24513 }
24514 else
24515 {
24516 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024517 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024518 if (name == *pp)
24519 name = NULL;
24520 }
24521 if (name != NULL)
24522 {
24523 name = vim_strsave(name);
24524 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024525 if (STRNCMP(name, "<SNR>", 5) == 0)
24526 {
24527 /* Change "<SNR>" to the byte sequence. */
24528 name[0] = K_SPECIAL;
24529 name[1] = KS_EXTRA;
24530 name[2] = (int)KE_SNR;
24531 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24532 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024533 goto theend;
24534 }
24535
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024536 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024537 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024538 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024539 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24540 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24541 {
24542 /* When there was "s:" already or the name expanded to get a
24543 * leading "s:" then remove it. */
24544 lv.ll_name += 2;
24545 len -= 2;
24546 lead = 2;
24547 }
24548 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024549 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024550 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024551 /* skip over "s:" and "g:" */
24552 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024553 lv.ll_name += 2;
24554 len = (int)(end - lv.ll_name);
24555 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024556
24557 /*
24558 * Copy the function name to allocated memory.
24559 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24560 * Accept <SNR>123_name() outside a script.
24561 */
24562 if (skip)
24563 lead = 0; /* do nothing */
24564 else if (lead > 0)
24565 {
24566 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024567 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24568 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024569 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024570 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024571 if (current_SID <= 0)
24572 {
24573 EMSG(_(e_usingsid));
24574 goto theend;
24575 }
24576 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24577 lead += (int)STRLEN(sid_buf);
24578 }
24579 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024580 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024581 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024582 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024583 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024584 goto theend;
24585 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024586 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024587 {
24588 char_u *cp = vim_strchr(lv.ll_name, ':');
24589
24590 if (cp != NULL && cp < end)
24591 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024592 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024593 goto theend;
24594 }
24595 }
24596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024597 name = alloc((unsigned)(len + lead + 1));
24598 if (name != NULL)
24599 {
24600 if (lead > 0)
24601 {
24602 name[0] = K_SPECIAL;
24603 name[1] = KS_EXTRA;
24604 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024605 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024606 STRCPY(name + 3, sid_buf);
24607 }
24608 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024609 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024610 }
24611 *pp = end;
24612
24613theend:
24614 clear_lval(&lv);
24615 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616}
24617
24618/*
24619 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24620 * Return 2 if "p" starts with "s:".
24621 * Return 0 otherwise.
24622 */
24623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024624eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024626 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24627 * the standard library function. */
24628 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24629 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 return 5;
24631 if (p[0] == 's' && p[1] == ':')
24632 return 2;
24633 return 0;
24634}
24635
24636/*
24637 * Return TRUE if "p" starts with "<SID>" or "s:".
24638 * Only works if eval_fname_script() returned non-zero for "p"!
24639 */
24640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024641eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642{
24643 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24644}
24645
24646/*
24647 * List the head of the function: "name(arg1, arg2)".
24648 */
24649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024650list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651{
24652 int j;
24653
24654 msg_start();
24655 if (indent)
24656 MSG_PUTS(" ");
24657 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024658 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 {
24660 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024661 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024662 }
24663 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024664 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024666 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 {
24668 if (j)
24669 MSG_PUTS(", ");
24670 msg_puts(FUNCARG(fp, j));
24671 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024672 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 {
24674 if (j)
24675 MSG_PUTS(", ");
24676 MSG_PUTS("...");
24677 }
24678 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024679 if (fp->uf_flags & FC_ABORT)
24680 MSG_PUTS(" abort");
24681 if (fp->uf_flags & FC_RANGE)
24682 MSG_PUTS(" range");
24683 if (fp->uf_flags & FC_DICT)
24684 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024685 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024686 if (p_verbose > 0)
24687 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024688}
24689
24690/*
24691 * Find a function by name, return pointer to it in ufuncs.
24692 * Return NULL for unknown function.
24693 */
24694 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024695find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024696{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024697 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024698
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024699 hi = hash_find(&func_hashtab, name);
24700 if (!HASHITEM_EMPTY(hi))
24701 return HI2UF(hi);
24702 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024703}
24704
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024705#if defined(EXITFREE) || defined(PROTO)
24706 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024707free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024708{
24709 hashitem_T *hi;
24710
24711 /* Need to start all over every time, because func_free() may change the
24712 * hash table. */
24713 while (func_hashtab.ht_used > 0)
24714 for (hi = func_hashtab.ht_array; ; ++hi)
24715 if (!HASHITEM_EMPTY(hi))
24716 {
24717 func_free(HI2UF(hi));
24718 break;
24719 }
24720}
24721#endif
24722
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024723 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024724translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024725{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024726 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024727 return find_internal_func(name) >= 0;
24728 return find_func(name) != NULL;
24729}
24730
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024731/*
24732 * Return TRUE if a function "name" exists.
24733 */
24734 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024735function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024736{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024737 char_u *nm = name;
24738 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024739 int n = FALSE;
24740
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024741 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024742 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024743 nm = skipwhite(nm);
24744
24745 /* Only accept "funcname", "funcname ", "funcname (..." and
24746 * "funcname(...", not "funcname!...". */
24747 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024748 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024749 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024750 return n;
24751}
24752
Bram Moolenaara1544c02013-05-30 12:35:52 +020024753 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024754get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024755{
24756 char_u *nm = name;
24757 char_u *p;
24758
Bram Moolenaar65639032016-03-16 21:40:30 +010024759 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024760
24761 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024762 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024763 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024764
Bram Moolenaara1544c02013-05-30 12:35:52 +020024765 vim_free(p);
24766 return NULL;
24767}
24768
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024769/*
24770 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024771 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24772 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024773 */
24774 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024775builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024776{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024777 char_u *p;
24778
24779 if (!ASCII_ISLOWER(name[0]))
24780 return FALSE;
24781 p = vim_strchr(name, AUTOLOAD_CHAR);
24782 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024783}
24784
Bram Moolenaar05159a02005-02-26 23:04:13 +000024785#if defined(FEAT_PROFILE) || defined(PROTO)
24786/*
24787 * Start profiling function "fp".
24788 */
24789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024790func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024791{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024792 int len = fp->uf_lines.ga_len;
24793
24794 if (len == 0)
24795 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024796 fp->uf_tm_count = 0;
24797 profile_zero(&fp->uf_tm_self);
24798 profile_zero(&fp->uf_tm_total);
24799 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024800 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024801 if (fp->uf_tml_total == NULL)
24802 fp->uf_tml_total = (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 if (fp->uf_tml_self == NULL)
24805 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024806 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024807 fp->uf_tml_idx = -1;
24808 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24809 || fp->uf_tml_self == NULL)
24810 return; /* out of memory */
24811
24812 fp->uf_profiling = TRUE;
24813}
24814
24815/*
24816 * Dump the profiling results for all functions in file "fd".
24817 */
24818 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024819func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024820{
24821 hashitem_T *hi;
24822 int todo;
24823 ufunc_T *fp;
24824 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024825 ufunc_T **sorttab;
24826 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024827
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024828 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024829 if (todo == 0)
24830 return; /* nothing to dump */
24831
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024832 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024833
Bram Moolenaar05159a02005-02-26 23:04:13 +000024834 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24835 {
24836 if (!HASHITEM_EMPTY(hi))
24837 {
24838 --todo;
24839 fp = HI2UF(hi);
24840 if (fp->uf_profiling)
24841 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024842 if (sorttab != NULL)
24843 sorttab[st_len++] = fp;
24844
Bram Moolenaar05159a02005-02-26 23:04:13 +000024845 if (fp->uf_name[0] == K_SPECIAL)
24846 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24847 else
24848 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24849 if (fp->uf_tm_count == 1)
24850 fprintf(fd, "Called 1 time\n");
24851 else
24852 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24853 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24854 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24855 fprintf(fd, "\n");
24856 fprintf(fd, "count total (s) self (s)\n");
24857
24858 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24859 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024860 if (FUNCLINE(fp, i) == NULL)
24861 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024862 prof_func_line(fd, fp->uf_tml_count[i],
24863 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024864 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24865 }
24866 fprintf(fd, "\n");
24867 }
24868 }
24869 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024870
24871 if (sorttab != NULL && st_len > 0)
24872 {
24873 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24874 prof_total_cmp);
24875 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24876 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24877 prof_self_cmp);
24878 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24879 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024880
24881 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024882}
Bram Moolenaar73830342005-02-28 22:48:19 +000024883
24884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024885prof_sort_list(
24886 FILE *fd,
24887 ufunc_T **sorttab,
24888 int st_len,
24889 char *title,
24890 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024891{
24892 int i;
24893 ufunc_T *fp;
24894
24895 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24896 fprintf(fd, "count total (s) self (s) function\n");
24897 for (i = 0; i < 20 && i < st_len; ++i)
24898 {
24899 fp = sorttab[i];
24900 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24901 prefer_self);
24902 if (fp->uf_name[0] == K_SPECIAL)
24903 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24904 else
24905 fprintf(fd, " %s()\n", fp->uf_name);
24906 }
24907 fprintf(fd, "\n");
24908}
24909
24910/*
24911 * Print the count and times for one function or function line.
24912 */
24913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024914prof_func_line(
24915 FILE *fd,
24916 int count,
24917 proftime_T *total,
24918 proftime_T *self,
24919 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024920{
24921 if (count > 0)
24922 {
24923 fprintf(fd, "%5d ", count);
24924 if (prefer_self && profile_equal(total, self))
24925 fprintf(fd, " ");
24926 else
24927 fprintf(fd, "%s ", profile_msg(total));
24928 if (!prefer_self && profile_equal(total, self))
24929 fprintf(fd, " ");
24930 else
24931 fprintf(fd, "%s ", profile_msg(self));
24932 }
24933 else
24934 fprintf(fd, " ");
24935}
24936
24937/*
24938 * Compare function for total time sorting.
24939 */
24940 static int
24941#ifdef __BORLANDC__
24942_RTLENTRYF
24943#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024944prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024945{
24946 ufunc_T *p1, *p2;
24947
24948 p1 = *(ufunc_T **)s1;
24949 p2 = *(ufunc_T **)s2;
24950 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24951}
24952
24953/*
24954 * Compare function for self time sorting.
24955 */
24956 static int
24957#ifdef __BORLANDC__
24958_RTLENTRYF
24959#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024960prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024961{
24962 ufunc_T *p1, *p2;
24963
24964 p1 = *(ufunc_T **)s1;
24965 p2 = *(ufunc_T **)s2;
24966 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24967}
24968
Bram Moolenaar05159a02005-02-26 23:04:13 +000024969#endif
24970
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024971/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024972 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024973 * Return TRUE if a package was loaded.
24974 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024976script_autoload(
24977 char_u *name,
24978 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024979{
24980 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024981 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024982 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024983 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024984
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024985 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024986 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024987 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024988 return FALSE;
24989
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024990 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024991
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024992 /* Find the name in the list of previously loaded package names. Skip
24993 * "autoload/", it's always the same. */
24994 for (i = 0; i < ga_loaded.ga_len; ++i)
24995 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24996 break;
24997 if (!reload && i < ga_loaded.ga_len)
24998 ret = FALSE; /* was loaded already */
24999 else
25000 {
25001 /* Remember the name if it wasn't loaded already. */
25002 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25003 {
25004 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25005 tofree = NULL;
25006 }
25007
25008 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025009 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025010 ret = TRUE;
25011 }
25012
25013 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025014 return ret;
25015}
25016
25017/*
25018 * Return the autoload script name for a function or variable name.
25019 * Returns NULL when out of memory.
25020 */
25021 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025022autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025023{
25024 char_u *p;
25025 char_u *scriptname;
25026
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025027 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025028 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25029 if (scriptname == NULL)
25030 return FALSE;
25031 STRCPY(scriptname, "autoload/");
25032 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025033 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025034 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025035 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025036 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025037 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025038}
25039
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25041
25042/*
25043 * Function given to ExpandGeneric() to obtain the list of user defined
25044 * function names.
25045 */
25046 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025047get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025048{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025049 static long_u done;
25050 static hashitem_T *hi;
25051 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052
25053 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025055 done = 0;
25056 hi = func_hashtab.ht_array;
25057 }
25058 if (done < func_hashtab.ht_used)
25059 {
25060 if (done++ > 0)
25061 ++hi;
25062 while (HASHITEM_EMPTY(hi))
25063 ++hi;
25064 fp = HI2UF(hi);
25065
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025066 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025067 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025069 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25070 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071
25072 cat_func_name(IObuff, fp);
25073 if (xp->xp_context != EXPAND_USER_FUNC)
25074 {
25075 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025076 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077 STRCAT(IObuff, ")");
25078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079 return IObuff;
25080 }
25081 return NULL;
25082}
25083
25084#endif /* FEAT_CMDL_COMPL */
25085
25086/*
25087 * Copy the function name of "fp" to buffer "buf".
25088 * "buf" must be able to hold the function name plus three bytes.
25089 * Takes care of script-local function names.
25090 */
25091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025092cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025094 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025095 {
25096 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025097 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025098 }
25099 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025100 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101}
25102
25103/*
25104 * ":delfunction {name}"
25105 */
25106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025107ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025108{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025109 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110 char_u *p;
25111 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025112 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113
25114 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025115 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025116 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025118 {
25119 if (fudi.fd_dict != NULL && !eap->skip)
25120 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025121 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025123 if (!ends_excmd(*skipwhite(p)))
25124 {
25125 vim_free(name);
25126 EMSG(_(e_trailing));
25127 return;
25128 }
25129 eap->nextcmd = check_nextcmd(p);
25130 if (eap->nextcmd != NULL)
25131 *p = NUL;
25132
25133 if (!eap->skip)
25134 fp = find_func(name);
25135 vim_free(name);
25136
25137 if (!eap->skip)
25138 {
25139 if (fp == NULL)
25140 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025141 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142 return;
25143 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025144 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025145 {
25146 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25147 return;
25148 }
25149
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025150 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025151 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025152 /* Delete the dict item that refers to the function, it will
25153 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025154 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025156 else
25157 func_free(fp);
25158 }
25159}
25160
25161/*
25162 * Free a function and remove it from the list of functions.
25163 */
25164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025165func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025166{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025167 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025168
25169 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025170 ga_clear_strings(&(fp->uf_args));
25171 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025172#ifdef FEAT_PROFILE
25173 vim_free(fp->uf_tml_count);
25174 vim_free(fp->uf_tml_total);
25175 vim_free(fp->uf_tml_self);
25176#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025177
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025178 /* remove the function from the function hashtable */
25179 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25180 if (HASHITEM_EMPTY(hi))
25181 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025182 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025183 hash_remove(&func_hashtab, hi);
25184
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025185 vim_free(fp);
25186}
25187
25188/*
25189 * Unreference a Function: decrement the reference count and free it when it
25190 * becomes zero. Only for numbered functions.
25191 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025192 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025193func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025194{
25195 ufunc_T *fp;
25196
25197 if (name != NULL && isdigit(*name))
25198 {
25199 fp = find_func(name);
25200 if (fp == NULL)
25201 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025202 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025203 {
25204 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025205 * when "uf_calls" becomes zero. */
25206 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025207 func_free(fp);
25208 }
25209 }
25210}
25211
25212/*
25213 * Count a reference to a Function.
25214 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025215 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025216func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025217{
25218 ufunc_T *fp;
25219
25220 if (name != NULL && isdigit(*name))
25221 {
25222 fp = find_func(name);
25223 if (fp == NULL)
25224 EMSG2(_(e_intern2), "func_ref()");
25225 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025226 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025227 }
25228}
25229
25230/*
25231 * Call a user function.
25232 */
25233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025234call_user_func(
25235 ufunc_T *fp, /* pointer to function */
25236 int argcount, /* nr of args */
25237 typval_T *argvars, /* arguments */
25238 typval_T *rettv, /* return value */
25239 linenr_T firstline, /* first line of range */
25240 linenr_T lastline, /* last line of range */
25241 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242{
Bram Moolenaar33570922005-01-25 22:26:29 +000025243 char_u *save_sourcing_name;
25244 linenr_T save_sourcing_lnum;
25245 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025246 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025247 int save_did_emsg;
25248 static int depth = 0;
25249 dictitem_T *v;
25250 int fixvar_idx = 0; /* index in fixvar[] */
25251 int i;
25252 int ai;
25253 char_u numbuf[NUMBUFLEN];
25254 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025255 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025256#ifdef FEAT_PROFILE
25257 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025258 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260
25261 /* If depth of calling is getting too high, don't execute the function */
25262 if (depth >= p_mfd)
25263 {
25264 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025265 rettv->v_type = VAR_NUMBER;
25266 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267 return;
25268 }
25269 ++depth;
25270
25271 line_breakcheck(); /* check for CTRL-C hit */
25272
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025273 fc = (funccall_T *)alloc(sizeof(funccall_T));
25274 fc->caller = current_funccal;
25275 current_funccal = fc;
25276 fc->func = fp;
25277 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025278 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025279 fc->linenr = 0;
25280 fc->returned = FALSE;
25281 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025283 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25284 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285
Bram Moolenaar33570922005-01-25 22:26:29 +000025286 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025287 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025288 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25289 * each argument variable and saves a lot of time.
25290 */
25291 /*
25292 * Init l: variables.
25293 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025294 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025295 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025296 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025297 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25298 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025299 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025300 name = v->di_key;
25301 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025302 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025303 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025304 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025305 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025306 v->di_tv.vval.v_dict = selfdict;
25307 ++selfdict->dv_refcount;
25308 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025309
Bram Moolenaar33570922005-01-25 22:26:29 +000025310 /*
25311 * Init a: variables.
25312 * Set a:0 to "argcount".
25313 * Set a:000 to a list with room for the "..." arguments.
25314 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025315 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025316 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025317 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025318 /* Use "name" to avoid a warning from some compiler that checks the
25319 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025320 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025321 name = v->di_key;
25322 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025323 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025324 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025325 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025326 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025327 v->di_tv.vval.v_list = &fc->l_varlist;
25328 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25329 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25330 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025331
25332 /*
25333 * Set a:firstline to "firstline" and a:lastline to "lastline".
25334 * Set a:name to named arguments.
25335 * Set a:N to the "..." arguments.
25336 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025337 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025338 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025339 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025340 (varnumber_T)lastline);
25341 for (i = 0; i < argcount; ++i)
25342 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025343 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025344 if (ai < 0)
25345 /* named argument a:name */
25346 name = FUNCARG(fp, i);
25347 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025348 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025349 /* "..." argument a:1, a:2, etc. */
25350 sprintf((char *)numbuf, "%d", ai + 1);
25351 name = numbuf;
25352 }
25353 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25354 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025355 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025356 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25357 }
25358 else
25359 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025360 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25361 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025362 if (v == NULL)
25363 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025364 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025365 }
25366 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025367 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025368
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025369 /* Note: the values are copied directly to avoid alloc/free.
25370 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025371 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025372 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025373
25374 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25375 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025376 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25377 fc->l_listitems[ai].li_tv = argvars[i];
25378 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025379 }
25380 }
25381
Bram Moolenaar071d4272004-06-13 20:20:40 +000025382 /* Don't redraw while executing the function. */
25383 ++RedrawingDisabled;
25384 save_sourcing_name = sourcing_name;
25385 save_sourcing_lnum = sourcing_lnum;
25386 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025387 /* need space for function name + ("function " + 3) or "[number]" */
25388 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25389 + STRLEN(fp->uf_name) + 20;
25390 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 if (sourcing_name != NULL)
25392 {
25393 if (save_sourcing_name != NULL
25394 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025395 sprintf((char *)sourcing_name, "%s[%d]..",
25396 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397 else
25398 STRCPY(sourcing_name, "function ");
25399 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25400
25401 if (p_verbose >= 12)
25402 {
25403 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025404 verbose_enter_scroll();
25405
Bram Moolenaar555b2802005-05-19 21:08:39 +000025406 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025407 if (p_verbose >= 14)
25408 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025409 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025410 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025411 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025412 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025413
25414 msg_puts((char_u *)"(");
25415 for (i = 0; i < argcount; ++i)
25416 {
25417 if (i > 0)
25418 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025419 if (argvars[i].v_type == VAR_NUMBER)
25420 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421 else
25422 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025423 /* Do not want errors such as E724 here. */
25424 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025425 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025426 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025427 if (s != NULL)
25428 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025429 if (vim_strsize(s) > MSG_BUF_CLEN)
25430 {
25431 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25432 s = buf;
25433 }
25434 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025435 vim_free(tofree);
25436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025437 }
25438 }
25439 msg_puts((char_u *)")");
25440 }
25441 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025442
25443 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444 --no_wait_return;
25445 }
25446 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025447#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025448 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025449 {
25450 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25451 func_do_profile(fp);
25452 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025453 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025454 {
25455 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025456 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025457 profile_zero(&fp->uf_tm_children);
25458 }
25459 script_prof_save(&wait_start);
25460 }
25461#endif
25462
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025464 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465 save_did_emsg = did_emsg;
25466 did_emsg = FALSE;
25467
25468 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025469 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025470 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25471
25472 --RedrawingDisabled;
25473
25474 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025475 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025477 clear_tv(rettv);
25478 rettv->v_type = VAR_NUMBER;
25479 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480 }
25481
Bram Moolenaar05159a02005-02-26 23:04:13 +000025482#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025483 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025484 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025485 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025486 profile_end(&call_start);
25487 profile_sub_wait(&wait_start, &call_start);
25488 profile_add(&fp->uf_tm_total, &call_start);
25489 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025490 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025491 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025492 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25493 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025494 }
25495 }
25496#endif
25497
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498 /* when being verbose, mention the return value */
25499 if (p_verbose >= 12)
25500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025502 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025505 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025506 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025507 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025508 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025509 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025510 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025511 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025512 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025513 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025514 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025515
Bram Moolenaar555b2802005-05-19 21:08:39 +000025516 /* The value may be very long. Skip the middle part, so that we
25517 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025518 * truncate it at the end. Don't want errors such as E724 here. */
25519 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025520 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025521 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025522 if (s != NULL)
25523 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025524 if (vim_strsize(s) > MSG_BUF_CLEN)
25525 {
25526 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25527 s = buf;
25528 }
25529 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025530 vim_free(tofree);
25531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532 }
25533 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025534
25535 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536 --no_wait_return;
25537 }
25538
25539 vim_free(sourcing_name);
25540 sourcing_name = save_sourcing_name;
25541 sourcing_lnum = save_sourcing_lnum;
25542 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025543#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025544 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025545 script_prof_restore(&wait_start);
25546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547
25548 if (p_verbose >= 12 && sourcing_name != NULL)
25549 {
25550 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025551 verbose_enter_scroll();
25552
Bram Moolenaar555b2802005-05-19 21:08:39 +000025553 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025554 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025555
25556 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557 --no_wait_return;
25558 }
25559
25560 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025561 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025562 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025563
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025564 /* If the a:000 list and the l: and a: dicts are not referenced we can
25565 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025566 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25567 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25568 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25569 {
25570 free_funccal(fc, FALSE);
25571 }
25572 else
25573 {
25574 hashitem_T *hi;
25575 listitem_T *li;
25576 int todo;
25577
25578 /* "fc" is still in use. This can happen when returning "a:000" or
25579 * assigning "l:" to a global variable.
25580 * Link "fc" in the list for garbage collection later. */
25581 fc->caller = previous_funccal;
25582 previous_funccal = fc;
25583
25584 /* Make a copy of the a: variables, since we didn't do that above. */
25585 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25586 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25587 {
25588 if (!HASHITEM_EMPTY(hi))
25589 {
25590 --todo;
25591 v = HI2DI(hi);
25592 copy_tv(&v->di_tv, &v->di_tv);
25593 }
25594 }
25595
25596 /* Make a copy of the a:000 items, since we didn't do that above. */
25597 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25598 copy_tv(&li->li_tv, &li->li_tv);
25599 }
25600}
25601
25602/*
25603 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025604 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025605 */
25606 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025607can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025608{
25609 return (fc->l_varlist.lv_copyID != copyID
25610 && fc->l_vars.dv_copyID != copyID
25611 && fc->l_avars.dv_copyID != copyID);
25612}
25613
25614/*
25615 * Free "fc" and what it contains.
25616 */
25617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025618free_funccal(
25619 funccall_T *fc,
25620 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025621{
25622 listitem_T *li;
25623
25624 /* The a: variables typevals may not have been allocated, only free the
25625 * allocated variables. */
25626 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25627
25628 /* free all l: variables */
25629 vars_clear(&fc->l_vars.dv_hashtab);
25630
25631 /* Free the a:000 variables if they were allocated. */
25632 if (free_val)
25633 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25634 clear_tv(&li->li_tv);
25635
25636 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025637}
25638
25639/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025640 * Add a number variable "name" to dict "dp" with value "nr".
25641 */
25642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025643add_nr_var(
25644 dict_T *dp,
25645 dictitem_T *v,
25646 char *name,
25647 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025648{
25649 STRCPY(v->di_key, name);
25650 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25651 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25652 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025653 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025654 v->di_tv.vval.v_number = nr;
25655}
25656
25657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658 * ":return [expr]"
25659 */
25660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025661ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025662{
25663 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025664 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025665 int returning = FALSE;
25666
25667 if (current_funccal == NULL)
25668 {
25669 EMSG(_("E133: :return not inside a function"));
25670 return;
25671 }
25672
25673 if (eap->skip)
25674 ++emsg_skip;
25675
25676 eap->nextcmd = NULL;
25677 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025678 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025679 {
25680 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025681 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025683 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025684 }
25685 /* It's safer to return also on error. */
25686 else if (!eap->skip)
25687 {
25688 /*
25689 * Return unless the expression evaluation has been cancelled due to an
25690 * aborting error, an interrupt, or an exception.
25691 */
25692 if (!aborting())
25693 returning = do_return(eap, FALSE, TRUE, NULL);
25694 }
25695
25696 /* When skipping or the return gets pending, advance to the next command
25697 * in this line (!returning). Otherwise, ignore the rest of the line.
25698 * Following lines will be ignored by get_func_line(). */
25699 if (returning)
25700 eap->nextcmd = NULL;
25701 else if (eap->nextcmd == NULL) /* no argument */
25702 eap->nextcmd = check_nextcmd(arg);
25703
25704 if (eap->skip)
25705 --emsg_skip;
25706}
25707
25708/*
25709 * Return from a function. Possibly makes the return pending. Also called
25710 * for a pending return at the ":endtry" or after returning from an extra
25711 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025712 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025713 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025714 * FALSE when the return gets pending.
25715 */
25716 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025717do_return(
25718 exarg_T *eap,
25719 int reanimate,
25720 int is_cmd,
25721 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025722{
25723 int idx;
25724 struct condstack *cstack = eap->cstack;
25725
25726 if (reanimate)
25727 /* Undo the return. */
25728 current_funccal->returned = FALSE;
25729
25730 /*
25731 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25732 * not in its finally clause (which then is to be executed next) is found.
25733 * In this case, make the ":return" pending for execution at the ":endtry".
25734 * Otherwise, return normally.
25735 */
25736 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25737 if (idx >= 0)
25738 {
25739 cstack->cs_pending[idx] = CSTP_RETURN;
25740
25741 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025742 /* A pending return again gets pending. "rettv" points to an
25743 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025744 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025745 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746 else
25747 {
25748 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025749 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025750 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025751 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025753 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754 {
25755 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025756 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025757 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758 else
25759 EMSG(_(e_outofmem));
25760 }
25761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025762 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025763
25764 if (reanimate)
25765 {
25766 /* The pending return value could be overwritten by a ":return"
25767 * without argument in a finally clause; reset the default
25768 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025769 current_funccal->rettv->v_type = VAR_NUMBER;
25770 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025771 }
25772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025773 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 }
25775 else
25776 {
25777 current_funccal->returned = TRUE;
25778
25779 /* If the return is carried out now, store the return value. For
25780 * a return immediately after reanimation, the value is already
25781 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025782 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025784 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025785 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025787 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025788 }
25789 }
25790
25791 return idx < 0;
25792}
25793
25794/*
25795 * Free the variable with a pending return value.
25796 */
25797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025798discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025799{
Bram Moolenaar33570922005-01-25 22:26:29 +000025800 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025801}
25802
25803/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025804 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025805 * is an allocated string. Used by report_pending() for verbose messages.
25806 */
25807 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025808get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025810 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025811 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025812 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025813
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025814 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025815 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025816 if (s == NULL)
25817 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025818
25819 STRCPY(IObuff, ":return ");
25820 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25821 if (STRLEN(s) + 8 >= IOSIZE)
25822 STRCPY(IObuff + IOSIZE - 4, "...");
25823 vim_free(tofree);
25824 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825}
25826
25827/*
25828 * Get next function line.
25829 * Called by do_cmdline() to get the next line.
25830 * Returns allocated string, or NULL for end of function.
25831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025832 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025833get_func_line(
25834 int c UNUSED,
25835 void *cookie,
25836 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025837{
Bram Moolenaar33570922005-01-25 22:26:29 +000025838 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025839 ufunc_T *fp = fcp->func;
25840 char_u *retval;
25841 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025842
25843 /* If breakpoints have been added/deleted need to check for it. */
25844 if (fcp->dbg_tick != debug_tick)
25845 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025846 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025847 sourcing_lnum);
25848 fcp->dbg_tick = debug_tick;
25849 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025850#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025851 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025852 func_line_end(cookie);
25853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025854
Bram Moolenaar05159a02005-02-26 23:04:13 +000025855 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025856 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25857 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 retval = NULL;
25859 else
25860 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025861 /* Skip NULL lines (continuation lines). */
25862 while (fcp->linenr < gap->ga_len
25863 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25864 ++fcp->linenr;
25865 if (fcp->linenr >= gap->ga_len)
25866 retval = NULL;
25867 else
25868 {
25869 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25870 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025871#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025872 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025873 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025874#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025876 }
25877
25878 /* Did we encounter a breakpoint? */
25879 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25880 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025881 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025882 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025883 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025884 sourcing_lnum);
25885 fcp->dbg_tick = debug_tick;
25886 }
25887
25888 return retval;
25889}
25890
Bram Moolenaar05159a02005-02-26 23:04:13 +000025891#if defined(FEAT_PROFILE) || defined(PROTO)
25892/*
25893 * Called when starting to read a function line.
25894 * "sourcing_lnum" must be correct!
25895 * When skipping lines it may not actually be executed, but we won't find out
25896 * until later and we need to store the time now.
25897 */
25898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025899func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025900{
25901 funccall_T *fcp = (funccall_T *)cookie;
25902 ufunc_T *fp = fcp->func;
25903
25904 if (fp->uf_profiling && sourcing_lnum >= 1
25905 && sourcing_lnum <= fp->uf_lines.ga_len)
25906 {
25907 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025908 /* Skip continuation lines. */
25909 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25910 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025911 fp->uf_tml_execed = FALSE;
25912 profile_start(&fp->uf_tml_start);
25913 profile_zero(&fp->uf_tml_children);
25914 profile_get_wait(&fp->uf_tml_wait);
25915 }
25916}
25917
25918/*
25919 * Called when actually executing a function line.
25920 */
25921 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025922func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025923{
25924 funccall_T *fcp = (funccall_T *)cookie;
25925 ufunc_T *fp = fcp->func;
25926
25927 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25928 fp->uf_tml_execed = TRUE;
25929}
25930
25931/*
25932 * Called when done with a function line.
25933 */
25934 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025935func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025936{
25937 funccall_T *fcp = (funccall_T *)cookie;
25938 ufunc_T *fp = fcp->func;
25939
25940 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25941 {
25942 if (fp->uf_tml_execed)
25943 {
25944 ++fp->uf_tml_count[fp->uf_tml_idx];
25945 profile_end(&fp->uf_tml_start);
25946 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025947 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025948 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25949 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025950 }
25951 fp->uf_tml_idx = -1;
25952 }
25953}
25954#endif
25955
Bram Moolenaar071d4272004-06-13 20:20:40 +000025956/*
25957 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025958 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959 */
25960 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025961func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025962{
Bram Moolenaar33570922005-01-25 22:26:29 +000025963 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025964
25965 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25966 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025967 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968 || fcp->returned);
25969}
25970
25971/*
25972 * return TRUE if cookie indicates a function which "abort"s on errors.
25973 */
25974 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025975func_has_abort(
25976 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025977{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025978 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025979}
25980
25981#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25982typedef enum
25983{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025984 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25985 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25986 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987} var_flavour_T;
25988
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025989static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025990
25991 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025992var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993{
25994 char_u *p = varname;
25995
25996 if (ASCII_ISUPPER(*p))
25997 {
25998 while (*(++p))
25999 if (ASCII_ISLOWER(*p))
26000 return VAR_FLAVOUR_SESSION;
26001 return VAR_FLAVOUR_VIMINFO;
26002 }
26003 else
26004 return VAR_FLAVOUR_DEFAULT;
26005}
26006#endif
26007
26008#if defined(FEAT_VIMINFO) || defined(PROTO)
26009/*
26010 * Restore global vars that start with a capital from the viminfo file
26011 */
26012 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026013read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026014{
26015 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026016 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026017 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026018 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026019
26020 if (!writing && (find_viminfo_parameter('!') != NULL))
26021 {
26022 tab = vim_strchr(virp->vir_line + 1, '\t');
26023 if (tab != NULL)
26024 {
26025 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026026 switch (*tab)
26027 {
26028 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026029#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026030 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026031#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026032 case 'D': type = VAR_DICT; break;
26033 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026034 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026036
26037 tab = vim_strchr(tab, '\t');
26038 if (tab != NULL)
26039 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026040 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026041 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026042 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026044#ifdef FEAT_FLOAT
26045 else if (type == VAR_FLOAT)
26046 (void)string2float(tab + 1, &tv.vval.v_float);
26047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026048 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026049 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026050 if (type == VAR_DICT || type == VAR_LIST)
26051 {
26052 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26053
26054 if (etv == NULL)
26055 /* Failed to parse back the dict or list, use it as a
26056 * string. */
26057 tv.v_type = VAR_STRING;
26058 else
26059 {
26060 vim_free(tv.vval.v_string);
26061 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026062 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026063 }
26064 }
26065
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026066 /* when in a function use global variables */
26067 save_funccal = current_funccal;
26068 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026069 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026070 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026071
26072 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026073 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026074 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26075 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026076 }
26077 }
26078 }
26079
26080 return viminfo_readline(virp);
26081}
26082
26083/*
26084 * Write global vars that start with a capital to the viminfo file
26085 */
26086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026087write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026088{
Bram Moolenaar33570922005-01-25 22:26:29 +000026089 hashitem_T *hi;
26090 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026091 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026092 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026093 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026094 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026095 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026096
26097 if (find_viminfo_parameter('!') == NULL)
26098 return;
26099
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026100 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026101
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026102 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026103 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026104 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026105 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026106 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026107 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026108 this_var = HI2DI(hi);
26109 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026110 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026111 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026112 {
26113 case VAR_STRING: s = "STR"; break;
26114 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026115 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026116 case VAR_DICT: s = "DIC"; break;
26117 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026118 case VAR_SPECIAL: s = "XPL"; break;
26119
26120 case VAR_UNKNOWN:
26121 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026122 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026123 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026124 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026125 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026126 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026127 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026128 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026129 if (p != NULL)
26130 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026131 vim_free(tofree);
26132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026133 }
26134 }
26135}
26136#endif
26137
26138#if defined(FEAT_SESSION) || defined(PROTO)
26139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026140store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026141{
Bram Moolenaar33570922005-01-25 22:26:29 +000026142 hashitem_T *hi;
26143 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026144 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026145 char_u *p, *t;
26146
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026147 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026148 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026149 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026150 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026151 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026152 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026153 this_var = HI2DI(hi);
26154 if ((this_var->di_tv.v_type == VAR_NUMBER
26155 || this_var->di_tv.v_type == VAR_STRING)
26156 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026157 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026158 /* Escape special characters with a backslash. Turn a LF and
26159 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026160 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026161 (char_u *)"\\\"\n\r");
26162 if (p == NULL) /* out of memory */
26163 break;
26164 for (t = p; *t != NUL; ++t)
26165 if (*t == '\n')
26166 *t = 'n';
26167 else if (*t == '\r')
26168 *t = 'r';
26169 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026170 this_var->di_key,
26171 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26172 : ' ',
26173 p,
26174 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26175 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026176 || put_eol(fd) == FAIL)
26177 {
26178 vim_free(p);
26179 return FAIL;
26180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026181 vim_free(p);
26182 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026183#ifdef FEAT_FLOAT
26184 else if (this_var->di_tv.v_type == VAR_FLOAT
26185 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26186 {
26187 float_T f = this_var->di_tv.vval.v_float;
26188 int sign = ' ';
26189
26190 if (f < 0)
26191 {
26192 f = -f;
26193 sign = '-';
26194 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026195 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026196 this_var->di_key, sign, f) < 0)
26197 || put_eol(fd) == FAIL)
26198 return FAIL;
26199 }
26200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026201 }
26202 }
26203 return OK;
26204}
26205#endif
26206
Bram Moolenaar661b1822005-07-28 22:36:45 +000026207/*
26208 * Display script name where an item was last set.
26209 * Should only be invoked when 'verbose' is non-zero.
26210 */
26211 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026212last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026213{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026214 char_u *p;
26215
Bram Moolenaar661b1822005-07-28 22:36:45 +000026216 if (scriptID != 0)
26217 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026218 p = home_replace_save(NULL, get_scriptname(scriptID));
26219 if (p != NULL)
26220 {
26221 verbose_enter();
26222 MSG_PUTS(_("\n\tLast set from "));
26223 MSG_PUTS(p);
26224 vim_free(p);
26225 verbose_leave();
26226 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026227 }
26228}
26229
Bram Moolenaard812df62008-11-09 12:46:09 +000026230/*
26231 * List v:oldfiles in a nice way.
26232 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026234ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026235{
26236 list_T *l = vimvars[VV_OLDFILES].vv_list;
26237 listitem_T *li;
26238 int nr = 0;
26239
26240 if (l == NULL)
26241 msg((char_u *)_("No old files"));
26242 else
26243 {
26244 msg_start();
26245 msg_scroll = TRUE;
26246 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26247 {
26248 msg_outnum((long)++nr);
26249 MSG_PUTS(": ");
26250 msg_outtrans(get_tv_string(&li->li_tv));
26251 msg_putchar('\n');
26252 out_flush(); /* output one line at a time */
26253 ui_breakcheck();
26254 }
26255 /* Assume "got_int" was set to truncate the listing. */
26256 got_int = FALSE;
26257
26258#ifdef FEAT_BROWSE_CMD
26259 if (cmdmod.browse)
26260 {
26261 quit_more = FALSE;
26262 nr = prompt_for_number(FALSE);
26263 msg_starthere();
26264 if (nr > 0)
26265 {
26266 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26267 (long)nr);
26268
26269 if (p != NULL)
26270 {
26271 p = expand_env_save(p);
26272 eap->arg = p;
26273 eap->cmdidx = CMD_edit;
26274 cmdmod.browse = FALSE;
26275 do_exedit(eap, NULL);
26276 vim_free(p);
26277 }
26278 }
26279 }
26280#endif
26281 }
26282}
26283
Bram Moolenaar53744302015-07-17 17:38:22 +020026284/* reset v:option_new, v:option_old and v:option_type */
26285 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026286reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026287{
26288 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26289 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26290 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26291}
26292
26293
Bram Moolenaar071d4272004-06-13 20:20:40 +000026294#endif /* FEAT_EVAL */
26295
Bram Moolenaar071d4272004-06-13 20:20:40 +000026296
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026297#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026298
26299#ifdef WIN3264
26300/*
26301 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26302 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026303static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26304static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26305static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026306
26307/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026308 * Get the short path (8.3) for the filename in "fnamep".
26309 * Only works for a valid file name.
26310 * When the path gets longer "fnamep" is changed and the allocated buffer
26311 * is put in "bufp".
26312 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26313 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026314 */
26315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026316get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026318 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026319 char_u *newbuf;
26320
26321 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026322 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026323 if (l > len - 1)
26324 {
26325 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026326 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026327 newbuf = vim_strnsave(*fnamep, l);
26328 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026329 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026330
26331 vim_free(*bufp);
26332 *fnamep = *bufp = newbuf;
26333
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026334 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026335 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026336 }
26337
26338 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026339 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026340}
26341
26342/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026343 * Get the short path (8.3) for the filename in "fname". The converted
26344 * path is returned in "bufp".
26345 *
26346 * Some of the directories specified in "fname" may not exist. This function
26347 * will shorten the existing directories at the beginning of the path and then
26348 * append the remaining non-existing path.
26349 *
26350 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026351 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026352 * bufp - Pointer to an allocated buffer for the filename.
26353 * fnamelen - Length of the filename pointed to by fname
26354 *
26355 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026356 */
26357 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026358shortpath_for_invalid_fname(
26359 char_u **fname,
26360 char_u **bufp,
26361 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026362{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026363 char_u *short_fname, *save_fname, *pbuf_unused;
26364 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026365 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026366 int old_len, len;
26367 int new_len, sfx_len;
26368 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026369
26370 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026371 old_len = *fnamelen;
26372 save_fname = vim_strnsave(*fname, old_len);
26373 pbuf_unused = NULL;
26374 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026375
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026376 endp = save_fname + old_len - 1; /* Find the end of the copy */
26377 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026378
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026379 /*
26380 * Try shortening the supplied path till it succeeds by removing one
26381 * directory at a time from the tail of the path.
26382 */
26383 len = 0;
26384 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026385 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026386 /* go back one path-separator */
26387 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26388 --endp;
26389 if (endp <= save_fname)
26390 break; /* processed the complete path */
26391
26392 /*
26393 * Replace the path separator with a NUL and try to shorten the
26394 * resulting path.
26395 */
26396 ch = *endp;
26397 *endp = 0;
26398 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026399 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026400 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26401 {
26402 retval = FAIL;
26403 goto theend;
26404 }
26405 *endp = ch; /* preserve the string */
26406
26407 if (len > 0)
26408 break; /* successfully shortened the path */
26409
26410 /* failed to shorten the path. Skip the path separator */
26411 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026412 }
26413
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026414 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026415 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026416 /*
26417 * Succeeded in shortening the path. Now concatenate the shortened
26418 * path with the remaining path at the tail.
26419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026420
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026421 /* Compute the length of the new path. */
26422 sfx_len = (int)(save_endp - endp) + 1;
26423 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026424
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026425 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026426 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026427 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026428 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026429 /* There is not enough space in the currently allocated string,
26430 * copy it to a buffer big enough. */
26431 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026432 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026433 {
26434 retval = FAIL;
26435 goto theend;
26436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026437 }
26438 else
26439 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026440 /* Transfer short_fname to the main buffer (it's big enough),
26441 * unless get_short_pathname() did its work in-place. */
26442 *fname = *bufp = save_fname;
26443 if (short_fname != save_fname)
26444 vim_strncpy(save_fname, short_fname, len);
26445 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026446 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026447
26448 /* concat the not-shortened part of the path */
26449 vim_strncpy(*fname + len, endp, sfx_len);
26450 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026451 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026452
26453theend:
26454 vim_free(pbuf_unused);
26455 vim_free(save_fname);
26456
26457 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026458}
26459
26460/*
26461 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026462 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026463 */
26464 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026465shortpath_for_partial(
26466 char_u **fnamep,
26467 char_u **bufp,
26468 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469{
26470 int sepcount, len, tflen;
26471 char_u *p;
26472 char_u *pbuf, *tfname;
26473 int hasTilde;
26474
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026475 /* Count up the path separators from the RHS.. so we know which part
26476 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026477 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026478 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026479 if (vim_ispathsep(*p))
26480 ++sepcount;
26481
26482 /* Need full path first (use expand_env() to remove a "~/") */
26483 hasTilde = (**fnamep == '~');
26484 if (hasTilde)
26485 pbuf = tfname = expand_env_save(*fnamep);
26486 else
26487 pbuf = tfname = FullName_save(*fnamep, FALSE);
26488
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026489 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026490
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026491 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26492 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026493
26494 if (len == 0)
26495 {
26496 /* Don't have a valid filename, so shorten the rest of the
26497 * path if we can. This CAN give us invalid 8.3 filenames, but
26498 * there's not a lot of point in guessing what it might be.
26499 */
26500 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026501 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26502 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026503 }
26504
26505 /* Count the paths backward to find the beginning of the desired string. */
26506 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026507 {
26508#ifdef FEAT_MBYTE
26509 if (has_mbyte)
26510 p -= mb_head_off(tfname, p);
26511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026512 if (vim_ispathsep(*p))
26513 {
26514 if (sepcount == 0 || (hasTilde && sepcount == 1))
26515 break;
26516 else
26517 sepcount --;
26518 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026520 if (hasTilde)
26521 {
26522 --p;
26523 if (p >= tfname)
26524 *p = '~';
26525 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026526 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026527 }
26528 else
26529 ++p;
26530
26531 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26532 vim_free(*bufp);
26533 *fnamelen = (int)STRLEN(p);
26534 *bufp = pbuf;
26535 *fnamep = p;
26536
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026537 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026538}
26539#endif /* WIN3264 */
26540
26541/*
26542 * Adjust a filename, according to a string of modifiers.
26543 * *fnamep must be NUL terminated when called. When returning, the length is
26544 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026545 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026546 * When there is an error, *fnamep is set to NULL.
26547 */
26548 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026549modify_fname(
26550 char_u *src, /* string with modifiers */
26551 int *usedlen, /* characters after src that are used */
26552 char_u **fnamep, /* file name so far */
26553 char_u **bufp, /* buffer for allocated file name or NULL */
26554 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026555{
26556 int valid = 0;
26557 char_u *tail;
26558 char_u *s, *p, *pbuf;
26559 char_u dirname[MAXPATHL];
26560 int c;
26561 int has_fullname = 0;
26562#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026563 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026564 int has_shortname = 0;
26565#endif
26566
26567repeat:
26568 /* ":p" - full path/file_name */
26569 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26570 {
26571 has_fullname = 1;
26572
26573 valid |= VALID_PATH;
26574 *usedlen += 2;
26575
26576 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26577 if ((*fnamep)[0] == '~'
26578#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26579 && ((*fnamep)[1] == '/'
26580# ifdef BACKSLASH_IN_FILENAME
26581 || (*fnamep)[1] == '\\'
26582# endif
26583 || (*fnamep)[1] == NUL)
26584
26585#endif
26586 )
26587 {
26588 *fnamep = expand_env_save(*fnamep);
26589 vim_free(*bufp); /* free any allocated file name */
26590 *bufp = *fnamep;
26591 if (*fnamep == NULL)
26592 return -1;
26593 }
26594
26595 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026596 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026597 {
26598 if (vim_ispathsep(*p)
26599 && p[1] == '.'
26600 && (p[2] == NUL
26601 || vim_ispathsep(p[2])
26602 || (p[2] == '.'
26603 && (p[3] == NUL || vim_ispathsep(p[3])))))
26604 break;
26605 }
26606
26607 /* FullName_save() is slow, don't use it when not needed. */
26608 if (*p != NUL || !vim_isAbsName(*fnamep))
26609 {
26610 *fnamep = FullName_save(*fnamep, *p != NUL);
26611 vim_free(*bufp); /* free any allocated file name */
26612 *bufp = *fnamep;
26613 if (*fnamep == NULL)
26614 return -1;
26615 }
26616
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026617#ifdef WIN3264
26618# if _WIN32_WINNT >= 0x0500
26619 if (vim_strchr(*fnamep, '~') != NULL)
26620 {
26621 /* Expand 8.3 filename to full path. Needed to make sure the same
26622 * file does not have two different names.
26623 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26624 p = alloc(_MAX_PATH + 1);
26625 if (p != NULL)
26626 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026627 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026628 {
26629 vim_free(*bufp);
26630 *bufp = *fnamep = p;
26631 }
26632 else
26633 vim_free(p);
26634 }
26635 }
26636# endif
26637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026638 /* Append a path separator to a directory. */
26639 if (mch_isdir(*fnamep))
26640 {
26641 /* Make room for one or two extra characters. */
26642 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26643 vim_free(*bufp); /* free any allocated file name */
26644 *bufp = *fnamep;
26645 if (*fnamep == NULL)
26646 return -1;
26647 add_pathsep(*fnamep);
26648 }
26649 }
26650
26651 /* ":." - path relative to the current directory */
26652 /* ":~" - path relative to the home directory */
26653 /* ":8" - shortname path - postponed till after */
26654 while (src[*usedlen] == ':'
26655 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26656 {
26657 *usedlen += 2;
26658 if (c == '8')
26659 {
26660#ifdef WIN3264
26661 has_shortname = 1; /* Postpone this. */
26662#endif
26663 continue;
26664 }
26665 pbuf = NULL;
26666 /* Need full path first (use expand_env() to remove a "~/") */
26667 if (!has_fullname)
26668 {
26669 if (c == '.' && **fnamep == '~')
26670 p = pbuf = expand_env_save(*fnamep);
26671 else
26672 p = pbuf = FullName_save(*fnamep, FALSE);
26673 }
26674 else
26675 p = *fnamep;
26676
26677 has_fullname = 0;
26678
26679 if (p != NULL)
26680 {
26681 if (c == '.')
26682 {
26683 mch_dirname(dirname, MAXPATHL);
26684 s = shorten_fname(p, dirname);
26685 if (s != NULL)
26686 {
26687 *fnamep = s;
26688 if (pbuf != NULL)
26689 {
26690 vim_free(*bufp); /* free any allocated file name */
26691 *bufp = pbuf;
26692 pbuf = NULL;
26693 }
26694 }
26695 }
26696 else
26697 {
26698 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26699 /* Only replace it when it starts with '~' */
26700 if (*dirname == '~')
26701 {
26702 s = vim_strsave(dirname);
26703 if (s != NULL)
26704 {
26705 *fnamep = s;
26706 vim_free(*bufp);
26707 *bufp = s;
26708 }
26709 }
26710 }
26711 vim_free(pbuf);
26712 }
26713 }
26714
26715 tail = gettail(*fnamep);
26716 *fnamelen = (int)STRLEN(*fnamep);
26717
26718 /* ":h" - head, remove "/file_name", can be repeated */
26719 /* Don't remove the first "/" or "c:\" */
26720 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26721 {
26722 valid |= VALID_HEAD;
26723 *usedlen += 2;
26724 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026725 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026726 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026727 *fnamelen = (int)(tail - *fnamep);
26728#ifdef VMS
26729 if (*fnamelen > 0)
26730 *fnamelen += 1; /* the path separator is part of the path */
26731#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026732 if (*fnamelen == 0)
26733 {
26734 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26735 p = vim_strsave((char_u *)".");
26736 if (p == NULL)
26737 return -1;
26738 vim_free(*bufp);
26739 *bufp = *fnamep = tail = p;
26740 *fnamelen = 1;
26741 }
26742 else
26743 {
26744 while (tail > s && !after_pathsep(s, tail))
26745 mb_ptr_back(*fnamep, tail);
26746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026747 }
26748
26749 /* ":8" - shortname */
26750 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26751 {
26752 *usedlen += 2;
26753#ifdef WIN3264
26754 has_shortname = 1;
26755#endif
26756 }
26757
26758#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026759 /*
26760 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026761 */
26762 if (has_shortname)
26763 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026764 /* Copy the string if it is shortened by :h and when it wasn't copied
26765 * yet, because we are going to change it in place. Avoids changing
26766 * the buffer name for "%:8". */
26767 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026768 {
26769 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026770 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026771 return -1;
26772 vim_free(*bufp);
26773 *bufp = *fnamep = p;
26774 }
26775
26776 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026777 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026778 if (!has_fullname && !vim_isAbsName(*fnamep))
26779 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026780 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026781 return -1;
26782 }
26783 else
26784 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026785 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026786
Bram Moolenaardc935552011-08-17 15:23:23 +020026787 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026788 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026789 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026790 return -1;
26791
26792 if (l == 0)
26793 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026794 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026795 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026796 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026797 return -1;
26798 }
26799 *fnamelen = l;
26800 }
26801 }
26802#endif /* WIN3264 */
26803
26804 /* ":t" - tail, just the basename */
26805 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26806 {
26807 *usedlen += 2;
26808 *fnamelen -= (int)(tail - *fnamep);
26809 *fnamep = tail;
26810 }
26811
26812 /* ":e" - extension, can be repeated */
26813 /* ":r" - root, without extension, can be repeated */
26814 while (src[*usedlen] == ':'
26815 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26816 {
26817 /* find a '.' in the tail:
26818 * - for second :e: before the current fname
26819 * - otherwise: The last '.'
26820 */
26821 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26822 s = *fnamep - 2;
26823 else
26824 s = *fnamep + *fnamelen - 1;
26825 for ( ; s > tail; --s)
26826 if (s[0] == '.')
26827 break;
26828 if (src[*usedlen + 1] == 'e') /* :e */
26829 {
26830 if (s > tail)
26831 {
26832 *fnamelen += (int)(*fnamep - (s + 1));
26833 *fnamep = s + 1;
26834#ifdef VMS
26835 /* cut version from the extension */
26836 s = *fnamep + *fnamelen - 1;
26837 for ( ; s > *fnamep; --s)
26838 if (s[0] == ';')
26839 break;
26840 if (s > *fnamep)
26841 *fnamelen = s - *fnamep;
26842#endif
26843 }
26844 else if (*fnamep <= tail)
26845 *fnamelen = 0;
26846 }
26847 else /* :r */
26848 {
26849 if (s > tail) /* remove one extension */
26850 *fnamelen = (int)(s - *fnamep);
26851 }
26852 *usedlen += 2;
26853 }
26854
26855 /* ":s?pat?foo?" - substitute */
26856 /* ":gs?pat?foo?" - global substitute */
26857 if (src[*usedlen] == ':'
26858 && (src[*usedlen + 1] == 's'
26859 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26860 {
26861 char_u *str;
26862 char_u *pat;
26863 char_u *sub;
26864 int sep;
26865 char_u *flags;
26866 int didit = FALSE;
26867
26868 flags = (char_u *)"";
26869 s = src + *usedlen + 2;
26870 if (src[*usedlen + 1] == 'g')
26871 {
26872 flags = (char_u *)"g";
26873 ++s;
26874 }
26875
26876 sep = *s++;
26877 if (sep)
26878 {
26879 /* find end of pattern */
26880 p = vim_strchr(s, sep);
26881 if (p != NULL)
26882 {
26883 pat = vim_strnsave(s, (int)(p - s));
26884 if (pat != NULL)
26885 {
26886 s = p + 1;
26887 /* find end of substitution */
26888 p = vim_strchr(s, sep);
26889 if (p != NULL)
26890 {
26891 sub = vim_strnsave(s, (int)(p - s));
26892 str = vim_strnsave(*fnamep, *fnamelen);
26893 if (sub != NULL && str != NULL)
26894 {
26895 *usedlen = (int)(p + 1 - src);
26896 s = do_string_sub(str, pat, sub, flags);
26897 if (s != NULL)
26898 {
26899 *fnamep = s;
26900 *fnamelen = (int)STRLEN(s);
26901 vim_free(*bufp);
26902 *bufp = s;
26903 didit = TRUE;
26904 }
26905 }
26906 vim_free(sub);
26907 vim_free(str);
26908 }
26909 vim_free(pat);
26910 }
26911 }
26912 /* after using ":s", repeat all the modifiers */
26913 if (didit)
26914 goto repeat;
26915 }
26916 }
26917
Bram Moolenaar26df0922014-02-23 23:39:13 +010026918 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26919 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026920 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026921 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026922 if (c != NUL)
26923 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026924 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026925 if (c != NUL)
26926 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026927 if (p == NULL)
26928 return -1;
26929 vim_free(*bufp);
26930 *bufp = *fnamep = p;
26931 *fnamelen = (int)STRLEN(p);
26932 *usedlen += 2;
26933 }
26934
Bram Moolenaar071d4272004-06-13 20:20:40 +000026935 return valid;
26936}
26937
26938/*
26939 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26940 * "flags" can be "g" to do a global substitute.
26941 * Returns an allocated string, NULL for error.
26942 */
26943 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026944do_string_sub(
26945 char_u *str,
26946 char_u *pat,
26947 char_u *sub,
26948 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026949{
26950 int sublen;
26951 regmatch_T regmatch;
26952 int i;
26953 int do_all;
26954 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026955 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026956 garray_T ga;
26957 char_u *ret;
26958 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026959 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026960
26961 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26962 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026963 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026964
26965 ga_init2(&ga, 1, 200);
26966
26967 do_all = (flags[0] == 'g');
26968
26969 regmatch.rm_ic = p_ic;
26970 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26971 if (regmatch.regprog != NULL)
26972 {
26973 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026974 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026975 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26976 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026977 /* Skip empty match except for first match. */
26978 if (regmatch.startp[0] == regmatch.endp[0])
26979 {
26980 if (zero_width == regmatch.startp[0])
26981 {
26982 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026983 i = MB_PTR2LEN(tail);
26984 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26985 (size_t)i);
26986 ga.ga_len += i;
26987 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026988 continue;
26989 }
26990 zero_width = regmatch.startp[0];
26991 }
26992
Bram Moolenaar071d4272004-06-13 20:20:40 +000026993 /*
26994 * Get some space for a temporary buffer to do the substitution
26995 * into. It will contain:
26996 * - The text up to where the match is.
26997 * - The substituted text.
26998 * - The text after the match.
26999 */
27000 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027001 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027002 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27003 {
27004 ga_clear(&ga);
27005 break;
27006 }
27007
27008 /* copy the text up to where the match is */
27009 i = (int)(regmatch.startp[0] - tail);
27010 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27011 /* add the substituted text */
27012 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27013 + ga.ga_len + i, TRUE, TRUE, FALSE);
27014 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027015 tail = regmatch.endp[0];
27016 if (*tail == NUL)
27017 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027018 if (!do_all)
27019 break;
27020 }
27021
27022 if (ga.ga_data != NULL)
27023 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27024
Bram Moolenaar473de612013-06-08 18:19:48 +020027025 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027026 }
27027
27028 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27029 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027030 if (p_cpo == empty_option)
27031 p_cpo = save_cpo;
27032 else
27033 /* Darn, evaluating {sub} expression changed the value. */
27034 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027035
27036 return ret;
27037}
27038
27039#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */